In [9]:
#Program 1
def avgofnums(nums):
s=0
count=0
for i in range(n):
if list1[i]%2==0:
s+=list1[i]
count+=1
return s/count
n=int(input("Enter the number of list elements : "))
list1=[]
print("Ente the elements : ")
for i in range(n):
num=int(input(""))
list1.append(num)
print(list1)
avg = avgofnums(list1)
print("The Average is : ",avg)
Enter the number of list elements : 5
Ente the elements :
1
2
3
4
5
[1, 2, 3, 4, 5]
The Average is : 3.0
In [11]:
#Program 2
import math
def circle_area(r):
return math.pi * r**2
def rectangle_area(l,w):
return l*w
def triangle_area(b,h):
return 0.5*b*h
print("Select a geometric shape.")
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
choice = int(input("Enter the choice : "))
if choice==1:
r=float(input("Enter the circle radius : "))
area = circle_area(r)
elif choice==2:
l=float(input("Enter the rectangle length : "))
w=float(input("Enter the rectangle width : "))
area = rectangle_area(l,w)
elif choice==3:
b=float(input("Enter the triangle base : "))
h=float(input("Enter the triangle height : "))
area = triangle_area(b,h)
else:
print("Invalid choice!")
print("The Area is : ",area)
Select a geometric shape.
1. Circle
2. Rectangle
3. Triangle
Enter the choice : 3
Enter the triangle base : 2
Enter the triangle height : 3
The Area is : 3.0
In [12]:
#Program 3
def trafficLight(istring):
if istring =="Red" or istring=="Yellow" or istring=="Green":
print("SPEED THRILLS BUT KILLS")
else:
print("Error!")
def light(istring):
if istring=="Red":
return 0
elif istring=="Yellow":
return 1
elif istring=="Green":
return 2
else:
return
istring=input("Enter a string : ")
trafficLight(istring)
value=light(istring)
if value==0:
print("STOP, your life is precious")
elif value==1:
print("Please WAIT, till the light is Green")
elif value==2:
print("GO! Thank you for being patient")
else:
print("Error!")
Enter a string : Red
SPEED THRILLS BUT KILLS
STOP, your life is precious
In [16]:
#Program 4
def fact(n):
if (n==1 or n==0):
return 1
else:
return (n*fact(n - 1))
num=int(input("Enter a number : "))
print("Factorial : ",fact(num))
Enter a number : 5
Factorial : 120
In [17]:
#Program 5
def myMean(list2):
mean = sum(list2) / len(list2)
return mean
fnum = [2.5, 3.0, 4.5, 1.8, 2.2]
result = myMean(fnum)
print("Mean:", result)
Mean: 2.8
In [20]:
#Program 6
def full_name(first, last):
full = first +" "+ last
return full
first = input("Enter your first name: ")
last = input("Enter your last name: ")
result = full_name(first, last)
print("Full Name:", result)
Enter your first name: Tony
Enter your last name: Stark
Full Name: Tony Stark
In [1]:
#Program 7
def fib(limit):
list3 = []
a=0
b=1
while a <= limit:
list3.append(a)
a= b
b=a + b
return list3
limit = 50
fibonacci = fib(limit)
print("Fibonacci series : ",fibonacci)
Fibonacci series : [0, 1, 2, 4, 8, 16, 32]
In [10]:
#Program 8
def palindrome(s):
s=s.lower()
if (s == s[::-1]):
return 1
else:
return 0
istring = "Radar"
result = palindrome(istring)
if result==1:
print("Given string is a palindrome.")
else:
print("Given string is NOT a palindrome.")
Given string is a palindrome.
In [11]:
#Program 9
def reverse(number):
rnum = 0
while number > 0:
digit = number % 10
rnum = rnum * 10 + digit
number //= 10
return rnum
num=int(input("Enter a number to reverse : "))
output = reverse(num)
print(f"Reversed number : ",output)
Enter a number to reverse : 123
Reversed number : 321
In [14]:
#Program 10
import random
import string
def generate(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
length=int(input("Enter the password length : "))
password = generate(length)
print("Random Password: ",password)
Enter the password length : 5
Random Password: #XKXB