ShareYourGroupShareYourGroupPython

Python practice questions

All 63 exercises from the lessons, gathered on one page. Use it to revise a topic, to test yourself after a break, or to pick something to practise today. The answers are at the end of each lesson, so that you are not tempted too soon.

1. Your first Python program

  1. Print your own name on one line and your city on the next line.
  2. Make Python work out 365 multiplied by 24 and print the answer. The multiplication sign in Python is the star.
  3. Print the sentence Python is fun three times using three lines of code.

Answers are at the end of the lesson

2. Print and comments

  1. Print today's date as three numbers joined by slashes, using sep. Use the values 12, 11 and 2026.
  2. Print a small menu of three items, each on its own line, using only one print.
  3. Print A B C using three separate print calls that all stay on the same line.

Answers are at the end of the lesson

3. Variables

  1. Make variables for a product name, its price and the quantity bought. Print the total cost.
  2. Start with runs = 0. Add 4, then add 6, then print the result. Use the short form +=.
  3. Two variables hold a = "tea" and b = "coffee". Swap them in one line and print both.

Answers are at the end of the lesson

4. Numbers and maths

  1. A shop sells a pen for 12 rupees. Work out the cost of 15 pens, and the change from a 200 rupee note.
  2. Turn 500 seconds into minutes and seconds using // and %.
  3. Marks in three subjects are 78, 85 and 91. Print the average, rounded to one decimal place.

Answers are at the end of the lesson

5. Strings

  1. Store your full name in a variable. Print its first letter, its last letter and its length.
  2. From the string "2026-11-12", print the year, the month and the day on separate lines using slices.
  3. Check whether the word "level" reads the same backwards. Print True or False.

Answers are at the end of the lesson

6. String methods and f-strings

  1. A user typed " RAVI kumar ". Clean it so it prints as Ravi Kumar with no spaces at the ends.
  2. Count how many words are in the sentence "practice makes a person perfect".
  3. Given item = "Rice", kg = 5 and rate = 62.5, print one line like Rice: 5 kg costs Rs 312.50 using an f-string.

Answers are at the end of the lesson

7. Input and type conversion

  1. Ask for the length and width of a room in feet and print its area. For the test, use 12 and 10.
  2. Ask for a temperature in Celsius and print it in Fahrenheit. The rule is multiply by 9, divide by 5, add 32. Test with 37.
  3. Ask for a bill amount and the number of friends, and print each person's share to two decimal places. Test with 1250 and 4.

Answers are at the end of the lesson

8. If, elif and else

  1. A number is stored in n. Print "even" or "odd". Test with 17.
  2. A cinema charges 100 rupees for children under 12, 150 for people of 60 and above, and 200 for everyone else. Print the price for age = 64.
  3. A year is a leap year if it divides by 4, except that century years must divide by 400. Print whether 1900 is a leap year.

Answers are at the end of the lesson

9. While loops

  1. Print the table of 7 from 7 times 1 to 7 times 10 using a while loop.
  2. Starting from 1, keep doubling a number until it is more than 1000. Print the final number and how many doublings it took.
  3. Add up the digits of 9875 using a loop with % 10 and // 10.

Answers are at the end of the lesson

10. For loops and range

  1. Print the squares of the numbers 1 to 10 on one line.
  2. Count how many vowels are in the sentence "education is the key".
  3. Print all numbers from 1 to 30 that divide by 3 but not by 2.

Answers are at the end of the lesson

11. Lists

  1. Make a list of five city names. Print the first, the last and how many there are.
  2. Start with scores = [12, 45, 7, 30]. Add 50 at the end, remove the 7, and print the list and its total.
  3. From numbers = [4, 9, 12, 7, 20, 3], build a new list holding only the even numbers.

Answers are at the end of the lesson

12. Sorting, slicing and searching lists

  1. Given prices = [250, 99, 480, 150, 320], print the two cheapest prices.
  2. Print the names in ["meena", "Asha", "ravi"] in alphabetical order, ignoring capital letters.
  3. Two lists hold items and quantities: ["pen", "book", "bag"] and [10, 4, 1]. Print each item with its quantity on one line.

Answers are at the end of the lesson

