[go: up one dir, main page]

0% found this document useful (0 votes)
35 views70 pages

All Practice Questions of Python

The document contains a comprehensive list of Python practice questions covering various topics such as input-output operations, arithmetic operations, conditional statements, and loops. It includes sample inputs and outputs for each question to guide users in understanding the expected results. The questions are designed to help learners practice and enhance their Python programming skills.

Uploaded by

hftcoder123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views70 pages

All Practice Questions of Python

The document contains a comprehensive list of Python practice questions covering various topics such as input-output operations, arithmetic operations, conditional statements, and loops. It includes sample inputs and outputs for each question to guide users in understanding the expected results. The questions are designed to help learners practice and enhance their Python programming skills.

Uploaded by

hftcoder123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

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 Input: enter you age: 12

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 Input: Enter you name: alice

Enter your age: 10

Enter your height: 3.5

Sample Output:

My name is Python, my age is 10 and height is 3.5

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.

Sample Input: Enter 1st numbers: 3


​ Enter 2nd number: 4

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?

Sample input: ‘234’

Sample output: 234

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:

Integer1 Integer2 Float_Number

Sample Input:

Enter the first integer: 5

Enter the second integer: 10

Enter the floating-point number: 3.14

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

2.​ Subtract 4 from 9 using Python.

a= 4

b =4

3.​ Multiply 6 by 8 in Python.

a= 6

b =8

Note: (Use pen copy to solve questions) then code on your system.

4.​ Perform floor division (integer division) on 15 by 2.


5.​ Find the remainder when 22 is divided by 5.
6.​ Find the remainder when -22 is divided by 8.
7.​ Find the remainder when 2 is divided by 7.
8.​ Addition Assignment,

If x = 10, add 5 to x using an assignment operator.


9.​ Subtraction Assignment,

If y = 20, subtract 3 from y using an assignment operator.

10. Multiplication Assignment,

If z = 7, multiply z by 2 using an assignment operator.

11. Division Assignment

If w = 40, divide w by 4 using an assignment operator.

12. Determine if 8 is greater than 6.

13. Determine if 5 is less than 10.

14. Check if 7 is greater than or equal to 7.

15. Calculate the result of (3 + 5) * 2 - 4 / 2 % 3 using python operators


(Use pen copy to solve question)

16. Evaluate the expression (7 ** 2 + 6 // 3) * (8 % 3 - 2)

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 )

Q1. Write a Python program to calculate the area of a rectangle.


Test case:
Input: Length = 5, Width = 10 Output: 50

Q2. Write a Python program to convert temperature from Celsius to


Fahrenheit.

Test case:

Input: Celsius = 25
Output: 77.0

Q3. Write a Python program to calculate the total price of items,


including tax.

Test case:

Input: Price = 1000, Tax Rate = 18


Output: 1180.0

Q4. Write a Python program to calculate the perimeter of a square. Test


case:Input: Side = 7 Output: 28

Q5. Write a Python program to find the remainder when dividing a


number by 7.
Test case:Input: Number = 20 Output: 6
Q6. Write a Python program to find the average of five numbers. Test
case:

Input: Num1 = 10, Num2 = 20, Num3 = 30, Num4 = 40, Num5 = 50

Output: 30.0

Q7. Write a Python program to calculate the simple interest. Test


case:
Input: Principal = 1000, Rate = 5, Time = 2
Output: 100.0

Q8. Write a Python program to calculate the total marks and percentage
from marks of five subjects.

Test case:

Input: Subject1 = 85, Subject2 = 90, Subject3 = 78, Subject4 = 88,

Subject5 = 92

Output: Total Marks = 433, Percentage = 86.6

Q9. Write a Python program to convert minutes into hours and minutes.
Test case:

Input: Minutes = 135

Output: Hours = 2, Remaining Minutes = 15

Q10. Write a Python program to calculate the total salary after


adding bonus.

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.

●​ Input: Enter a number: -4


●​ Output: 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 a number: 25


