Python Practice: Exercises With Solutions In Python - Variables & Strings

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 display their maximum.

Exercise 4 || Solution

Write a program in Python that displays the first 100 integers : 1  2  3  ...  100

Exercise 5 || Solution

Write a program in Python language which asks the user to enter an integer number 'n' and to display whether 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 are major!" if the typed age is greater than or equal to 18 and the message  "you are a minor!"  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 displays 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 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 displays all the divisors of this number.

Exercise 12 || Solution

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

Exercise 13 || Solution

Write a program program which asks the user to enter two integers 'a' and 'b' and display the quotient and the remainder of the Euclidean division of 'a' by 'b'.

Exercise 14 || Solution

Write a python algorithm which asks the user to type an integer n and display whether this number is a perfect square or not.

Exercise 15 || Solution

Write a python algorithm which asks the user to type an integer n and to return whether this number is prime or not.

Exercise 16 || Solution

Write a python algorithm which display all characters of a string variable s. 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 character string, the number of occurrences of each character in the string. Example for the string s = "Python.org" the program should display:
The character: "P" appears once in the string s
The character: "y" appears once in the string s
The character: "t" appears once 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: ". " appears once in the string s
The character: "r" appears once in the string s
The character: "g" appears once in the string s

Exercise 18 || Solution

Write a program in Python that asks the user to enter a string of characters s and send back a message indicating whether the string contains the letter 'a' while indicating its position on the string. Example if the user types the string s = 'language' the program returns:
The letter 'a' is at position: 1
The letter 'a' is at position: 4

Exercise 19 || Solution

Write a program in Python that allows you to list the strings that make up the list l = ["laptop", "iphone", "tablet"] while indicating the length of each string.

Exercise 20 || Solution

Write a Python algorithm, that swap the first and the last character of a given string.



Exercise 21 || Solution

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

Exercise 22 || Solution

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

Exercise 23 || Solution

Write a program in Python language that asks the user to enter the name of a file and return its 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 in which the order of the letters remains the same whether it is read from left to right or from right to left. For example: 'laval' , 'radar, 'sos'... are palindromes. Writing a program in Python that asks the user to type in a word and send back whether it's a palindrome or not?

Exercise 25 || Solution

Write a program that asks the user to type a word and return its inverse. 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 returns all the words beginning with the letter a.

Exercise 27 || Solution

Write a program in Python in the form of a function that calculates the sum of the elements of a list of numbers. And another that allows you to multiply 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.

Exercise 29 || Solution

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

Exercise 30 || Solution

Write a function in Python that compares two lists and tells us if these two lists have a common value or not.

Younes derfoufi
CRMEF OUJDA

 

1 thought on “Python Practice: Exercises With Solutions In Python - Variables & Strings

Leave a Reply

Your email address will not be published. Required fields are marked *