Python Java SQL Course C C++ HTML CSS JS

Python Practice Questions

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

Basics of Python Practice

Fundamental Python concepts — variables, data types, input/output, comments, and basic syntax.

1
Print Your Name
Easy
Write a Python program that takes your name as input and prints a greeting message: "Hello, [name]! Welcome to Python."
A single line containing a string — the name of the user.
Print: Hello, [name]! Welcome to Python.
Sample Input 1
Alice
Sample Output 1
Hello, Alice! Welcome to Python.
Sample Input 2
Rahul Kumar
Sample Output 2
Hello, Rahul Kumar! Welcome to Python.
Sample Input 3
X
Sample Output 3
Hello, X! Welcome to Python.
1 ≤ |name| ≤ 100. Name may contain letters and spaces.
Use the input() function to read the name and print() with an f-string or concatenation.
2
Sum of Two Numbers
Easy
Read two integers from the user and print their sum.
Two integers A and B, each on a separate line.
Print a single integer — the sum of A and B.
Sample Input 1
5
8
Sample Output 1
13
Sample Input 2
-7
12
Sample Output 2
5
Sample Input 3
1000000000
1000000000
Sample Output 3
2000000000
-10^9 ≤ A, B ≤ 10^9
Use int(input()) to read each number, add them, and print the result.
3
Area of a Rectangle
Easy
Given the length and breadth of a rectangle, calculate its area.
Two space-separated integers L and B representing length and breadth.
Print the area (L × B) as an integer.
Sample Input 1
10 5
Sample Output 1
50
Sample Input 2
12 8
Sample Output 2
96
Sample Input 3
1 1
Sample Output 3
1
1 ≤ L, B ≤ 10^6
Use split() to read two values from one line. Area = length × breadth.
4
Swap Two Variables
Easy
Take two integers X and Y as input. Swap their values and print them after swapping.
Two space-separated integers X and Y.
Print the values after swapping in the format: X = [newX], Y = [newY]
Sample Input 1
7 3
Sample Output 1
X = 3, Y = 7
Sample Input 2
-5 9
Sample Output 2
X = 9, Y = -5
Sample Input 3
100 200
Sample Output 3
X = 200, Y = 100
-10^9 ≤ X, Y ≤ 10^9
Use tuple unpacking: X, Y = Y, X
5
Convert Temperature
Easy
Convert Celsius to Fahrenheit. F = (C × 9/5) + 32
A single integer C.
Print Fahrenheit rounded to 2 decimal places.
Sample Input 1
0
Sample Output 1
32.00
Sample Input 2
-40
Sample Output 2
-40.00
Sample Input 3
100
Sample Output 3
212.00
-100 ≤ C ≤ 100
Apply formula, use format().
6
Simple Interest
Easy
SI = (P × R × T) / 100
Three ints: P R T
Print SI rounded to 2 decimal places.
Sample Input 1
1000 5 2
Sample Output 1
100.00
Sample Input 2
5000 10 3
Sample Output 2
1500.00
Sample Input 3
2500 4 5
Sample Output 3
500.00
1≤P≤10⁷, 1≤R≤100, 1≤T≤30
Apply formula.
7
String Repeater
Easy
Repeat string S, N times.
Line1: S. Line2: N
Print S repeated N times.
Sample Input 1
Hi
3
Sample Output 1
HiHiHi
Sample Input 2
Ab
2
Sample Output 2
AbAb
Sample Input 3
Hello
1
Sample Output 3
Hello
1≤|S|≤100, 1≤N≤100
Use S * N operator.
8
Type of Variable
Easy
Print data type name of a value.
A single value
"int","float","str", or "bool".
Sample Input 1
3.14
Sample Output 1
float
Sample Input 2
42
Sample Output 2
int
Sample Input 3
True
Sample Output 3
bool
Standard Python types
Use type(val).__name__
9
Find ASCII Value
Easy
Print ASCII (ordinal) value of a character.
A single character
Print integer ASCII value.
Sample Input 1
A
Sample Output 1
65
Sample Input 2
z
Sample Output 2
122
Sample Input 3
1
Sample Output 3
49
Printable ASCII
Use ord().
10
Convert Minutes to Seconds
Easy
Convert M minutes to seconds.
Single int M
Print seconds.
Sample Input 1
5
Sample Output 1
300
Sample Input 2
1
Sample Output 2
60
Sample Input 3
1000
Sample Output 3
60000
1≤M≤10⁶
Multiply by 60.
1
Simple Calculator
Medium
Read two integers and an operator (+, -, *, /) from the user. Perform the operation and print the result. For division, print the result rounded to 2 decimal places.
First line: integer A. Second line: integer B. Third line: a single character representing the operator.
Print the result of the operation. For division, display with exactly 2 decimal places.
Sample Input 1
10
3
/
Sample Output 1
3.33
Sample Input 2
25
4
+
Sample Output 2
29
Sample Input 3
17
5
-
Sample Output 3
12
-10^6 ≤ A, B ≤ 10^6. B ≠ 0 for division.
Use if-elif to check the operator. Use format() or f-string for decimal places.
2
Even or Odd Checker
Medium
Read an integer N and determine whether it is even or odd.
A single integer N.
Print "Even" if N is divisible by 2, otherwise print "Odd".
Sample Input 1
42
Sample Output 1
Even
Sample Input 2
-3
Sample Output 2
Odd
Sample Input 3
100
Sample Output 3
Even
-10^9 ≤ N ≤ 10^9
Use the modulo operator %: if N % 2 == 0, it is even.
3
Reverse a String
Medium
Reverse string S and print.
Single line S
Print reversed string.
Sample Input 1
Python
Sample Output 1
nohtyP
Sample Input 2
racecar
Sample Output 2
racecar
Sample Input 3
A
Sample Output 3
A
1≤|S|≤1000
Use S[::-1]
4
Count Vowels
Medium
Count vowels (a,e,i,o,u) case-insensitive.
Single line S
Print vowel count.
Sample Input 1
Hello World
Sample Output 1
3
Sample Input 2
AEIOU
Sample Output 2
5
Sample Input 3
xyz
Sample Output 3
0
1≤|S|≤1000
Loop and check.
5
Palindrome String
Medium
Check if S is palindrome (same forwards & backwards).
Single line S
"Palindrome" or "Not a Palindrome"
Sample Input 1
racecar
Sample Output 1
Palindrome
Sample Input 2
hello
Sample Output 2
Not a Palindrome
Sample Input 3
Aba
Sample Output 3
Not a Palindrome
1≤|S|≤1000
Compare S with S[::-1]
6
GCD of Two Numbers
Medium
Find GCD using Euclidean algorithm.
Two ints A B
Print GCD.
Sample Input 1
48 18
Sample Output 1
6
Sample Input 2
17 5
Sample Output 2
1
Sample Input 3
100 75
Sample Output 3
25
1≤A,B≤10⁶
while b: a,b = b, a%b
7
LCM of Two Numbers
Medium
LCM = (A×B)/GCD(A,B)
Two ints A B
Print LCM.
Sample Input 1
12 18
Sample Output 1
36
Sample Input 2
5 7
Sample Output 2
35
Sample Input 3
16 24
Sample Output 3
48
1≤A,B≤10⁶
Find GCD first, apply formula.
8
Largest Digit
Medium
Find largest digit in N.
Single int N
Print largest digit.
Sample Input 1
27941
Sample Output 1
9
Sample Input 2
1000000000
Sample Output 2
1
Sample Input 3
0
Sample Output 3
0
1≤N≤10⁹
max(str(N))
9
Sum of Digits
Medium
Sum all digits of N.
Single int N
Print sum.
Sample Input 1
1234
Sample Output 1
10
Sample Input 2
0
Sample Output 2
0
Sample Input 3
999999999
Sample Output 3
81
1≤N≤10⁹
Loop %10 //10 or sum(str())
10
Remove Spaces
Medium
Remove all whitespace from string.
Single line S
String without spaces.
Sample Input 1
Hello   World
Sample Output 1
HelloWorld
Sample Input 2
no spaces
Sample Output 2
nospaces
Sample Input 3

                      
Sample Output 3

                      
1≤|S|≤500
S.replace(" ","")
1
Compound Interest Calculator
Hard
Given principal amount P, annual interest rate R (in percentage), time period T (in years), and compounding frequency N (number of times interest is compounded per year), calculate the compound interest and the final amount.
A single line with four space-separated values: P R T N
Print two lines: "Amount: [final amount]" and "Interest: [compound interest]". Round both to 2 decimal places.
Sample Input 1
1000 5 3 4
Sample Output 1
Amount: 1160.75
Interest: 160.75
Sample Input 2
2000 10 2 12
Sample Output 2
Amount: 2440.78
Interest: 440.78
Sample Input 3
500 0 1 1
Sample Output 3
Amount: 500.00
Interest: 0.00
1 ≤ P ≤ 10^7, 0 ≤ R ≤ 100, 1 ≤ T ≤ 30, 1 ≤ N ≤ 365
Formula: A = P(1 + R/(100*N))^(NT). Use ** for exponentiation.
2
Perfect Number Check
Hard
Sum of proper divisors equals N? Check N.
Single int N
"Perfect Number" or "Not a Perfect Number"
Sample Input 1
28
Sample Output 1
Perfect Number
Sample Input 2
6
Sample Output 2
Perfect Number
Sample Input 3
8128
Sample Output 3
Perfect Number
1≤N≤10⁶
Loop 1..N//2, sum divisors.
3
Base Converter
Hard
Convert decimal to binary or hexa.
Line1: N. Line2: "binary" or "hexa"
Converted string (hex uppercase).
Sample Input 1
255
hexa
Sample Output 1
FF
Sample Input 2
10
binary
Sample Output 2
1010
Sample Input 3
255
binary
Sample Output 3
11111111
1≤N≤10⁶
bin(N)[2:] or hex(N)[2:].upper()
4
Second Largest Number
Hard
Find second largest distinct number.
Line1: N. Line2: N ints
Print second largest or "Not Found".
Sample Input 1
6
10 5 20 20 8 15
Sample Output 1
15
Sample Input 2
3
5 5 5
Sample Output 2
Not Found
Sample Input 3
2
10 20
Sample Output 3
10
1≤N≤10⁵
set(), sorted, index 1.
5
Decimal to Roman
Hard
Convert N (1-3999) to Roman numeral.
Single int N
Roman numeral string.
Sample Input 1
1994
Sample Output 1
MCMXCIV
Sample Input 2
58
Sample Output 2
LVIII
Sample Input 3
3999
Sample Output 3
MMMCMXCIX
1≤N≤3999
Mapping table, subtract iteratively.
6
Validate Email
Hard
Check standard email format: local@domain.tld
Single line (email)
"Valid Email" or "Invalid Email"
Sample Input 1
user.name@example.com
Sample Output 1
Valid Email
Sample Input 2
plainaddress
Sample Output 2
Invalid Email
Sample Input 3
user@domain
Sample Output 3
Invalid Email
Length≤254
Check @, local, domain parts.
7
Prime Factors
Hard
Find all prime factors with multiplicity.
Single int N
Space-separated factors.
Sample Input 1
60
Sample Output 1
2 2 3 5
Sample Input 2
100
Sample Output 2
2 2 5 5
Sample Input 3
97
Sample Output 3
97
2≤N≤10⁶
Divide by 2,3,5,... repeatedly.
8
Anagram Check
Hard
Two strings are anagrams? Case-insensitive, ignore spaces.
Line1: A. Line2: B
"Anagrams" or "Not Anagrams"
Sample Input 1
listen
silent
Sample Output 1
Anagrams
Sample Input 2
Listen
Silent
Sample Output 2
Anagrams
Sample Input 3
hello
world
Sample Output 3
Not Anagrams
1≤|A|,|B|≤1000
sort chars and compare.
9
Caesar Cipher
Hard
Shift letters by K. Wrap Z→A. Preserve case.
Line1: S. Line2: K
Encrypted string.
Sample Input 1
Hello, World!
3
Sample Output 1
Khoor, Zruog!
Sample Input 2
abc
1
Sample Output 2
bcd
Sample Input 3
XYZ
25
Sample Output 3
WXY
1≤|S|≤1000, 1≤K≤25
ord()/chr() ASCII arithmetic.
10
Word Frequency Counter
Hard
Count frequency of each word, sort by freq desc then alpha.
Single line paragraph
"word: count" per line sorted.
Sample Input 1
the cat and the dog and the bird
Sample Output 1
the: 3
and: 2
bird: 1
cat: 1
dog: 1
Sample Input 2
apple banana apple
Sample Output 2
apple: 2
banana: 1
Sample Input 3
one one two one
Sample Output 3
one: 3
two: 1
≤5000 chars
Dict, sort by (-count, word).
Competitive MCQs — Basics of Python
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
What is the correct file extension for Python files?
Correct!
Wrong — correct answer is .
Python source files use the .py extension (e.g. hello.py).
Q2
Which of these is NOT a valid way to write a comment in Python?
Correct!
Wrong — correct answer is .
// is a comment style in C/Java; Python comments start with #. Triple quotes create docstrings/strings.
Q3
What is the output of this code?
python
1
print(type(10))
Correct!
Wrong — correct answer is .
Integer literals have type int; type() returns .
Q4
What is the output of print(type(10))?
Correct!
Wrong — correct answer is .
Integer literals have type int; type() returns .
Q5
Which function is used to read a line of input from the user?
Correct!
Wrong — correct answer is .
Python uses input() to read from stdin. scanf/gets are C; readline is used on file objects.
Q6
What will this code output?
python
1
print(2 ** 3)
Correct!
Wrong — correct answer is .
** is the exponent operator, so 2 ** 3 = 8.
Q7
What will print(2 ** 3) output?
Correct!
Wrong — correct answer is .
** is the exponent operator, so 2 ** 3 = 8.
Q8
Which of the following is a reserved keyword in Python?
Correct!
Wrong — correct answer is .
lambda is a Python keyword; the others are not Python keywords.
Q9
What is the output of this code?
python
1
print("Hello" + " " + "World")
Correct!
Wrong — correct answer is .
The + operator concatenates strings, giving "Hello World".
Q10
What is the output of print(\"Hello\" + \" \" + \"World\")?
Correct!
Wrong — correct answer is .
The + operator concatenates strings, giving "Hello World".
Q11
How do you create a variable x with the value 5 in Python?
Correct!
Wrong — correct answer is .
Python is dynamically typed: x = 5 declares and assigns.
Q12
What is the result of this code?
python
1
print(7 // 2)
Correct!
Wrong — correct answer is .
// is floor division; 7 // 2 = 3 (discards the remainder).
Q13
What is the result of print(7 // 2)?
Correct!
Wrong — correct answer is .
// is floor division; 7 // 2 = 3 (discards the remainder).
Q14
Which statement correctly assigns the same value 0 to variables a, b and c?
Correct!
Wrong — correct answer is .
Chained assignment a = b = c = 0 works, as do three separate assignments.
Q15
What is the output of print(10 / 3)?
Correct!
Wrong — correct answer is .
The / operator always returns a float in Python 3, even when both operands are ints.
Q16
Which of these is a valid variable name in Python?
Correct!
Wrong — correct answer is .
Identifiers can start with a letter or underscore; a leading digit, a hyphen, and a keyword are all invalid.
Q17
Is Python case-sensitive? For example, are Age and age the same variable?
Correct!
Wrong — correct answer is .
Python distinguishes names by case, so Age and age are two different identifiers.
Q18
What is the output of print(int("25"))?
Correct!
Wrong — correct answer is .
int() converts a numeric string such as "25" into the integer 25.
Q19
What is the output of print(float(2))?
Correct!
Wrong — correct answer is .
float(2) converts the integer 2 into the floating-point value 2.0.
Q20
Which function converts a value into a string?
Correct!
Wrong — correct answer is .
str(value) returns the string representation of the given value.
Q21
Which of the following is NOT a built-in data type in Python?
Correct!
Wrong — correct answer is .
list, tuple, and dict are built-in; array must be imported from the array module.
Q22
What is the output of print(type(True))?
Correct!
Wrong — correct answer is .
True and False are instances of the built-in bool type.
Q23
What is the output of print("a", "b", sep="-")?
Correct!
Wrong — correct answer is .
The sep parameter replaces the default space placed between printed values with "-".
Q24
What does print("Hi", end="!") produce?
Correct!
Wrong — correct answer is .
end="!" replaces the default trailing newline of print() with "!".
Q25
Which of these is a Python keyword?
Correct!
Wrong — correct answer is .
None is Python's null keyword; Null and nil belong to other languages.
Q26
What is the output of print(5, 10)?
Correct!
Wrong — correct answer is .
Multiple arguments to print() are separated by a space by default.
Q27
What is the output of print(int(3.99))?
Correct!
Wrong — correct answer is .
int() truncates toward zero, dropping the fractional part instead of rounding.
Q28
What is the output of print(bool(0))?
Correct!
Wrong — correct answer is .
Zero is falsy in Python, so bool(0) evaluates to False.
Q29
What is the output of print(round(3.567, 2))?
Correct!
Wrong — correct answer is .
round(3.567, 2) rounds to two decimal places, giving 3.57.
Q30
Which expression correctly reads an integer from the user?
Correct!
Wrong — correct answer is .
input() always returns a string, so wrap it with int() to obtain an integer.
Q31
Which of the following is a floating-point literal?
Correct!
Wrong — correct answer is .
The decimal point makes 10.0 a float; the other literals are integer forms.
Q32
What is the output of print(9 ** 0.5)?
Correct!
Wrong — correct answer is .
A fractional exponent produces a float: 9 ** 0.5 gives 3.0, the square root of 9.
Q33
What is the output of print(2 + 2 * 2)?
Correct!
Wrong — correct answer is .
Multiplication binds more tightly than addition: 2 + (2 * 2) = 6.
Q34
Which symbol continues a long Python statement onto the next line?
Correct!
Wrong — correct answer is .
A trailing backslash is the explicit line-continuation character in Python.