●​ Output: Divisible
Q4. Write a program that takes four angles and checks if they can form a
quadrilateral. (The sum of all four angles should be 360)

●​ 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.

●​ Input: Enter a number: 90


●​ Output: Divisible

Q6. Write a program that takes a month number from the user and
determines the season.

3 <= month <= 5; print "Spring"

6 <= month <= 8 then print "Summer"

9 <= month <= 11, then print "Autumn"

12<= month <= 2 then print "Winter"

●​ Input: Enter the month number: 4


●​ Output: Spring
If else Part - 2

Q1. W.A.P to check if a number is positive, negative, or zero.

Input: enter a number: 8

Output: positive

Q2. W.A.P to check if a number is even or odd.

Input: 9

Output: odd

Q3. W.A.P to Determine grade based on marks.

marks >= 90: Grade A

​ marks >= 80: Grade: B

​ Marks >= 70: Grade C

​ marks >= 60: Grade: D

otherwise,

​ Grade: E

Test Case 1:

​ Input: 50

​ Output: E

Q4. W.A.P to Check if a number is divisible by both 3 and 5.

Input: 15
Output: Divisible

Q5. program to determine if a person is a child, teenager, adult, or senior


based on their age

age <= 12:"Child"

age <= 19: "Teenager"

age <= 64: "Adult"

age > = 64:"Senior"

Test Case 1:

Enter age: 8

Child

Test Case 2:

Enter age: 16

Teenager

Q6. Check if a year is a leap year.

Test case 1:

​ Input: 1600

​ Output: Leap year

Test Case 2:

​ Input: 1900

​ Output: Not a leap year.


Q7. W.A.P to find the largest of three numbers.

Input: enter 3 number: 78, 56, 100

Output: greatest out of 3: 100

Q8. W.A.P Determine if a triangle is equilateral, isosceles, or scalene.

Input: enter 3 sides of a triangle 7, 7, 1

Output: isosceles

Q9. Determine if a year is a century year.

Example of century year 1800, 2000, 2100 many more…

Q10.Write a Python program that accepts three numbers and check All
numbers are equal or not.

Q11. Write a python program which takes x and y as a input and


determine the quadrant of a point in a coordinate system as per the
given information below:

​ x> 0, y> 0: first quadrant

​ x < 0, y > 0: second quadrant

​ x< 0, y<0: third quadrant

​ x> 0, y < 0: fourth quadrant

Test Case 1:

​ Input: x = 8 and y = -1

​ Output: fourth quadrant


Q11. Determine the season based on month

3 <= month <= 5; "Spring"

6 <= month <= 8"Summer"

9 <= month <= 11"Autumn"

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.

INPUT: LOWER RANGE: 10

​ UPPER RANGE: 89

​ INPUT_NUMBER: 77

OUTPUT: ‘YES’

Q13. Input 3 angle and determine if they form a triangle or not.

Q14. program that reads 2 numbers and an arithmetic operator like +, -,


*, /, % and display the computed result:

Example

Enter the 1 number: 5

Enter the 2 number: 2


Enter the operator: *

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:

input cost price: 1000

Input selling price: 1500

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

​ Output: Decreasing Order

Q18. While purchasing certain items, a discount of 10% is offered if the


quantity purchased is more than 100. If quantity and price per item are
input through the keyboard, write a program to calculate the total
expenses.

Q19. Write a program to input electricity unit charges and calculate total
electricity bill

according to the given condition:

• For first 50 units Rs. 0.50/unit

• For next 150 units Rs. 0.75/unit

• For next 250 units Rs. 1.20/unit

• For unit above 450 Rs. 1.50/unit

• An additional surcharge of 20% is added to the bill

Test Case 1:

Input: 100 units


Expected Output:

Rs. 62.5

With Surcharge: Rs. 75

Test Case 3:

Input: 300 units

Expected Output:

Rs. 257.5

With Surcharge: Rs. 309

Q20. Write a python program which accept the kilometres covered and
calculate the bill according to the following criteria:

First 10 Km -> Rs11/km

Next 90Km -> Rs 10/km

After that -> Rs9/km

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

