Chapter 1
Chapter 1
5. Write a program to enter any number and calculate sum of its digits.
n=int(input('enter a number'))
sum=0
while(n!=0):
remainder=n%10
sum=sum+remainder
n=n//10
print(sum)
if(n1==strong):
print('strong number is',strong)
13. Write a program to print Fibonacci series up to n terms.
a=0
b=1
print(a,b,end=',')
n=int(input('enter a number'))
for i in range(1,n):
c=a+b
print(c,end=',')
a=b
b=c
14. Star pattern programs - Write a program to print the given star patterns.
n=6
for i in range(1,n):
for j in range(0,i):
print('*',end=' ')
print()
n=6
for i in range(1,n):
for j in range(0,i):
print('*',end=' ')
print()
for i in range(n,0,-1):
for j in range(0,i):
print('*',end=' ')
print()
n=6
for i in range(0,n):
print(' '*(n-i-1)+'* '*(i+1))
for i in range(0,n):
print(' '*(i+1)+'* '*(n-i-1))
2.Write a program to convert lower case to upper case from given string?
s=input('string')
s1=''
for i in s:
s1=s1+chr(ord(i)-32)
print(s1)
5. Write a program to find no_ words, no_letters, no_digits and no_blanks in a line?
s=input('enter a line')
w,l,d,b=0,0,0,0
k=s.split()
w=w+len(k)
for i in s:
7. To find the first character from given string, count the number of times repeated and
replaced with * except first character then print final string?
s='kiran kumar kiran '
print(s.count(s[0]))
print(s[0]+s[1:].replace(s[0],'*'))
8. To find the strings in a list which are matched with first character equals to last character
in a string?
l=['kiran','harish','did','do']
for i in l:
if i[0]==i[-1]:
print(i)
9. Write a program that accepts a string from user and redisplays the same string after
removing vowels from it?
s=input('enter a string containing vowels')
for i in s:
if i not in ['a','e','i','o','u','A','E','I','O','U']:
print(i,end='')
10. This is a Python Program to take in two strings and display the larger string without using
built-in functions.?
s1=input('string 1')
s2=input('string 2')
c1,c2=0,0
for i in s1:
c1=c1+1
for i in s2:
c2=c2+1
if c1>c2:
print(s1)
else:
print(s2)
12. Python Program to Calculate the Number of Upper-Case Letters and Lower-Case Letters
in a String?
s='VGb chgkJM cgLBJ yggJSZKGB B'
l,u=0,0
for i in s:
if i.islower():
l=l+1
elif i.isupper():
u=u+1
print(l,u)
OR
c1=0
c2=0
for i in s:
if i<='z' and i>='a':
c1=c1+1
elif i>='A' and i<='Z':
c2=c2+1
print('small',c1,'capital',c2)
14. Python Program to Create a Dictionary with Key as First Character and Value as Words
Starting with that Character
d={}
s='sxwvbnj ahhhah ajkb ahjnvc svghnvf kkk'
l=s.split()
for word in l:
if word[0] not in d.keys():
d[word[0]]=[]
d[word[0]].append(word)
else:
if word[0] in d.keys():
1. Write a Python program to find whether the given number is divisible by 7 and multiple of
5?
n=int(input('enter a number'))
if n%7==0 and n%5==0:
print(n,'the given number is divisible by 7 and multiple of 5')
else:
print(n,'the given number is not divisible by either 7 or the multiple of 5')
2. Write a program to input any number and check whether it is even or odd
n=int(input('enter a number'))
if n%2==0:
print(n,'is a even number')
else:
print(n,'is a odd number')
3. Write a program to input any number and check whether it is negative, positive or zero
n=int(input('enter a number'))
if n>0:
print(n,'is a positive number')
elif(n<0):
print(n,'is a negative number')
else:
print('zero')
4. A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended. And print
percentage of class attended
and student is allowed to sit in exam or not.
nch=int(input('no of classes held'))
nca=int(input('no. of classes attended'))
p=((nch-nca)/nch)*100
a=100-p
print(a)
if a>=75:
print('student is allowed to write exams')
else:
print('student is not allowed to write exams')
1. Take values of length and breadth of a rectangle from user and check if it is square or not.
l=int(input('enter length'))
b=int(input('enter breadth'))
if l==b:
print('it is a square')
else:
print('it is not a square')
2. Take two int values from user and print greatest among them
a=int(input('enter a number'))
b=int(input('enter a number'))
if a>b:
print('greatest number=',a)
else:
print('greatest number',b)
3. A shop will give discount of 10%, if the cost of purchased quantity is more than 1000.
Ask user for quantity Suppose, one unit will cost 100.
Ex: quantity=11
cost=11*100=1100>1000 so, he will get 10% discount, that is 110.
Final cost is 1100-110=990
q=int(input('enter quanitity'))
cost=q*100
d=(cost/100)*10
if cost>1000:
print('final cost is',1000-d)
else:
print('final cost is ',costs)
4. A company decided to give bonus of 5% to employee if his/her year of service is more than 5
years.Ask user for their salary and year of service and print the net bonus amount.
6. Take input of age of 3 people by user and determine oldest and youngest among them.
a1=int(input('age of 1st person'))
a2=int(input('age of 2nd person'))
a3=int(input('age of 3rd person'))
if a1>a2 and a1>a3:
print('oldest',a1)
elif a2>a1 and a2>a3:
print('oldest',a2)
else:
print('oldest',a3)
if a1<a2 and a1<a3:
print('youngest',a1)
elif a2<a1 and a2<a3:
print('youngest',a2)
else:
print('youngest',a3)
8. Modify the 4th question (in solved problem set) to allow student to sit if he/she has medical
cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
nch=int(input('no of classes held'))
nca=int(input('no. of classes attended'))
mc=input('enter Y or N if you have medical cause(y-yes,N-No)')
p=((nch-nca)/nch)*100
a=100-p
print(a)
if a>=75:
print('student is allowed to write exams')
elif(mc=='Y'):
print('student is allowed to write exams')
else:
print('student is not allowed to write exams')