Questions and Answers on Python Data Types
Questions and Answers on Python Data Types
Loops
1. Question: What are Python’s common data types, and provide examples for each?
Answer: Python's common data types include:
x = 10 # Integer
y = 3.14 # Float
print(x, y, name)
3. Question: What is the difference between list, tuple, and set in Python?
Answer:
o set: Unordered, mutable, and does not allow duplicates. Example: {1, 2, 3}
4. Question: Write a program using if-else to check if a number is positive, negative, or zero.
Answer:
if num > 0:
print("Positive")
print("Negative")
else:
print("Zero")
5. Question: How does the range() function work in a for loop? Provide an example.
Answer: The range() function generates a sequence of numbers. Example:
print(i)
6. Question: Write a program using a while loop to print all even numbers between 1 and 20.
Answer:
num = 2
print(num)
num += 2
print("Grade: A")
print("Grade: B")
print("Grade: C")
print("Grade: D")
else:
print("Grade: F")
8. Question: Write a for loop to iterate over a list and print strings with more than 5 characters.
Answer:
if len(word) > 5:
print(word)
9. Question: Write a while loop to keep asking for a number until the user enters -1.
Answer:
while True:
if num == -1:
break
10. Question: Write a program to print numbers from 1 to 50 with special rules for multiples of 3
and 5.
Answer:
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)