Cheatography
https://cheatography.com
tests allows the computer to make descisions (if)
Instructions break & continue
The break statement is used to terminate the loop immediately
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-elif-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 floating-point numbers (hence their name!), this leads to certain limitations. 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 |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Theo666