PythonProgPract-1
PythonProgPract-1
Print out a message addressed to them that tells them the year that they will
turn 100 years old.
import datetime
O/P:-
1.b) Enter the number from the user and depending on whether the number
is even or odd, print out an appropriate message to the user.
n=int(input("Enter a number:"))
if n%2==0:
print(n,"is even")
else:
print(n,"is odd")
O/P:-
1.c) Write a program to generate the Fibonacci series.
n1=0
n2=1
n=int(input("Enter a Number:"))
print(n1)
print(n2)
for i in range(2,n):
n3=n1+n2
print(n3)
n1=n2
n2=n3
O/P:-
1.d) Write a function that reverses the user defined value.
n=int(input("Enter a number:"))
revNo=0
while n>0:
d=n%10
revNo=revNo*10+d
n=int(n/10)
print("Reverse number=",revNo)
O/P:-
1.e) Write the function to check the input value is Armstrong and also write
the function for palindrome.
def armstrong(n):
newNo=n
sum=0
while n>0:
d=n%10
sum=sum+d*d*d
n=int(n/10)
if newNo==sum:
print(newNo,"is Armstrong")
else:
def palindrome(num):
n=num
rev=0
while num!=0:
rev=rev*10
rev=rev+ int(num%10)
num=int(num/10)
if n==rev:
print(n,"is Palindrome")
else:
armstrong(n)
palindrome(n)
O/P:-
1.f) Write a recursive function to print the factorial for a given number.
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
n=int(input("Enter a Number:"))
fact=factorial(n)
O/P:-
2.a) Write a function that takes a character and returns True if it is a vowel,
False otherwise.
def vowelCheck(char):
or char=='U'):
return 'True'
else:
return 'False'
vchar=input('Enter a character:')
if(vowelCheck(vchar)=='True'):
print(vchar,'is vowel')
else:
O/P:-
count=0
for i in str:
count=count+1
return count
str1=input('Enter a string:')
print('Length of',str1,'=',lenCount(str1))
O/P:-
2.c) Define a procedure histogram() that takes a list of integers and prints a
histogram to the screen. For example, histogram([4,9,7])
def histogram(inList):
for i in range(len(inList)):
print(inList[i]*'*')
List=[4,9,7]
histogram(List)
O/P:-
3.a) A pangram is a sentence that contains all the letters of the English
alphabet at least once, for example: The quick brown fox jumps over the lazy
dog. Your task here is to write a function to check a sentence to see if it is a
pangram or not.
import re
def isPangram(inSentence):
alphaList='abcdefghijklmnopqrstuvwxyz'
alphaCount=0
if len(inSentence)<26:
return False
else:
inSentence=re.sub('[^a-zA-Z]','',inSentence)
inSentence=inSentence.lower()
for i in range(len(alphaList)):
if alphaList[i] in inSentence:
alphaCount=alphaCount+1
if alphaCount==26:
return True
else:
return False
inSentence=input('Enter a String:')
if (isPangram(inSentence)):
else:
O/P:-
O/P:-
3.b) Take a list, say for example this one: a=[1,1,2,3,5,8,13,21,34,55,89] and
write a program that prints out all elements of the list that are less than 20.
a=[1,1,2,3,5,8,13,21,34,55,89]
print('List Elements')
for number in a:
print(number,end=' ')
for number in a:
if number<20:
print(number)
O/P:-