## Friday, August 16, 2019

Python practice : Exercises with solution- part 1- exercise on strings and lists

## 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 program in Python to display for a given string the number of occurrences of each character in the string. Example for the string s = "Python.org" the program must display: The character: "P" appears once in the string s The character: "y" appears once in the string s The character: "t" figure 1 time in the string s The character: "h" appears once in the string s The character: "o" appears twice in the string s The character: "n" appears once in the string s The character: " . "figure 1 time in the string s The character:" r "figure 1 time in the string s The character:" g "figure 1 time 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, 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  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 = "programming is a Python language"

Younes Derfoufi
my-courses.net 