0% found this document useful (0 votes)
4 views45 pages

Python Questions

Uploaded by

aksjat19
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)
4 views45 pages

Python Questions

Uploaded by

aksjat19
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

info@[Link] Code and Debug codeanddebug.

in

QUESTIONS 1-3
Q1. Write a program that prints a path like this:
C:\Users\John\Desktop\[Link] using the appropriate escape sequences.

Q2. Write a Python program that prints a message with a double-quote


character inside it.

For example: He said, "Hello!".

Q3. Create a program that prints a message containing both single and
double quotes, like this: She said, 'It's cold'.
info@[Link] Code and Debug [Link]

QUESTIONS 4-11
Q4. Write a Python program to add two numbers entered by the user.

Q5. Convert a string to an integer and vice versa.

Q6. Write a Python program to calculate the area of a rectangle using user
input for length and width.

Q7. Write a Python program to calculate the average of three numbers


entered by the user.

Q8. Convert a float to an integer and vice versa.

Q9. Write a program that converts a temperature in Fahrenheit to Celsius.


The formula is: Celsius = (Fahrenheit - 32) * 5/9.

Q10. Calculate sum of 5 subjects and Find percentage.

Q11. Ask number of games played in a tournament. Ask the user number of
games won and number of games loss. Calculate number of tie and total
Points. (1 win= 4 points, 1 tie =2 points)

Video Solution is given just after this resource.


info@[Link] Code and Debug [Link]

QUESTIONS 12-15
ARITHMETIC & ASSIGNMENT
Q12. Write a Python program that takes two numbers as input and
performs the following operations: addition, subtraction, multiplication,
division, and modulus. Display the results.

Q13. Write a Python program to swap the values of two variables without
using a temporary variable.

Q14. Write a Python program to calculate the compound interest for a


given principal, rate of interest, and time period. Ask everything from the
user.

Q15. Write a Python program that takes the radius of a circle as input and
calculates its area. Use the formula: Area = 3.14 * r^2.
info@[Link] Code and Debug [Link]

QUESTIONS 16-22
COMPARISON & LOGICAL OP
Q16. Guess the output:

Q17. Guess the output:

Q18. Guess the output:

Q19. Guess the output:


Q20. Guess the output:

Q21. Guess the output:

Q22. Guess the output:


info@[Link] Code and Debug [Link]

QUESTIONS 23-26
IF - ELSE
Q23. Write a Python program that takes an integer input and prints
whether it's positive, negative. (Consider 0 as positive)

Q24. Write a program that takes a character as input and prints whether
it's a vowel or a consonant. (Multiple OR will be used)

Q25. Write a program that takes two numbers as input and checks if the
first number is divisible by the second.

Q26. A student will not be allowed to sit in exam if his/her attendance is


less than 75%.

Take following input from user

Number of classes held


Number of classes attended.

1. Print percentage of class attended


2. Print Is student is allowed to sit in exam or not.
info@[Link] Code and Debug [Link]

QUESTIONS 27-37
IF - ELIF - ELSE
Q27. Write a program to check if the number is ODD, EVEN or Equal to Zero.

Q28. Write a program to check if number is divisible by 2 and 3 but not 8.

Q29. Write a program to print the last digit of a number. (NOT A IF ELSE
QUESTION)

Example 1

Input: 45321

Output: 1

Example 2

Input: 459094

Output: 4

Q30. Write a program to check if the last digit of a number is divisible by 5


or not.

Q31. Write a program to calculate bill. Ask the final amount from the user.

You have to give discount and print the final bill after discount.

50000 above - 30% discount

40000 - 49999 - 25% discount

30000 - 39999 - 20% discount

10000 - 29999 - 10% discount

1 - 9999 - No discount

Print the discount and the final amount to be paid.


Example 1

Enter bill amount = 80000

You got 30% discount

Your final bill is Rs. 56000

Q32. Ask 4 numbers from user. Make sure all the numbers entered by user
are different. Print which number is the smallest.

Q33. Ask a number from user.

Print “Fizz” if the number is divisible by 3.


Print “Buzz” if the number is divisible by 5.
Print “FizzBuzz” if the number is divisible by 3 and 5.
Print the number itself if none of the conditions are true.

Below questions, do on your own.

Q34. A student will not be allowed to sit in exam if his/her attendance is


less than 75%.

a. Take following input from user

i. Number of classes held

ii. Number of classes attended.

b. Print percentage of class attended

c. Print Is student is allowed to sit in exam or not.

Q35. Take Salary as input from User and Update the salary of an employee.

salary less than 10,000, 5 % increment


