Note: The exercises are note ranked in order of difficulty !

Python training provided by programmers (les programmeurs)

Python practice : Exercises with solution

Part 1- exercises on strings and lists

Exercise 1 || Solution

Write a program in Python language that asks the user to enter their name and display their name with a welcome message!

Exercise 2 || Solution

Write a program in Python that asks the user to enter two numbers a and b and display their sum: a + b.

Exercise 3 || Solution

Write a program in Python that asks the user to enter two numbers a and b and to display their maximum

Exercise 4 || Solution

Write a program in Python language that displays the first 100 integers

Exercise 5 || Solution

Write a program in Python language that asks the user to enter their integer number and to display it if this number is even or odd

Exercise 6  ||  Solution

Write a program in Python language that asks the user to enter their age and display the message "You're Major!" If the age typed is greater than or equal to 18 and the message "you are under age!" If the typed age is less than 18

Exercise 7  ||  Solution

Write a program in Python that asks the user to enter 3 numbers x, y, and z and display their maximum

Exercise 8  ||  Solution

Write a program in Python that asks the user to enter an integer n and display the value of the sum = 1 + 2 + ... + n

Exercise 9  ||  Solution

Write a program in Python that asks the user to enter an integer n and display it n!

Exercise 10  ||  Solution

Write a program in Python that asks the user to enter the radius of a circle and return the area and perimeter.

Exercise 11  ||  Solution

Write a program in Python that asks the user to enter an integer n and display all the divisors of that number.

Exercise 12  * ||  Solution

1) - Write a program in Python that asks the user to enter an integer n and display the multiplication table of that number.
2) - Improve the program so that it displays multiplication tables of all numbers between 1 and 9

Exercise 13   ||  Solution

Write a program in Python language that asks the user to enter two integers a and b and to display the quotient and the remainder of the Euclidean division of a by b.

Exercise 14   ||  Solution

Write a program in Python language that asks the user to enter an integer n and display it if this number is perfect square or not

Exercise 15  * ||   Solution

Write a program in Python language that asks the user to enter an integer n and display it if this number is prime or not

Exercise 16.   ||  Solution

Write a program in Python language that asks the user to enter a string s and then display him all the characters contained in this string.
Example for s = "Python", the program displays the characters:
P
y
t
h
o
n

Exercise 17 * ||  Solution

Write a Python algorithm that takes a string s as input and display the number of occurrences of each character in this string.
Example for the string s = "Python Programming" the program must display:
The character ' P ' appears 2 time(s) in the string s
The character ' y ' appears 1 time(s) in the string s
The character ' t ' appears 1 time(s) in the string s
The character ' h ' appears 1 time(s) in the string s
The character ' o ' appears 2 time(s) in the string s
The character ' n ' appears 2 time(s) in the string s
The character ' ' appears 1 time(s) in the string s
The character ' r ' appears 2 time(s) in the string s
The character ' g ' appears 2 time(s) in the string s
The character ' a ' appears 1 time(s) in the string s
The character ' m ' appears 2 time(s) in the string s
The character ' i ' appears 1 time(s) in the string s

Exercise 18  ||  Solution

Write a program in Python that asks the user to enter a string s and return a message indicating whether the string contains the letter 'a' by indicating its position on the string. Example if the user types the string s = 'language' the program returns: The letter 'a' is in position: 1 The letter 'a' is in position: 5

Exercise 19   ||   Solution

Write a program in Python that lists the strings that make up the list l = ["laptop", "iphone", "tablet", "printer", "Ipad"] by indicating the length of each string.

Exercise 20   ||   Solution

Write a program in Python language, to exchange the first and last characters of a given string.

Exercise 21  ||  Solution

Write a program in Python language, which allows you to count the number of vowels in a given string. Example for the string s = 'anticonstitutionally' the program must return the following message: The string anconstitutionally has 9 vowels.

Exercise 22 * ||  Solution

Write a program in Python, which returns the first word of a given text. Example for the text: t = 'Python is a wonderful programming language', the program must return Python

Exercise 23 *  ||  Solution

Write a program in Python language that asks the user to enter the name of a file and return the extension. Example if the user enters 'coursPython.pdf' the program returns the message "The file extension is .pdf".

Exercise 24   ||  Solution

A palindrome is a word whose order of letters remains the same whether it is read from left to 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 return it if it is a palindrome or not?

Exercise 25  ||  Solution

Write a program that asks the user to enter a word and return the opposite. Example if the user enters the word python, the program returns nohtyp.

Exercise 26  * ||  Solution

Write a program that asks the user to enter a text and return all words beginning with the letter a.

Exercise 27  ||  Solution

Write a program in Python as a function that calculates the sum of elements in a list of numbers. And another that multiplies all the elements of a list of numbers

Exercise 28  ||  Solution

