Practice Problems Python
Python Practice Set: Fundamentals & Operators
This practice set covers Introduction, Variables, Comments, Data Types, Input/Output, and Operators. Use these questions to test your understanding of Python basics.
1. Introduction & Basics
Q1: What is the main difference between a Compiler and an Interpreter? How does Python use these?
Answer: A Compiler translates the entire program into machine code at once, while an Interpreter translates and executes code line-by-line. Python is often called an interpreted language, but it technically compiles code into bytecode first, which is then interpreted by the Python Virtual Machine (PVM).
Q2: Which symbol is used to add comments in Python?
- A)
// - B)
/* */ - C)
#? - D)
<!-- -->
2. Variables & Data Types
Q3: Identify the data types of the following variables:
x = 10.5
y = "The Royal Coding"
z = True
w = [1, 2, 3]
| Variable | Value | Data Type |
| :--- | :--- | :--- |
| x | 10.5 | float |
| y | "The Royal Coding" | str (String) |
| z | True | bool (Boolean) |
| w | [1, 2, 3] | list |
Q4: Can a variable name in Python start with a number?
Answer: No. Variable names must start with a letter (a-z, A-Z) or an underscore (
_). They cannot start with a digit.
3. Input & Output (I/O)
Q5: Write a program to take two numbers as input from the user and display their sum.
# Taking input
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
# Conversion is needed because input() always returns a string
sum_result = float(num1) + float(num2)
# Displaying output
print("The sum of {0} and {1} is {2}".format(num1, num2, sum_result))
4. Operators Practice
Q6: What is the output of the following Modulus operations?
| Expression | Expected Result | Why? |
| :--- | :--- | :--- |
| 10 % 3 | 1 | 10 รท 3 leaves a remainder of 1 |
| 15 % 5 | 0 | 15 is perfectly divisible by 5 |
| 7 % 10 | 7 | 7 cannot be divided by 10, so 7 remains |
Q7: Predict the output of this logical expression:
x = 5
print(x > 3 and x < 10)
print(x > 10 or x < 2)
print(not(x == 5))
Results:
True(Both conditions are real)False(Neither condition is real)False(Reverse of True is False)
5. Coding Challenges (Small Tasks)
Task 1: Area of a Circle
Write a Python script that asks for the radius and calculates the area.
import math
radius = float(input("Enter radius: "))
area = math.pi * (radius ** 2)
print(f"The area is: {area:.2f}")
Task 2: Swap Two Variables
Write a program to swap values of two variables a and b without using a third variable.
a = 5
b = 10
# Pythonic way to swap
a, b = b, a
print(f"a is now {a}, b is now {b}")