Hidden features of Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

What are the lesser-known but useful features of the Python programming language?

    • Try to limit answers to Python core.
    • One feature per answer.
    • Give an example and short description of the feature, not just a link to documentation.
    • Label the feature using a title as the first line.

Quick links to answers:

Answers

Chaining comparison operators:

>>> x = 5
>>> 1 < x < 10
True
>>> 10 < x < 20 
False
>>> x < 10 < x*10 < 100
True
>>> 10 > x <= 9
True
>>> 5 == x > 4
True

In case you re thinking it s doing 1 < x, which comes out as True, and then comparing True < 10, which is also True, then no, that s really not what happens (see the last example.) It s really translating into 1 < x and x < 10, and x < 10 and 10 < x * 10 and x*10 < 100, but with less typing and each term is only evaluated once.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/101268/hidden-features-of-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils