-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-conditionals.py
More file actions
62 lines (43 loc) · 1.33 KB
/
7-conditionals.py
File metadata and controls
62 lines (43 loc) · 1.33 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
# If/ Else conditions are used to decide to do something based on something being true or false
x = 10
y = 8
# Comparison Operators (==, !=, >, <, >=, <=) - Used to compare values
# simple if
if x == y:
print(f'{x} is equal to {y}')
# If/else
if x > y:
print(f'{x} is greater than {y}')
else:
print(f'{x} is less than {y}')
# If/elif/else
if x > y:
print(f'{x} is greater than {y}')
if x == y:
print(f'{x} is equal to {y}')
else:
print(f'{x} is less than {y}')
# nested If
if x > 2:
if x<=10:
print(f'{x} is greater than {y} and x is greater than 2')
# Logical operators (and, or, not) - Used to combine conditional statements
if x > 2 and x <= 10:
print(f'{x} is greater than {y} and x is greater than 2')
#not
if not (x == y ):
print(f'{x} does not equal to {y}')
# Membership Operators (not, not in) - Membership operators are used to test if a sequence is presented in an object
numbers = [90, 2, 1, 4, 5, 6, 20, 10]
if x in numbers:
print(x in numbers)
if y not in numbers:
print(y 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:
a = 10
b = 10
if a is b:
print(a is b)
c = 231
if a is not c:
print(a is c)