You are on page 1of 14

Computer science

Vacation homework

Name : alfi aziz


Std : xii a
Roll no: 1213
1. Create a python program to show the sum of digits of a number.
a=int(input("enter first number : "))
b=int(input("enter second number : "))
print("sum of",a, "and",b,"is",a+b)

2. Create a python program to calculate the power of a number.


a= int(input("enter a number"))
b= int(input("enter a power"))
if b==0:
print(1)
else:
for i in range(1,b+1):
result=a**i
print(result)
3.
6.Create a python program which counts the number of occurences of a word in a text.

def word_count(str):
counts = dict()
words = str.split()

for word in words:


if word in counts:
counts[word] += 1
else:
counts[word] = 1

return counts

print( word_count('the quick brown fox jumps over the lazy dog.'))
7.
8.LOWERCASE &UPPERCASE

string=str(input("Enter string:"))
count1=0
count2=0
for I in string:
if(i.islower()):
count1=count1+1
elif(i.isupper()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)

9. # declaring a string variable


str = "An apple A day keeps doctor Away."

# replacing character a with $ sign


str = str.replace('a', '$')
print("Modified string : ")
print(str)

10. python program to remove the nth index character from a non-empty string

str = "Geeksforgeeks is fun."


n=8
first_part = str[0:n]
second_part = str[n+1:]
print("Modified string after removing ", n, "th character ")
print(first_part+second_part)

11. ANAGRAM

s1=input("Enter first string:")


s2=input("Enter second string:")
if(sorted(s1)==sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")

12. exchanged

def change(string):
return string[-1:] + string[1:-1] + string[:1]
string=input("Enter string:")
print("Modified string:")
print(change(string))

13 . vowels

string=input("Enter string:")
vowels=0
for I in string:
if(I=='a' or I=='e' or I=='I' or I=='o' or I=='u' or I=='A' or I=='E' or I=='I' or I=='O' or I=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
14. hyphen

string=input("Enter string:")
string=string.replace(' ','-')
print("Modified string:")
print(string)

15.length
string=input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
16. odd index

def modify(string):
final = ""
for I in range(Len(string)):
if I % 2 == 0:
final = final + string[I]
return final
string=input("Enter string:")
print("Modified string is:")
print(modify(string))

17.no of words

string=input("Enter string:")
char=0
word=1
for I in string:
char=char+1
if(I==' '):
word=word+1
print("Number of words in the string:")
print(word)
print("Number of characters in the string:")
print(char)
18.larger and smaller words

string1=input("Enter first string:")


string2=input("Enter second string:")
count1=0
count2=0
for I in string1:
count1=count1+1
for j in string2:
count2=count2+1
if(count1<count2):
print("Larger string is:")
print(string2)
elif(count1==count2):
print("Both strings are equal.")
else:
print("Larger string is:")
print(string1)

19.lowercase

string=input("Enter string:")
count=0
for I in string:
if(i.islower()):
count=count+1
print("The number of lowercase characters is:")
print(count)

20.palindrome

string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")

21.

string=input("Enter string:")
count1=0
count2=0
for I in string:
if(i.islower()):
count1=count1+1
elif(i.isupper()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
22.PANGRAM
from string import ascii_lowercase as asc_lower
def check(s):
return set(asc_lower) - set(s.lower()) == set([])
strng=input("Enter string:")
if(check(strng)==True):
print("The string is a pangram")
else:
print("The string isn't a pangram")

23. hyphen separated sequence of words

print("Enter a hyphen separated sequence of words:")


lst=[n for n in input().split('-')]
lst.sort()
print("Sorted:")
print('-'.join(lst))

24.no of digit

string=input("Enter string:")
count1=0
count2=0
for I in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)

25.first 2nd and last 2nd

string=input("Enter string:")
count=0
for I in string:
count=count+1
new=string[0:2]+string[count-2:count]
print("Newly formed string is:")
print(new)

26.occurence of a word

string=input("Enter string:")
word=input("Enter word:")
a=[]
count=0
a=string.split(" ")
for I in range(0,len(a)):
if(word==a[I]):
count=count+1
print("Count of the word is:")
print(count)

27.substring is a string

string=input("Enter string:")
sub_str=input("Enter word:")
if(string.find(sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring in string!")

THE END

You might also like