
Test your Python skills with these 20 practice questions and solutions from Day 3 of Learning Python for Data Science. Covers string operations, data types, and more!
1. What will be the output of the following code?
x = 10
if x > 5:
print("Greater")
else:
print("Smaller")
Answer:
Greater
Explanation:
Since x = 10
is greater than 5, the condition if x > 5
is True
, so "Greater"
is printed.
2. Write a Python program using an if-else statement to check if a number is positive, negative, or zero.
Answer:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Explanation:
- If
num > 0
, it prints"Positive"
. - If
num < 0
, it prints"Negative"
. - Otherwise, it prints
"Zero"
.
3. What will be the output of the following code?
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
Answer:
nginxCopyEditOdd
Explanation:
7 % 2
equals 1
, so the condition num % 2 == 0
is False
, and the "Odd"
statement is executed.
4. What will be the output of the following nested if condition?
x = 15
if x > 10:
if x < 20:
print("Between 10 and 20")
Answer:
sqlCopyEditBetween 10 and 20
Explanation:
Both conditions x > 10
and x < 20
are True
, so the nested if
block executes.
5. Write a Python program that uses nested if statements to check whether a given number is positive, even, and greater than 10.
Answer:
num = int(input("Enter a number: "))
if num > 0:
if num % 2 == 0:
if num > 10:
print("Positive, Even, and Greater than 10")
Explanation:
- The first condition checks if the number is positive.
- The second condition checks if it’s even.
- The third condition checks if it’s greater than 10.
6. What is the output of the following program?
x = 5
y = 10
if x > 2:
if y > 5:
print("Valid")
Answer:
Valid
Explanation:
Since both x > 2
and y > 5
are True
, the print statement executes.
7. Write a Python program that asks for a user’s age and prints their category.
Answer:
age = int(input("Enter your age: "))
if age <= 12:
print("Child")
elif age <= 19:
print("Teenager")
elif age <= 59:
print("Adult")
else:
print("Senior")
Explanation:
- It checks for age categories and prints the appropriate label.
8. What will be the output of the following code?
pythonCopyEditx = 50
if x >= 50:
if x == 50:
print("Exactly 50")
else:
print("Greater than 50")
else:
print("Less than 50")
Answer:
nginxCopyEditExactly 50
Explanation:
Since x == 50
, the "Exactly 50"
statement executes.
9. Write a Python program using the pass
statement inside an empty if
condition.
Answer:
x = 10
if x > 5:
pass # Placeholder for future code
else:
print("Smaller")
Explanation:
pass
allows an empty block of code without errors.
10. What will the following program print?
pythonCopyEditx = 20
y = 30
if x < y:
if x + 10 == y:
print("Match")
Answer:
Match
Explanation:
Since x + 10 == y
(20 + 10 = 30), the condition is True
, so "Match"
prints.
11. Write a Python program to check if a number is positive or negative using an if-else statement.
Answer:
num = int(input("Enter a number: "))
if num >= 0:
print("Positive")
else:
print("Negative")
12. Modify the code to use an elif condition to check for zero as well.
Answer:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
13. What is the output of this code snippet?
x = 10
if x > 5:
print("High")
if x > 8:
print("Higher")
if x > 15:
print("Highest")
Answer:
High
Higher
Explanation:
Since x = 10
, the first two if
conditions are True
, but the last one is False
.
14. Write a program that checks if a number is divisible by both 2 and 3 using a nested if statement.
Answer:
num = int(input("Enter a number: "))
if num % 2 == 0:
if num % 3 == 0:
print("Divisible by both 2 and 3")
15. Write a Python program that checks if a number is a multiple of both 4 and 6 using a nested if
statement.
Answer:
num = int(input("Enter a number: "))
if num % 4 == 0:
if num % 6 == 0:
print("Multiple of both 4 and 6")
16. Modify the program to check if a number is even and greater than 10.
Answer:
num = int(input("Enter a number: "))
if num % 2 == 0:
if num > 10:
print("Even and Greater than 10")
17. Write a Python program using if-elif-else conditions to categorize a number.
Answer:
num = int(input("Enter a number: "))
if num <= 10:
print("Small")
elif num <= 50:
print("Medium")
else:
print("Large")
18. What is the output of this nested condition?
x = 3
y = 9
if x < 5:
if y > 8:
print("Success")
Answer:
Success
19. Modify the program to check if a number is between 10 and 50.
Answer:
num = int(input("Enter a number: "))
if 10 < num < 50:
print("Between 10 and 50")
20. Write a Python program that checks if a number is prime using conditional statements.
Answer:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
Conclusion
These practice questions cover Python string manipulation, type conversions, and essential built-in functions. Practicing them will help solidify your understanding of these fundamental concepts.
We hope this article was helpful for you and you learned a lot about data analyst interview from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science
Also Read:
- Practice Questions and Answers for Day 3 of Learning Python for Data Science
- Day 3 of Learning Python for Data Science
- Practice Questions and Answers for Day 2 of Learning Python for Data Science
- PayPal Data Analyst Interview Experience: CTC – 22 LPA
- Understanding Dynamic Sets in Tableau
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.