0% found this document useful (0 votes)
15 views32 pages

Functions

The document contains various Python code snippets demonstrating the use of functions, including mathematical operations using the math module, random number generation, time functions, and user-defined functions for tasks like checking for prime numbers and implementing a menu-driven calculator. It covers topics such as importing modules, defining functions, and using control structures. Overall, it serves as a practical guide for implementing functions in Python programming.

Uploaded by

KASHISH MADAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Functions

The document contains various Python code snippets demonstrating the use of functions, including mathematical operations using the math module, random number generation, time functions, and user-defined functions for tasks like checking for prime numbers and implementing a menu-driven calculator. It covers topics such as importing modules, defining functions, and using control structures. Overall, it serves as a practical guide for implementing functions in Python programming.

Uploaded by

KASHISH MADAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

WORKING WITH

FUNCTIONS
# FUNCTIONS USING MATH MODULE
from math import sqrt

x=16

print(sqrt(16))

print(sqrt(x))

print(sqrt(2*x-5))

y=sqrt(x)

print(y)

y=2*sqrt(x+16)-4

print(y)

y=sqrt(sqrt(256.0))

print(y)

print(sqrt(int('45')))
from math import sqrt

b=sqrt(4)

print(b)

from math import *

b=ceil(4.25)

print(b)

floorvalue=floor(4.25)

print(floorvalue)

import math

print(math.e)

print(math.pi)

expval=math.exp(-45.78)

print(expval)

expval=math.exp(45.78)

print(expval)

logval=math.log(47.8)

print(logval)

powval=pow(3,4)

print(powval)

powval=pow(4,3)
print(powval)

import math

x=math.pow(3,0)

print(x)

x=math.pi

print(x)
#FUNCTIONS USING RANDOM MODULE
import random

x=random.randint(1,9)

print(x)

y=random.randint(-10,-1)

print(y)
from random import randint

def generator():

return randint(1,10)

def rand_guess():

random_number=generator() # Function calling

guess_left=3

flag=0

while guess_left>0:

guess=int(input("Pick your number to enter the lucky draw\n"))

if guess==random_number:

flag=1

break

else:

print("Wrong guess")

guess_left=-1# guest_left=guest_left-1

if flag is 1:

return True

else:

return False

if __name__== '__main__':
if rand_guess is True:

print("Congrats u win")

else:

print("sorry u lost")

rand_guess()
#USE OF UNIFORM-
import random
a=4
b=9
print("The random number generated between 4 and 9
is ",end="")
print(random.uniform(a,b))
# MATH MODULE
import math
test_int=4
test_neg_int=-3
test_float=0.00
print(math.exp(test_int))
print(math.exp(test_neg_int))
print(math.exp(test_float))
import math

from math import sin,cos,sqrt,pi

p_x=100

p_y=0

degrees=10*pi/180

cos10=cos(degrees)

sin10=sin(degrees)

x,y=eval(input("Enter initial Satellite coordinates(x,y):"))

d1=sqrt((p_x-x)*(p_x-x)+(p_y-y)*(p_y-y))

x_old=x

x=x*cos10-y*sin10

y=x_old*sin10+y*cos10

d2=sqrt((p_x-x)*(p_x-x)+(p_y-y)*(p_y-y))

print("Difference in distances.%.3f"%(d2-d1))
#FUNCTIONS USING TIME MODULE
def checkOdds():

from datetime import datetime

odds=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59]

minute=datetime.today().minute

if minute in odds:

print("This minute seem to be odd")

else:

print(" nothing to be shown")

checkOdds()

import time
start = time.time()

from math import sin,cos,sqrt,pi

p_x=100

p_y=0

degrees=10*pi/180

cos10=cos(degrees)

sin10=sin(degrees)

x,y=eval(input("Enter initial Satellite coordinates(x,y):"))

d1=sqrt((p_x-x)*(p_x-x)+(p_y-y)*(p_y-y))

x_old=x

x=x*cos10-y*sin10

y=x_old*sin10+y*cos10

d2=sqrt((p_x-x)*(p_x-x)+(p_y-y)*(p_y-y))

print("Difference in distances.%.3f"%(d2-d1))

end = time.time()

print(end - start)
#FUNCTION TO FIND VOWELS
def vowels():

vowels=['a','e','i','o','u']

word='Milliways'

for letter in word:

if letter in vowels:

print (letter)

vowels()
def timeaddition():

sum=0

import time

start=time.time()

for i in range(1,1000):

sum+=i

end=time.time()

elapsed=end-start

print("sum: ",sum,"time:", elapsed)

timeaddition()
#USE OF SLEEP FUNCTION
from time import sleep
for count in range(10,-1,-1):
print(count)
sleep(1)
#USE OF SEED FUNCTION
from random import randrange,seed,randint