While Loop Part - 2

Q1. Write a python program that calculates the factorial of a number.

Factor of n is: 1 * 2 * 3 * …….. * n

Input: 5

Expected Output: 120

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

Expected output: 1,2,3,4,6,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.

While loop Part - 3

Q1. Write a python program to find the Sum of the Digits of a


given number as an input.

Input Output Explanation

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.

Input Output Explanation

123 321 Reverse of 123 is 321.

456 654 Reverse of 456 is 654.

789 987 Reverse of 789 is 987.

Q3. Write a python program which find the Factors of a given


number as an input.

Explanation: Given an integer input, the objective is to find all


the factors ofthe number by checking divisibility.

Input Output Explanation

12 1, 2, 3, 4, 6, 12 Factors of 12 are 1, 2, 3, 4, 6, and 12.

15 1, 3, 5, 15 Factors of 15 are 1, 3, 5, and 15.

18 1, 2, 3, 6, 9, 18 Factors of 18 are 1, 2, 3, 6, 9, and 18.


Q4. Write a python program which check Whether the given number as
an input is a Palindrome or not.

Explanation: palindrome numbers are those whose reverse is


equal to the original number.

Input Output Explanation

121 Palindrome Reverse of 121 is 121, so it’s a palindrome.

123 Not a Reverse of 123 is 321, so it’s not a palindrome.


Palindrome

1331 Palindrome Reverse of 1331 is 1331, so it’s a palindrome.

Q5. Write a python program which finds Factorial of a Number.

Input Output Explanation

5 120 Factorial of 5 is 5! = 5×4×3×2×1 = 120.

6 720 Factorial of 6 is 6! = 6×5×4×3×2×1 = 720.

0 1 Factorial of 0 is 1 (by definition).

Q6. Check Whether or Not the Number is an Armstrong Number.

Explanation: An Armstrong number is a number that equals


the sum of its digits, each raised to a power (length of that
number or count of digit in that number).
Input Output Explanation

153 Armstrong 1³ + 5³ + 3³ = 153.

123 Not Armstrong 1³ + 2³ + 3³ ≠ 123.

370 Armstrong 3³ + 7³ + 0³ = 370.

Q7. Write a python program to check whether or Not the given


number is a Perfect Number.

Explanation: Given an integer input, the objective is to check


whether the sum of its factors (excluding the number itself)
equals the number. If so, the numberis a Perfect Number.

Input Output Explanation

6 Perfect Factors of 6 are 1, 2, 3. Sum = 1+2+3 = 6.

28 Perfect Factors of 28 are 1, 2, 4, 7, 14. Sum = 28.

10 Not Factors of 10 are 1, 2, 5. Sum = 8 ≠ 10.


Perfect

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

For loop Part - 1:

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

​ Example: If user enters ‘5’ then,


​ Output: 5 x 1 = 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

3. Write a python program that print the given series.

Expected Output: 20, 16, 12, 8, 4, 0

4. Write a python program to generate all the factors of the given


number. Using for loop

Input: 12

Output: 1, 2, 3, 4, 6, 12

5. Write a Python program to find the sum of digit of a given number


using for loop. (between 100 and 999).

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

7. Write a Python program to count how many numbers between 1 and


200 are divisible by 5 but not by 2.

8. Write a Python program to find the reverse of a given number using


for loop. (between 100 and 999).

Input: 897

Output: 798

9. Write a python program to input a number and calculate the ‘double’


factorial using for loop.

Explanation: Double factorial,

· 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

For Loop Part - 2

(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

* ​ *

* ​ * ​ *

* ​ * ​ * ​ *

* ​ * ​ *

* ​ *

For Loop Part - 4


(Hollow 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. 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

Q1. Write a Python function to find the maximum of three numbers

Q2. Write a Python function that takes a number as a parameter and


checks whether the number is prime or not.

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.

Q9. Write a function list_intersection(list1, list2) that


returns a list of elements that are present in both list1 and list2

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:-

words = ["apple", "banana", "kiwi", "grapefruit",


"orange"]

Output:- ('kiwi', 'grapefruit')


LIST Part - 1

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:

Lst = [15, 22, 35, 24, 65]


Q9. Write a program to which add the element 7 at the last of list Lst =
[4, 2, 5, 1, 3, 6, 8, 10, 3, 5], then sort the list. Print the list after these
operations.

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.

Note: ‘n’ can be any input value.

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]

Note: If list changes your code should work properly.

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]

