Python_Chap_6 Cheat Sheet

A Programming › Python cheat sheet in English by Theo666

tests allows the computer to make descisions (if)

2,127 views 711 downloads 25 Sep 2023 History

Instru­ctions break & continue

The break statement is used to terminate the loop immedi­­ately
The continue statement is used to skip the current iteration of the loop and move on to the next iteration.
Both can be used with for and while loops.

Test with serveral conditions

# if-elif-else 
if condition_1 :
    #bloc of instruction
elif condition_2:
    #bloc of instruction
elif condition_3:
    #bloc of instruction:
else:
    #bloc of instruction
The if-eli­f-else statement : the code under the first true condition will be executed, and the rest of the conditions will be skipped.

Multiple Tests

all([x, y, z])
x and y and z
x or y or z
(x or y) and z
(x and y) or z
etc
those operators usually combine with if and while

Value Tests on Floats

Since Python stores the numerical values of floats as floati­ng-­point numbers (hence their name!), this leads to certain limita­tions. For example,
(3 - 2.7) == 0.3 returns False,
3 - 2.7 returns 2.999...8.
Tips: For the reasons mentioned above, you should never test if a float is equal to a certain value. The best practice is to check if a float is within a range with a certain precision.
>>> delta = 0.0001
>>> var = 3.0 - 2.7
>>> 0.3 - delta < var < 0.3 + delta
True
>>> abs(var - 0.3) < delta
True

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.