GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Books
  • Mini
  • Tips
  • Tags
  • About
  • learnbyexample
  • Python tip 4: comparison chaining

This view

  • Original page
  • New address

GeezerVison is showing learnbyexample.github.io — Python tip 4: comparison chaining

You can chain comparison operators arbitrarily. Apart from terser code, this also has the advantage of having to evaluate the middle expression only once.

>>> from math import factorial

# factorial function gets called twice for this example
>>> factorial(3) > 2 and factorial(3) < 10
True

# function needs to be called only once here
>>> 2 < factorial(3) < 10
True

# another example
>>> 'bat' < 'cat' < 'cater'
True

Video demo:


info See my 100 Page Python Intro ebook for a short, introductory guide for the Python programming language.

#python#tip

← Python tip 5: random choice and sample

Python tip 3: expression and result with f-string →


📰 Use this link for the Atom feed.
✅ Follow me on Twitter, GitHub and Youtube for interesting tech nuggets.


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.