Solution of worksheet
BASIC
1. Write a Python program that accepts marks in 5 subjects and
print the average marks.
Ans:
total=0
for i in range(1,6):
marks = float(input("Enter marks for subject " + str(i) + ": "))
total= total+marks
avg=total/5
print("Avergre marks is " ,avg)
2. Write a program to accept a character from the user and
display whether it is a vowel or consonant.
Ans:
ch=input("Enter a character")
if ch=="A" or ch=="a":
print(ch,"ch is a vowel")
elif ch=="E" or ch=="e":
print(ch,"ch is a vowel")
elif ch=="I" or ch=="i":
print(ch,"ch is a vowel")
elif ch=="O" or ch=="o":
print(ch,"ch is a vowel")
elif ch=="U" or ch=="u":
print(ch,"ch is a vowel")
else:
print(ch,"ch is a consonent")
3. Write a program to count no. of vowels and consonant in given
string.
Ans:
str = input("Enter a string: ")
v = 0
c = 0
for i in str:
if i in "AEIOUaeiou":
v=v+1
elif i not in "AEIOUaeiou":
c=c+1
# Output the counts of vowels and consonants
print("Number of vowels:", v)
print("Number of consonants:",c)
4. Write a program to accept number from user and print the table
of the number.
Ans:
# Accept a number from the user
number = int(input("Enter a number: "))
# Print the multiplication table
print("Multiplication table of", number, ":")
for i in range(1, 11):
print(number, "x", i, "=", number * i)
5. Write a program to enter a number and check if it is a prime
number or composite number.
Ans:
num = int(input("Enter a number: "))
for i in range(2,num):
if num%i==0:
print(num,"is not a prime number")
break
else:
print(num," is a prime number")
6. Find the sum of digit of a number.
Ans:
# Accept a number from the user
num = int(input("Enter a number: "))
# Initialize sum of digits
sum= 0
# Extract digits and calculate sum
while num > 0:
digit = num % 10
sum = sum+digit
num =num//10
# Output the sum of digits
print("Sum of digits:", sum)
7. Write a program to enter a three digit number and check if it
is an Armstrong number or not.
Ans:
num=int(input("Enter three digit number")) #371
temp=num
sum=0
digit=0
while temp>0:
digit=temp%10
sum=sum+digit**3
temp=temp//10
if temp==sum:
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
(String)
8. Write a program to count the number of vowels in the
string.
Ans:
str="We are learning python"
v=0
for i in str:
if i in "AEIOUaeiou":
v=v+1
print("No. of vowels in given string",v)
9. Write a program to count the number of uppercase and
lowercase character in the string.
Ans:
str="We Are Learning Python"
u=0
l=0
s=0
for i in str:
if i.isupper():
u=u+1
elif i.islower():
l=l+1
elif i.isspace():
s=s+1
print("No.of uppercase are:",u)
print("No.of lowercase are:",l)
print("No.of space are:",s)
10. Write a program to check whether the string is a
palindrome or not.
Ans:
str=input("Enter any word")
if str==str[::-1]:
print(str," is palindrome")
else:
print(str," is not palindrome")
11. Write a program that reads a line, then counts words
and displays how many words are there in the line.
Ans:
str="We Are Learning Python"
str=str.split(“ “)
print(len(str))
or
str="We Are Learning Python"
c=0
str=str.split(“ “)
for i in str:
c=c+1
print("No. of words in given string are:",c)
12. Write a program to change the case of string.
Ans:
str="We Are Learning Python"
str=str.swapcase()
print(str)
or
str="We Are Learning Python"
str2=""
for i in str:
if i.isupper():
str2=str2+i.lower()
elif i.islower():
str2=str2+i.upper()
elif i.isspace():
str2=str2+i
print(str2)
13.Write a program to remove vowels from string.
Ans:
str="We Are Learning Python"
str2=""
for i in str:
if i not in "AEIOUaeiou":
str2=str2+i
print(str2)
14.Write a program to remove duplicate characters of a given
string.
ANS:
str = "prashant dixit"
unique_str = " "
for i in str:
if i not in unique_str:
unique_str=unique_str+i
print("String after removing duplicates:", unique_str)
15.Write a program to count the number of occurrences of a
character in an inputted string.
Ans:
str="We Are Learning Python"
print(str.count("e"))
or
ch=input("enter any character:")
str="We Are Learning Python"
c=0
for i in str:
if i==ch:
c=c+1
print(c)
16. Write a program to get a string from a given string
where all occurrences of its first char have been changed to
'S', except the first character itself.
Ans:
input_string = input("Enter a string: ")
# Get the first character of the input string
first_char = input_string[0]
# Replace all occurrences of the first character (except the first one)
with 'S'
modified_string = first_char + input_string[1:].replace(first_char, 'S')
print("Modified string:", modified_string)
17.Write a program to remove the nth index character from a
non-empty string.
Ans:
input_string = input("Enter a string: ")
n = int(input("Enter the index of the character to remove (0-
based): "))
# Check if the index is within the range of the string
if 0 <= n < len(input_string):
# Remove the character at the nth index
modified_string = input_string[:n] + input_string[n+1:]
print("Modified string:", modified_string)
else:
print("Invalid index. Please enter a valid index within
the range of the string.")
18.Write a program to get a first three character of string,
if the length of the string is less than three get
original string.
input_string = input("Enter a string: ")
# Check if the length of the string is less than 3
if len(input_string) < 3:
print("Original string:", input_string)
else:
# Get the first three characters of the string
first_three_chars = input_string[:3]
print("First three characters of the string:",first_three_chars)
19. Write a python program to print the index of the character in a
string.
str = input("Enter a string: ")
# Iterate through each character in the string
for i in range(len(str)):
print("Index of character", str[i], "is",i)