python_Lec-2-for student
python_Lec-2-for student
a = 33
b = 200
if b > a:
print("b is greater than
a")
a = 2
if a > b: print("a is greater than b = 330
b") print("A") if a >
Python: if Statement
a = 200
b = 33
AND operation c = 500
if a > b and c > a:
print("Both conditions are
True")
a = 200
OR operation b = 33
c = 500
if a > b or a > c:
print("At least one of the
conditions is True")
a = 33
b = 200
NOT operation if not a > b:
print("a is NOT greater than
b")
Python: Nested If Statement
x = 41
if x > 10:
print("Above ten,")
You can have if statements inside if statements, if x > 20:
this is called nested if statements. print("and also above
20!")
else:
print("but not above
20.")
a = 33
if statements cannot be empty, but if you for
b = 200
some reason have an if statement with no
content, put in the pass statement to avoid if b > a:
getting an error pass
Python: Match Statement
Instead of writing many if..else statements, you day = 4
can use the match statement. match day:
The match statement selects one of many code case 1:
blocks to be executed. print("Monday")
case 2:
This is how it works: print("Tuesday")
•The match expression is evaluated once. case 3:
print("Wednesday")
•The value of the expression is compared with the
case 4:
values of each case. print("Thursday")
•If there is a match, the associated block of code case 5:
is executed. print("Friday")
case 6:
match expression:
print("Saturday")
case x:
case 7:
code block
print("Sunday")
case y:
code block
case z:
code block
Python: Match Statement
day = 4
Use the underscore character _ as the last case match day:
value if you want a code block to execute when case 6:
there are not other matches print("Today is
Saturday")
case 7:
print("Today is Sunday")
case _:
print("Looking forward to
Use the pipe character | as an or operator in the Weekend")
the case evaluation to check for more than one
value match in one case day = 4
match day:
case 1 | 2 | 3 | 4 | 5:
print("Today is a
weekday")
case 6 | 7:
print("I love weekends!")
Python: Match Statement
month = 5
day = 4
match day:
case 1 | 2 | 3 | 4 | 5 if month == 4:
print("A weekday in April")
case 1 | 2 | 3 | 4 | 5 if month == 5:
print("A weekday in May")
case _:
print("No match")
The value _ will always match, so it is important to place it as the last case to make
it behave as a default case.
Python Loops
Python has two primitive loop
commands:
•while loops
•for loops
i = 0
while i < 6:
With the continue statement we can i += 1
stop the current iteration, and if i == 3:
continue with the next: continue
print(i)
i = 1
while i < 6:
print(i)
With the else statement we can run i += 1
a block of code once when the else:
condition no longer is true: print("i is no longer less than
Python For loops
A for loop is used for iterating over a
sequence (that is either a list, a tuple, fruits =
a dictionary, a set, or a string). ["apple", "banana", "cherry"]
for x in fruits:
print(x)
With the for loop we can execute a
set of statements, once for each item
in a list, tuple, set etc fruits =
["apple", "banana", "cherry"]
for x in fruits:
print(x)
With the break statement we can stop if x == "banana":
break
the loop before it has looped through fruits =
all the items: ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
Python For loops
fruits =
With the continue statement we can ["apple", "banana", "cherry"]
stop the current iteration of the loop, for x in fruits:
if x == "banana":
and continue with the next: continue
print(x)
To loop through a set of code a specified
number of times, we can use for x in range(6):
the range() function. print(x)