Python Cheat Sheet: Conditional Tests (Comparisons)
Python Cheat Sheet: Conditional Tests (Comparisons)
Python Cheat Sheet Conditional tests evaluate to whether a statement is True or False. even_numbers = [2*x for x in range(10)]
Conditional tests odd_numbers = [2*x+1 for x in range(10)]
Expressions and Variables 1 < 2 # True divby3 = [x for x in range(100) if x%3==0]
Python evaluates expressions. The results can be stored in variables, 1+1 >= 2 # True Conditional tests with lists
which can be reused. Spaces around operators are optional! 1 < 2 < 3 # True (chaining) 2 in numbers # True
Simple mathematical expressions 1 == 1 # True: 1 equals 1 17 in numbers # False
1 + 1 # evaluates to: 2 Note: Double equal signs (==) have to be used for equality testing! 'Charlie' not in names # False
# Text after ‘#’ is a comment (ignored by Py) 1 != 2 # True: 1 is not equal 2 Removing items from lists
1+2*3 # 7 Boolean expressions numbers = [1,2,3,4,5,6]
(1+2)*3 # 9 x = 7 numbers.remove(4) # now: [1,2,3,5,6]
5**2 # 25 (power) x < 10 or x > 15 # True del numbers[2] # now: [1,2,5,6]
Division x < 10 and x > 7 # False del numbers[3:] # now: [1,2]
5/2 # 2.5 x < 10 and not x > 7 # True Copying lists
5//2 # 2 (integer division!) x = [1,2,3]
5 % 2 # 1 (modulo, remainder) Lists y = x # y refers to the same list as x
Lists are a container for data. They contain multiple items in a
Variables y[0] = 10 # modify y
particular order.
x = 2 # Assignment: x is now 2 print(x) # [10,2,3] – x was modified, too!
Creating a list
y = 1+1 # y is now 2 z = x[:] # z is a copy of x
numbers = [1,2,3,4,5]
z = x + y # z is now 4 z[0] = 5 # modify only z, not x
names = ['Alice', 'Bob', "Charlie"]
empty_list = [] Length of a list
Strings and Printing lst = [2,4,6,8]
Strings are sequences of characters; a representation of text. Printing
Get items from a list (indexing)
print(names[0]) # Alice len(lst) # 4
refers to the output of text from the program.
Hello world print(names[2]) # Charlie
print(numbers[-1]) # 5 ([-1] -> last item)
User Input
print('Hello world!')
Your programs can prompt (ask) the user to enter a value. The value is
print("Hello world!") names[0] = 'Al' # ['Al', 'Bob', 'Charlie'] always stored as a string (i.e., text) and you may need to convert it.
Note: Only straight single ( ' ) or double ( " ) quotes can be used (not Adding items to a list Prompting for a value
mixed)! Backticks and accents ( ´ or ` ) will not work. numbers.append(6) name = input("What's your name? ")
Hello world with variables more_names = ['Dave', 'Eve'] Note, how the careful use of " and ' in the previous line allows the
msg = 'Hello world!' names.extend(more_names) printing of the character '
print(msg) [1,2] + [3,4] # [1,2,3,4] print('Hello ' + name)
String concatenation Slicing a list (getting a range from a list) Prompting for a numerical value
first = 'Albert' names[2:4] # ['Charlie', 'Dave'] age = input('How old are you? ')
last = 'Einstein' numbers[4:5] # [5] age = int(age) # Convert to integer number
full_name = first + ' ' + last numbers[4:] # [5, 6]
print(full_name) # Albert Einstein numbers[0:5:2] # [1,3,5] (step: 2) pi = input("What's the value of pi? ")
print(first, last) # same result numbers[::-1] # [6,5,4,3,2,1] (step: -1) pi = float(pi) # Convert to decimal number
If-statements Functions Modules
If-statements allow conditional execution of your code. You can reuse code by defining functions (similar to print(…) ) Python comes with an extensive standard library of useful functions,
Simple tests Defining and invoking (calling) functions grouped into modules. Refer to the online documentations!