[go: up one dir, main page]

0% found this document useful (0 votes)
96 views9 pages

Python Assignment 6: Functions

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

PYTHON ASSIGNMENT 6

FUNCTIONS

1. Write a function that takes three numbers as parameters,


and returns the median value of those parameters as its result.
Include a main program that reads three values from the user
and displays their median.

INPUT (TEXT):
a = float(input("Input first number: "))
b = float(input("Input second number: "))
c = float(input("Input third number: "))
def median(a,b,c):
if a > b > c or c > b > a or a == b:
m = b
elif c > a > b or b > a > c or a == c:
m = a
else:
m = c
print("The median is ",m)
median(a,b,c)

OUTPUT (IMAGE):

2. Python Program to Find the Sum of Sine Series

INPUT (TEXT):
import math
def sin(x,n):
sine = 0
for i in range(n):
sign = (-1)**i
pi = 22/7
y = x*(pi/180)
sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign
return sine
x=int(input("Enter the value of x in degrees: "))
n=int(input("Enter the number of terms: "))
print("sin(",x,") =",round(sin(x,n),2))

OUTPUT (IMAGE):

3. Write a program to print all the odd numbers between


two ranges (Include the range too):

INPUT (TEXT):
l_limit = int(input("Enter the lower limit for the range: "))
u_limit = int(input("Enter the upper limit for the range: "))
def odd(l_limit,u_limit):
for i in range(l_limit,u_limit+1):
if i % 2 != 0:
print(i)
odd(l_limit,u_limit)

OUTPUT (IMAGE):
4. Write a main program that reads an integer from the user
and displays a message indicating whether or not it is prime:

INPUT (TEXT):
num = int(input("Enter a number: "))
def prime(x):
a=0
for i in range(1,x+1):
if (x%i==0):
a=a+1
if a==2:
print ("TRUE")
else:
print("FALSE")
prime(num)

OUTPUT (IMAGE):

5. Write a main program that reads a string from the user


and reports whether or not it represents an integer:

INPUT (TEXT):
a=input()
def is_Integer(x):
l=list(x)
c=0
for i in range(len(l)):
ascii=ord(l[i])
if (ascii>=48 and ascii<=57):
c=c+1
if c>0:
print("Contains Integer")
else:
print("Does not Contain")
is_Integer(a)
OUTPUT (IMAGE):

6. Include a main program that reads the number of items


purchased from the user and displays the shipping charge:

INPUT (TEXT):
a=int(input("Enter number of items: "))
def cost(x):
if x==1:
print(750)
else:
print(750+(x-1)*200)
cost(a)

OUTPUT (IMAGE):

7. Python Program to Find the Sum of First N Natural


Numbers:

INPUT (TEXT):
a=int(input("Enter any natural number: "))
def sum(x):
print(int(x*(x+1)/2))
sum(a)

OUTPUT (IMAGE):
8. Python function to calculate the GRADE systems of marks:

INPUT (TEXT):
a,b,c,d,e = [int(x) for x in input("Input 5 numbers: ").split()]

def grade(v,w,x,y,z):
avg=(v+w+x+y+z)/5
if avg>=90:
print("Grade:A")
elif avg>70:
print("Grade:B")
elif avg>50:
print("Grade:C")
else:
print("Grade:D")
grade(a,b,c,d,e)

OUTPUT (IMAGE):

9. Python function to calculate the LCM of numbers:

INPUT (TEXT):
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
def hcf(a, b):
while(b):
a, b = b, a % b
return a
def lcm(a,b):
lcm = a*b/hcf(a,b)
return lcm
print("The LCM is: ",lcm(a,b))

OUTPUT (IMAGE):
10. Python Program to Find the Gravitational Force Acting
Between Two Objects:

INPUT (TEXT):
m1 = int(input("Value of first mass(in kg): "))
m2 = int(input("Value of second mass(in kg): "))
r = int(input("Distance between the centres of the masses(in m): "))
def gravity(m1,m2,r):
G = 6.67*10**-11
F = G*m1*m2/r**2
print("The gravitational force is",F,"N")
gravity(m1,m2,r)

OUTPUT (IMAGE):

11. Python Program to Find the Sum of the Series: 1 + 1/2 +


1/3 + ...+ 1/N:

INPUT (TEXT):
n=int(input("Enter the number of terms: "))
def inv_sum(n):
sum1=0
for i in range(1,n+1):
sum1=sum1+(1/i)
sum = round(sum1,2)
print("The sum of series is",sum)
inv_sum(n)

OUTPUT (IMAGE):
12. Write a Python Program to Check if a Numberis a Perfect
Number:

INPUT (TEXT):
n = int(input("Enter any number: "))
def perf(n):
sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")
perf(n)

OUTPUT (IMAGE):

13. Python program for calculating GST taxi fares:

INPUT (TEXT):
dist = int(input("Enter distance travelled(in km): "))
def fare(dist):
fare = 200 + ((1000*dist/140)*8)
print(fare)
fare(dist)

OUTPUT (IMAGE):
14. Python program that determines whether or not a
password is good:

INPUT(TEXT):
import re
password = str(input("Enter password: "))
def good(password):
flag = 0
while True:
if (len(password)<8):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[_@$]", password):
flag = -1
break
elif re.search("\s", password):
flag = -1
break
else:
flag = 0
print("Good Password")
break

if flag ==-1:
print("Try Again")
good(password)

OUTPUT(IMAGE):
15. Write a python function to calculate the GCD of numbers:

INPUT(TEXT):
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
def hcf(a, b):
while(b):
a, b = b, a % b
return a
print("The GCD of the two numbers is",hcf(a,b))

OUTPUT(IMAGE):

SUBMITTED BY: - Jay Deep Singh


ROLL NO.: - 211020421
BRANCH: - DSAI

You might also like