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."
Input Format
A single line containing a string — the name of the user.
Output Format
Print: Hello, [name]! Welcome to Python.
Sample Test Cases
|
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. |
Constraints
1 ≤ |name| ≤ 100. Name may contain letters and spaces.
Explanation
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.
Input Format
Two integers A and B, each on a separate line.
Output Format
Print a single integer — the sum of A and B.
Sample Test Cases
|
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 |
Constraints
-10^9 ≤ A, B ≤ 10^9
Explanation
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.
Input Format
Two space-separated integers L and B representing length and breadth.
Output Format
Print the area (L × B) as an integer.
Sample Test Cases
|
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 |
Constraints
1 ≤ L, B ≤ 10^6
Explanation
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.
Input Format
Two space-separated integers X and Y.
Output Format
Print the values after swapping in the format: X = [newX], Y = [newY]
Sample Test Cases
|
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 |
Constraints
-10^9 ≤ X, Y ≤ 10^9
Explanation
Use tuple unpacking: X, Y = Y, X
5
Convert Temperature
Easy
Convert Celsius to Fahrenheit. F = (C × 9/5) + 32
Input Format
A single integer C.
Output Format
Print Fahrenheit rounded to 2 decimal places.
Sample Test Cases
|
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 |
Constraints
-100 ≤ C ≤ 100
Explanation
Apply formula, use format().
6
Simple Interest
Easy
SI = (P × R × T) / 100
Input Format
Three ints: P R T
Output Format
Print SI rounded to 2 decimal places.
Sample Test Cases
|
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 |
Constraints
1≤P≤10⁷, 1≤R≤100, 1≤T≤30
Explanation
Apply formula.
7
String Repeater
Easy
Repeat string S, N times.
Input Format
Line1: S. Line2: N
Output Format
Print S repeated N times.
Sample Test Cases
|
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 |
Constraints
1≤|S|≤100, 1≤N≤100
Explanation
Use S * N operator.
8
Type of Variable
Easy
Print data type name of a value.
Input Format
A single value
Output Format
"int","float","str", or "bool".
Sample Test Cases
|
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 |
Constraints
Standard Python types
Explanation
Use type(val).__name__
9
Find ASCII Value
Easy
Print ASCII (ordinal) value of a character.
Input Format
A single character
Output Format
Print integer ASCII value.
Sample Test Cases
|
Sample Input 1
A |
Sample Output 1
65 |
|
Sample Input 2
z |
Sample Output 2
122 |
|
Sample Input 3
1 |
Sample Output 3
49 |
Constraints
Printable ASCII
Explanation
Use ord().
10
Convert Minutes to Seconds
Easy
Convert M minutes to seconds.
Input Format
Single int M
Output Format
Print seconds.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
300 |
|
Sample Input 2
1 |
Sample Output 2
60 |
|
Sample Input 3
1000 |
Sample Output 3
60000 |
Constraints
1≤M≤10⁶
Explanation
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.
Input Format
First line: integer A. Second line: integer B. Third line: a single character representing the operator.
Output Format
Print the result of the operation. For division, display with exactly 2 decimal places.
Sample Test Cases
|
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 |
Constraints
-10^6 ≤ A, B ≤ 10^6. B ≠ 0 for division.
Explanation
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.
Input Format
A single integer N.
Output Format
Print "Even" if N is divisible by 2, otherwise print "Odd".
Sample Test Cases
|
Sample Input 1
42 |
Sample Output 1
Even |
|
Sample Input 2
-3 |
Sample Output 2
Odd |
|
Sample Input 3
100 |
Sample Output 3
Even |
Constraints
-10^9 ≤ N ≤ 10^9
Explanation
Use the modulo operator %: if N % 2 == 0, it is even.
3
Reverse a String
Medium
Reverse string S and print.
Input Format
Single line S
Output Format
Print reversed string.
Sample Test Cases
|
Sample Input 1
Python |
Sample Output 1
nohtyP |
|
Sample Input 2
racecar |
Sample Output 2
racecar |
|
Sample Input 3
A |
Sample Output 3
A |
Constraints
1≤|S|≤1000
Explanation
Use S[::-1]
4
Count Vowels
Medium
Count vowels (a,e,i,o,u) case-insensitive.
Input Format
Single line S
Output Format
Print vowel count.
Sample Test Cases
|
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 |
Constraints
1≤|S|≤1000
Explanation
Loop and check.
5
Palindrome String
Medium
Check if S is palindrome (same forwards & backwards).
Input Format
Single line S
Output Format
"Palindrome" or "Not a Palindrome"
Sample Test Cases
|
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 |
Constraints
1≤|S|≤1000
Explanation
Compare S with S[::-1]
6
GCD of Two Numbers
Medium
Find GCD using Euclidean algorithm.
Input Format
Two ints A B
Output Format
Print GCD.
Sample Test Cases
|
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 |
Constraints
1≤A,B≤10⁶
Explanation
while b: a,b = b, a%b
7
LCM of Two Numbers
Medium
LCM = (A×B)/GCD(A,B)
Input Format
Two ints A B
Output Format
Print LCM.
Sample Test Cases
|
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 |
Constraints
1≤A,B≤10⁶
Explanation
Find GCD first, apply formula.
8
Largest Digit
Medium
Find largest digit in N.
Input Format
Single int N
Output Format
Print largest digit.
Sample Test Cases
|
Sample Input 1
27941 |
Sample Output 1
9 |
|
Sample Input 2
1000000000 |
Sample Output 2
1 |
|
Sample Input 3
0 |
Sample Output 3
0 |
Constraints
1≤N≤10⁹
Explanation
max(str(N))
9
Sum of Digits
Medium
Sum all digits of N.
Input Format
Single int N
Output Format
Print sum.
Sample Test Cases
|
Sample Input 1
1234 |
Sample Output 1
10 |
|
Sample Input 2
0 |
Sample Output 2
0 |
|
Sample Input 3
999999999 |
Sample Output 3
81 |
Constraints
1≤N≤10⁹
Explanation
Loop %10 //10 or sum(str())
10
Remove Spaces
Medium
Remove all whitespace from string.
Input Format
Single line S
Output Format
String without spaces.
Sample Test Cases
|
Sample Input 1
Hello World |
Sample Output 1
HelloWorld |
|
Sample Input 2
no spaces |
Sample Output 2
nospaces |
|
Sample Input 3
|
Sample Output 3
|
Constraints
1≤|S|≤500
Explanation
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.
Input Format
A single line with four space-separated values: P R T N
Output Format
Print two lines: "Amount: [final amount]" and "Interest: [compound interest]". Round both to 2 decimal places.
Sample Test Cases
|
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 |
Constraints
1 ≤ P ≤ 10^7, 0 ≤ R ≤ 100, 1 ≤ T ≤ 30, 1 ≤ N ≤ 365
Explanation
Formula: A = P(1 + R/(100*N))^(NT). Use ** for exponentiation.
2
Perfect Number Check
Hard
Sum of proper divisors equals N? Check N.
Input Format
Single int N
Output Format
"Perfect Number" or "Not a Perfect Number"
Sample Test Cases
|
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 |
Constraints
1≤N≤10⁶
Explanation
Loop 1..N//2, sum divisors.
3
Base Converter
Hard
Convert decimal to binary or hexa.
Input Format
Line1: N. Line2: "binary" or "hexa"
Output Format
Converted string (hex uppercase).
Sample Test Cases
|
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 |
Constraints
1≤N≤10⁶
Explanation
bin(N)[2:] or hex(N)[2:].upper()
4
Second Largest Number
Hard
Find second largest distinct number.
Input Format
Line1: N. Line2: N ints
Output Format
Print second largest or "Not Found".
Sample Test Cases
|
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 |
Constraints
1≤N≤10⁵
Explanation
set(), sorted, index 1.
5
Decimal to Roman
Hard
Convert N (1-3999) to Roman numeral.
Input Format
Single int N
Output Format
Roman numeral string.
Sample Test Cases
|
Sample Input 1
1994 |
Sample Output 1
MCMXCIV |
|
Sample Input 2
58 |
Sample Output 2
LVIII |
|
Sample Input 3
3999 |
Sample Output 3
MMMCMXCIX |
Constraints
1≤N≤3999
Explanation
Mapping table, subtract iteratively.
6
Validate Email
Hard
Check standard email format: local@domain.tld
Input Format
Single line (email)
Output Format
"Valid Email" or "Invalid Email"
Sample Test Cases
|
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 |
Constraints
Length≤254
Explanation
Check @, local, domain parts.
7
Prime Factors
Hard
Find all prime factors with multiplicity.
Input Format
Single int N
Output Format
Space-separated factors.
Sample Test Cases
|
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 |
Constraints
2≤N≤10⁶
Explanation
Divide by 2,3,5,... repeatedly.
8
Anagram Check
Hard
Two strings are anagrams? Case-insensitive, ignore spaces.
Input Format
Line1: A. Line2: B
Output Format
"Anagrams" or "Not Anagrams"
Sample Test Cases
|
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 |
Constraints
1≤|A|,|B|≤1000
Explanation
sort chars and compare.
9
Caesar Cipher
Hard
Shift letters by K. Wrap Z→A. Preserve case.
Input Format
Line1: S. Line2: K
Output Format
Encrypted string.
Sample Test Cases
|
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 |
Constraints
1≤|S|≤1000, 1≤K≤25
Explanation
ord()/chr() ASCII arithmetic.
10
Word Frequency Counter
Hard
Count frequency of each word, sort by freq desc then alpha.
Input Format
Single line paragraph
Output Format
"word: count" per line sorted.
Sample Test Cases
|
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 |
Constraints
≤5000 chars
Explanation
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.