Informatique4-Chapitre3
Informatique4-Chapitre3
Conditional structures
2024/2025
SAYAH Chaimaa
[email protected]
Python conditions and if statements
1
Python conditions and if statements
• Python uses indentation (white space at the beginning of a line) to indicate a block
of code, if the indentation is not respected an error will be raised.
• : indicates the end of the condition in an if statement and marks the beginning of
the indented block of code that should be executed if the condition is true.
2
Python conditions and if statements
3
Python conditions and if statements
3. If..Elif..else
“Elif” statement stands for “else if” in
Python. It allows multiple conditions to be
checked.
• If condition is evaluated first, if it’s
“False” Python inspects each “elif”
code block in sequential order until
one of them is “True”.
• If all the “elif” conditions are “False”,
the “else” block is executed.
4
Python conditions and if statements
4. Nested if..else
Nested “if..else” means an “if..else” statement in another “if” statement.
The inner “if..else” statement is executed only if the outer if condition is “True”.
If the outer condition is “False” the inner condition is never checked.
5
Limitations of If conditional statements
• Too many nested if statements are hard to read and maintain and mistakes in
indentation can cause logic or syntax errors.
• No pattern matching before Python 3.10 : older versions lacked match-case and
required to use multiple if..elif..else statements.
6
Boolean values
7
Comparison operators
Comparison operators are used to compare values and return a Boolean result.
8
Logical operators
Logical operators are used to combine multiple conditions.
9
Predicates
Predicates are expressions that returns a Boolean value, they are used to check if the
input meets some condition
Some Python predicates are :
Predicate Description Example Result
isinstance(obj, Checks if obj is of type type isinstance(“Hello”, str) True
type)
str.islower() Checks if all letters in str are “hello”.islower() True
lowercase
str.isupper() Checks if all leters in str are “YES”.isupper() True
uppercase
all(iterable Returns True if all elements all([1,”Hello”,True])
are truthy
any(iterable) Returns True if at least one any([“Hello”, 1, False]) True
element is truthy 10
Exercise1
Write a program that asks the user to enter his score and checks and displays his
grade.
Grades can be :
• 90-100 → A.
• 80-89 → B
• 70-79 → C
• 60-69 → D
• < 60 → F
11
Solution : exercise 1
12
Chapter 4
Loops
While loop
The “while” loop keeps running as long as a condition is True.
Syntax :
while condition:
#code to execute
13
For loop
The “for” loop is used for iterating
over a sequence (like a list, tuple,
string or range).
Syntax :
for variable in sequence:
# Code to execute
14
Nested loops
A nested loop is a loop inside a loop.
The “inner loop” will be executed one time for each iteration of the “the outer loop”.
15
Keywords break & continue
• The “continue” statement skips the current iteration and moves to the next one.
16
Keywords break & continue
• The “break” statement can stop the loop before it has looped though all items.
17
Exercise : Number guessing game
Create a program that generates a random number between 1 and 10, and ask the
user to guess it.
• If the guess is too high , print “Too high! Try again”
• If the guess is too low, print “Too low! Try again”
• If the guess is correct, print “Congratulations, you guessed the number!”
Hint
• Import the module random and random.randint(1,10) to generate a random
number.
Merci!
Avez-vous des questions?