salary between 10,000 and 20, 000, 10 % increment
salary between 20,000 and 50,000, 15 % increment
salary more than 50,000, 20 % increment

Q36. Take three numbers as input from User and print which one is greater
or are they equal.
Q37. An extra day is added to the calendar almost every four years as
February 29, and the day is called a leap day. A leap year contains a leap
day.

These are the conditions used to identify leap years:

if the year can be evenly divided by 4, it is then a leap year


but if the year is evenly divided by 4 and also by 100, then it is NOT a
leap year
but if the year is evenly divided by 4 and also by 400, then it is a leap
year

This means the years 2000 and 2400 are leap years, while 1800, 1900,
2100, 2200, 2300 and 2500 are NOT leap years.

Ask a year input from user. And tell if the year entered by user is leap or
not.
info@[Link] Code and Debug [Link]

QUESTIONS 38 - 41
NESTED IF - ELSE
Q38. Write a program that takes three numbers as input and determines
the largest one using nested if-else statements. Make sure all 3 numbers
entered by user are different.

Q39. Write a program that checks if a given year is a leap year. Leap years
are divisible by 4, but not by 100 unless they are also divisible by 400.

Q40. Create a program that calculates the price of a movie ticket based on
the age of the customer. If the customer is under 12, the ticket costs $5; if
they are between 12 and 65, the ticket costs $10; otherwise, it's $7.

Q41. Write a program that calculates a person's BMI based on their height
(in meters) and weight (in kilograms). Use the following formula: BMI =
weight / (height^2). Then, classify the BMI according to the following
ranges:

Underweight: BMI less than 18.5


Normal weight: BMI 18.5 - 24.9
Overweight: BMI 25 - 29.9
Obesity: BMI 30 or greater
info@[Link] Code and Debug [Link]

QUESTIONS 42-53
BASIC WHILE LOOP
Q42. Ask a number from user. Print all the numbers from 1 to that number.

Q43. Ask a number (N) from user. Print all the numbers from N to 1.

Q44. Ask start number and end number from user. Print all the numbers
from start to end using while loop.

Q45. Calculate the sum of all the numbers from 1 to 10.

Q46. Calculate product of all the numbers from 1 to 10.

Q47. Calculate how many numbers are divisible by 7 from 1 to 100.

Q48. Calculate how many numbers are divisible by both 6 and 7 between 1
to 200.

Q49. Write a program to calculate the sum of all the numbers divisible by 4
from 20 to 50.

DO ON YOUR OWN

Q50. Calculate how many numbers are divisible by 6 and 7 between 1 to


200.

Q51. Ask a number from user. Print the multiplication table of that number.

Example

Enter a number = 8

8x1=8

8 x 2 = 16

8 x 3 = 24
.....

8 x 10 = 80

Q52. Calculate factorial of a number entered by user.

Example:

Enter a number = 5

Factorial of a number means product of all the numbers from 1 to that


number.

5 factorial = 5 x 4 x 3 x 2 x 1

Output = 120

Q53. Ask to numbers x and y from the user. If x<y then print all the
numbers from x to y, but if y<x then print all the numbers from y to x.
info@[Link] Code and Debug [Link]

QUESTIONS 54-65
BASIC FOR LOOP
Q54. Ask a number from user. Print all the numbers from 1 to that number.

Q55. Ask a number (N) from user. Print all the numbers from N to 1.

Q56. Ask start number and end number from user. Print all the numbers
from start to end using while loop.

Q57. Calculate the sum of all the numbers from 1 to 10.

Q58. Calculate product of all the numbers from 1 to 10.

Q59. Calculate how many numbers are divisible by 7 from 1 to 100.

Q60. Calculate how many numbers are divisible by both 6 and 7 between 1
to 200.

Q61. Write a program to calculate the sum of all the numbers divisible by 4
from 20 to 50.

DO ON YOUR OWN

Q62. Calculate how many numbers are divisible by 6 and 7 between 1 to


200.

Q63. Ask a number from user. Print the multiplication table of that number.

Example

Enter a number = 8

8x1=8

8 x 2 = 16

8 x 3 = 24
.....

8 x 10 = 80

Q64. Calculate factorial of a number entered by user.

Example:

Enter a number = 5

Factorial of a number means product of all the numbers from 1 to that


number.

5 factorial = 5 x 4 x 3 x 2 x 1

Output = 120

Q65. Ask to numbers x and y from the user. If x<y then print all the
numbers from x to y, but if y<x then print all the numbers from y to x.
info@[Link] Code and Debug [Link]

