Python Practice Tasks (With Answers)
1. Display Last 3 Characters
Question:
Get a string from the user and display the last three characters.
s = input("Enter a string: ")
print("Last three characters:", s[-3:])
2. First and Last Character in Capital
Question:
Get a string from the user and display the first and last character in uppercase.
s = input("Enter a string: ")
first = s[0].upper()
last = s[-1].upper()
print("First Character:", first)
print("Last Character:", last)
3. Count Vowels
Question:
Get user input and count the number of vowels.
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for i in s:
if i in vowels:
count += 1
print("Number of vowels:", count)
4. Count Duplicates
Question:
Count the number of 'a' in the string.
A = "Arunachalam"
count = 0
Novitech
for i in [Link]():
if i == 'a':
count += 1
print("Number of 'a':", count)
5. Count Digits
Question:
Get user input and count number of digits.
s = input("Enter a string: ")
count = 0
for i in s:
if [Link]():
count += 1
print("Number of digits:", count)
6. Count Words
Question:
Find the number of words in user input.
s = input("Enter a sentence: ")
words = [Link]()
print("Number of words:", len(words))
7. Find Length Without Using len()
Question:
Find the length of string without using len()
s = input("Enter a string: ")
count = 0
for i in s:
count += 1
print("Length of string:", count)
Novitech
8. Count Letters
Question:
Count only alphabet characters in the string.
s = input("Enter a string: ")
count = 0
for i in s:
if [Link]():
count += 1
print("Number of letters:", count)
9. Find Largest Word
Question:
Find the largest word in the sentence.
s = input("Enter a sentence: ")
words = [Link]()
largest = ""
for i in words:
if len(i) > len(largest):
largest = i
print("Largest word:", largest)
10. Store Odd Numbers in List
Question:
Accept 10 numbers from user and store only odd numbers in a list.
numbers = []
for i in range(10):
num = int(input("Enter number: "))
if num % 2 != 0:
[Link](num)
Novitech
print("Odd numbers list:", numbers)
11. Extract Digits from String
Question:
Find digits from string and display them as string format
Example
Input: "45 ram was 67 coimbatore"
Output: "4567"
a = "45 ram was 67 coimbatore"
result = ""
for i in a:
if [Link]():
result += i
print("Output:", result)
12. Print Series
Question:
Print series 2, 22, 222, 2222 … n terms
n = int(input("Enter number of terms: "))
num = ""
for i in range(n):
num += "2"
print(num)
13. Divisible by 13 but Not by 3
Question:
Display numbers between 100 and 500 divisible by 13 but not by 3
for i in range(100, 501):
if i % 13 == 0 and i % 3 != 0:
print(i)
Novitech
14. Find Largest & Smallest Number
Question:
Get 10 numbers from user and display largest and smallest
numbers = []
for i in range(10):
num = int(input("Enter number: "))
[Link](num)
print("Largest:", max(numbers))
print("Smallest:", min(numbers))
15. Print Odd Numbers Using While Loop
numbers = [2,5,7,8,9,10,11]
i=0
while i < len(numbers):
if numbers[i] % 2 != 0:
print(numbers[i])
i += 1
16. Armstrong Number
num = int(input("Enter number: "))
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp = temp // 10
if num == sum:
print("Armstrong Number")
else:
print("Not Armstrong")
Novitech
17. Textile Shop Discount
amount = float(input("Enter purchase amount: "))
if amount > 5000:
discount = amount * 0.10
final = amount - discount
print("Discounted price:", final)
else:
print("Total amount:", amount)
18. Same Program (User Text Input)
choice = input("Enter above 5000 or below 5000: ")
amount = float(input("Enter amount: "))
if choice == "above 5000":
discount = amount * 0.10
final = amount - discount
print("Discounted amount:", final)
else:
print("Amount:", amount)
19. Convert List to Capital Letters
a = ["python","data","analytics","course"]
result = []
for i in a:
[Link]([Link]())
print(result)
20. Remove Duplicates and Merge Lists
Novitech
a = [3,5,4,6,2,3,5,7]
b = [6,7,8,9,65]
combined = a + b
result = list(set(combined))
print(result)
21. Reverse a String
Question:
Get a string from the user and display the reverse of the string.
s = input("Enter a string: ")
rev = ""
for i in s:
rev = i + rev
print("Reversed string:", rev)
22. Check Palindrome
Question:
Check whether the given string is Palindrome.
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
23. Sum of Digits
Question:
Get a number from user and find sum of digits.
num = int(input("Enter a number: "))
total = 0
while num > 0:
Novitech
digit = num % 10
total += digit
num = num // 10
print("Sum of digits:", total)
24. Multiplication Table
Question:
Print multiplication table for a given number.
num = int(input("Enter a number: "))
for i in range(1,11):
print(num,"x",i,"=",num*i)
25. Count Uppercase Letters
Question:
Count number of uppercase letters in a string.
s = input("Enter a string: ")
count = 0
for i in s:
if [Link]():
count += 1
print("Uppercase letters:", count)
26. Print Even Numbers
Question:
Print even numbers between 1 and 50.
for i in range(1,51):
if i % 2 == 0:
print(i)
27. Replace Spaces With Hyphen
Novitech
Question:
Replace spaces in the string with -
s = input("Enter a sentence: ")
result = [Link](" ","-")
print(result)
28. Find Factorial
Question:
Find factorial of a number.
num = int(input("Enter number: "))
fact = 1
for i in range(1,num+1):
fact = fact * i
print("Factorial:",fact)
29. Count Characters Without Spaces
Question:
Count characters excluding spaces.
s = input("Enter a sentence: ")
count = 0
for i in s:
if i != " ":
count += 1
print("Characters without spaces:",count)
30. Remove Vowels
Question:
Remove vowels from a string.
Novitech
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
result = ""
for i in s:
if i not in vowels:
result += i
print(result)
31. Find Largest Number in List
numbers = [23,45,12,67,34]
largest = numbers[0]
for i in numbers:
if i > largest:
largest = i
print("Largest number:",largest)
32. Count Elements in List
a = [10,20,30,40,50]
count = 0
for i in a:
count += 1
print("Total elements:",count)
33. Print Numbers Divisible by 5
for i in range(1,101):
if i % 5 == 0:
print(i)
Novitech
34. Swap Two Numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a=b
b = temp
print("After swapping:")
print("a =",a)
print("b =",b)
35. Find Common Elements in Two Lists
a = [1,2,3,4,5]
b = [4,5,6,7]
for i in a:
if i in b:
print(i)
36. Convert String to List
s = input("Enter words: ")
words = [Link]()
print(words)
37. Count Frequency of Character
s = input("Enter a string: ")
char = input("Enter character to count: ")
count = 0
for i in s:
if i == char:
count += 1
print("Frequency:",count)
Novitech
38. Find Average of Numbers
numbers = [10,20,30,40,50]
total = 0
for i in numbers:
total += i
avg = total / len(numbers)
print("Average:",avg)
39. Remove Duplicate Characters From String
s = input("Enter string: ")
result = ""
for i in s:
if i not in result:
result += i
print("After removing duplicates:",result)
40. Find Second Largest Number
numbers = [10,45,23,67,89,34]
[Link]()
print("Second largest:",numbers[-2])
41. Count Even and Odd Numbers in List
Question:
Find how many even and odd numbers are present in a list.
numbers = [10,23,45,60,78,11]
even = 0
odd = 0
for i in numbers:
Novitech
if i % 2 == 0:
even += 1
else:
odd += 1
print("Even numbers:", even)
print("Odd numbers:", odd)
42. Print Numbers in Reverse Order
Question:
Print numbers from 10 to 1.
for i in range(10,0,-1):
print(i)
43. Find Sum of List Elements
numbers = [5,10,15,20]
total = 0
for i in numbers:
total += i
print("Sum:", total)
44. Find Minimum Number Without min()
numbers = [23,11,45,6,78]
smallest = numbers[0]
for i in numbers:
if i < smallest:
smallest = i
print("Smallest number:", smallest)
45. Print First 10 Natural Numbers
Novitech
for i in range(1,11):
print(i)
46. Convert List Numbers to String
numbers = [1,2,3,4,5]
result = ""
for i in numbers:
result += str(i)
print(result)
Output:
12345
47. Find Numbers Greater Than Average
numbers = [10,20,30,40,50]
avg = sum(numbers)/len(numbers)
for i in numbers:
if i > avg:
print(i)
48. Print Squares of Numbers
for i in range(1,11):
print(i*i)
Output
1
4
9
16
25
36
49
Novitech
64
81
100
49. Create List From User Input
numbers = []
for i in range(5):
num = int(input("Enter number: "))
[Link](num)
print(numbers)
50. Count Positive and Negative Numbers
numbers = [10,-5,20,-8,15,-2]
positive = 0
negative = 0
for i in numbers:
if i > 0:
positive += 1
else:
negative += 1
print("Positive numbers:", positive)
print("Negative numbers:", negative)
Novitech