0% found this document useful (0 votes)
11 views13 pages

Python Unit 3

Uploaded by

akram bennani
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)
11 views13 pages

Python Unit 3

Uploaded by

akram bennani
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/ 13

Python Programming

Ahmed ZELLOU
[email protected]

UM6P, IADT, 2024-2025.


Unit 3 Python
Repetitive instruction
n A loop allows to repeat instructions infinitely as needed.

while
while condition :
instructions

n While executes instructions repeatedly as long as a condition is


true
n Example : Write a program that displays a string (for example
"Python is a programming language") 10 times
i=0
while i< 10 :
print(i+1,"Python is a programming language")
2 i = i+ 1
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Exercises

1. Write a program that displays numbers from 1 to


10.
2. Write a program that calculates the sum of
integers from 1 to 100.
3. Write a program that calculates the sum and
product of integers from 1 to a limit given by the
user.
4. Write a program that enters two positive integers
and calculates the first to the power of the second.
5. Enter an integer, calculate n!.
6. Use a while loop to prompt the user for input
until they enter "exit". Print each input received
3
from the user.
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n When you want to repeat the same instruction or block of instructions
a given number of times.

for
commande
for i in range (start, stop, step) :
instructions
for est la plus appropriée.
start Optional. An integer number specifying at which position to start. Default is 0
stop Required. An integer number specifying at which position to stop (not
included).
step Optional. An integer number specifying the incrementation. Default is 1

n Examples : for i in range (5, 10):


print(i)
for i in range (100): # for i ranging from 0 to 99
print("Python is a programming language") for i in range (5, 10, 2):
print(i)
4
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Exercises

1. Calculate the sum of the first N terms of the series S = 1 +


1/2 + 1/3 + ... + 1/N
2. Calculate by successive multiplications XN of two natural
integers X and N entered on the keyboard.
3. Calculate P(X) for a given value X of a polynomial of degree
n P(X) = AnXn + An-1Xn-1 + ... + A1X1 + A0. Values of n, An,
... , A0 and X will be given on the keyboard.
4. Calculate the GCD of two natural integers entered on the
keyboard using EUCLID's algorithm.
5. Count the total number of digits in a number.
6. Write a program to enter a number and check if the number
is perfect or not. Example: 28 = 1 + 2 + 4 + 7 + 14.
7. Write a program to check if a given number is Strong or not.
A strong number is equal to the sum of the factorial of its
digits. Example: 145 = 1! + 4! + 5!
5
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n break
n The break statement allows to "break"
the execution of a loop.

n Example

n=int(input("Give a number:"))
for i in range(2, n):
if n % i == 0:
print(n, " is not a prime number")
break

n If we find a divisor, we stop the


loop with break.
n Otherwise, it is a prime
number.
6
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n continue
n The continue statement allows to move on to
the next loop prematurely.

n Example

n=0
while n <= 100 :
n += 1
if n % 2 != 0 : n The first instruction increases the value of
continue
print(n) the variable.
n If it is odd, we stop executing the body of
the loop and we go directly to the next
round.
n Otherwise, we display the value of n
7
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n else
n The else statement allows to execute some code
when the loop condition becomes false

n Example
number = 0
while number < 10:
number += 1
print("Current number is:", number)
else:
print("The loop has completed, number
reached 10.")

8
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n A string is a variable that stores a sequence of characters of type char.
n Example1 index 0 1 2 3 4 5 6 7 8 9
character I A D T U M 6 P \0

n When you want to iterate on string.

n
for
Example1
for char in string :
instructions on char

str = "Hello, how are you ?"


for c in str:
print(c)

n Example 2
n end to not add a line break str = "IADT UM6P"
for element in str[0:4:1]:
print(element, end =‘ ')
9
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Exercises

1. Write a loop that counts and prints the number of vowels in a


string.
2. Create a loop that reverses a string and prints the result.
3. Write a loop that converts all characters in a string to uppercase
and prints the new string.
4. Read a string and a characters and count the frequency of this
character.
5. Read a first string and second string and check if the second string
in the first.
6. Write a program that can read a string and check if this string is a
palindrome. Reminder: a palindrome is a string that is read from
left to right and from right to left in the same way. Example:
RADAR.

10
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Repetitive instruction
n Nested loops
n A loop can be nested inside another loop.
n Consist of an outer loop and one or more inner loops.

n Each time the outer loop is iterated, the inner loops are executed
in full.
for i in range(4):
n Example for j in range(4):
print("loop 1 iteration n:", i, "loop 2 iteration:", j)
11
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Unit 3 Python
Exercises

1. Create a program that prints a multiplication table (1 to 10).


2. Write a program to print a triangle pattern of asterisks. For
example, for 5 rows:

3. Read a number num and find all prime numbers between 1 and
num using nested loops.
4. Read a string and reverse each word in a sentence using nested
loops. Example, "IADT" become "TDAI".
5. Read a string, find and print all unique characters (ignoring
duplicates).
6. Read three colors (e.g., red, green, and yellow), generate and
print all possible color combinations.
7. Checks if a given 9x9 Sudoku board is valid.
12
@ A.ZELLOU, A.BELHAJ and A.ZINEDDINE, IADT, UM6P
Python

End Unit 3

13

You might also like