PROGRAMMING BASICS (PYTHON) – QUESTIONS
& ANSWERS
1. What is a variable in Python?
Answer:
A variable is used to store data temporarily.
name = "Ali"
age = 16
2. Write 3 data types in Python with examples.
Answer:
int → Integer → x = 10
float → Decimal → y = 3.14
str → String → name = "Ahmet"
3. What do these operators mean? (==, !=, >)
Answer:
== → equal to
!= → not equal to
> → greater than
4. Explain the if-else structure with an example.
Answer:
number = 10
if number > 0:
print("Positive")
else:
print("Negative or zero")
5. What does this code print?
Answer:
Odd
x = 7
if x % 2 == 0:
print("Even")
else:
print("Odd")
6. What is a for loop? Write code from 1 to 5.
Answer:
for i in range(1, 6):
print(i)
7. Write a while loop from 1 to 5.
Answer:
i = 1
while i <= 5:
print(i)
i += 1
8. What does break do?
Answer:
Stops the loop.
while True:
if x == 0:
break
9. What does continue do?
Answer:
Skips one iteration.
for i in range(1, 6):
if i == 3:
continue
print(i)
10. Program: Stop when negative age is entered.
Answer:
while True:
age = int(input("Enter age: "))
if age < 0:
break
11. Program: Average of 5 numbers.
Answer:
total = 0
for i in range(5):
num = int(input("Enter number: "))
total += num
print("Average:", total / 5)
12. Sum of even numbers (1–100).
Answer:
i = 2
total = 0
while i <= 100:
total += i
i += 2
print(total)
13. What is a list?
Answer:
A structure that stores multiple values.
fruits = ["apple", "banana", "pear"]
14. Print the 2nd item of a list.
Answer:
colors = ["red", "blue", "green"]
print(colors[1])
15. Print all elements of a list.
Answer:
my_list = [1, 2, 3, 4]
for item in my_list:
print(item)
16. Keep asking names until "Ali" is entered.
Answer:
while True:
name = input("Enter name: ")
if name == "Ali":
break
17. Skip numbers divisible by 3 (1–20).
Answer:
i = 1
while i <= 20:
if i % 3 == 0:
i += 1
continue
print(i)
i += 1
18. What is a function?
Answer:
Reusable code block.
def add(a, b):
return a + b
19. Function with parameter.
Answer:
def greet(name):
print("Hello", name)
20. Output of nested loops:
Answer:
for i in range(3):
for j in range(2):
print(i, j)
# Output:
0 0
0 1
1 0
1 1
2 0
2 1