Akshay Python 10-11
Akshay Python 10-11
#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
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
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!")
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
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!")
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
In [20]:
#Program 6
In [1]:
#Program 7
def fib(limit):
list3 = []
a=0
b=1
limit = 50
fibonacci = fib(limit)
print("Fibonacci series : ",fibonacci)
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.")
In [11]:
#Program 9
def reverse(number):
rnum = 0
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