All Practice Questions of Python
All Practice Questions of Python
Input- output
Q1. Write a python program to accept integer input from user and print
that integer.
Q2. Write a python program to take input from user age then increase
his/her age by 10 years.
Sample Output: 22
Q3. Take input user name, age, height then print output as “my name is
(your name), my age is (you age), and height is (your age)”.
Sample Output:
Q4. How do you input a floating-point number from the user and store it
in a variable called price?
Q5. Write a Python program that asks the user for two numbers and then
prints their sum.
Sampel Output: 7
Q6. Write a Python program that asks the user for their age and then
prints a message stating their age.
Q7. Write a Python program that asks the user for the radius of a circle
and then prints the area of the circle.
Q8. How do you take a string input from the user and convert it to an
integer?
Q9. Write a Python program that asks the user for their height in feet and
converts it to meters (1 foot = 0.3048 meters).
Q10. Write a Python program that takes three inputs from the user: two
integers and one floating-point number. Your task is to print these three
values in the following format:
Sample Input:
Sample output:
5 10 3.14
Operator Practice:
Part - 1
1. Write a Python expression that adds two numbers: 7 and 5.
a= 7
b= 5
a= 4
b =4
a= 6
b =8
Note: (Use pen copy to solve questions) then code on your system.
17. a = 8
b=a*2
c = b // 3 + a
print(a, b, c)
18. p = 10
q=p-4
r=q+p*2
print(p, q, r)
19. m = 3
n = m ** 2
o=n%m+1
print(m, n, o)
20. a = 12
b=a/3
c = b ** 2
d = (a + b + c) % 5
print(a, b, c, d)
21. c, d = 12, 6
c, d = d + 4, c - 2
d, c = c // 3, d * 3
print(c, d +1)
22. m, n a = 8, 3,-4
m, n = n * 2, m+ a - 5
n, m = m + 4, n // 2
print(m, n)
23. a, b = 5, 8
a += b
b -= 2
a, b = b, a - 4
print(a, b)
24. x, y = 12, 3
y = x // 2
x *= 3
x, y = y + 1, x - 5
print(x, y)
25. c, d = 8, 20
d=c*2
c += 5
c, d = d - 7, c // 3
print(c, d)
Part - 2 ( Operators )
Test case:
Input: Celsius = 25
Output: 77.0
Test case:
Input: Num1 = 10, Num2 = 20, Num3 = 30, Num4 = 40, Num5 = 50
Output: 30.0
Q8. Write a Python program to calculate the total marks and percentage
from marks of five subjects.
Test case:
Subject5 = 92
Q9. Write a Python program to convert minutes into hours and minutes.
Test case:
Test case:
Input: Salary = 50000, Bonus = 10
Output: 55000.0
If-else Part - 1:
Q1. Write a program to check if a number is positive or negative.
Q2. Write a program that takes two numbers from the user and prints the
largest number.
● Input:
○ Enter a number: 15
○ Enter another number: 25
● Output: 25
Q3. Write a program that takes a number from the user and checks if it is
divisible by 5.
● Input:
○ Enter angle 1: 90
○ Enter angle 2: 80
○ Enter angle 3: 110
○ Enter angle 4: 80
● Output: Can form a quadrilateral
Q5. Write a program that takes a number from the user and checks if it is
divisible by 5.
Q6. Write a program that takes a month number from the user and
determines the season.
Output: positive
Input: 9
Output: odd
otherwise,
Grade: E
Test Case 1:
Input: 50
Output: E
Input: 15
Output: Divisible
Test Case 1:
Enter age: 8
Child
Test Case 2:
Enter age: 16
Teenager
Test case 1:
Input: 1600
Test Case 2:
Input: 1900
Output: isosceles
Q10.Write a Python program that accepts three numbers and check All
numbers are equal or not.
Test Case 1:
Input: x = 8 and y = -1
Otherwise,
"Winter"
Test Case 1:
Input: 7
Output: Summer
Q12. Write a python program which takes three input and check if third
number is within a given range between first and second number.
UPPER RANGE: 89
INPUT_NUMBER: 77
OUTPUT: ‘YES’
Example
Output: 10.0
Q15. Write a python program to input cost price and selling price of a
product and check profit or loss. Also calculate total profit or loss using if
else.
Test case 1:
Output:
Profit is 500
Q16. Write a python program to input week number and print week day.
Test case 1:
Input: 1
Output: Monday
Test case 2:
Input: 7
Output: Sunday
Q17. Write a python program that accepts three numbers from the user
and check if numbers are in "increasing" or "decreasing" order.
Test Case 1:
Input: 10,17,20
Output: Increasing Order.
Test case 2:
Input: 90,69,36
Q19. Write a program to input electricity unit charges and calculate total
electricity bill
Test Case 1:
Rs. 62.5
Test Case 3:
Expected Output:
Rs. 257.5
Q20. Write a python program which accept the kilometres covered and
calculate the bill according to the following criteria:
Test Case 1:
Input: 50
Output: 510
Test Case 2:
Input: 100
Output: 1010
WHILE LOOP Part - 1
Q1. Write a program that prints number from 1 to 10 number with their
square.
Q2. Write a program that prints a table of a number, take that number
from the user.
Sample input:
Enter a number: 5
Output:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Q3. Write a program that calculates the product of all numbers between
1 to 10 using a while loop.
Q4. Write a program that prints numbers divisible of 7 between 1 to 70
Q5. Write a program that takes input from the user and print the reverse
counting of that number.
Sample Input: 5
Sample output: 5 4 3 2 1
Input: 5
Q2. Write a python program that asks the user for input until the user
enters a number 0 and print each number while taking input.
Q3. Write a python program that takes 2 number from user, then print
numbers from 1 to 20 which divides both the numbers (common factor of
those 2 number)
Q4. Write a program to find the factors of a given number by the user as
an input.
Input: 12
Q5. Write a python program that takes a number from the user, then
print count of even and odd factors separately of the input number.
123 6 1+2+3 = 6
456 15 4+5+6 = 15
789 24 7+8+9 = 24
Q2. Write a python program which finds the reverse of a given
number.
Q8. Write a program to enter the number till the user enters 0 and at the
end it should display the sum of all the numbers entered.
Example:
Enter a
number: 5
Enter a
number: 8
Enter a
number: 1
Enter a
number: -6
Enter a
number: 0
Output: 8
How output is 8:
5 + 8 +1 + (-6)
=8
1. Write a python program that take a number from user then add all the
natural number which are divisible by 3 or 5 both till that input number.
Sample input:
Enter a number: 10
Output: 33 (3+5+6+9+10)
2. Write a python program that takes input from user then print the table
of the entered number using for loop
5 x 2=10
5 x 3 =15
5 x 4 =20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 =50
Input: 12
Output: 1, 2, 3, 4, 6, 12
Input: 123
Output: 6
6. write a python program that takes number from user then prints
count of even and odd number from 1 to till that number
Sample input:
Enter a number: 10
Output:
Count_of_even_no.s = 5
Count_of_odd_no.s = 5
Input: 897
Output: 798
· For an even integer n, the double factorial is the product of all even
integers less than or equal to n.
· For an odd integer p, the double factorial is the product of all odd
integers less than or equal to p.
10. Write a python program which takes number as an input and check
whether that number is prime or not.
Input: 13
Output: prime
(Pattern)
Note: Write a python program to generate the following pattern
using for loop by taking ‘n’ as an input. (given patten for n = 5)
1. 2.
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
3. 4.
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
5.
* * * * *
* * * *
* * *
* *
6.
* * *
* * * * *
* * * * * * *
* * * * * * * * *
7.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
8. 9.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
*
10. 11.
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
12.
* * *
* * * * *
* * *
13.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
* * *
* * * * *
* * * * * * *
* * * * * * * * *
14.
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
15.
* *
* * * * *
* *
*
16.
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * *
17.
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
18. If n = 7
* *
* * *
* * * *
* * *
* *
1.
* * * * *
* *
* *
* *
* * * * *
2. 3.
* *
* * * * *
* *
* *
* *
* *
* * * * *
* *
*
4.
* * * * *
* *
* * 5. *
* * * *
* * *
* *
* * * * *
6.
* *
* *
* *
* *
7.
* *
* *
* *
* * * * * * * * *
8.
* * * * * * * * *
* *
* *
* *
9.
* *
* * * *
* * * *
* * * *
* * * * * * * * *
10.
* *
* *
* *
11.
* * * * *
*
12. IF (input) n=7,
* * * * * * *
* * * * * *
* * * *
* *
* * * *
* * * * * *
* * * * * * *
Functions Part – 1
Q1. Write a function add_numbers(a, b) that takes two arguments and
returns their sum.
Q2. Write a function is_even(n) that takes an integer and returns True if
the number is even, otherwise False.
Q3. Write a function factorial(n) that takes a non-negative integer and
returns its factorial.
Q4. Write a function max_of_two(a, b) that takes two numbers and
returns the larger of the two.
Q5. Write a function celsius_to_fahrenheit(c) that takes a temperature in
Celsius and returns the temperature in Fahrenheit. (Formula: F = C * 9/5
+ 32)
Q6. Write a function multiplication_table(n) that takes an integer and
prints its multiplication table from 1 to 10.
Function Part - 2
Q3. Write a Python function that takes a list and returns a new list with
distinct elements from the first list.
Sample List: [1,2,3,3,3,3,4,5]
Unique List: [1, 2, 3, 4, 5]
Q4. Write a function intreverse(n) that takes as input a positive integer n and
returns the integer obtained by reversing the digits in n.
Here are some examples of how your function should work.
Test Case 1:
Input: intreverse(546)
Output: 645
Test Case 2:
Input: intreverse(3)
Output: 3
Q5. Write a function sumprimes(l) that takes as input a list of integers l and
retuns the sum of all the prime numbers in l.
Here are some examples to show how your function should work.
Test Cases:
Input: sumprimes([3,3,1,13])
Output: 19
Input: sumprimes([2,4,6,9,11])
Output: 13
Q6. Write a function that calculates the nth terms in an arithmetic sequence,
which takes first term, common difference and value of n as an argument and
default value of n is 10.
Q7. Create a function that returns the list of factorials of each element of a
given list as argument.
Test Case 1:
Input as argument: [2,3,4,5]
Ouput: [2, 6, 24, 120]
Q8. Write a python function that accepts a list of numbers and returns the sum
of the squares of only the prime numbers.
Q10. Write a Python function that takes a list of strings as input and
returns a tuple containing the shortest and longest word from the list, in
that order. If there are multiple words of the same shortest or longest
length, return the first shortest/longest word found.
Input:-
Q1. Write a program which add the element 13 to the end of the list
numbers = [10, 22, 35, 45, 53] after that Print the list.
Q2. Write a program which insert the element 20 at index 2 in the list
numbers = [5, 2, 6, 4, 9]. Print the list after inserting.
Q3. If List1 = [1,2,3,4] and List2 = [8,6,7,5] then what should be the
code for the output: [1,2,3,4,8,6,7,5]
Q4. Delete the element at index 3 from the list numbers = [10, 2, 36, 41,
5]. Print the list after deletion.
Q5. Delete the element at index 4 from the list Lst = ['apple' , 'banana' ,
'orange' , 'kiwi' , 'guava'] and Print the list after deletion and also print
that deleted element.
Q6. Remove the first occurrence of the element 3 from the list numbers
= [1, 25, 3, 44, 33, 56]. Print the list after removing.
Q7. Count the number of times the element 2 appears in the list Lst =
[15, 2, 2, 3, 4, 2 ,7 ,8 ,45 ,2 ,2]. Print the count.
Q8. Write a program which prints the reverse list of the given list:
Q10. Write a program which insert the element 10 at index 1 in the list
Lst = [12, 24, 35, 24, 45], then remove the element 35. Print the list after
these operations.
LIST Part -2
Q1. Write a python program which iterates the given list prints all the
values inside the list via loop .
lst = ['mango','apple','banana','ornage','kiwi']
Q2. Write a python program which finds the sum to all the elements of
the list by iterating that given list.
lst = [2,4,6,8,12,10,14,24]
Q3. Write a python program which finds the value (n) in the given list =
[4,5,3,2,1,6,8], if present then return True otherwise False.
Q4. Write a python program which update odd indices elements of the
list by ‘hii’ through iterating that given list.
lst = [2,4,6,8,1]
Q5. Write a python program which finds and print the average of
elements of the given list of integers.
lst = [10,45,23,46,98,56]
Q6. Write a python program which finds the sum of squares of all the
elements of the given list.
lst = [2,4,6,8,1]
List Part - 3
Q1. Write a python program to divide the list into two equal halves and
print the sum of each half.
lst1 = [2,4,6,8,3,5,8,9]
(3+5+8+9)
(2+4+6+8)
Output: 20, 25
Q2. Write a program to delete the element at the index equal to half the
length of the given list. Print the list before and after deletion.
Before: [2,3,4,5,6,7,8]
After: [2,3,4,6,7,8]
Q3. Write a program to append a new integer to this list, which is the
sum of the first and last elements of the list. Print the list before and after
appending.
(23+87)
Output: [23,43,56,87,110]
Q4. Write a program to insert the product of the first and last elements at
the third position in the list. Print the list before and after insertion.
(34 * 46)
Output: [34,56,23,1564,76,23,46]
Q5. Write a program to print the list of first and last elements of each list
of lists.
lst2 = [[23,45,76],[45,3,12],[3,5,20]]
Output: [23,76,45,12,3,20]
Q6. Write a program to calculate and print the sum of max elements of
each list of lists.
lst3 = [[1,2,4,5],[3,5,4,3],[4,5,3,2]]
(5+5+5)
Output: 15
Q7. Write a program to calculate and print the sum of elements in each
list of lists and add the resultant values. Without using sum() function.
(12+15+14)
41
lst = [2,5,7,1,4,6,10,11,6,12]
Q8. Find the largest element of the above given list without using max()
and sort() functions.
Q9. Find the Smallest element of the above given list without using
min() and sort() functions.
Q10. Find the Second largest element of the above given list without
using max() and sort() functions.
Q11. Find the Second Smallest element of the above given list without
using min() and sort() functions.
List Part -4
Q1. Create a new list which have the squares of every element of the
given list using list comprehension.
lst = [2,3,4,5,6,7]
Output = [4,9,16,25,36,49]
Output: [1, 2, 3, 4, 5, 6, 7, 8]
Output: [5, 7, 9]
lst = [[1,2,3],[3,4,5],[3,4,5],[5,7,3,2]]
Output: [6,12,12,17]
lst = [[1,2,3],[3,4,5],[3,4,5],[5,7,3,2]]
Output: [1,3,3,2]
TUPLE Part - 1
Q2. Write a python program which prints the second last element and
check if 'blue' is in the given tuple.
Q3. Write a python program in which slice the given tuple to get the
elements from the second to the fourth position. Print the sliced tuple
and its length.
Q4. Write a python program to concatenate the given tuples. Print the
result and then sort the tuple and print it.
Q5. Write a program to count how many times 5 appears and find the
index of the first occurrence of 3. Print both results.
t = (1, 2, 3, 4, 5, 5, 5, 6).
Q6. Write a program to create a tuple with elements ('cat', 'dog', 'mouse',
'elephant'). Slice it to get the first two elements and print them and also
print the after sorting the tuple.
Q7. Write a python program which prints the last element of given tuple
and check if 'Hello' is in the tuple. Print the results.
Output: ‘ruby’
False
Output: 3
[‘hello’,’world’,’python’,3]
Q9. Write a python program which access first six elements of the given
tuple, then count how many times 5 appears in the new tuple. Print both
results.
t = (1, 2, 5, 5, 6, 3, 4, 5, 5, 5, 6)
Q10. Write a python program in which concatenate the given tuples and
print the resulting tuple and the third element of generated tuple.
t1 = ('java', 'python')
t2 = ('c', 'ruby')
Tuple Part - 2
Check if White presenet in said tuple of tuples!
True
Check if White presenet in said tuple of tuples!
True
Check if Olive presenet in said tuple of tuples!
False
Q3. WAP to check if all the element of a tuple are in descending order or
not.
Q4. WAP to check if the elements in the first half of a tuple are sorted in
ascending or not.
Q5. WAP to inputs name of n students and store them in a tuple . Also,
input a name from the user and if this student is present in the tuple or
not. Example:
Input:-
Output:-
Q8. Write a Python program to calculate the product, multiplying all the
numbers in a given tuple.
Original Tuple:
(4, 3, 2, 2, -1, 18)
Product - multiplying all the numbers of the said tuple: -864
Original Tuple:
(2, 4, 8, 8, 3, 2, 9)
Product - multiplying all the numbers of the said tuple:
String Part -1
Q1. Write a program to check if the word ‘am’ is present within a given
string, and if so, return the index of its first occurrence.
Output: 15
Q2. Write a python program which takes string as input and replace
every occurrence of input string in the given the list to ‘hii’ and also print
list which have length of all the strings inside that list.
Sample Input:
Input_string : ‘am’
Output:
[1, 3, 10, 3, 1, 3, 5]
Q3. Write a program to take two strings and an integer. The program
should slice the first string till the given integer and concatenate the
resulting substrings with the second string.
Output: $o$$ate$atio$
Q5. Write a program to remove all vowels from a given string.
Output: cnctnt
Q6. Write a python program which takes character as a input and count
the occurrences of that character into the given string and convert all
them into ‘#’.
String = ‘programming’
Sample Input: g
Output: 2
‘pro#rammin#’
Q7. Write a python program which takes string as an input and removes
the character at even indices.
Output: pto
Q8. Write a program that splits a given string into words, sorts the words
in alphabetical order, and joins them back into a single string.
Q9. Write a python program which takes string as input and swap first
and last character of that string.
Output: ‘oellh’
Q10. Write a program to check if a given string is a palindrome.
Q11. Write a python program which convert given list into a single string
where each name is capitalized and separated by a semicolon.
String Part – 2
Ex: example@domain.com
Q3. Write a Python program that takes a string and a character as input
and returns a list of indices where the character occurs in the string.
Input: "programming"
Output: 3
Q7. Write a Python program to split a string into a list of words and then
join them back into a single string with a hyphen - as a separator.
Output: "Python-is-awesome"
Q8. Write a Python program to capitalize the first and last character of
every word in a given string.
Input: "hello world from python"
Input: "Hello@World!123"
Output: "HelloWorld123"
Set Part 1
Q1. Write a Python program to create two sets, A and B, and find the
elements that are common to both sets.
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Q2. Write a Python program to create two sets, X and Y, and find the
elements that are unique to each set (not present in both).
Q3. Write a Python program to check if all elements of one set are
contained within another set.
S1 = {1, 2, 3}
S2 = {1, 2, 3, 4, 5}
Q4. Write a Python program which create a new set from given two sets
have only that elements which are not common.
A = {2, 4, 6}
B = {1, 3, 5}
Q5. Write a Python program to combine all elements from three different
sets into one.
Set1 = {1, 2}
Set2 = {2, 3}
Set3 = {3, 4}
Q6. Write a Python program to find all elements that are in one set but
not in another.
S = {1, 2, 3, 4, 5}
Test case 1: n = 2
Test case 2: n = 7
Set Part - 2
Q1. Write a program to multiply all the float element in the set. set should
be taken as input such that a set should contain integer float and string
element.
Input:- Enter a set:-{5, 'abc', 7, 8.0, 'def', '10.0', 11, 12.0, '123', 14.0}
Output:- 1344.0
Q2. Write a program to add all the integer element in a set and set
should be taken as input such that a set should contain integer float and
string element.
Input:- Enter a set:-{5, 'abc', 7, 8.0, 'def', '10.0', 11, 12.0, '123', 14.0}
Output:- 23
Q3. Write a Python program to convert all the even number of a set into a
string and set should be user defined.
Input:- Enter a set:-{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
Output:- {'10', 5, 7, 9, 11, 13, '14', '6', '8', '12'}
Q4. Write a Python program to convert all the odd number of a set into a float
and set should be user defined.
Input:- Enter a set:-{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
Output:-{5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12, 13.0, 14}
Q5. Write a program to convert all the integer element of a set into float
and the float element of a set into integer and set should be user defined
Set Part - 3
Write a python program which find all elements that are in either A
or B, but not in both. Create a third set with these elements.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Y = {1, 2, 3, 4, 5}
Y = {1, 2, 3, 4, 5}
Q3. You are given three lists, L1, L2, and L3.
L1 = [1, 2, 3, 4]
L2 = [3, 4, 5, 6]
L3 = [1, 4, 6, 7]
Q4. Write a python program if a set and list is given then, for each
element in L, if it's not already in S, add it; otherwise, remove it from S
and then print S.
S = {1, 2, 3}
L = [2, 3, 4, 5]
Output: {1, 4, 5}
A = {2, 3, 5}
B = {5, 7, 11}
Frozen Set
Q1. Write a Python program to create a frozenset from a list of numbers.
Q2. Write a Python program which firstly create two frozenset of integers
and find the common elements between those frozensets.
Q3. Write a Python program which create two frozenset of float values
as a tuple and combine those frozensets into one.
Q4. Write a Python program to find elements that are in one frozenset
but not in another.
Q5. Write a Python program to find elements that are unique to each of
two frozensets (not in both).
Dictionary Part - 1
Q2: Write a Python program to access and print the grade of 'Bob' from
the given dictionary.
Sample Output: 90
Q3: Write a Python program to add a new student 'David' with a grade of
92 to the given dictionary and print it.
Q5: Write a Python program to delete the student 'David' from the given
dictionary using the del keyword, also print the updated dictionary.
Q6: Write a Python program to check if the key 'Eve' is present in the
given dictionary, if not then print “Eve is not present in the dictionary”.
Q7: Write a Python program to print all the student names in the given
dictionary.
Alice
Bob
Charlie
Output:
Q8: Write a Python program to print all the grades in the given dictionary.
85
90
80
Output:
Q9: Write a Python program to print all key-value pairs in the given
dictionary in the format “Student: Grade”.
Alice: 85
Bob: 90
Charlie: 80
Output:
Q10: Write a Python program to clear all elements from the given
dictionary.
Q11. Write a python program to count, how many key-value pairs are
present in the dictionary by traversing on the given dictionary.
student_grade = {'Alice': 85, 'Bob': 90, 'Charlie': 78}
D = {}
Output: True
Sample Input: 2
Dictionary Part – 2
25
Q4: Write a Python program to remove the last key-value pair from the
given dictionary and print the removed pair.
Q5: Write a Python program to check if the key 'grape' is not present in
the fruit_prices dictionary, if not then add ‘grape’ with value 30 and print
the updated dictionary.
Dictionary Part - 3
Q4. Write a Python program to reverse the keys and values of a given
dictionary using dictionary comprehension.