Python Programming Practice Problems

Kushal Shah
7 min readApr 30, 2023

Here is a list of problems with gradually increasing difficulty levels that can help students in learning Python programming in a systematic way. The problems are chosen such that they are related to basic math so that there can be a better appreciation for the task and the solution. Write a function for each of the given function with a suitable input and output. Avoid using builtin functions.

Concept 1 : Conditionals [If-else]

  1. Given an integer, return True if it is even, else return False.
  2. Given two integers, return the larger one.
  3. Given a temperature in Celsius, convert it to Fahrenheit using the formula F = (C * 1.8) + 32. If the temperature is above 90°F, return True otherwise, return False.
  4. Given a year, return True if it is a leap year, else return False. A leap year is divisible by 4, except for years that are divisible by 100. However, years that are divisible by 400 are also leap years.
  5. Given two numbers, a and b, return the value of a/b (return None if b is zero).
  6. Given a point (x1, y1), return the quadrant (integer values given below) in which this point lies.
    [0: origin, 1: first quadrant, 2: second quadrant, 3: third quadrant, 4: fourth quadrant, 12: positive y-axis, 23: negative x-axis, 34: negative y-axis, 41: positive x-axis]
  7. Given two points (x1, y1) and (x2, y2), return the slope and intercept of the line joining these two points (the line may be perfectly horizontal or vertical). Input should be in this format : (x1, y1, x2, y2).
  8. Given a quadratic equation with coefficients a, b and c, return the two solutions (may be real or complex). You should not take the square root of a negative number in your code. Output should be a list of two tuples. So if the roots are 1+2j and 1–2j, the output of the function should be [(1,2), (1,-2)]. If the roots are real, then the second part of both the tuples becomes zero.
  9. Given three points, return True if they lie on the same straight line, else return False. Input should be in this format : (x1, y1, x2, y2, x3, y3).
  10. Given three integers, return the smallest one.

Concept 2 : For Loops (without lists)

  1. Given a positive integer, return its factorial.
  2. Given a positive integer, return True if it’s prime, else return False.
  3. Given a positive integer, return the sum of all integers from 1 up to this number.
  4. Given a positive integer, return the sum of all odd numbers from 1 up to this number.
  5. Given a positive integer greater than 1, return the sum of all even numbers from 2 up to this number.
  6. Given a positive integer, return its binary representation (output using integer datatype).
  7. Given a binary representation of an integer (input using integer datatype), return the corresponding integer value in decimal representation.
  8. Given a positive integer, return True if its a palindrome, else return False.
  9. Given a positive integer, return the number of digits in it.
  10. Given a positive integer, return the number of even digits in it.

Concept 3 : For Loops (with lists)

  1. Given a list of numbers, return its length and the sum of all these numbers.
  2. Given a list of numbers, return a list of the squares of all the numbers.
  3. Given a list of numbers, return their mean and standard deviation.
  4. Given a list of integers, return the count of even numbers in it.
  5. Given a list of numbers, return the list in reverse order (without using list splicing).
  6. Given a list of numbers, return the maximum number in it.
  7. Given a list of integers and another integer, return the index of this given integer.
  8. Given a list of integers, return their Least Common Multiple (LCM).
  9. Given a list of integers, return their Greatest Common Divisor (Divisor).
  10. Given a positive integer (n), return a list containing the first n integers in the Fibonacci series.

Concept 4 : Nested Loops

  1. Given a list of distinct numbers, return another list which contains the sum of all pairs of numbers in the given list (the same pair should not be taken twice).
  2. Given a list of distinct numbers (may contain zero), return another list which contains the ratio of all pairs of numbers in the given list (the same pair should not be taken twice).
  3. Given a list of positive integers, return a list of the factorial of all these numbers.
  4. Given a positive integer, return a list of all prime numbers from 1 up to this number.
  5. Given a positive integer, return the sum of all prime numbers from 1 up to this number.
  6. Given a list of numbers, return another list of co-primes and count how many co-primes are there in this given list.
  7. Given two 2D matrices of the same dimensions, return their sum.
  8. Given a list of integers, sort it on your own and return the median.
  9. Given a list of integers, return its mode (list of numbers with highest frequency of occurrence). Do not use a dictionary.
  10. Given a list of lists of integers, return a list that is sorted based on the sum of each inner list. Do not use any inbuilt function for sorting.

