-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_conditionals.py
More file actions
67 lines (48 loc) · 1.36 KB
/
07_conditionals.py
File metadata and controls
67 lines (48 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# If/ Else conditions are used to decide to do something based on something being true or false
x = 21
y = 20
# Comparison Operators (==, !=, >, <, >=, <=) - Used to compare values
# Simple if
if x > y:
print(f'{x} is greater than {y}')
# If/else
if x > y:
print(f'{x} is greater than {y}')
else:
print(f'{y} is greater than {x}')
# elif
if x > y:
print(f'{x} is greater than {y}')
elif x == y:
print(f'{x} is equal to {y}')
else:
print(f'{y} is greater than {x}')
# Nested if
if x > 2:
if x <= 10:
print(f'{x} is greater than 2 and less than or equal to 10')
# Logical operators (and, or, not) - Used to combine conditional statements
# and
if x > 2 and x <= 10:
print(f'{x} is greater than 2 and less than or equal to 10')
# or
if x > 2 or x <= 10:
print(f'{x} is greater than 2 or less than or equal to 10')
# not
if not(x == y):
print(f'{x} is not equal to {y}')
# Membership Operators (not, not in) - Membership operators are used to test if a sequence is presented in an object
numbers = [1,2,3,4,5]
# in
if x in numbers:
print(x in numbers)
# not in
if x not in numbers:
print(x not in numbers)
# Identity Operators (is, is not) - Compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
# is
if x is y:
print(x is y)
# is not
if x is not y:
print(x is not y)