Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Pro Lab

Program

Uploaded by

lerneandrago
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Pro Lab

Program

Uploaded by

lerneandrago
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Programs

1.Write a program to check whether a given number is Armstrong number or


not.

num = input("Enter your number :")


b = 0
for i in range (len(num)):
a = int(num[i])
a = a**len(num)
b += a
if str(b) == num:
print(num,"is a armstrong number")
else:
print(num,"is not an armstrong number ")

2. Write a python program to check whether a given number is positive


negative or zero.

num = int(input("Enter a number :"))


if num<0:
print(f"{num} ,is a negative number")
elif num>0:
print(f"{num} , is a positive number")
else :
print(f"{num} , is zero")

3. Write a program to guess a secret number between 1 to 25 within 5


guess , if the guess is right then print right guess , else wrong guess.

import random
num = random.randint(1,25)
for i in range(5):
user = int(input("Enter your guess : "))
if user == num:
print ("Right guess")
break
else :
print("Wrong Guess, try again\n")

4. write a program to print even number using step size in range().

n = int(input("How many numbers do u want? => "))


for i in range(0,n*2,2):
print (i)
5. Develop a program to read name and year of birth of a person.Print
whether the person is a senior citizen or not.

import datetime
name = input("Enter the name of the person : ")
birthyear = int(input("Enter your birth year: "))
today = datetime.date.today()
year = today.year
age = year - birthyear
if age >=60 :
print(name,"is a senior citizen")
else :
print(name,"is not a senior citizen")

6. Read n numbers from console and create a list .Develop a program to


print their mean, variance and standard deviation.

num=[]
x = int (input("How many input values do you have ? => "))
for i in range (x):
y = int(input("Enter you value : "))
num.append(y)
def mean(num):
a=0
for ch in num:
a += ch
a = a/len(num)
return a
def variance(num):
c =0
a = mean(num)
for ch in num:
b = (ch-a)**2
c+=b
c = c /len(num)
return c
def stddev(num):
a = variance (num)
a = a**0.5
return a
print("Mean :",mean(num))
print("Variance :",variance(num))
print("Standard deviation:",stddev(num))
7. Develop a program to swap cases of a given string.

def alternator(n):
a = ''
for ch in n :
if ch.isupper():
a+= ch.lower()
else:
a+= ch.upper()
return a
line = input("Enter a string : ")
print ("\nyour string:",line)
print("Modified string :",alternator(line))

8. Develop a program to add elements to a dictionary using while loop.

spam = {}
n = int (input("How many key-values do you want to add : "))
a = 1
while a<n+1:
key = input(f"{a}.Key: ")
val = input(f"{a}.Value: ")
print('\n')
spam[key] = val
a +=1
print(spam)

9. Write a program to count the occurrences of characters in a string and


print output.

import pprint
count = {}
line = input ("Enter your string : ")
for ch in line:
if ch in count:
count[ch]+=1
else:
count[ch] =1
print("\nThe count of occurrences is as follows:")
pprint.pprint(count)

10. Develop a program to read n numbers from console and compute ‘mean’.

def mean(n):
a=0
for ch in n:
a+= ch
a =a/len(n)
return a
num = []
n = int (input("How many numbers do you have ? => "))
for i in range (n):
a = int(input("Enter your digit : "))
num.append(a)
print(num)
print ("Mean : ", mean(num))
11. Write a program that repeatedly asks the user for their age and name
until they provide a valid input.

while True :
name = input("Enter your name :")
age = input("Enter your age : ")
if name.isalnum() :
if age.isdecimal():
break
else:
print("Please a enter a valid input")
print("(age must be a number)\n")
else:
print("Please enter a valid input")
print("(name can be letters and numbers)\n")

12. Write a program that finds missing numbers from the given list of
number.(There are no duplicates).

def finder(n):
return sorted(set(range(n[0],n[-1]))-set(n))
num = []
n = int (input("How many numbers do you have ? => "))
for i in range (n):
a = int(input("Enter your digit : "))
num.append(a)
num.sort()
print (num)
print ("Missing numbers :",finder(num))

13. Develop a program to find out whether a given input is palindrome or


not.

line = input("Enter your string : ")


if line ==line[::-1]:
print("it is a palindrome")
else :
print ("it is not a palindrome ")

14. Write a program that accepts a sentence and finds the number of
words, digits, lowercase letter and uppercase letters.

line = input ("Enter your string: ")


word,up,low,num =0,0,0,0
for ch in line:
if ch.isalnum():
word+=1
if ch.isupper():
up+=1
if ch.islower():
low+=1
if ch.isdecimal():
num+=1
print("number of words :",word)
print("number of uppercase letters :",up)
print("number of lowercase leters :",low)
print("number of digits:",num)
15. Read a multi-digit number from console. Develop a program to find
frequency of each digit.

count = {}
digit = input("Enter a multi-digt number : ")
for ch in digit :
if ch in count:
count[ch]+=1
else:
count[ch]=1
print("the frequency of respective digits is as follows")
print(count)

16. Write a program to accept a string and display he total total number
of alphabets.

line = input ("Enter a string : ")


count = 0
for ch in line :
if ch .isalpha():
count+=1
print( "Total number of alphabets:",count)

17.write a program to make a new string with all the vowels eliminated
from the string , read string from the user.

line = input ("Enter a string : ")


new = ""
for ch in line:
if ch not in "aeiou":
new+=ch
print("\nYour string:",line)
print("your string after elimination :",new)

You might also like