Concept 5 : Dictionaries

  1. Given two lists of the same length as input, return a dictionary with keys taken from the first list and values from the second list. If the list sizes are different, consider the length of the shorter one for creating the dictionary.
  2. Given two dictionaries, merge them and return a single dictionary.
  3. Given a dictionary and a key, return True if the key is present in the dictionary, else return False.
  4. Given a dictionary and a list of keys, return another dictionary which only has the keys given in the input list, and values taken from the input dictionary.
  5. Given a dictionary, return the inverted dictionary, i.e. keys of the output dictionary are the values of the input dictionary, and values of the output dictionary are the keys of the input dictionary.
  6. Given a dictionary containing the student names as keys and list of marks in 3 subjects as the dictionary values, return a list containing the average marks for each subject.
  7. Given a dictionary containing the student names as keys and list of marks in 3 subjects as the dictionary values, return a dictionary where the keys are the student names and values are the student CGPA. Above 80 marks is A grade with 10 grade points, 60–80 marks is B grade with 8 grade points, 40–60 marks is C grade with 6 grade points, and below 40 is F with 0 grade points. Course1 is of 4 credits, Course2 of 10 credits, and Course3 of 6 credits.
  8. Given a fruit name and a dictionary whose keys are people names and the values are the list of fruits they like, return the list of names of people who like this fruit.
  9. Given a list of integers, return its mode (list of numbers with highest frequency of occurrence) by using a dictionary in your code.
  10. Given a text file with one word in each line, return a dictionary where the key is the word and the value is the number of times it occurs in the text file.

Concept 6 : Strings

  1. Given a string as input, return its length without using the inbuilt length function.
  2. Given a string as input, return the string in reverse order (without using string splicing).
  3. Given a string as input, return the number of vowels in it.
  4. Given a string as input, return the number of words in it. Use space as a separator for words.
  5. Given a string as input, return the string with the first letter of each word capitalised.
  6. Given a string as input, return the length of the longest word in it. Use space as a separator for words.
  7. Given a string as input, return True if its a valid email address, else return False.
  8. Given a string as input, return True if its a valid mobile number in India, else return False.
  9. Given a string as input, return True if its a palindrome, else return False (case sensitive and ignore spaces). Do not use string splicing.
  10. Given a paragraph as input, return a list of sentences. Use full-stop, exclamation and question mark as the three allowed delimiters between sentences. These punctuation marks should be included in the output list of sentences.

Higher Level Programming Problems

  1. Find the exit path in a 2D maze from an arbitrary starting point.
  2. Given an arithmetic expression as an input string, solve the expression and return the result (without using the in-built eval function).
  3. Implement the Tic-Tac-Toe game. The program should allow two players to play against each other and display the board after each move.
  4. Determine whether a given graph is planar or not.
  5. Simulate the motion of a particle in an arbitrary polygon. The particle moves in straight lines between collisions, and the collisions could be inelastic.
  6. Given a sorted list of integers, search for a given integer using the binary search algorithm.
  7. Given two strings as input, find out if these are the same or not (allowing for maximum one mismatch).
  8. Find out the product of two 3x3 matrices. Can you generalise it to a square matrices of higher dimensions?
  9. Find out the inverse of a given 3x3 matrix. Can you generalise it to a square matrix of higher dimensions?
  10. Given a dataset of texts (a .txt file with paragraphs separated by a new line) and a user query, find out which paragraph from this database would be the most suitable response to the user query.

What next?

Once you have successfully completed all the above problems, I would advice you to learn HTML, PostgreSQL and Flask, and start building your own interactive website for a problem that is dear to your heart. Humans learn best when they do things that they truly care for! And if you have built something cool, please do share it with me.

--

--

Kushal Shah

Building Self Shiksha, a free e-learning platform for AI/ML and teaching at Sitare University. Studied at IIT Madras, and taught at IIT Delhi and IISER Bhopal.