-
Notifications
You must be signed in to change notification settings - Fork 2
/
if_statement_examples.py
132 lines (107 loc) · 3.1 KB
/
if_statement_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
# Explanation video: http://youtu.be/pDpNSck2aXQ
# Variables used in the example if statements
a = 4
b = 5
c = 6
# Basic comparisons
if a < b:
print("a is less than b")
if a > b:
print("a is greater than than b")
if a <= b:
print("a is less than or equal to b")
if a >= b:
print("a is greater than or equal to b")
# NOTE: It is very easy to mix when to use == and =.
# Use == if you are asking if they are equal, use =
# if you are assigning a value.
if a == b:
print("a is equal to b")
# Not equal
if a != b:
print("a and b are not equal")
# And
if a < b and a < c:
print("a is less than b and c")
# Non-exclusive or
if a < b or a < c:
print("a is less than either a or b (or both)")
# Boolean data type. This is legal!
a = True
if a:
print("a is true")
if not a:
print("a is false")
a = True
b = False
if a and b:
print("a and b are both true")
a = 3
b = 3
c = a == b
print(c)
# These are also legal and will trigger as being true because
# the values are not zero:
if 1:
print("1")
if "A":
print("A")
# This will not trigger as true because it is zero.
if 0:
print("Zero")
# Comparing variables to multiple values.
# The first if statement appears to work, but it will always
# trigger as true even if the variable a is not equal to b.
# This is because "b" by itself is considered true.
a = "c"
if a == "B" or "b":
print("a is equal to b. Maybe.")
# This is the proper way to do the if statement.
if a == "B" or a == "b":
print("a is equal to b.")
# Example 1: If statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
print("Done")
# Example 2: Else statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
else:
print("It is not hot outside")
print("Done")
#Example 3: Else if statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature < 30:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")
# Example 4: Ordering of statements
# Something with this is wrong. What?
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature > 110:
print("Oh man, you could fry eggs on the pavement!")
elif temperature < 30:
print("It is cold outside")
else:
print("It is ok outside")
print("Done")
# Comparisons using string/text
# Note, this example does not work when running under Eclipse
# because the input will contain an extra carriage return at the
# end. It works fine under IDLE.
userName = input("What is your name? ")
if userName == "Paul":
print("You have a nice name.")
else:
print("Your name is ok.")