Write a Python program that tests whether a list is empty or not. Same question for a string of characters.

Exercise 29  * ||  Solution

Write a Python program that removes duplicate items from a list.

Exercise 30  ||  Solution

Write a function in Python that allows to compare two lists and to tell us if these two lists have a common value or not.

Exercise 31  ||  Solution

Write a Python program that retrieves the list of even integers and the list of odd integers from a list of numbers.

Exercise 32 ** ||  Solution

Write a program in Python that returns all the lists obtained by swapping the terms of a given list.

Exercise 33  ||  Solution

Write a program in Python that asks the user to enter a string and display the characters of even index. Example for the string s = "Python", the program returns "Pto".

Exercise 34  ||  Solution

Given the list of student notes: notes = [12, 4, 14, 11, 18, 13, 7, 10, 5, 9, 15, 8, 14, 16]. Write a Python program that allows you to extract from this list  another list that contains only the notes above the average ( notes > = 10 ).

 Exercise 35  ||  Solution

Write a program in Python that allows you to transform a given url address into a link.

Exercice36  ||  Solution

Write a program in Python to delete multiple spaces in a string

Exercise 37  * ||  Solution

Write a Python program that creates a list whose elements are the words common to two strings s1 and s2.

Exercise 38 * ||  Solution

Write a Python program to find the longest word on a string

 Exercise 39 ||  Solution

Write a Python program that counts the number of words on a string

 Exercise 40 * ||  Solution

Write a Python program that allows you to swap the first and the last word. Example if s = "Python is a programming language", the program returns the string s2 = "language is a programming Python"

Exercise 41 || Solution

Write a Python program allowing from a given list to create 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 42 || Solution

Write a python program that asks the user to enter numbers separated by semicolons ';' and generates a list make up of the entered numbers.

Exercise 43 || Solution

Write a python program that asks the user to enter the height and base of a triangle and returns the area of the triangle.

Exercise 44 || Solution

Write a python program which groups in a list all the words which begin with a capital letter in a given string s.
Example for s = "Python is a high level programming language. Originally developed by Guido van Rossum in 1989." The program should return the list:

["Python", "Originally", "Guido", "Rossum"]

Exercise 45 || Solution

Write a python program as a function which takes as parameters a character string s and which returns another string obtained from s by replacing all the characters in upper case by the diese char '#'

Exercise 46  * || Solution

Write a Python program as a function which takes as parameter a string s and which returns the list of all the words contained in s whose length is greater than or equal to 8. Example if s = 'Python is oriented programming language. Python is the most used programming language'. The function returns the words list :

['oriented', 'programming', 'language', 'programming', 'language']

Exercise 47 || Solution

Write a python program that determines the list of all the words which contains at least one uppercase character in a given text T. Example if T = "Python is a high level programming language. Originally developed by Guido van Rossum in 1989." The program returns the list:

['Python', 'Originally', 'Guido', 'Rossum']

Exercise 48 || Solution

Write a program as a function which takes as parameters a pair of strings (s1, s2) and which returns the string s obtained by concatenating in an alternative way the characters of s1 and s2. Example for

(s1, s2) = ("Python" , "Java")

the function returns the string s = 'PJyatvha'

Exercise 49 || Solution

Write a Python program as a function that counts the number of times a character appears in a string without using any predefined functions.

Exercise 50 || Solution

Write a python program as a function which takes a string s as a parameter and returns the list of numeric characters contained in the string s. Example if s = '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 51 || Solution

Using ascii code, write a python program that returns and displays the list of all uppercase characters [A, B, C, ..., Z] and the list of all lowercase characters [a, b, c,. .., z]

Exercise 52 * || Solution

Using Exercise 51, create a python program as a function which takes as a parameter a string 's' and which returns the same string in uppercase without using the upper() method. You can use the chr() and ord() methods.

Exercise 53 || Solution

Using Exercise 51, create a python program as a function which takes as a parameter a string s and which returns the same string in lowercase without using the lower() method. You can use the chr() and ord() methods.

Exercise 54 ** || Solution

Write a python program as a function which takes as a parameter a string s and which 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 using the lstrip() neither any predefined method. Exemple if

s = "     Hello    "

, the function returns the string

"Hello"

Exercise 55  ** || Solution

Write a python program as a function which takes as parameters a string s and which returns a string obtained from the string s by transforming each uppercase character into a lowercase character and vice versa without using the swapcase() method. Example if s = "Hello Wordls!" , the function returns the string "hELLO wORLD!"

Exercise 56 * || Solution

Write a python program as a function which takes as parameter a tuple of string (s, s1) and which returns the index of the first occurrence of s1 found within the string s. The function must returns -1 if s1 is not found within the string s Example if s = "Python Programming" and s1 = "thon" , the function returns the index 2

