Python Practice Questions & Solutions Day 5 of Learning Python for Data Science
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.
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))
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.")
If we get "abc" as a input code will throw error as we cannot convert text into number.
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])
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])
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)
string = input('Enter a palindrom')
if string == string[::-1]:
print('Palindrom')
else:
print('Not a Palindrom')
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)
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')
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)
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 = ', ')
num = input('Enter a number')
sum = 0
for i in num:
sum+=int(i)
print(sum)
num = input('Enter a number')
num1 = int(num[::-1])
print(num1)
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.')
num = int(input('Enteer number'))
fact = 1
for i in range(1, num+1):
fact *= i
print(fact)
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
a
and b
are initialized with 0, 1 which represents the first 2 number of fibonachi series.num = input('Enter and number : ')
print(num,f' has {len(num)} digits.')
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))
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))
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
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.
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…
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…