QUESTIONS 66-72
BASIC NESTED LOOPS
Q66. Print the following pattern.

Q67. Print the following pattern.

Q68. Print the following pattern. (Do on your own).

Q69. Print the following pattern.


Q70. Print the following pattern. (Do on your own).

Q71. Ask N from user. N means number of lines. Then print the following
pattern.

Q72. Ask N from user. N means number of lines. Then print the following
pattern. (Do on your own)
info@[Link] Code and Debug [Link]

QUESTIONS 73-84
INTERMEDIATE NESTED LOOPS

Q73. Print the following pattern.

Q74. Print the following pattern.

Q75. Print the following pattern.


Q76. Print the following pattern.

Q77. Print the following pattern. (Do on your own)

Q78. Print the following pattern. (Do on your own)

Q79. Print the following pattern.


Q80. Print the following pattern. (Do on your own)

Q81. Print the following pattern. (Do on your own)

Q82. Print the following pattern.


Q83. Print the following pattern. (Do on your own)

Q84. Print the following pattern. (Do on your own)


info@[Link] Code and Debug [Link]

QUESTIONS 85 - 91
ADVANCE NESTED LOOPS

Q85. Print the following pattern.

Q86. Print the following pattern. (Do on your own)

Q87. Print the following pattern. (Do on your own)


Q88. Print the following pattern.

Q89. Print the following pattern.

Q90. Print the following pattern. (Do on your own)

Q91. Print the following pattern. (Do on your own)


info@[Link] Code and Debug [Link]

QUESTIONS 92-103
BASIC LIST ITERATION

Q92. Make your own list. Print the list in reverse.

Q93. Make your own list. Print all the even numbers present in the list.

Q94. Make your own list. Print all the odd numbers present in the list. (Do
on your own)

Q95. Make your own list. Print all the elements present at even index
position.
Q96. Make your own list. Print the sum of all elements present in that list.

Q97. Make your own list. Count the number of even numbers present in
that list.

Q98. Make your own list. Count how many numbers are divisible by both 2
and 5 in that list. (Do on your own)

Q99. Make your own list. Find the sum of all even numbers present in that
list.
Q100. Make your own list. Find the sum of all numbers divisible by 3 or 4.
(Do on your own)

Q101. Make your own list. Print how many positive and negative numbers
are here. (Do on your own)

Q102. Make your own list. Print the largest number present in that list.

Q103. Make your own list. Print the smallest number present in that list.
(Do on your own)
info@[Link] Code and Debug [Link]

QUESTIONS 104-109
LIST METHODS (BASIC)

Q104. Write a program that prompts the user to specify the length of a list
and then requests numbers to populate that list. Display the final list as
the output.

Q105. Create a list and prompt the user for an 'old number' followed by a
'new number.' If the 'old number' exists in the list, replace it with the 'new
number' provided by the user.

Q106. Remove all the even numbers from the list.

Q107. Ask the user for a number. Then, from a list of numbers, remove all
the numbers that can be divided by the number the user entered. (Do on
your own).
Q108. Generate a list of at least 10 numbers. Then, create two separate
lists called 'odd' and 'even.' Put all the odd numbers from the original list
into the 'odd' list, and all the even numbers into the 'even' list.

Q109. Start by creating two separate lists with random numbers. Then,
create a third list that merges the numbers from the first and second lists
together.
info@[Link] Code and Debug [Link]

QUESTIONS 110-120
LIST METHODS (INTERMEDIATE)

Q110. Make a list of your own. And remove all the duplicates element from
that list.

Q111. Make a list. Then ask a number from user. If number exists in that list
then print the position of the element else print -1.

Q112. Take 10 integer inputs from user and store them in a list. Now, copy
all the elements in another list but in reverse order.
Q113. Write a program to find the average of all the numbers present in the
list. (Do on your own)

Q114. Write a Python code to find the occurrence of each element in a list
and print the element with the highest occurrence.

Q115. Write a program that has two lists and make a new list that contains
only the common elements between them without duplicates.

Q116. Write a Python code to find the second largest element in a list
without sorting.

Q117. Make a program that takes a list of integers and returns the product
of all the elements.

Q118. Write a program to find and print all prime numbers within a given
list.
Q119. Write a program to split a given list into two halves. (Do on your own)

Q120. Write a program that swaps the first and last elements of a given list.
info@[Link] Code and Debug [Link]

QUESTIONS 121-124
LIST COMPREHENSION

Q121. Generate a list of squares of numbers from 1 to 10 using list


comprehension.

Q122. Given a list of strings, create a new list containing the lengths of
each string using list comprehension.

