Subjective Quiz: Basic Python & Loops
Instructions: Answer the following questions using complete sentences. Write code where
required.
❉ Part A: Python Basics
1. What is a variable in Python? Give an example.
In Python, a variable is a name that stores a value. It acts like a container to hold data,
such as numbers, text, or other types of information, so that you can use and change
them later in your program.
name = "Alya"
age = 14
2. Explain the difference between input() and print().
input()
input() is used to get information from the user during the program's execution.
Example : name = input("What is your name? ")
print()
print() is used to display information or messages on the screen.
Example :print("Hello, welcome to the program!")
3. What is the purpose of indentation in Python?
In Python, indentation is used to define the structure of the code — especially for blocks
like if statements, loops, and functions.
Why is indentation important?
It shows which lines of code belong together.
It helps the Python interpreter understand what code should run inside a loop, function,
or condition.
Incorrect indentation will cause an error or make your code do the wrong thing.
4. Write a Python line that takes the user’s name and stores it in a variable called
username.
username = str(input("What is your name? "))
5. Look at the code below. What will be printed if the user inputs the number 10?
Python
number = int(input("Enter a number: "))
if number > 5:
print("Big number")
else:
print("Small number")
Big number
❉ Part B: Loops
6. What is the difference between a for loop and a while loop in Python? Give an
example of each.
For Loop
Used when you know how many times you want to repeat something.
It loops through a sequence like a list or range.
Example:
for i in range(5):
print("This is loop number", i)
While Loop
Used when you want to repeat something until a condition is no longer true.
The loop continues as long as the condition is True.
Example:
count = 0
while count < 5:
print("Count is", count)
count += 1
7. Write a for loop that prints all numbers from 1 to 5.
for number in range(1, 6):
print(number)
8. Write a while loop that asks the user to enter a password until they enter
“admin123”.
password = ""
while password != "admin123":
password = input("Enter the password: ")
print("Access granted!")
9. Explain what this loop does:
Python
for letter in "hello":
print(letter)
"hello" is a string, and in Python, strings are iterable — meaning you can loop through
each character one by one.
The for loop goes through each letter in the string "hello".
10. Write a Python code that uses a for loop and an if statement together to print only
even numbers between 1 and 10.
for number in range(1, 11):
if number % 2 == 0:
print(number)