Exercise 57 * || Solution

Write a python program as a function which takes as parameter a tuple of string (s, s1) and which returns the number of the occurrences of s1 within the string s. Example if s = "Python is object oriented and dynamically typed " and s1 = "ed" , the function returns 2

Exercise 58 || Solution

Write a Python program as a function which takes as parameter a string s and which returns another string obtained from s by replacing the possibly existing parentheses '(' and ')' in the string s with square brackets '[' and ']' without using the replace() method. Example if s = "Python (created by Guido van Rossam) is oriented programming language (oop)." The function must returns: "Python [created by Guido van Rossam] is oriented programming language [oop]"

Exercise 59 || Solution

Write a python algorithm as a function which determines the length of a string without using the len() method or any other predefined method in python.

Exercise 60 || Solution

Write a python program as a function which takes as a parameter a string s and return an other string obtained from s by replacing the existing spaces within s by hyphen '-' without using the replace() method. Example if s = "Python is object oriented programming language" , the function must returns: "Python-is-object-oriented-programming-language".

Exercise 61 * || Solution

Write a Python program as function which takes as parameter a string s and return an other string obtained by removing the duplicate characters of s. Example if s = "Programming", the function must retun "Progamin"

Exercise 62 || Solution

Write a Python program that determines the list of digits within a given string. Example if s = "Python3.8 is more powerful than Python2.7 " the program must returns the list :

['3', '8', '2', '7']

Exercise 63 * || Solution

Write a Python program which compute the sum of digits numbers within a given string 's'.

Exercise 64 || Solution

Write a Python program to count the Uppercase characters within a given string. Example if s = "Python Programming", the program must returns 2.

Exercise 65 || Solution

Write a Python program as function which takes as parameter a string s and returns True if the first character is the same as the last character of the string, False if not. Example if s = "render", the function must returns True. If s = "Python" the function returns False

Exercise 66 * || Solution

Write a Python program that extract the list of all urls from given a string s. Example if s = "You can use google https://www.google.com or facebook https://www.facebook.com" The algorithm returns the list

['https://www.google.com' , 'https://www.facebook.com']

Exercise 67 * || Solution

Write a Python function which takes as parameter a tuple of string (s , s1) and returns True if s start with s1 and False if not, without using the Python startswith() method.

Exercise 68 * || Solution

Write a python algorithm that find the number of common characters in two strings s1 and s2 without repetition. Example if s = "Hello" and s2 = "World", the shared characters are 'l' and 'o', then the algorithm returns 2.

Exercise 69 || Solution

Write a Python program as a function which takes a string s as parameter and which returns True if the string is duplicated and False if not. Example if s = "HelloHello", the function must returns True.

Exercise 70 || Solution

Write a python program that extract from given string s , the list of all word whose len is less than or equal to 4. Example if s = "The Python programming language is open source and very easy to learn" , the program must returns the list

L = ['The', 'is', 'open', 'and', 'very', 'easy', 'to']

Exercise 71 ** || Solution

Write a Python program which determines the minimum digit within a given string s. Example if s = "Python3.7 is more power than Python2.7" , the algorithm must returns 2. We assume that the string s does not contain any negative number.

Exercise 72  || Solution

Write a python algorithm to examine if an occurrence is present within a given string.

Exercie 73 || Solution

Write a python algorithm which delete all vowels from a given string s. Example if s = "Python is hight level programming language", the algorithm returns the string: "Pthn s hght lvl prgrmmng lngg"

Exercise 74 || Solution

Write a Python program that replace the characters with odd index of a given string by '#'. Exemple if s = "Python" , the algorithm returns the string: "P#t#o#"

Exercise 75 || Solution

Write a python algorithm which determines the list of all common characters to two strings s1 and s2 without repetition. Example if s1 = "Python language" and s2 = "Programming", the algorithm returns the liste ['P', 'o', 'n', 'a', 'g'] ( the charcter 'g' must be added only once even if shared twice)

Exercise 76 || Solution

Write a python algorithm that transform a given string s by swapping the second character (s[1]) with the befor last character. We assume that len(s) is greater or equal than 4. Example if s = "Python" , the algorithm returns the string: "Pothyn".

Exercise 77 *|| Solution

Write a Python algorithm which determines the first index of an existing occurrence within a given string s without using any predefined method as find() or rfind()... The algorithm must return -1 if the occorrence does not exist within the string s. Example if s = "Python programming language", and occ = "prog" the algorithm return 7

Exercise 78** || Solution

Write a python algorithm which returns the list of all index where an occurrence occ is found within a given string s without using any predefined method as find(), rfind() , index()... The function returns the empty list [], if occ does not exist in s.
Example:
s = "Python is an interpreted language. Python is open source. Python is easy to learn"
occ = "Python" , the algorithm returns the list: [0, 35, 58]