seed(23)

for i in range(0,100):

print(randint(1,1000),end=" ")

print()
#RANDRANGE
from random import randrange

for i in range(0,3):# (0,1,2,3)

# i

value=randrange(1,6)

print("+-------+")

if value==1:

print("| |")

print("| * |")

print("| |")

if value==2:

print("|* |")

print("| |")

print("| *|")

if value==3:

print("| *|")

print("| * |")

print("|* |")

if value==4:

print("|* *|")
print("| |")

print("|* *|")

if value==5:

print("|* *|")

print("| * |")

print("|* *|")

if value==6:

print("|* *|")

print("|* *|")

print("|* *|")

else:

print("illegal value")

print("+-------+")
def increment(x):
print("Beginning execution of increment,x= ",x)

x+=1

print("Ending execution of increment,x=",x)

def main():

x=5

print("Before increment, x=",x)

increment(x)

print("After increment,x=",x)

main()
#FUNCTION TO PRINT PRIME NUMBERS
def is_prime(n):

import math

result=True

root=math.sqrt(n)

trial_factor=2

while result and trial_factor<=root:# True and 3<=2

result=(n%trial_factor!=0)# result=False

trial_factor+=1

return result# False

def main():

max_value=int(input("Display primes upto what value?"))

for val in range(2,max_value+1):# 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18

if is_prime(val):

print(val,end=" ")

print()

main()
#FUNCTION OF MENU DRIVEN CALCULATOR
def help_screen():

print(" Add: Add two numbers")

print("Substract: Substract two numbers")

print("Print: Displays the result of the latest operation")

print("Help: Display this help screen")

print("Quit Exits the program")

def menu():

return input("===A)dd S)ubtract P)rint H)elp")

def main():

result=0.0

done=False;

while not done:

choice=menu()

if choice=="A" or choice=="a":

arg1=float(input("Enter arg1: "))

arg2=float(input("Enter arg2: "))

result=arg1+arg2

print(result)

elif choice=="S" or choice=="s":


arg1=float(input("Enter arg1: "))

arg2=float(input("Enter arg2: "))

result=arg1-arg2

print(result)

elif choice=="P" or choice=="p":

print(result)

elif choice=="H" or choice=="h":

help_screen()

elif choice=="Q" or choice=="q":

done=True

main()
#FUNCTION OF RANDOM NUMBER
def get_init_in_range(first,last):# (10,5)

if first>last:

first,last=last,first

in_value=int(input("Please enter values in range\n"+str(first) +"..."+str(last)))

while in_value<first or in_value>last:

print(in_value," is not in the range",first,"...",last)

in_value=int(input("Please try again later"))

return in_value

print(in_value)

def main():

print(get_init_in_range(10,20))

print(get_init_in_range(20,10))

print(get_init_in_range(5,5))

print(get_init_in_range(-100,100))
main()
#USE OF GLOBAL
def help_screen():

print(" Add: Add two numbers")

print("Substract: Substract two numbers")

print("Print: Displays the result of the latest operation")

print("Help: Display this help screen")

print("Quit Exits the program")

def menu():

return input("===A)dd S)ubtract P)rint H)elp Q)uit")

result=0.0

arg1=0.0

arg2=0.0

def get_input():

global arg1,arg2

arg1=float(input("Enter arg1: "))

arg2=float(input("Enter arg2: "))


def report():

print(result)

def add():

global result

result=arg1+arg2

def subtract():

global result

result=arg1-arg2

def main():

done=False;

while not done:

choice=menu()

if choice=="A" or choice=="a":

get_input()

add()

report()

elif choice=="S" or choice=="s":

get_input()

subtract()

report()
elif choice=="P" or choice=="p":

report()

elif choice=="H" or choice=="h":

help_screen()

elif choice=="Q" or choice=="q":

done=True

main()
# FUNCTION AS DATA
def add(x,y):
''' Adds the parameter x and y and return the result'''
return x+y
def multiply(x,y):
''' Multiplies the parameters x and y values and returns the result'''
return x*y
def evaluate(f,x,y):
'''Test the add , multiply and evaluate functions'''

return f(x,y)
def main():
print(add(2,3))
print(multiply(2,3))
print(evaluate(add,2,3))
print(evaluate(multiply,2,3))
main()
# PROGRAM USING APPEND

l=[]
l1=[]
l2=[]
for i in range(1,10):
l.append(i)
for i in range(10,1,-2):
l1.append(i)
for i in range(len(l1)):
l2.append(l1[i]+l[i])
l2.append(len(l)-i+1)
print(l2)

You might also like