-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathconditionals_examples.py
92 lines (82 loc) · 2.39 KB
/
conditionals_examples.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Conditionals Examples
# if , elif , else
# Condition Testing operators
# == checks equality
# != not equal to
# > greater than
# < less than
# <= smaller than equal to
# >= greater than equal to
# Let's start with if
# Let's say we have a robot and we want it to move forward by 20 steps
robotMoving = True
if robotMoving == True:
print('Move 20 steps')
# Okay what about if robot is not moving
# This is where else : statement comes in
robotMoving = False
else :
print('You are not moving')
# elif
# What if there are multiple things to check like if
# We need more if statements but each if will run
# Thus we need elif(else if) statement
# Only else : statements dont contain values to check
start = str(input('Enter a or b or c : '))
# we need to check if entered value is equal to a or b or c
if start == 'a':
print('You entered ' + start)
elif start == 'b':
print('You entered ' + start)
elif start == 'c':
print('You entered ' + start)
else:
print('Invalid Input')
# Another Example
numsA = input('Enter a : ')
numsB = input('Enter b : ')
if numsA > numsB:
print(str(numsA) + ' is greater than ' + str(numsB))
elif numsA < numsB:
print(str(numsB) + ' is greater than ' + str(numsA))
else :
print('Numbers are equal')
# nested if else
# You can nest conditionals inside other conditionals
startProgram = True
numsA = input('Enter a : ')
numsB = input('Enter b : ')
if startProgram == True:
if numsA > numsB:
print(str(numsA) + ' is greater than ' + str(numsB))
elif numsA < numsB:
print(str(numsB) + ' is greater than ' + str(numsA))
else :
print('Numbers are equal')
else:
print('Can\'t access program')
# and or not are helpful
# and returns True if both conditions are true
# or returns True if one of both condition is true
# not returns True if condition is false and False if condition is True
# Think you have bike and you want to go out on a ride but you have to check if you have enough fuel
# remember not evaluates first then and then or
# and
youHaveBike = True
fuel = 30
if youHaveBike == True and fuel > 65:
print('You are good to go')
else:
print('You need to refill fuel')
# or
extraFuel = True
if extraFuel = True or fuel > 65:
print('You are good to go')
else:
print('You need to refill fuel')
# not
number = 12
if not(number != 11):
print('True')
else:
print('False')