Python Practice Questions
Q1. Write a Python program that defines a function called "add_numbers" that takes two arguments
(i.e., numbers) and returns their sum. Within the function, add the two numbers together and return the
result using the return statement. Call the function with the values 5 and 6, and print out the returned
result. This will result in the addition of 5 and 6, with the output of the program being the sum of these
two numbers.
Q2. write a program that determines the nature of a given number (in this case, 87) as being positive,
negative, or zero? The program should be designed to take the number as input and perform the
necessary calculations to determine if the number is positive (i.e., greater than zero), negative (i.e., less
than zero), or zero (i.e., equal to zero). The output of the program should indicate which of these three
categories the given number falls into.
Q3. create a program that determines whether a given number (in this case, 98) is even or odd? The
program should be designed to take the number as input and perform the necessary calculations to
determine whether it is divisible by two. If the number is divisible by two without leaving a remainder,
it is an even number, and if there is a remainder, it is an odd number. The output of the program should
indicate whether the given number is even or odd.
Q4. Write a program for sum of [Link] digits are 76543 and the output should be 25.
[Link] a program for reversing the given number 5436 and the output should be 6345.
[Link] a list in python using the followings: 2,3,4,5,6,7 with variable ‘a’ Add ‘mango to the above
list Also add banana, grapes & orange in the list insert apple in the 5th position of a variable ‘a’ Remove
last item from the list.
Q7. L = [1,2,3,4,5,6,7] Using the above list slice from 1:4
Q8. Reverse the order of given string L = [4,5,6,8,3] Without using reverse() function.
Q9. Use list comprehension(shortcut method) to square the given list L=[2,4,7,3,6,8]
Q5. Create a function that takes in a tuple of integers and returns the sum of the integers. Test the
function with a tuple of your choice.
Q6. Create two sets of your favourite fruits, and use the union() method to combine them into a single
set. Print the resulting set to the console.
Q7. Create a set of random words, and use the add() method to add a new word to the set. Print the
resulting set to the console.
Q8. Create a set of your favourite animals, and use the remove() method to remove one animal from
the set. Print the resulting set
Q9. favorite_books = {"1984", "To Kill a Mockingbird", "Pride and Prejudice"} favorite_movies =
["The Shawshank Redemption", "The Godfather", "The Dark Knight"] Use the zip() function to
combine the book set and movie list into a list of tuples representing book/ movie pairs. Print the
resulting list.
Q10. Write a Python program to find the difference between consecutive numbers in a list.
Q11. Create a dictionary called fruits with the following key-value pairs:
"apple": 0.75
"banana": 1.25
"orange": 0.90
Then, print out the price of a banana.
Q12. Create an empty dictionary called ages. Add the following key-value pairs to the dictionary:
"Alice": 30
"Bob": 25
"Charlie": 35
Then, print out the age of Charlie.
Q13. Write a function called word_count(text) that takes a string as input and returns a dictionary where
each key is a word in the text and its value is the number of times that word appears in the text. For
example, word_count("hello world hello") should return {"hello": 2, "world": 1}.
Q14. Create a dictionary called phone_book with the following key-value pairs:
"Alice": "555-1234"
"Bob": "555-5678"
"Charlie": "555-9012"
Then, prompt the user to enter a name and print out the corresponding phone number. If the name is
not in the phone book, print out a message saying that the name was not found.
Q15. Write a program that prompts the user to enter a number between 1 and 10. If the number is less
than 5, print out "Too low!", otherwise print out "Too high!".
Q16. Write a program that prompts the user to enter a password. If the password is "password123",
print out "Access granted", otherwise print out "Access denied".
Q17. Write a program that prompts the user to enter a positive integer. Then, use a loop to print out all
the odd numbers from 1 to that integer.
Q18. Write a program that prompts the user to enter their age and then prints out whether they are a
child (age 0-12), a teenager (age 13-19), an adult (age 20-59), or a senior (age 60+)
Q19. Write a Python class called "Rectangle" with attributes for "width" and "height". Implement
methods to calculate the area and perimeter of the rectangle. Create an instance of this class and use it
to print out the rectangle's area and perimeter.
Q20. Write a program that prints the first 10 even numbers using a for loop.
Q21. Write a program that takes a list of strings and prints out each string in reverse order using a for
loop.
Q23. Write a program that prints the multiplication table of a given number using a for loop.
Q24. Write a program that takes a list of integers as input and returns the sum of all the numbers in the
list using a for loop.
Q25. Write a program that prompts the user for a positive integer and then prints out all the prime
numbers up to that number using a for loop.
Q26. Write a program that prompts the user to enter a password until the correct password is entered
using a while loop.
Q27. Write a program that takes a list of strings and prints out each string in reverse order using a while
loop.
Q28. Write a program that prompts the user to enter a positive integer and then prints out all the
Fibonacci numbers up to that number using a while loop.
Q29. Write a program that takes a list of integers as input and returns the product of all the numbers in
the list using a while loop.
Q30. Write a program that prompts the user to enter a positive integer and then prints out the factorial
of that number using a while loop.