0% found this document useful (0 votes)
4 views21 pages

Informatique4-Chapitre3

Chapter 3 covers conditional structures in Python, including if statements, if-else, if-elif-else, and nested if-else statements, explaining how they control the flow of code based on conditions. It also discusses boolean values, comparison operators, logical operators, and predicates that return boolean results. Additionally, the chapter introduces loops (while and for), nested loops, and keywords like break and continue, along with exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Informatique4-Chapitre3

Chapter 3 covers conditional structures in Python, including if statements, if-else, if-elif-else, and nested if-else statements, explaining how they control the flow of code based on conditions. It also discusses boolean values, comparison operators, logical operators, and predicates that return boolean results. Additionally, the chapter introduces loops (while and for), nested loops, and keywords like break and continue, along with exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 3

Conditional structures

2024/2025

SAYAH Chaimaa
[email protected]
Python conditions and if statements

Conditional structures in Python allow the execution of different blocks of code


depending on certain conditions. The main conditional statements in Python are :
1. If conditional statement
The if statement executes a block of code only if a certain condition is true.

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

2. If..else conditional statement


The “if else” conditional statement consists of two blocks of code :
• The first block associated with “if” is executed only if the condition is “True”.
▪ The else block is executed if the previous condition is “False”.

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

In Python, boolean values are :


• True
• False
These are case-sensitive, so true and false
(lowercase) are invalid in Python.

7
Comparison operators
Comparison operators are used to compare values and return a Boolean result.

Operator Meaning Example Result


== Equal to 5 == 5 True
!= Not equal to 5 != 8 True
> Greater than 10 > 3 True
>= Greater or equal to 10 >= 10 True
< Less than 10 < 80 True
<= Less or equal to 80 <= 80 True

8
Logical operators
Logical operators are used to combine multiple conditions.

Operator Meaning Example Result


and Both conditions must be true (5 > 3 and 7 > 1) True
or At least one condition must be true (5 > 3 or 7 < 1) True
not Reverses the Boolean value not(5 > 3) False

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?

You might also like