Q123. Generate a list of strings where each string repeats itself three times,
using list comprehension.
Q124. Generate a list of list using list comprehension where format should
be [[1, ”ODD”], [2, “EVEN”], [3, ”ODD”]].
info@[Link] Code and Debug [Link]

QUESTIONS 125-131
LIST SLICING

Q125. Write a python program to reverse a list using slicing.

Q126. Write a python program to get every third element from a list using
slicing.

Q127. Implement a python program to split a list into two equal parts using
slicing. One list should contain 1st half and another list should contain 2nd
half.

Q128. Implement a python program to get the last 'n' elements from a list
using slicing.

Q129. Ask ‘n’ from user. Create a list of last n elements but in reverse order
using slicing.
Q130. Ask start and end index from the user. Create a list from start index
to end index using slicing.

Q131. Ask ‘n’ from user. Create a list of first n elements but in reverse order
using slicing.
info@[Link] Code and Debug [Link]

QUESTIONS 132-137
STRING METHODS - 1

Q132. Ask a string from user. Count how many alphabets are there in that
string.

Q133. Ask a string from user. Count the number of uppercase and
lowercase characters in that String.

Q134. Ask a string from user. Convert all the alphabets to uppercase.

Q134. Ask a string from user. Convert all the alphabets to lowercase. (Do
on your own)

Q135. Ask a string from user. Convert uppercase to lowercase and convert
lowercase to uppercase and don’t change the other letters. (Do on your
own)

Q136. Count the number of spaces in a string entered by user.

Q137. Ask a string from user. Print the count of how many alphabets, digits,
spaces and symbols (everything else) are there in that string. (Do on your
own)
info@[Link] Code and Debug [Link]

QUESTIONS 138-141
STRING METHODS - 2

Q138. Write a program to reverse the order of words.

Q139. Write a program that accepts a string and capitalizes the first letter
of each word while converting all other letters to lowercase.

Q140. Write a program that reverses each word in a sentence while


maintaining the word order. For example, "Hello World" should become
"olleH dlroW".

Q141. Write a program that converts a string in camelCase to snake_case.


For example, converting "helloWorldHowAreYou" should result in
"hello_world_how_are_you". (Do on your own)
info@[Link] Code and Debug [Link]

QUESTIONS 142-150
DICTIONARY ITERATION

Q142. Ask subject name and marks from the user and keep adding it to
dictionary.

Q143. Convert two lists into a dictionary. Make two list on your own of
same length, and convert them to dictionary.

Q144. Write a Python program to sum all the items in a dictionary.

Q145. Write a Python program to multiply all the items in a dictionary. (Do
on your own)
Q146. Ask a string from user. Display the dictionary where each key is a
character and value is the frequency of that character that comes in that
string.

Q147. Store “name” of a student as Key, “list of 5 marks” of that student as


a Value. Store atleast 5 student names. Print the sum and percentage of all
the students.

Q148. Store marks of 5 different subjects in a dictionary. Ask subject name


as an input from the User. Print the marks of that subject entered by User.
If subject does not exist, print “Invalid”.
Q149. Store name as a Key, and 5 marks in a List as a value in dictionary.
Store details of at least 5 students. Print the name of the student who got
highest marks.

Q150. Write a Python program to combine two dictionary by adding values


for common keys.
info@[Link] Code and Debug [Link]

QUESTIONS 151-157
SETS IN PYTHON

Q158. Given two lists a, b. Check if two lists have at least one element
common in them.

Q159. Python program to find common elements in three lists using sets.

Q160. Create 3 sets of your own, find the union of three sets.

Q161. Write a Python program to remove all elements from a given set.

Q162. Write a Python program to find the length of a set.

Q163. Write a Python program to check if two given sets have no elements
in common.

Q164. Write a Python program to find elements in a given set that are not
in another set.

Q165. Ask a string from user, remove all the duplicates from that string and
print that string again (order does’nt matter)
info@[Link] Code and Debug [Link]

QUESTIONS 166 - 172


FUNCTIONS IN PYTHON

Q166. Write a function that accepts an integer and prints the


multiplication table for that number up to 10.

Q167. Write a function that accepts an integer and prints whether it is odd
or even.

Q168. Write a function that takes three numbers as parameters and prints
the largest among them.

Q169. Write a function that takes an integer and prints whether it is a


prime number.

Q170. Write a function that takes a list of numbers and prints the sum and
average of these numbers.

Q171. Write a function that accepts a string and prints the frequency of
each character in the string.

Q172. Write a function that takes a string and prints whether it is a


palindrome.

You might also like