Exercice 80 || Solution

Write a python algorithm which determines the list of all common words to two text T1 and T2 without repetition. Example if:
T1 = "Python is open source programming language  created on 1991"
and
T2 = "Python is the most popular programming language "
, the algorithm returns the liste

['Python', 'is', 'programming', 'language']

 

Exercise 81 || Solution

Write a Python algorithm as function which takes as parameter a string s and that return the dictionary whose keys are the words that making up the string s and whose values are the number of occurrences of words within the string Text s. Example: if s = "I use Python for datascience but I don't use Python for mobile", the algorithm returns the dictionary:

d = {'I': 2, 'use': 2, 'Python': 2, 'for': 2, 'datascience': 1, 'but': 1, "don't": 1, 'mobile': 1}

Exercise 82 || Solution

Write a Python algorithm that remove all empty strings from list of strings Example:
If :

L = ["Python" , "" , "is" , "" , "the",  "most" , "", "used" , "programming", "language" , ""]

The output is:

['Python', 'is', 'the', 'most', 'used', 'programming', 'language']

Exercise 83 * || Solution

Write a Python program as function that takes as parameter a string s and which returns the first repeated character in the string s. Example: if s = "django framework", the fuction returns the character 'a'

Exercise 84 *|| Solution

Write a Python program as function that takes as parameter a string s and which returns the list of all repeated character in the string s. Example: if s = "python language", the fuction returns the list :

['n', 'a', 'g']

Exercise 85 || Solution

Write a python algorithm which determines the set of all characters that make up a string s. Example if s = "Python programming", the algorithm returns the set:

{'p', 'i', '', 'r', 'o', 'm', 't', 'a', 'h', 'y', 'P', 'g' , 'n'}

Eercise 86 *|| Solution

Write an algorithm in Python which determines the set of words that make up a text string s. Example if s = "Python is more power than Java", the algorithm returns the set:

{'Python', 'is', 'more', 'power',  'than', 'Java'}

Exercie 87 || Solution

Write a Python algorithm as a function which takes as parameter a tuple of two strings (s1, s2) and which returns the list of common characters to s1 and s2.
Example: if s1 = 'Python language' and s2 = 'Java Programming', the function returns:

['P', 'o', 'n', ' ', 'a', 'g']

Exercise 87 *|| Solution

Write a Python algorithm as a function which takes as parameter a tuple of two strings (s1, s2) and which returns the list of characters in s1 and not in s2. Example: if s1 = 'Python language' and s2 = 'Java Programming', the function returns the list:

['y', 't', 'h']

Exercise 88 * || Solution

Write a Python program as function that takes as parameter a string s and which returns the first repeated word within the string s. Example: if s = "python programming language, is the most popular programming language", the fuction returns the word: 'programming'

Exercise 89 ** || Solution

Write a Python algorithm which determines the list of repeated charcters in given string. Example: if s = "Programming language", the algorithm returns the list:

['r', 'g', 'a', 'm', 'n']

Exercise 90 **|| Solution

Write a Python program which determines the list of all characters repeated at least twice in a given string.
Example: si s = "Programming  language", the algorithm returns the list :

['r' , 'a' , 'g' , 'm']

Exercise 91 *** || Solution

Write a python algorithm which determines the common words with maximum length of two strings text T1 and T2. Example if: T1 = "Python created by Guidorossum is open source programming language. Python was created on 1991"and T2 = "Python created by Guidorossum is the most popular programming language Guidorossum", the algorithm returns the list:

['Guidorossum', 'programming']

Exercise 92 || Solution

Write a python program as a function which takes a string s as parameter and returns True if the string s contains at least one uppercase character and False otherwise.

Exercise 93 || Solution

Write a Python program which wrap a given string into a paragraph of given width

Exercise 94* || Solution

Write a Python program as function that takes as parameter a string s and which returns the list repeated word within the string s. Example: if s = "Python is object oriented programming language. Python is also also used for data sciences", the fuction returns the list: ['Python', 'is', 'also']

Exercise 95* || Solution

Write a Python function to get any string/sentence and returns the most repeated word.

Exercise 96 *|| Solution

Write an algorithm in Python as a function which takes a text string as parameter and which returns the list of words containing at least two uppercase letters. Example if

 s = 'The PySide GUI library is more popular than PyQt'

, the function returns the list:

['GUI', 'PySide', 'PyQt']

Exercise 97 *|| Solution

Write a python algorithm as a function which takes as parameter a couple (s, x) formed by a text string s and a char x and which returns the index of the second position of the character x in the string s without using no predefined function. The function should return -1 if the char x is not in the string s. Example if s = 'python programming language' and x = 'p', the function returns the index: 7

Younes Derfoufi
my-courses.net

Leave a Reply