42 == 42
True
42 == 123
False
42 == 42.0
True
42 == 42.000000000000001
True
bool
Type¶True
True
type(True)
bool
False
False
type(False)
bool
None
type(None)
NoneType
[True, False, None, True, False, None]
[True, False, None, True, False, None]
42 == 123
False
42 != 123
True
42 < 123
True
42 <= 123
True
42 > 123
False
42 >= 123
False
a = 42
b = 87
a > 5 and b <= 100
True
(a > 5) and (b <= 100)
True
a <= 5 or not b > 100
True
(a <= 5) or not (b > 100)
True
5 < a and a < 87
True
5 < a < 87
True
(a - 40) and (b < 100)
True
bool(a - 40)
True
bool(a - 42)
False
bool(a - 44)
True
bool(None)
False
bool([])
False
bool([False])
True
bool("")
False
bool("Lorem ipsum dolor sit amet.")
True
0 or 1
1
1 or 2
1
0 or 1 or 2
1
False or [] or 0
0
0 and 1
0
1 and 0
0
1 and 0 and 2
0
1 and 2 and 3
3
if
Statement¶numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]
import random
2
, 3
, both, or none?¶number = random.choice(numbers)
if number % 2 == 0:
print(number, "is divisible by 2 only")
elif number % 3 == 0:
print(number, "is divisible by 3 only")
elif number % 2 == 0 and number % 3 == 0:
print(number, "is divisible by 2 and 3")
else:
print(number, "is divisible by neither 2 nor 3")
6 is divisible by 2 only
2
, 3
, both, or none?¶number = random.choice(numbers)
if number % 3 == 0 and number % 2 == 0:
print(number, "is divisible by 2 and 3")
elif number % 3 == 0:
print(number, "is divisible by 3 only")
elif number % 2 == 0:
print(number, "is divisible by 2 only")
else:
print(number, "is divisible by neither 2 nor 3")
6 is divisible by 2 and 3
2
, 3
, both, or none?¶number = random.choice(numbers)
if number % 6 == 0:
print(number, "is divisible by 2 and 3")
elif number % 3 == 0:
print(number, "is divisible by 3 only")
elif number % 2 == 0:
print(number, "is divisible by 2 only")
else:
print(number, "is divisible by neither 2 nor 3")
6 is divisible by 2 and 3
if
-clause is mandatory¶if random.random() > 0.5:
print("You read this as often as you see heads when tossing a coin")
You read this as often as you see heads when tossing a coin
number = random.choice(numbers)
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
6 is even
number = random.choice(numbers)
if number % 2: # Note the opposite meaning!
print(number, "is odd")
else:
print(number, "is even")
6 is even
if
Statements¶number = random.choice(numbers)
# Coin is heads.
if random.random() > 0.5:
if number % 2 == 0:
print(number, "can be divided by 2 without a rest")
else:
print(number, "divided by 2 results in a non-zero rest")
# Coin is tails.
else:
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
6 can be divided by 2 without a rest
if
Statements¶number = random.choice(numbers)
coin_is_heads = random.random() > 0.5
number_is_even = number % 2 == 0
if coin_is_heads and number_is_even:
print(number, "can be divided by 2 without a rest")
elif coin_is_heads and not number_is_even:
print(number, "divided by 2 results in a non-zero rest")
elif not coin_is_heads and number_is_even:
print(number, "is even")
else:
print(number, "is odd")
6 can be divided by 2 without a rest
if
Expression¶$ y = f(x) = \begin{cases} 0, \text{ if } x \le 0 \\ x, \text{ otherwise} \end{cases} $
x = 3
if x <= 0:
y = 0
else:
y = x
y
3
x = 3
y = 0 if x <= 0 else x
y
3
x = 3
y = max(0, x)
y
3
try
Statement¶user_input = random.choice([0, 1, 2, 3, 4, 5])
1 / user_input
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[78], line 3 1 user_input = random.choice([0, 1, 2, 3, 4, 5]) ----> 3 1 / user_input ZeroDivisionError: division by zero
user_input = random.choice([0, 1, 2, 3, 4, 5])
try:
print("The result is", 1 / user_input)
except:
print("Something went wrong")
Something went wrong
user_input = random.choice([0, 1, 2, 3, 4, 5])
try:
print("The result is", 1 / user_input)
except ZeroDivisionError:
print("Something went wrong")
Something went wrong
user_input = random.choice([0, 1, 2, 3, 4, 5])
try:
result = 1 / user_input
except ZeroDivisionError:
print("Oops. Division by 0. How does that work?")
else:
print("The result is", result)
finally:
print("I am always printed")
Oops. Division by 0. How does that work? I am always printed