[go: up one dir, main page]

0% found this document useful (0 votes)
6 views20 pages

python_Lec-2-for student

The document provides an overview of Python control flow statements including if, elif, else, match, and loops (for and while). It explains the syntax and usage of these statements, along with examples and special cases such as nested statements and the use of break and continue. Additionally, it includes practical programming exercises to apply these concepts in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

python_Lec-2-for student

The document provides an overview of Python control flow statements including if, elif, else, match, and loops (for and while). It explains the syntax and usage of these statements, along with examples and special cases such as nested statements and the use of break and continue. Additionally, it includes practical programming exercises to apply these concepts in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Python: If Statement

a = 33
b = 200
if b > a:
print("b is greater than
a")

If statement, without indentation (will raise an error)


Python: elif Statement
a = 33
b = 33
The elif keyword is Python's way of saying
if b > a:
"if the previous conditions were not true, then print("b is greater than
try this condition". a")
elif a == b:
print("a and b are equal")
a = 200
The else keyword catches anything which b = 33
isn't caught by the preceding conditions if b > a:
print("b is greater than a")
elif a == b:
If you have only one statement to execute, print("a and b are equal")
you can put it on the same line as the if else:
print("a is greater than b")
statement: Short Hand of If

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

You can add if statements in the case evaluation


as an extra condition-check:

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

With the while loop we can execute a i = 1


while i < 6:
set of statements as long as a condition print(i)
is true. i += 1

Note: remember to increment i, or else the loop will continue forever.


Break Statement
i = 1
while i < 6:
With the break statement we can print(i)
stop the loop even if the while if i == 3:
condition is true: break
i += 1

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)

for x in range(2, 6):


Note that range(6) is not the values of 0 print(x)
to 6, but the values 0 to 5.
for x in range(2, 30, 3):
print(x)
it is possible to specify the starting value
by adding a parameter: range(2, 6),
it is possible to specify the increment value by
which means values from 2 to 6 (but not adding a third parameter: range(2, 30, 3)
including 6):
Python For loops
for x in range(6):
print(x)
else:
The else keyword in a for loop specifies print("Finally finished!")
a block of code to be executed when
for x in range(6):
the loop is finished. if x == 3: break
print(x)
else:
print("Finally finished!")

A nested loop is a loop inside a loop. adj = ["red", "big", "tasty"]


fruits =
The "inner loop" will be executed one ["apple", "banana", "cherry"]
time for each iteration of the "outer
for x in adj:
loop”. for y in fruits:
print(x, y)
Python For loops

for loops cannot be empty, but if you


for some reason have a for loop with for x in [0, 1, 2]:
pass
no content, put in the pass statement
to avoid getting an error.
Solve some real problem!

1. Grading System (if, elif, nested if)


Write a program that:
•Asks the user for a marks input (0 to 100).
•Based on the marks, print the grade:
• 90-100 → A+
• 80-89 → A
• 70-79 → B
• 60-69 → C
• 50-59 → D
• Below 50 → Fail
•Tricky part: If marks are exactly 100, print "Perfect Score!" in addition to
grade.
Solve some real problem!

2. Food Menu Selection (match)


Write a program that:
•Displays a menu:
1. Pizza
2. Burger
3. Pasta
4. Salad
•Takes user's choice (1-4).
•Use a match-case to print a different message for each food item.
•Tricky part: If they input something else, print "Invalid choice. Try again!"
Solve some real problem!
3. Guess the Secret Number (while loop with break/continue)
Write a program that:
•A secret number (say 7) is fixed.
•The user is asked to guess the number.
•If guess is correct, print "Congratulations!" and break the loop.
•If guess is wrong:
• If the guess is negative, continue without any message.
• Else print "Wrong! Try again."

4. Multiplication Table (for loop)


Write a program that:
•Asks the user for a number.
•Print its multiplication table from 1 to 10 using a for loop.
•Tricky part: If the number is negative, print "Tables not available for negative
numbers." and stop.
Solve some real problem!

5. Number Triangle (nested for loop)


Write a program that:
•Asks the user for a number (e.g., 5).
•Print a number triangle like this:
1
12
123
1234
12345
Use nested for loops: Outer loop for rows, inner loop for numbers
in the row.
Solve some real problem!

6. Odd or Even Game (nested if with while loop)


Write a program that:
•Keeps asking the user for a number until they enter 0.
•For each number:
• Check if it is even or odd.
• Then check:
• If even and greater than 50, print "Big even number!"
• If odd and less than 10, print "Tiny odd number!"
• Otherwise, just print "Even" or "Odd" normally.
Solve some real problem!

7. Match a Month (match-case + nested if)


Write a program that:
•Asks the user for a month number (1-12).
•Use a match-case to:
• Print the month's name (e.g., 1 → January).
•Then inside the matched case, using nested if, check:
• If the month is January, print "Happy New Year!"
• If the month is December, print "Merry Christmas!"

You might also like