Python Pro Lab
Python Pro Lab
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")
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")
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))
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)
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))
14. Write a program that accepts a sentence and finds the number of
words, digits, lowercase letter and uppercase letters.
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.
17.write a program to make a new string with all the vowels eliminated
from the string , read string from the user.