Practice Problems
Sharpen your Python skills with 45 coding challenges
Basics(5 problems)
Print Your First Message
beginnerWrite a Python program that prints the exact message: Hello, Python learner!
Simple Calculator
beginnerWrite a function add(a, b) that returns the sum of two numbers.
Sum of Three Numbers
beginnerWrite a function sum_of_three(a, b, c) that returns the sum of three numbers. This reinforces defining and calling functions with multiple parameters.
Greet the User
beginnerWrite a program that asks the user for their name using input() and then prints a greeting in the format: Hello, <name>!
Sum of Two String Numbers
beginnerWrite a function sum_two_strings(a, b) where a and b are strings representing integers. Convert them to integers and return their numeric sum.
Loops(2 problems)
Average Student Marks
beginnerGiven a list of integer marks, write a function average_mark(marks) that returns the average as a float.
Count Passing Students
beginnerGiven a list of integer marks, write a function count_pass(marks) that returns how many marks are 40 or above.
Conditionals(1 problem)
Strings(4 problems)
Count Vowels in a String
beginnerWrite a function count_vowels(text) that returns how many vowels (a, e, i, o, u) appear in the string, ignoring case.
Reverse a String
beginnerWrite a function reverse_string(text) that returns the input string in reverse order.
Format a Sentence
beginnerWrite a function format_msg(name, score) that returns a string formatted as: 'User {name} scored {score} points.'
Extract Email Domain
intermediateWrite a function get_domain(email) that returns the domain part of an email address (everything after the '@' symbol).
Arrays Lists(3 problems)
Access First and Last
beginnerWrite a function get_ends(items) that returns a new list containing only the first and last elements of the input list.
Growing the List
beginnerWrite a function manage_list(items) that adds 'apple' to the end and 'milk' at index 1, then returns the modified list.
Shrinking the List
beginnerWrite a function shrink_list(items) that removes 'banana' from the list and pops the last element, then returns the list.
List Operations(5 problems)
Extract the Middle
beginnerGiven a list of 4 items, write a function get_middle(items) that returns the middle two elements using slicing.
Reverse without Methods
intermediateWrite a function manual_reverse(items) that returns a reversed copy of the list WITHOUT using the .reverse() method or slicing.
Find Min and Max
intermediateWrite a function get_extremes(items) that returns a tuple (min_val, max_val) containing the smallest and largest numbers in the list.
To-Do List Manager
intermediateWrite a function manage_todo(tasks, done_index) that removes the task at done_index and returns the remaining tasks as a formatted string: 'Remaining: task1, task2...'
Filter Even Numbers
beginnerWrite a function filter_even(numbers) that returns a new list containing only the even numbers from the input list.
Comprehensions(2 problems)
Flatten a Grid
intermediateWrite a function flatten(grid) that takes a 2D list (list of lists) and returns a single 'flattened' list of all elements.
Squares with Comprehension
intermediateWrite a function squares(n) that returns a list of squares from 0 to n-1 using a list comprehension.
Dictionaries(1 problem)
Exceptions(1 problem)
Files(2 problems)
Count Lines in a File
intermediateWrite a function count_lines(path) that opens a text file and returns how many lines it contains.
Write to a File
intermediateWrite a function write_message(filename, message) that writes the given message to a file with the given filename. (Note: In this web environment, valid file I/O is simulated.)
Classes(2 problems)
Create a Person Class
beginnerCreate a Person class with 'name' and 'age' attributes. Add a method 'introduce()' that returns the string: 'Hi, my name is [name]'.
Student Class & Logic
intermediateDefine a Student class with name and marks attributes and an is_pass method that returns True if marks are 40 or above.
Advanced Structures(1 problem)
Essential Algorithms(5 problems)
Binary Search Implementation
advancedWrite a function `binary_search(arr, target)` that returns the index of the target in a sorted list, or -1 if not found. Use the efficient O(log n) approach.
Divide and Conquer: Quick Sort
advancedImplement the Quick Sort algorithm. Given a list of numbers, return a new sorted list using the recursive divide-and-conquer approach.
Social Network BFS
advancedFind the shortest path distance (number of edges) between two nodes in an adjacency list graph using BFS. Return -1 if no path exists.
The Knapsack Problem
advancedWrite a function `knapsack(weights, values, capacity)` that returns the maximum value that can fit into the given capacity using 0/1 logic (use DP).
Huffman Tree Frequencies
advancedImplement the frequency map part of Huffman Compression. Given a string, return a dictionary mapping each character to its frequency count.