Note: List can be changed.

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.

​ Sample List: [23,43,56,87]

(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.

​ Sample list = [34,56,23,76,23,46]

(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.

​ Sample list = [[1,2,4,5],[3,5,4,3],[4,5,3,2]]

​ Output: [12, 15, 14]

(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]

Q2. Write a Python program using a list comprehension to create a list of


even numbers less than 8 from 1 to 20.

Q3. Write a Python program using a list comprehension to create a list of


numbers from 1 to 10, but only include numbers that are greater than 5.

Q4. Write a Python program using a list comprehension to create a list of


the first letters of each word in the given list.

​ lst = ['apple', 'banana', 'cherry'].

Q5. Write a Python program using a list comprehension to create a list of


the lengths of each word in the given list.
​ lst = ['python', 'java', 'c++'].

Q6. Write a Python program using a nested list comprehension to flatten


the given list.

​ Input list: [[1, 2, 3], [4, 5], [6, 7, 8]].

​ Output: [1, 2, 3, 4, 5, 6, 7, 8]

Q7. Write a Python program using a list comprehension to create a list of


numbers from 1 to 20 that are divisible by both 2 and 3.

Q8. Write a Python program using a list comprehension to create a list of


the sums of corresponding elements from the lists [1, 2, 3] and [4, 5, 6].

​ Output: [5, 7, 9]

Q9. Write a python program using list comprehension to create a list


which has the sums of elements of list of given list.

​ lst = [[1,2,3],[3,4,5],[3,4,5],[5,7,3,2]]

​ Output: [6,12,12,17]

Q10. Write a python program using list comprehension to create a list


which has the min of elements of list of given list.

​ lst = [[1,2,3],[3,4,5],[3,4,5],[5,7,3,2]]

​ Output: [1,3,3,2]
TUPLE Part - 1

Q1. Write a python program to create a tuple with elements 'apple',


'banana', 'cherry'. Print the first and last elements.

Q2. Write a python program which prints the second last element and
check if 'blue' is in the given tuple.

​ t = ('red', 'green', 'blue', 'yellow')

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.

​ tpl = ('a', 'b', 'c', 'd', 'e')

Q4. Write a python program to concatenate the given tuples. Print the
result and then sort the tuple and print it.

​ t1 = ('a', 'b', 'c')

​ t2 = ('d', 'e', 'f')

​ Output: ('a', 'b', 'c', 'd', 'e', 'f')

​ ('a', 'b', 'c', 'd', 'e', 'f')

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.

​ tpl = ('python', 'java', 'c++', 'ruby')

​ Output: ‘ruby’

​ False

Q8. Write a python program to create a tuple with elements ('hello',


'world', 'python'). Print its length then add ‘3’ as int at the last of tuple and
Print

​ 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

Q1. Write a Python program to check if a specified element appears in a


tuple of tuples.​
Original Tuple:​
(('Red', 'White', 'Blue'), ('Green', 'Pink', 'Purple'), ('Orange', 'Yellow',
'Lime'))


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

Q2. WAP to check the tuple contain any duplicate element

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:- ​

How many student?=5


Enter name of student 1: Anaya

Enter name of student 2:Ausha

Enter name of student 3:Kirat

Enter name of student 4:Kyle

Enter name of student 5:Suji

Output:-

Enter name to be Searched for : Suji

Suji exists in a tuple

Q6. Python program to find unique numbers in a given tuple –

original tuple: (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)

Unique numbers: (1, 9, 6, 3, 4, 5, 2, 7, 8)

Q7. Write a Python program to convert a tuple of string values to a tuple


of integer values.​
Original tuple values:​
(('333', '33'), ('1416', '55'))​
New tuple values:​
((333, 33), (1416, 55))

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.

​ String = “Hello World, I am Programmer”

​ 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’

List = [‘I’ , ‘am’ , ‘programmer’ , ‘and’ , ‘I’ , ‘am’ , ‘coder’]

Output:

[‘I’ , ‘hii’ , ‘programmer’ , ‘and’ , ‘I’ , ‘hii’ , ‘coder’]

[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.

Q4. Write a python program which convert all occurrences of first


character and last character of given string into ‘$’.

​ Sample Input: ‘concatenation’

​ Output: $o$$ate$atio$
Q5. Write a program to remove all vowels from a given string.

​ Sample Input: ” concatenate”

​ 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.

​ Sample Input: ‘python’

​ 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.

​ String = ‘hello, i am a machine learning engineer’

​ Output = a am engineer hello, i learning machine

Q9. Write a python program which takes string as input and swap first
and last character of that string.

​ Sample Input: ‘hello’

​ Output: ‘oellh’
Q10. Write a program to check if a given string is a palindrome.

​ Palindrome: The string is palindrome if the sequence of characters


is in same order from backward as well as forward reading.

Example: wow, rotator, pop, noon.

Q11. Write a python program which convert given list into a single string
where each name is capitalized and separated by a semicolon.

​ list = ['bhopal' , 'vidisha' , 'rewa' , 'bina']

String Part – 2

Q1. Write a Python program to check if a given string is a valid email


address.

​ Ex: example@domain.com

Hint: Check ‘@’ comes earlier than ‘dot’

Q2. Write a Python program to reverse the order of words in a given


string without reversing the individual words.

​ Input: "Hello World from Python"

Output: "Python from World Hello"

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", 'm'


Output: [6, 7]

Q4. Write a Python program to remove all occurrences of a given


substring from a string.

Input: "This is a test string for testing", "test"

Output: "This is a string for ing"

Q5. Write a Python program to find all unique characters in a given


string.

Input: "programming"

Output: ['p', 'o', 'r', 'g', 'a', 'm', 'i', 'n']

Q6. Write a Python program to count the number of words in a given


string that start with a specific letter which was taken as a input.

Input: "This is a test string for testing", 't'

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.

Input: "Python is awesome"

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"

Output: "HellO WorlD FroM PythoN"

Q9. Write a Python program to remove given special characters from a


given string.

Special characters include symbols like @, #, !.

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).

X = {10, 20, 30, 40}

Y = {30, 40, 50, 60}

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.

A = {10, 20, 30}

B = {20, 30, 40}

Q7. Write a Python program to remove a specific element(n) from a set,


but only if it exists in the set.

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

Input:- {5.0, 6, 7.0, 8, 9.0, 10, 11.0, 12, 13.0, 14}


Output:- {5, 6.0, 7, 8.0, 9, 10.0, 11, 12.0, 13, 14.0}

Set Part - 3

Q1. Given two sets, A and B.

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}

Q2. Given two sets, X and Y.

Write a python program which check if all elements of X are


present within Y. If present, then remove those elements from Y;
otherwise, add the elements of X to Y.

Test case 1: X = {2, 4}

​ Y = {1, 2, 3, 4, 5}

​Test case 2: X = {6, 7}

​ Y = {1, 2, 3, 4, 5}
Q3. You are given three lists, L1, L2, and L3.

Write a python program which combine all unique elements from


these lists into a set. After that, remove any elements that appear in all
three lists.

L1 = [1, 2, 3, 4]

L2 = [3, 4, 5, 6]

L3 = [1, 4, 6, 7]

Hint: Use the concept of ‘set’

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}

