Show Menu

Python_Chap_6 Cheat Sheet by

tests allows the computer to make descisions (if)

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:
    #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
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
>>> abs(var - 0.3) < delta


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Studying Cheat Sheet
          Python_Chap_4 Cheat Sheet

          More Cheat Sheets by Theo666

          Python_Chap_2 Cheat Sheet
          Python_Chap_4 Cheat Sheet
          Chap_3 Cheat Sheet