0% found this document useful (0 votes)
6 views

PythonProgPract-1

Python code

Uploaded by

sakshi313kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PythonProgPract-1

Python code

Uploaded by

sakshi313kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.a) Create a program that asks the user to enter their name and their age.

Print out a message addressed to them that tells them the year that they will
turn 100 years old.
import datetime

name=input("Enter your name:")

age=int(input("Enter your age:"))

print("You will become 100 years old in:",(100-age)+datetime.datetime.now().year)

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:

print(newNo,"is not Armstrong")

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:

print(n,"is not Palindrome")

n=int(input("Enter a number to check for armstrong:"))

armstrong(n)

n=int(input("Enter a number to check for palindrome:"))

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)

print('factorial of ', n , '=',fact )

O/P:-
2.a) Write a function that takes a character and returns True if it is a vowel,
False otherwise.
def vowelCheck(char):

if(char=='a' or char=='A' or char=='e'

or char=='E' or char=='i' or char=='I'

or char=='o' or char=='O' or char=='u'

or char=='U'):

return 'True'

else:

return 'False'

vchar=input('Enter a character:')

if(vowelCheck(vchar)=='True'):

print(vchar,'is vowel')

else:

print(vchar,'is not vowel')

O/P:-

2.b)Define a function that computes the length of a given list or string


def lenCount(str):

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)):

print("Input Sentence is a Pangram")

else:

print("Input Sentence is not a Pangram")

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=' ')

print('\n Numbers less than 20 are:')

for number in a:

if number<20:

print(number)

O/P:-

You might also like