Q5. Write a python program which combine three sets, A, B, and C.


Then, if any number in the set is greater than 10, remove all such
numbers.

​A = {2, 3, 5}

B = {5, 7, 11}

C = {11, 13, 17}

Q6. Given two sets, X and Y,


Write a python program which find the common elements and
store them in a new set. Then, create another set with elements that are
unique to either X or Y, but not both.

X = {10, 20, 30, 40}

Y = {30, 40, 50, 60}

Frozen Set
Q1. Write a Python program to create a frozenset from a list of numbers.

numbers = [10, 20, 30, 40, 50]

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.

​F1 = frozenset([100, 200, 300])

F2 = frozenset([200, 300, 400])

Q5. Write a Python program to find elements that are unique to each of
two frozensets (not in both).

fs1 = frozenset([2, 4, 6, 8])

fs2 = frozenset([4, 8, 12, 16])


Q6. Write a Python program to check if one frozenset is a subset of
another.

small_set = frozenset([1, 2])

large_set = frozenset([1, 2, 3, 4])

Dictionary Part - 1

Note: Dictionary for all the questions,

​ student_grade = {'Alice': 85, 'Bob': 90, 'Charlie': 78}

Q1: Write a Python program to create a dictionary named


student_grades with the following key-value pairs: 'Alice': 85, 'Bob': 90,
'Charlie': 78 and also print the dictionary.

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.

