-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_exp.py
More file actions
244 lines (204 loc) · 6.47 KB
/
Copy pathconditional_exp.py
File metadata and controls
244 lines (204 loc) · 6.47 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Python If...Else Conditional Statements
# if statements are used to execute a block of code based on a condition.
a=33
b=200
if b > a:
print("b is greater than a")
# In this example we used two variables, a and b, which are used as the part of the if statement to check whether b is greater than a.
# If the condition evaluates to True, the code block inside the if statement is executed.
# If the condition evaluates to False, the code block inside the if statement is skipped.
# Indentation is important in Python. The code block inside the if statement must be indented.
# If the code block is not indented, Python will raise an IndentationError.
# If statement without indentation raises an error
# elif - The elif keyword is python's way of saying 'if the previous conditions were not true, then try this condition'.
a = 33
b = 200
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
# Else - The else keyword is python's way of saying 'if the previous conditions were not true, then do this'.
a=200
b=33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
# You can also have an else statement without an elif statement.
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
# one line if statement
a = 200
b = 33
if a > b: print("a is greater than b")
# one line if else statement
a=2
b=330
print("A") if a > b else print("B")
# You can also have multiple else statements on the single line.
a=330
b=330
print("A") if a > b else print("=") if a == b else print("B")
# And - The and keyword is a logical operator, and is used to combine conditional statements.
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
# Or - The or keyword is a logical operator, and is used to combine conditional statements.
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
# Nested If - You can have an if statement inside another if statement.
x=41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
# The pass statement - The pass statement is a null operation; nothing happens when it executes.
a=33
b=200
if b > a:
pass # Do nothing, just a placeholder
# Match Case Statement - Python 3.10 introduced the match-case statement, which is similar to switch-case statements in other languages.
# It allows you to match a variable against different values and execute corresponding code blocks..
day = 4
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
# Use the underscore character _ as the last case value if you want a code block to execute when there are not other matches.
day =4
match day:
case 6:
print("Today is Saturday")
case 7:
print("Today is Sunday")
case _:
print("Looking forward to the Weekend!") # This will execute if no other case matches
# Combine values in a case using the | character.
day = 3
match day:
case 1 | 2 | 3 | 4 | 5:
print("It's a weekday")
case 6 | 7:
print("It's the weekend!")
case _:
print("Invalid day")
# If statement as a Guards.
# You can use an if statement as a guard in a case block to add an additional condition that must be met for that case to execute.
month =5
day =4
match day:
case 1 | 2 | 3 | 4| 5 if month ==4:
print("Weekday in April")
case 1 | 2 | 3 | 4| 5 if month ==5:
print("Weekday in May")
# While loop
i = 1
while i < 6:
print(i)
i += 1 # i = i + 1
# Print x as long as x is less than 9
x = 0
while x < 9:
print(x)
x = x + 1
# Exit the loop when x is 3
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
# Continue - The continue statement is used to skip the current iteration of a loop and continue with the next iteration.
# Continue to the next iteration of the loop when i is 3
i = 0
while i<6:
i+=1
if i==3:
continue
print(i)
# The else block in a while loop - The else block in a while loop is executed when the condition of the while loop becomes false.
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
# for loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Looping through a string
for x in "banana":
print(x)
# The break statement - The break statement is used to exit a for loop prematurely.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break # Exit the loop when x is "banana"
print(x)
# Exit the loop when x is 'banana' , but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break # Exit the loop when x is "banana"
print(x)
# The continue statement - The continue statement is used to skip the current iteration of a loop and continue with the next iteration.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue # Skip the rest of the loop when x is "banana"
print(x)
# The range() function - The range() function is used to generate a sequence of numbers.
# It can be used in for loops to iterate over a sequence of numbers.
for x in range(6):
print(x)
# Using the start parameter
for x in range(2, 6):
print(x)
# The range() function defaults to incrementing by 1, but you can specify a different increment using the step parameter.
for x in range(2, 30, 3):
print(x)
# Else block in a for loop - The else block in a for loop is executed when the loop completes all iterations without encountering a break statement.
for x in range(6):
print(x)
else:
print("Loop completed without break")
# Break the loop when x is 3, and see what happens with the else block
for x in range(6):
if x == 3:break
print(x)
else:
print("Loop completed without break")
# Nested loops - A nested loop is a loop inside another loop.
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
# The pass statement - The pass statement is a null operation; nothing happens when it executes.
for x in [0, 1, 2]:
pass # Do nothing, just a placeholder