
Welcome back to Day 5 of Learning Python for Data Science journey! In the last article, we explored:
✅ Typecasting in Python
✅ Indexing and Slicing in Strings
Now, it’s time to solve the practice questions given in the previous article.
Each question is followed by a detailed explanation and output.
Write a program that asks the user to input three values:
A whole number
A decimal number
A string
Then print the type of each input using the type() function.
name = input('Enter your name :')
age = int(input('Enter your age :'))
balance = float(input('Enter your balance :'))
print(name, type(name))
print(age, type(age))
print(balance, type(balance))
Write a program that asks the user to input a string containing a number (e.g., “123”). Convert this string into an integer, add 50 to it, and print the result.
try: user_input = input("Enter a string containing a number: ") number = int(user_input) result = number + 50 print(result) except ValueError: print("Invalid input. Please enter a string that can be converted to an integer.")
What happens if the string is “abc” instead?
If we get "abc" as a input code will throw error as we cannot convert text into number.
Write a program that takes a user-input string and prints:
The first character
The last character
The character at index 3
Handle cases where the string has less than 3 characters.
name = input('Enter your name :')
print('First charecter : ',name[0])
print('Last charecter : ',name[-1])
if len(name)<3:
print('length less than 3 charecter')
else:
print('Third charecter : ',name[2])
Write a program that takes a user-input string and prints:
The first 3 characters
The last 3 characters
The entire string in reverse order
string = input('Enter a logn string')
print('The first 3 characters :',string[0:2])
print('The last 3 characters :',string[-3:])
print('The entire string in reverse order :',string[::-1])
Write a program that takes a user-input string and a character. Count and print how many times the character appears in the string. Example: Input: “hello world”, ‘o’ → Output: 2.
word = input('enter a word : ')
char = input('Enter the charecter to count : ')
count = 0
for i in word:
if i == char:
count+=1
print(char, f'appears {count} times in ', word)
Write a program to check if a user-input string is a palindrome (a string that reads the same backward as forward). Example: Input: “madam” → Output: “Palindrome”.
string = input('Enter a palindrom')
if string == string[::-1]:
print('Palindrom')
else:
print('Not a Palindrom')
Write a program using if-elif-else statements to find the largest of three numbers entered by the user.
num1 = int(input('Enter the first number :'))
num2 = int(input('Enter the second number :'))
num3 = int(input('Enter the third number :'))
if num1 > num2 and num1 > num3:
print('Largest number is : ',num1)
elif num2 > num3 and num3 > num1:
print('Largest number is : ',num2)
else:
print('Largest number is : ',num3)
Write a program that takes a single character as input and checks whether it is a vowel, consonant, or neither (e.g., a digit or symbol). Use if-elif-else statements for this.
char = input('Enter a charecter : ').lower()
vowels = 'aeiou'
if char in vowels:
print('Vowel')
elif char not in vowels:
if char.isdigit():
print('Digit')
else:
print('consonant')
Write a program to calculate the sum of all even numbers from 1 to a user-defined number (inclusive) using a while loop.
num = int(input('Enter a number'))
sum = 0
i = 1
while i <=num:
if i % 2 == 0:
sum+=i
i+=1
print(sum)
num = int(input('Enter a number'))
sum = 0
for i in range(num+1):
if i % 2 == 0:
sum += i
i += 1
print(sum)
Write a program that takes two numbers as input and prints all the prime numbers between them using nested loops. Example: Input: 10, 20 → Output: 11, 13, 17, 19.
num1 = int(input('Enter a number'))
num2 = int(input('Enter a number'))
for i in range(num1, num2+1):
for j in range(2, i):
if i % j == 0:
break
else:
print(i, end = ', ')
Write a program that takes an integer as input and calculates the sum of its digits using a loop. Example: Input: 123 → Output: 6 (1 + 2 + 3)
num = input('Enter a number')
sum = 0
for i in num:
sum+=int(i)
print(sum)
Write a program that takes an integer and reverses its digits. Example: Input: 1234 → Output: 4321
num = input('Enter a number')
num1 = int(num[::-1])
print(num1)
Check for Armstrong Number, A number is an Armstrong number if the sum of its digits raised to the power of the number of digits equals the number itself. Example: Input: 153 → Output: Armstrong number (1³ + 5³ + 3³ = 153)
num = input('Enter a number : ') length = len(num) armstrong_sum = 0 for i in num: armstrong_sum += int(i)**length if int(num) == armstrong_sum : print(num, ' is an armstrong number.') else: print(num, ' is not an armstrong number.')
Write a program that calculates the factorial of a given number using a loop. Example: Input: 5 → Output: 120 (5 × 4 × 3 × 2 × 1)
num = int(input('Enteer number'))
fact = 1
for i in range(1, num+1):
fact *= i
print(fact)
Write a program to print the first N numbers of the Fibonacci sequence using a loop. Example: Input: 7 → Output: 0, 1, 1, 2, 3, 5, 8
num = int(input('Enter a number: '))
a, b = 0, 1
for i in range(num):
print(a, end=', ' if i < num - 1 else '')
a, b = b, a + b
How this works¶
a
andb
are initialized with 0, 1 which represents the first 2 number of fibonachi series.- When for loop starts
- Iteration…….current value..print(a)……..a, b = b, a + b
- 1st iteration : a = 0, b = 1 Print: a = 0, Update: a = 1, b = 1
- 2nd iteration : a = 1, b = 1 Print: a = 1, Update: a = 1, b = 2
- 3rd iteration : a = 1, b = 2 Print: a = 1, Update: a = 2, b = 3
- 4th iteration : a = 2, b = 3 Print: a = 2, Update: a = 3, b = 5
- 5th iteration : a = 3, b = 5 Print: a = 3, Update: a = 5, b = 8
- 6th iteration : a = 5, b = 8 Print: a = 5, Update: a = 8, b = 13
Write a program that takes an integer as input and counts the number of digits in it. Example: Input: 34567 → Output: 5
num = input('Enter and number : ')
print(num,f' has {len(num)} digits.')
Write a program that finds the GCD (Greatest Common Divisor) of two numbers using a loop.¶ Example: Input: 48, 18 → Output: 6
num1, num2 = int(input('Enter the first number : ')), int(input('Enter the second number : '))
gcd = []
'''
Using min because the CGD can not be greater than the small number,
uning +1 because GCD counld be the smaller number istself
'''
for i in range(2, min(num1, num2)+1): # explained above
if num1 % i == 0 and num2 % i == 0:
gcd.append(i)
else:
continue
print('GCD is : ', max(gcd))
Write a program that calculates the Least Common Multiple (LCM) of two numbers using loops.¶ Example: Input: 4, 5 → Output: 20
num1, num2 = int(input('Enter the first number : ')), int(input('Enter the second number : ')) gcd = [] for i in range(1, max(num1, num2)): if num1 % i == 0 and num2 % i == 0: gcd.append(i) else: continue print('LCM is : ', (num1 * num2)/max(gcd))
Write a program that checks whether a given number is a palindrome (same forward and backward).¶ Example: Input: 121 → Output: Palindrome Input: 123 → Output: Not a palindrome
num = abs(int(input('Enter a number'))) if str(num) == str(num)[::-1]: print(num, 'is a Palindrome') else: print(num, 'is not a palindrome')
Understanding these basics is essential for writing robust Python programs. Keep practicing with different inputs to solidify your understanding!
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:
- Python Practice Questions & Solutions Day 5 of Learning Python for Data Science
- Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing
- Python Practice Questions & Solutions Day 4 of Learning Python for Data Science
- Day 4 of Learning Python for Data Science
- Practice Questions and Answers for Day 3 of Learning 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.