Output: {'Alice': 85, 'Bob': 90, 'Charlie': 78, 'David': 92}

Q4: Write a Python program to update the grade of 'Charlie' to 80 in the


given dictionary and print the dictionary.
Output: {'Alice': 85, 'Bob': 90, 'Charlie': 80, 'David': 92}

Q5: Write a Python program to delete the student 'David' from the given
dictionary using the del keyword, also print the updated dictionary.

Output: {'Alice': 85, 'Bob': 90, 'Charlie': 80}

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}

Q12. Write a Python program to check if a dictionary is empty or not.

​ D = {}

​ Output: True

Q13. Write a Python program to access dictionary key's element by


index (user input).

​ student_grade = {'Alice': 85, 'Bob': 90, 'Charlie': 78}

​ Sample Input: 2

​ Expected Output: bob

Dictionary Part – 2

Note: Dictionary for all question numbers,

​ fruit_prices = {'apple': 30, 'banana': 10, 'cherry': 25}

Q1: Write a Python program to create a dictionary named fruit_prices


using the dict() constructor with the following key-value pairs: 'apple': 30,
'banana': 10, 'cherry': 25 and print the dictionary.

Q2: Write a Python program to update the price of 'apple' to 35 and


'banana' to 12 in the given dictionary.

Output: {'apple': 35, 'banana': 12, 'cherry': 25}


Q3: Write a Python program to delete the key 'cherry' from the given
dictionary and print the updated dictionary and removed value.

Output: {'apple': 35, 'banana': 12}

25

Q4: Write a Python program to remove the last key-value pair from the
given dictionary and print the removed pair.

Output: ('banana', 12)

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.

Q6: Write a Python program to create a shallow copy of the given


dictionary and name it copied_prices and print both dictionaries.

Dictionary Part - 3

Q1. Write a Python program to create a dictionary named even_squares


that contains squares of even numbers between 1 and 10 using
dictionary comprehension.

Expected Output: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}

Q2. Write a Python program to create a dictionary using dictionary


comprehension from a given dictionary, where the new dictionary only
includes items with values greater than 10.
Use the dictionary: {'a': 5, 'b': 12, 'c': 7, 'd': 20}.

Q3. Write a Python program to create a dictionary using dictionary


comprehension where the keys are numbers from 1 to 10, and the
values are "even" or "odd" depending on the key value.

Q4. Write a Python program to reverse the keys and values of a given
dictionary using dictionary comprehension.

Dictionary: {'x': 1, 'y': 2, 'z': 3}

Expected Output: {1: 'x', 2: 'y', 3: 'z'}.

Q5. Write a Python program to create a dictionary using dictionary


comprehension that maps each word in a list to its length.

list: ['apple', 'banana', 'cherry', 'date'].

Q6. Write a Python program to create a dictionary using dictionary


comprehension where the keys are numbers from 1 to 10, and the
values are categorized as 'low' if the key is less than 5, 'medium' if
between 5 and 7, and 'high' if greater than 7.

You might also like