Basic cheatsheet for Python mostly based on the book written by Al Sweigart, Automate the Boring Stuff with Python under the Creative Commons license and many other sources.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
< |
Less than |
> |
Greater Than |
<= |
Less than or Equal to |
>= |
Greater than or Equal to |
These operators evaluate to True or False depending on the values you give them.
Examples:
42 == 4240 == 42'hello' == 'hello''hello' == 'Hello''dog' != 'cat'42 == 42.042 == '42'Never use == or != operator to evaluate boolean operation. Use the is or is not operators,
or use implicit boolean evaluation.
NO (even if they are valid Python):
True == TrueTrue != FalseYES (even if they are valid Python):
True is TrueTrue is not FalseThese statements are equivalent:
if a is True:
pass
if a is not False:
pass
if a:
passAnd these as well:
if a is False:
pass
if a is not True:
pass
if not a:
passThere are three Boolean operators: and, or, and not.
The and Operator’s Truth Table:
| Expression | Evaluates to |
|---|---|
| True and True | True |
| True and False | False |
| False and True | False |
| False and False | False |
The or Operator’s Truth Table:
| Expression | Evaluates to |
|---|---|
| True or True | True |
| True or False | True |
| False or True | True |
| False or False | False |
The not Operator’s Truth Table:
| Expression | Evaluates to |
|---|---|
| not True | False |
| not False | True |
(4 < 5) and (5 < 6)(4 < 5) and (9 < 6)(1 == 2) or (2 == 2)You can also use multiple Boolean operators in an expression, along with the comparison operators:
2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2if name == 'Alice':
print('Hi, Alice.')name = 'Bob'
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.')name = 'Bob'
age = 5
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')name = 'Bob'
age = 30
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
else:
print('You are neither Alice nor a little kid.')spam = 0
while spam < 5:
print('Hello, world.')
spam = spam + 1If the execution reaches a break statement, it immediately exits the while loop’s clause:
while True:
print('Please type your name.')
name = input()
if name == 'your name':
break
print('Thank you!')When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello, Joe. What is the password? (It is a fish.)')
password = input()
if password == 'swordfish':
break
print('Access granted.')print('My name is')
for i in range(5):
print('Jimmy Five Times ({})'.format(str(i)))The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
for i in range(0, 10, 2):
print(i)You can even use a negative number for the step argument to make the for loop count down instead of up.
for i in range(5, -1, -1):
print(i)This allows to specify a statement to execute in case of the full loop has been executed. Only
useful when a break condition can occur in the loop:
for i in [1, 2, 3, 4, 5]:
if i == 3:
break
else:
print("only executed when no item of the list is equal to 3")import random
for i in range(5):
print(random.randint(1, 10))import random, sys, os, mathfrom random import *.import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed {}.'.format(response))