Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Functions Practice

Write reusable functions with parameters, return values, scope and built-ins.

1
Add Two Numbers Using Function
Easy
Write a function add(a, b) that returns the sum of two numbers. Read a and b and print the result.
A single line containing two space-separated integers a and b.
Print a single integer — their sum.
Sample Input 1
5 7
Sample Output 1
12
Sample Input 2
10 20
Sample Output 2
30
Sample Input 3
-5 8
Sample Output 3
3
2
Greet Using Function
Easy
Write a function greet(name) that prints 'Hello, [name]!' and call it with the input name.
A single line containing a name.
Print the greeting message.
Sample Input 1
Alice
Sample Output 1
Hello, Alice!
Sample Input 2
Bob
Sample Output 2
Hello, Bob!
Sample Input 3
Sam
Sample Output 3
Hello, Sam!
3
Square of a Number
Easy
Write a function square(n) that returns the square of a number.
A single line containing integer n.
Print the square of n.
Sample Input 1
9
Sample Output 1
81
Sample Input 2
12
Sample Output 2
144
Sample Input 3
0
Sample Output 3
0
4
Area of Rectangle
Easy
Write a function area(length, breadth) that returns the area of a rectangle.
A single line containing two space-separated integers length and breadth.
Print a single integer — the area.
Sample Input 1
5 3
Sample Output 1
15
Sample Input 2
7 2
Sample Output 2
14
Sample Input 3
10 10
Sample Output 3
100
5
Check Even or Odd Using Function
Easy
Write a function is_even(n) that returns True if n is even, else False.
A single line containing integer n.
Print True or False.
Sample Input 1
10
Sample Output 1
True
Sample Input 2
7
Sample Output 2
False
Sample Input 3
0
Sample Output 3
True
6
Find Maximum of Two Numbers
Easy
Write a function max_of_two(a, b) that returns the larger of the two numbers.
A single line containing two space-separated integers.
Print the larger number.
Sample Input 1
4 9
Sample Output 1
9
Sample Input 2
10 3
Sample Output 2
10
Sample Input 3
-1 -5
Sample Output 3
-1
7
Cube of a Number
Easy
Write a function cube(n) that returns the cube of a number.
A single line containing integer n.
Print the cube of n.
Sample Input 1
4
Sample Output 1
64
Sample Input 2
5
Sample Output 2
125
Sample Input 3
0
Sample Output 3
0
8
Multiply Three Numbers
Easy
Write a function multiply(a, b, c) that returns the product of three numbers.
A single line containing three space-separated integers.
Print a single integer — the product.
Sample Input 1
2 3 4
Sample Output 1
24
Sample Input 2
1 2 3
Sample Output 2
6
Sample Input 3
2 2 2
Sample Output 3
8
9
Factorial Using Function
Easy
Write a function factorial(n) that returns the factorial of n using a loop.
A single line containing integer n.
Print the factorial of n.
Sample Input 1
5
Sample Output 1
120
Sample Input 2
4
Sample Output 2
24
Sample Input 3
0
Sample Output 3
1
10
Print Numbers 1 to N Using Function
Easy
Write a function print_numbers(n) that prints numbers from 1 to n, each on a new line.
A single line containing integer n.
Print the numbers from 1 to n, one per line.
Sample Input 1
4
Sample Output 1
1
2
3
4
Sample Input 2
3
Sample Output 2
1
2
3
Sample Input 3
1
Sample Output 3
1
1
Sum of Digits of a Number
Medium
Write a function sum_digits(n) that returns the sum of digits of a number.
A single line containing integer n.
Print the sum of digits.
Sample Input 1
12345
Sample Output 1
15
Sample Input 2
999
Sample Output 2
27
Sample Input 3
100
Sample Output 3
1
2
Check Prime Number Using Function
Medium
Write a function is_prime(n) that returns True if n is prime, else False.
A single line containing integer n.
Print True or False.
Sample Input 1
17
Sample Output 1
True
Sample Input 2
19
Sample Output 2
True
Sample Input 3
21
Sample Output 3
False
3
Reverse a Number Using Function
Medium
Write a function reverse_num(n) that returns the reversed number.
A single line containing integer n.
Print the reversed number.
Sample Input 1
1234
Sample Output 1
4321
Sample Input 2
120
Sample Output 2
21
Sample Input 3
1000
Sample Output 3
1
4
Fibonacci Using Function
Medium
Write a function fibonacci(n) that returns the nth Fibonacci number (0-indexed).
A single line containing integer n.
Print the nth Fibonacci number.
Sample Input 1
7
Sample Output 1
13
Sample Input 2
9
Sample Output 2
34
Sample Input 3
0
Sample Output 3
0
5
GCD of Two Numbers Using Function
Medium
Write a function gcd(a, b) that returns the greatest common divisor of two numbers.
A single line containing two space-separated integers.
Print the GCD.
Sample Input 1
12 18
Sample Output 1
6
Sample Input 2
7 5
Sample Output 2
1
Sample Input 3
17 31
Sample Output 3
1
6
LCM of Two Numbers Using Function
Medium
Write a function lcm(a, b) that returns the least common multiple of two numbers.
A single line containing two space-separated integers.
Print the LCM.
Sample Input 1
4 6
Sample Output 1
12
Sample Input 2
3 5
Sample Output 2
15
Sample Input 3
7 11
Sample Output 3
77
7
Check Palindrome Number Using Function
Medium
Write a function is_palindrome(n) that returns True if the number reads the same forwards and backwards.
A single line containing integer n.
Print True or False.
Sample Input 1
121
Sample Output 1
True
Sample Input 2
12321
Sample Output 2
True
Sample Input 3
12
Sample Output 3
False
8
Count Vowels in a String Using Function
Medium
Write a function count_vowels(s) that returns the number of vowels in the string.
A single line containing a string.
Print the number of vowels.
Sample Input 1
education
Sample Output 1
5
Sample Input 2
sky
Sample Output 2
0
Sample Input 3
AEIOU
Sample Output 3
5
9
Nested Function — Inner Helper
Medium
Write a program with an outer function that contains an inner helper function to double a number, and returns the result of doubling the input twice.
A single line containing integer n.
Print the result of applying the inner function twice.
Sample Input 1
5
Sample Output 1
20
Sample Input 2
3
Sample Output 2
12
Sample Input 3
0
Sample Output 3
0
10
Default Argument Function
Medium
Write a function power(base, exp=2) that returns base raised to exp. If exp is not given, it defaults to 2.
A single line containing integer base, optionally followed by integer exp. If only one number is given, use default exp=2.
Print the power result.
Sample Input 1
3
Sample Output 1
9
Sample Input 2
4 3
Sample Output 2
64
Sample Input 3
5
Sample Output 3
25
1
Recursive Function — Factorial
Hard
Write a recursive function factorial(n) that returns the factorial of n using recursion (no loops).
A single line containing integer n.
Print the factorial of n.
Sample Input 1
6
Sample Output 1
720
Sample Input 2
7
Sample Output 2
5040
Sample Input 3
0
Sample Output 3
1
2
Function with *args — Sum All
Hard
Write a function sum_all(*args) that returns the sum of all arguments passed.
A single line containing N space-separated integers.
Print their sum.
Sample Input 1
1 2 3 4 5
Sample Output 1
15
Sample Input 2
10 20 30
Sample Output 2
60
Sample Input 3
-1 -2 -3
Sample Output 3
-6
3
Function with **kwargs — Build Dict
Hard
Write a function build_dict(**kwargs) that returns the dictionary formed by the keyword arguments.
Multiple lines: each line contains a key and value separated by a space. End input with an empty line.
Print the resulting dictionary.
Sample Input 1
a 1
b 2
c 3
Sample Output 1
{'a': '1', 'b': '2', 'c': '3'}
Sample Input 2
x 10
y 20
Sample Output 2
{'x': '10', 'y': '20'}
Sample Input 3
a 5
Sample Output 3
{'a': '5'}
4
Lambda in Function
Hard
Write a function apply_operation(a, b, op) where op is a lambda that performs the operation, and call it with a lambda to multiply.
A single line containing two space-separated integers.
Print the product using the lambda.
Sample Input 1
6 7
Sample Output 1
42
Sample Input 2
8 9
Sample Output 2
72
Sample Input 3
0 5
Sample Output 3
0
5
Higher-Order Function — Map Double
Hard
Write a function map_double(lst) that uses map() with a lambda to double every element and returns the list.
First line: integer N. Second line: N space-separated integers.
Print the doubled list.
Sample Input 1
4
1 2 3 4
Sample Output 1
[2, 4, 6, 8]
Sample Input 2
3
5 6 7
Sample Output 2
[10, 12, 14]
Sample Input 3
1
1
Sample Output 3
[2]
6
Function Returning Multiple Values
Hard
Write a function min_max(lst) that returns both the minimum and maximum of a list.
First line: integer N. Second line: N space-separated integers.
Print the minimum and maximum separated by a space.
Sample Input 1
5
3 1 4 1 5
Sample Output 1
1 5
Sample Input 2
4
9 8 7 6
Sample Output 2
6 9
Sample Input 3
2
5 5
Sample Output 3
5 5
7
Decorator Function
Hard
Write a decorator uppercase that converts the return value of a function to uppercase, and apply it to a function greet(name).
A single line containing a name.
Print the uppercase greeting.
Sample Input 1
alice
Sample Output 1
HELLO, ALICE!
Sample Input 2
bob
Sample Output 2
HELLO, BOB!
Sample Input 3

                      
Sample Output 3
HELLO, !
8
Closure — Counter
Hard
Write a function make_counter() that returns a nested function which increments and returns a counter each time it is called. Call it 3 times and print the values.
No input.
Print the counter values after each of the 3 calls, one per line.
Sample Input 1

                      
Sample Output 1
1
2
3
Sample Input 2

                      
Sample Output 2
1
2
3
Sample Input 3

                      
Sample Output 3
1
2
3
9
Generator Function — Even Numbers
Hard
Write a generator function even_numbers(n) that yields even numbers up to n. Read n and print all yielded values.
A single line containing integer n.
Print the even numbers from 2 up to n, one per line.
Sample Input 1
10
Sample Output 1
2
4
6
8
10
Sample Input 2
8
Sample Output 2
2
4
6
8
Sample Input 3
1
Sample Output 3

                      
10
Function Composition
Hard
Write a program that composes two functions: f(x) = x + 1 and g(x) = x * 2. Read x and print f(g(x)) and g(f(x)) on separate lines.
A single line containing integer x.
Print f(g(x)) on line 1 and g(f(x)) on line 2.
Sample Input 1
5
Sample Output 1
11
12
Sample Input 2
0
Sample Output 2
2
1
Sample Input 3
10
Sample Output 3
21
22