Python Practice Questions & Solutions Day 4 of Learning Python for Data Science
Welcome back to Day 4 of Learning Python for Data Science! In the last article, we explored:
✅ Comparison Operators
✅ Logical Operators
✅ Loops in Python
✅ Loop Control Statements
Now, it’s time to solve the practice questions given in the previous article.
Each question is followed by a detailed explanation and output.
python
print(10 == 10)
Solution:
This checks if 10
is equal to 10
, which is True
.
Output:
True
Solution:
num = int(input("Enter a number: "))
if num > 0:
print("Positive Number")
elif num < 0:
print("Negative Number")
else:
print("Zero")
Example Input/Output:
Enter a number: 5
Positive Number
print(5 > 3 and 10 > 5)
Solution:
Both conditions 5 > 3
and 10 > 5
are True
, so the result is True
.
Output:
True
Solution:
num = int(input("Enter a number: "))
if num % 2 == 0 and num % 5 == 0:
print("Divisible by both 2 and 5")
else:
print("Not divisible by both")
Example Input/Output:
Enter a number: 10
Divisible by both 2 and 5
while
loop?x = 1
while x < 5:
print(x)
x += 1
Solution:
The loop runs while x
is less than 5, printing 1, 2, 3, 4
.
Output:
1
2
3
4
for
loop instead of while
:Solution:
for x in range(1, 6):
print(x)
Output:
1
2
3
4
5
break
statement is removed from the following loop?for i in range(1, 6):
if i == 4:
break
print(i)
Solution:
Without break
, the loop prints all numbers from 1 to 5 instead of stopping at 4
.
Output without break
:
1
2
3
4
5
Solution:
for i in range(1, 21):
if i % 2 == 0:
print(i, end=" ")
Output:
2 4 6 8 10 12 14 16 18 20
for i in range(1, 6):
if i == 3:
continue
print(i)
Solution:
The continue
statement skips printing 3
.
Output:
1
2
4
5
continue
.Solution:
for i in range(1, 11):
if i % 3 == 0:
continue
print(i, end=" ")
Output:
1 2 4 5 7 8 10
for
loop that prints numbers from 1 to 10 but stops when it reaches 7.Solution:
for i in range(1, 11):
if i == 7:
break
print(i)
Output:
1
2
3
4
5
6
x = 5
y = 10
print(x > 2 and y < 15)
Solution:
Both conditions are True
, so the result is True
.
Output:
True
for
loop.Solution:
total = 0
for i in range(1, 101):
total += i
print("Sum:", total)
Output:
Sum: 5050
x
is greater than 50.Solution:
x = 1
while x < 100:
if x > 50:
break
print(x, end=" ")
x += 5
Output:
1 6 11 16 21 26 31 36 41 46
Solution:
count = 0
for i in range(1, 51):
if i % 2 == 0:
count += 1
print("Total even numbers:", count)
Output:
Total even numbers: 25
pass
when x == 5
.Solution:
for x in range(10):
if x == 5:
pass # Placeholder
else:
print(x)
Output:
0
1
2
3
4
6
7
8
9
for i in range(1,21):
if i % 2 == 0:
print(i)
else:
continue
Output
2
4
6
8
10
12
14
16
18
20
num = 1
sum = 0
while num <=50:
sum += num
num += 1
print(sum)
Output
1275
name = 'Vishal'
rev = ''
for i in name:
rev = i + rev
print(rev)
Output
lahsiV
Explanation In this code when loop is accesing the first charecter from name it gets 'V'
and stores in rev
. in the next iteration it get i
and since condition to store is defined to prepend as i + rev
it stores as 'iV'
similarly in the next iteration it gets 's'
and store as 'siV'
and so on.
num = 5
fac = 1
for i in range(1,num+1):
fac*=i
print(fac)
Output
120
# Initialize first two terms
a, b = 0, 1
# Number of terms to generate
count = 0
n = 10 # Total terms
while count <= n:
print(a, end=", ") # Print the current term
a, b = b, a + b # Update values for the next iteration
count += 1
Output
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
num = int(input('Enter a number: '))
if num <= 1:
print(f'{num} is not prime.')
else:
for i in range(2,num):
if num % i == 0:
print(f'{num} is not prime.')
break
else:
print(f'{num} is prime.')
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
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.
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…
Test your Python skills with these 20 practice questions and solutions from Day 3 of…
Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…
Test your Python skills with these 20 practice questions and solutions from Day 2 of…