Python Programming Exercises Guide
Python Programming Exercises Guide
Exercise 1:
Write a program in Python that asks the user to enter their
name and display his name with a welcome message!
Exercise 2:
Write a program in Python that asks the user to enter two numbers a
and b and display their sum: a + b
Exercise 3:
Write a program in Python that asks the user to enter two numbers a
and b and display their maximum.
Exercise 4:
Write a program in Python that displays the first 100 numbers
integers
Exercise 5:
Write a program in Python that asks the user to enter their
whole number and display whether this number is even or odd.
Exercise 6:
Write a program in Python that asks the user to enter their age.
and to display the message 'you are of legal age!' if the entered age is greater than or
equal to 18 and the message 'you are a minor!' if the entered age is less than 18
Exercise 7:
Write a program in Python that asks the user to enter 3 numbers x, y and
and to display their maximum
Exercise 8:
Write a program in Python that asks the user to enter a number
integer n and to display the value of the sum 1 + 2 + ... + n =
Exercise 9:
Write a program in Python that asks the user to enter a number
integer n and display n!
Exercise 10:
Write a program in Python that asks the user to enter the radius of a
circle and return its area and perimeter.
Exercise 11:
Write a program in Python that asks the user to enter a number.
integer n and display all the divisors of this number.
Exercise 12:
1) - Write a program in Python that asks the user to enter a number
an integer n and display the multiplication table for this number.
PROF: Y. MERZAK
2) - Improve the program so that it displays the multiplication tables of all the
numbers between 1 and 9
Exercise 13:
Write a program in Python that asks the user to enter two
integers a and b and display the quotient and the remainder of the division
euclidean of a by b.
Exercise 14:
Write a program in Python that asks the user to enter a
integer n and display whether this number is a perfect square or not
Exercise 15:
Write a program in Python that asks the user to enter a
integer number n and display whether this number is prime or not.
Exercise 16:
Write a program in Python that allows you to browse and display the
characters of a variable of the string type. Example for s = "Python"
, the program displays the characters:
P
y
t
h
o
n
Exercise 17:
Write a program in Python that displays for a string of characters
given, the number of occurrences of each character in the
[Link] for the string = "[Link]" the program must
display
The character: " P " appears 1 time in the string s
The character: " y " appears 1 time in the string s.
The character: " t " appears 1 time in the string s
The character: " h " appears 1 time in the string s
The character: " o " appears 2 times in the string s
The character: "n" appears 1 time in the string s
The character: " . " appears 1 time in the string s
The character: " r " appears 1 time in the string s
The character: " g " appears 1 time in the string s
Exercise 18:
Write a program in Python that asks the user to enter a string of
character s and return a message indicating whether the string contains the letter 'a'
while indicating its position in the string. For example, if the user types the string s =
The letter 'a' is found at position: 1 The letter 'a'
is located at position: 4
Exercise 19:
Write a program in Python that lists the strings that make up the
liste l = ["laptop", "iphone", "tablet"] tout en indiquant la longueur de chaque chaine.
Exercise 20:
PROF: Y. MERZAK
Write a program in Python language that allows swapping the first and the
last character of a given string.
Exercise 21:
Write a program in Python language that counts the number of
vowels in a given string. Example for the string
anticonstitutionally the program must return the following message: The
the word anticonstitutionally has 10 vowels.
Exercise 22:
Write a program in Python that returns the first word of a
Python is a wonderful programming language
programming, the program must return Python
Exercise 23:
Write a program in Python that asks the user to enter their name.
of a file and return its extension. For example, if the user inputs
[Link] the program returns the message 'The file extension'
.pdf .
Exercise 24:
A palindrome is a word whose order of letters remains the same when read from left.
to the right or from right to left. For example: 'laval', 'radar', 'sos'... are
palindromes. Write a program in Python that asks the user to enter a
word and to send it back if it is a palindrome or not?
Exercise 25 :
Write a program that asks the user to enter a word and to it
let's return it in reverse. For example, if the user types the word python, the program will
typeonwhere
Exercise 26:
Write a program that asks the user to enter a text and returns it.
all the words that begin with the letter a.
Exercise 27:
Write a program in Python in the form of a function that calculates the sum of
elements of a list of numbers. And another that allows multiplying all the
elements of a list of numbers.
Exercise 28:
Write a Python program that tests if a list is empty or not. Same question
for a string.
Exercise 29:
Write a Python program that allows you to remove duplicate elements from a
list.
Exercise 30:
Write a function in Python that allows us to compare two lists and ...
indicate whether these two lists have a common value or not.
PROF: Y. MERZAK
Exercise 31:
Write a Python program that extracts the list of even integers and the list
odd integers from a list of numbers.
Exercise 32:
Write a program in Python that returns all the lists obtained by permuting.
the terms of a given list.
Exercise 33:
Write a program in Python that asks the user to enter a string of
characters and display the characters at even indices. Example for the strings =
Python, the program returns 'Pto'.
Exercise 34:
Etant donnée la liste des notes des élèves : notes = [12 , 04 , 14 , 11 , 18 , 13 , 07,
10, 05, 09, 15, 08, 14, 16] Write a Python program that allows you to extract from
this list and create another list that contains only the grades above the
my average (the grades >= 10)
Exercise 35:
Write a program in Python that allows you to transform a entered URL.
keyboard in a hyperlink.
Exercise 36:
Write a program in Python to remove spaces
multiples in a chain
Exercise 37:
Write a Python program that allows grouping words in a list.
common to two chains s1 and s2.
Exercise 38:
Write a Python program that allows you to search for the longest word in a
chains
Exercise 39:
Write a Python program that counts the number of words in a
chain s. It is assumed that the text is well formed (one space after each
punctuation and no space before punctuation)
Exercise 40:
Write a Python program that swaps the first and the last word.
Example sis = "Python is a programming language", the program returns
lachaine s2 = "programmation est un langage de Pyhon".On suppose que le
text is well-formed ( a space after each punctuation and no space before the
punctuation).
Exercise 41:
Create a Python function called nombreDivisibles, which applies to a list of
names and an integer n, and which returns the number of elements in the list that
are divisible by n.
Exercise 42:
PROF: Y. MERZAK
Create a function in Python called nombreOccurences() that applies to a list L and
an element x as parameters that returns the number of times the element x
appears in the list L without using the count() function.
Exercise 43:
Create a Python function named InsertEtoile() that places "stars" in between
each character of a provided input string. Example for the string s =
Python, Insert Star(s) gives P*t*h*o*n
Exercise 44:
Create a Python function named toutEnMajuscule() that transforms
a list of strings in another list made up of uppercase strings.
Example if L = ["Python", "is", "a", "language", "of",
programming
# La fonction doit retourner la lste L2 = ["PYTHON", "EST" , "UN",
"LANGAGE", "DE", "PROGRAMMATION"]
Exercise 45:
Write a function in Python that takes a string as an argument.
which returns the number of lowercase and uppercase letters contained in the string.
Exercise 46
Write a function in Python that provides the list of digits of a
number written in base 10 without converting the number to string and without using any
predefined function.
Exercise 47:
Write an algorithm in Python that returns a list made up of
common words in two texts. Example if T1 = "Python is a programming language of
programming and T2 = "Python is object-oriented", the program should return the
list of common words L = ['Python', 'is'].
Exercise 48:
Write a Python program in the form of a function that takes as a parameter
a string s, and which returns 'True' if the first character is identical to the last
character of the string, and returns 'False' otherwise.
For example, if sis = 'render', the function must return true.
Sis = "Python", la fonction renvoieFalse
Exercise 49:
Write a program in Python in the form of a function that takes as a parameter
a tuple of string(s, s1) that returns the index of the first
Occurrence found in the chain without using the index() method.
The function must return -1 if s is not found in the string.
Example sis = "Python programming" ets1 = "thon", the function returns index 2
Exercise 50* :
Write a Python program in the form of a function that takes a parameter
string s and returns the first repeated character in the string s.
Exemple: sis = "django framework", la fonction renvoie le caractère'a'
PROF: Y. MERZAK
Exercise 51 * :
Write a Python program in the form of a function that takes a parameter
a set that returns the list of all the repeated characters in the string s.
Example: sis = "python language", the function returns the list:
[n, a 'g']
Exercise 52:
Write an algorithm in Python that determines the set of characters that
comprise a string s.
Example sis = 'Python programming', the algorithm returns the set of
characters:
{'p', I ' ', r o 'm 't a h y P g 'n
Exercise 53:
Write an algorithm in Python that determines the set of words that compose
a text string s.
Example sis = 'Python is more powerful than Java', the algorithm returns the set:
Java is more powerful than Python.
Exercise 54:
Write a Python program that, from a given list, creates a
text file whose lines are the elements of this list.
Example if the list is:
List_programming_books= Python programming books Java
programming books C ++ programming books C # programming
books
the generated file will be formed by the lines:
Python programming books
Java programming books
C ++ programming books
C# programming books
Exercise 55:
Write a Python program that asks the user to enter numbers
separated by
semicolon ';' and generates a list made up of the entered numbers.
Exercise 56:
Write a program in Python in the form of a function that takes a parameter
tuple formed of two strings (s1, s2) and which returns the list of common characters
to s1 and s2.
Example: sis1 = 'Python language' ets2 = 'Java Programming', the function
send back
P o 'n' ' ', a g
Exercise 57:
Write a program in Python in the form of a function that takes a parameter
tuple made up of two strings (s1, s2) that returns the list of characters of the
string s1 that is not found in string s2.
PROF: Y. MERZAK
Exemple: sis1 = 'Python language'ets2 = 'Java Programming', la fonction
return the list:
y 't h
Exercise 58:
Write a Python program in the form of a function that takes parameters
a pair of strings (s1, s2)
and returns the string s obtained by alternatively concatenating the
characters of s1 and s2.
Exemple: pour(s1, s2) = ("Python", "Java"), la fonction renvoie la chaînes =
PJyatvha
Exercise 59:
Write a Python program in the form of a function that counts the number of times
that a character appears in a string without using predefined functions.
Exercise 60:
Write a Python program in the form of a function that takes a parameter
string s and which returns the list of numeric characters contained in the string s.
Example sis = 'Python 3.0, released in 2008 and completely revised in 2020'
the function returns the list: [3, 0, 2, 0, 0, 8, 2, 0, 2, 0].
Exercise 61
Write a Python program that determines the list of all characters
maximum occurrence in a given string.
Example: sis = "Programming", the algorithm returns the list:
r g 'm
Exercise 62:
Using ASCII code, write a Python program that returns and displays the list of
all uppercase letters [A, B, C, ..., Z] and the list of all characters
lowercase [a, b, c, ..., z]
Exercise 63:
Using Exercise 62, create a Python program in the form of a function that
takes a string s as a parameter and returns the same string in uppercase without
use the upper() method.
You can use the methods chr() and ord().
Exercise 64:
Write a Python program in the form of a function that takes as a parameter
a string s that returns another string obtained from s by removing
the spaces at the beginning of the string s and the spaces at the end of the string s without
use the lstrip() method or any predefined method.
Exemple si s = " Hello , the function returns the string: "Hello"
Exercise 65:
Write a Python program in the form of a function that takes a parameter a
string s and returns a string obtained from the string s by transforming
each uppercase letter to lowercase and vice versa without using
the swapcase() method.
Example sis = "Hello World!", the function returns the string "hELLO wORLD!"
PROF: Y. MERZAK
Exercise 67:
Write a python program in the form of a function that takes a tuple as a parameter.
unleash (s, s1)
and which returns the index of the first occurrence of 1 found in the string s
without using any predefined function.
The function must return -1 if s1 is not found in the string s
For example, if s = "Python language" and s1 = "lan", the function returns the index 7
Exercise 68:
Write a Python algorithm that calculates the number of common characters in two
chains s1 and s2.
Example sis = 'Hello' ets2 = 'World', the common characters are 'l' and 'o', so
the algorithm returns 2.
Exercise 70:
Write a program in Python that extracts from a data string, the list of all
the words whose length is less than or equal to 4. Example sis = "The language of
Python programming is open source and very easy to learn.
the program must return the list:
L= The of is open and very 'à']
Exercise 71:
Write a Python program that determines the minimum digit in a string
data s.
Examplesis = "Python3.7 is more powerful than Python2.7", the algorithm must
Return 2. We assume that the string s contains no negative numbers.
Exercise 72:
Write an algorithm in Python that allows you to examine if an occurrence is
present in a given string or not.
Exercise 73:
Write a Python algorithm that removes all the vowels from a given string.
s.
Example if s = "Python is high level programming language", the algorithm
Python is a high level programming language
Exercise 74:
Write a Python program that replaces the characters at odd indexes of a
given chain
For example: sis = "Python", the algorithm returns the string: "P#t#o#"
Exercise 75:
Write a python algorithm that determines the list of all characters
common to two chains s1 and s2 without repetition.
Exemple: sis1 = "langage Python"ets2 = "Programmation",
the algorithm returns the list:
P o 'n' a 'g']
PROF: Y. MERZAK
(the character 'g' must be added only once even if shared twice)
Exercise 76:
Write a python algorithm that transforms a given string s by swapping the
second character (s[1])
with the penultimate character. We assume that len(s) is greater than or equal to 4.
Exemple: sis = "Python", l'algorithme renvoie la chaîne:"Pothyn".
Exercise 77:
Write a Python algorithm that determines the first index of an occurrence
existing in a chain
give s without using predefined methods like find() or find()...
The algorithm should return -1 if the occurrence does not exist in the string s.
Example: sis = "Python programming language" and occ = "prog" the algorithm
returns 7
Exercise 78:
Write a python algorithm that returns the list of all the indexes where
An occurrence has been found.
in a given linked list without using any predefined methods like find(),
rfind(), index()... The function returns the empty list [], if the occurrence occ does not exist
in s.
Example:
sis = Python es un lenguaje interpretado. Python es de código abierto. Python es fácil
to learn
occ = "Python", la fonction renvoie la liste:
1
2 [0, 35, 58]
3
Exercise 80:
Write a python algorithm that determines the list of all common words in two
texts T1 and T2
without repetition.
Example if:
T1 = “Python is open source programming language. Python was created on
Python is the most popular programming language
send back the list
1
Python is a programming language.
3
The word 'Python' should be added only once even if it is shared multiple times.
time)
PROF: Y. MERZAK
Exercise 81:
Write a Python algorithm in the form of a function that takes a parameter
string s and returns the dictionary where the keys are the words that make up the
string s and whose key values are the occurrence counts of the words in
the string Text s.
Example: sis = "I use Python for datascience but I don't use Python for mobile"
the algorithm returns the dictionary:
1
2 d = {'I': 2, 'use': 2, 'Python': 2, 'for': 2, 'datascience': 1, 'but': 1, "don't": 1, 'mobile': 1}
3
Exercise 82:
Write a Python algorithm that removes all empty strings from a list.
chains.
Example: If:
1
2 L = ["Python" , "" , "is" , "" , "the", "most" , "", "used" , "programming", "language" , ""]
3
the algorithm returns:
1
Python is the most used programming language
3
Exercise 83:
Write a Python program in the form of a function that takes as a parameter
a string s
and returns the first repeated character in the string s.
Example: sis = "django framework", the function returns the character 'a'
Exercise 84 ** :
Write a Python program in the form of a function that takes as a parameter
a string s
and which returns the list of all the repeated characters in the string without using
no method or predefined module in Python.
Example: if s = "Python language", the function returns the list:
PROF: Y. MERZAK
1
2 ['n', 'a', 'g']
3
Exercise 85:
Write a python algorithm that determines the set of all characters that
component
a chain s.
Exemple sis = "programmation Python", l'algorithme renvoie l'ensemble:
1
I am programming.
3
Exercise 86:
Write an algorithm in Python that determines the set of words that make up
a string of text s.
Exemple sis = "Python is more power than Java", l'algorithme renvoie l'ensemble:
1
Java is more powerful than Python.
3
Exercise 87:
Write a Python algorithm in the form of a function that takes a tuple as a parameter.
from two strings (s1, s2) and returns the list of common characters in s1 and s2.
Example: sis1 = 'Python language' ets2 = 'Java Programming', the function
send back the list
1
Pon ag
3
Exercise 88:
PROF: Y. MERZAK
Write a Python program in the form of a function that takes a parameter
string s and returns the first repeated word in the string s.
Example: sis = "python programming language, is the most popular"
programming language", the function returns the word: 'programming'.
Exercise 89:
Write a Python algorithm that determines the list of repeated characters in
a given string.
Example: sis = "Programming language", the algorithm returns the list:
1
["r","g","a","m","n"]
3
Exercise 90
Write a Python program that determines the list of all characters
maximum occurrence in a given string s.
Exemple: sis = "Programming", l'algorithme renvoie la liste:
1
["r","g","m"]
3
Exercise 91 *** :
Write a python algorithm that determines the list of words of lengths
maximum common to two text chains T1 and T2.
Example siT1 = 'Python created by Guido van Rossum is open source programming
Python was created in 1991.
etT2 = "Python created by Guidorossum is the most popular programming
Guidorossum language", the algorithm returns the list:
1
["Guidorossum","programming"]
3
Exercise 92:
Write a python program in the form of a function that takes as a parameter a
chain s
and returns True if the string s contains at least one uppercase character and False
otherwise.
PROF: Y. MERZAK
Exercise 93:
1 - Write a function named compte_les_caracteres() that accepts a string
of characters and which returns the occurrence of the characters contained in the string
in the form of a dictionary.
2 - Write a function named count_words_line() that accepts a string of
characters and which returns the occurrence of the words contained in the string in the form
of dictionary.
Exercise 94:
Write an algorithm in Python in the form of a function that takes a parameter.
text chain and which returns the list of words that start with a capital letter.
Example sis = 'Python is more powerful than Java and C++', the function returns the
list:
1
2 L = ['Python' , 'Java' , 'C++']
3
Exercise 95:
Write an algorithm in Python in the form of a function that takes a parameter
chain text and which returns the list of words that do not contain any
[Link] = 'Python is more popular than Java', the function
send back the list:
1
is more popular than
3
Exercise 96:
Write an algorithm in Python in the form of a function that takes as a parameter a
string of text that returns the list of words containing at least two uppercase letters.
For example, s = 'The PySide GUI library is more popular than PyQt', the function
return the list:
1
["GUI","PySide","PyQt"]
3
Exercise 97* :
Write an algorithm in Python in the form of a function that takes as a parameter
uncouple (s,x) formed of a text chain s and a character x and which returns the index of
PROF: Y. MERZAK
the second position of the character x in the string without using any
predefined function. The function must return -1 if the character x is not present in
the chain s.
Exemplesis = 'langage python'etx = 'a', la fonction renvoie l'index : 4
Exercise 98*
Write a function in Python that transforms strings.
IN CAPITAL LETTERS WITHOUT USING THE UPPER() METHOD.
Exercise 99*
Write a function in Python that allows you to transform strings
in lowercase without using the lower() method.
Exercise 100*
Write a function in Python to swap the case of characters in a
chains without using the methods upper(), lower(), swapcase()...
PROF: Y. MERZAK