13. Tuples and sets

  1. Store a time as the tuple (9, 30). Unpack it into hour and minute and print it as 09:30.
  2. How many different letters are in the word "mississippi"? Print them in alphabetical order too.
  3. Monday's visitors were ["Asha", "Ravi", "Tara"] and Tuesday's were ["Ravi", "Joy", "Asha"]. Print who came on both days.

Answers are at the end of the lesson

14. Dictionaries

  1. Make a dictionary of three fruits and their prices per kg. Print the price of one fruit, then add a fourth fruit and print how many there are.
  2. Count how many times each letter appears in the word "banana".
  3. A shop's stock is {"pen": 40, "book": 0, "bag": 7, "ink": 0}. Print the names of the items that are out of stock.

Answers are at the end of the lesson

15. Functions

  1. Write a function square(n) that returns the square of a number. Print the squares of 4 and 11.
  2. Write a function to_fahrenheit(c) that returns a Celsius temperature in Fahrenheit. Test it with 0 and 100.
  3. Write a function count_vowels(text) that returns how many vowels are in a string. Test it with "Programming in Python".

Answers are at the end of the lesson

16. Function arguments and scope

  1. Write power(base, exponent=2) that returns the base raised to the exponent. Print power(5) and power(2, 10).
  2. Write total_price(price, quantity=1, discount=0) where discount is in percent. Print the cost of 3 items at 200 rupees with a 10 percent discount, using keyword arguments.
  3. Write longest(*words) that returns the longest of any number of words. Test it with four words.

Answers are at the end of the lesson

17. Modules and import

  1. Use math to find the length of the long side of a right-angled triangle whose short sides are 5 and 12.
  2. Simulate rolling two dice and print both numbers and their total. Use random.seed(7) so the answer can be checked.
  3. How many days are there between 15 August 1947 and 26 January 1950?

Answers are at the end of the lesson

18. Reading and writing files

  1. Write the names of three friends to a file called friends.txt, one per line. Then read the file and print how many lines it has.
  2. Write the numbers 1 to 5 to a file, one per line. Read them back and print their sum.
  3. Create a file with one line, then append a second line, then print the file in capital letters.

Answers are at the end of the lesson

19. Errors and try/except

  1. Convert the string "12a" to an integer inside a try. If it fails, print Invalid number.
  2. Write a function safe_divide(a, b) that returns the result, or None if b is zero. Test both cases.
  3. A dictionary holds {"tea": 12, "lassi": 40}. Look up "coffee" inside a try and print Not on the menu if the key is missing.

Answers are at the end of the lesson

20. List comprehensions

  1. Make a list of the cubes of the numbers 1 to 6.
  2. From temps = [31, 38, 29, 41, 36], make a list of the temperatures above 35.
  3. From the sentence "learning python step by step", make a list of the first letter of each word, in capitals.

Answers are at the end of the lesson

21. Classes and objects

  1. Write a class Rectangle with a length and a width, and a method area. Print the area of a 12 by 5 rectangle.
  2. Write a class Counter that starts at 0 and has a method click that adds 1. Click it three times and print the count.
  3. Add a method grade to a Student class that returns "A" for 75 marks and above, "B" for 50 and above, and "C" otherwise. Test with 82.

Answers are at the end of the lesson

How to use these questions

To revise: pick a topic you finished some days ago and try its three questions without opening the lesson. If you can do all three, the topic has stayed with you. If not, reread that lesson's examples, close it, and try again tomorrow.

To practise daily: do three questions a day from different topics. Mixing topics is harder than doing one topic in a block, and that extra effort is what makes knowledge last.

To go further: after solving a question, change it. Make the list longer, the limit different, the input wrong. Ask "what if the list is empty?" or "what if the number is negative?" and make your program cope.

Checking your answer

Run your program and compare what it prints with the output shown under our answer in the lesson. Your code can look quite different from ours and still be right. There are many ways to write most programs. Once yours works, read our version too, because seeing a second way to solve the same problem is one of the quickest ways to learn.

If your program stops with an error, read the last line of the message and look it up in the errors section.

Want something bigger?

When single questions feel easy, move on to the small projects, where one program grows over several stages.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.