Python - Function and Modules

 

Python

Function  and Modules



Fill in the blanks

1.    : (colon)  symbol to mark the end of function header

2.   Write Function keyword  def

3.   def a(n):

print(“hai”+’  ’+n).

         what is the output of a(‘roja’)=   hai  roja

          4.The first string after the function header is called Docstring

          5.def fun(name,msg):

                   print(“hai”,name,msg)

what is the output fun(msg=’have a nice day’,name=’karthick’)

hai karthick have a nice day

6.Write a symbol for Arbitrary Argument   *  

7.Function call itself is called Recursive Function

8.Anonymous function/Lambda keyword lambda

9.We can use already saved python files using  import .[T/F]. True

10.  double = lambda x:x*3. print(double(4)) =  12

11. A File containing Python statement and definitions are called Modules

12.Write a Module for specifying path in python are  import sys

13. c=8+3j. print(isinstance(c,complex)) = True

14.Write a Module for Fraction import fractions

15.print(fraction.Fraction(‘1.1’))  =   11/10

16.print(1/F(4,7)) = 7/4

17.Write random() function module  import random

18. Write mathematical function module  import math

19.Write decimal function module  import decimal

20.We can rename modules.[T/F].  True

 

  Answer the following Question

                                                                              (4 marks)

                1.Write a program for Arithmetic Operation using Modules

jksample.py

def jkaddition(x,y):
    a=x+y

    return(a)

def jksubtraction(x,y):
    a=x-y

    return(a)

def jkmultiplication(x,y):
    a=x*y

    return(a)

def jkdivision(x,y):
    a=x/y

    return(a)

how to call from >>> prompt

call from promt

>>>import jksample

>>>print jkmultiplicatiom(3,4)

12

>>>print jkaddition(100,200)

300

>>>print jkdivision(20,2)

10

>>>print jksubtraction(30,2)

28

>>> 

 

          2.Write a program for square value of number

def sample(a) :

c=a*a

return (c)

 

#function call

n=int(input("enter a number:"))

res=sample(n)

print("the square value of %d is %d "%(n,res))

          3.Write a program to find factorial using function

def factorial(num):

fact=1

for i in range(1, num+1):

fact=fact*i

return fact

number=int(input("Please enter any number to find factorial: "))

result=factorial(number)

print("The factorial of %d = %d"%(number,result))

          4.Write a program Reverse a digit using function

num = int(input("Enter a number: "))

reverse = 0

while num > 0:

    rem = num % 10            

    reverse = reverse * 10 + rem   

    num //= 10                     

print("Reversed number: ", reverse)

          5.Write a program for default argument using function

def welcome_func(name , msg = "good morning !"):

"""

this function welcomes to

the person with the

provided message .

 

if messege is not provided,

it defaults to "good morning!"

"""

print ("hello",name + ','+msg)

welcome_func("kumerasan")

welcome_func("prakash", "have a nice day")

          6.Write a program for Recursive Function

Function call itself is called Recursive Function

 

def calc_factorial(x):

"""this is a recursive function

to find the factorial of an integer """

    if x==1:

        return 1

    else:

        return (x*calc_factorial(x-1))

num=4

print("the factorial of ",num,"is",calc_factorial(num))

 

          7.Write a program for Arbitrary Argument function

def arb_func(*names):

"""this function welcomes all

the person in the names tuple."""

#names is a tuple with arguements

    for na in names :

          print("hello",na)

arb_func("priyadharshini","radhika","samantha","jheevikha")

          8.Write a program for random() function.

import random

#output: 16

print(random.randrange(10,20))

x=['a','b','c','d','e']

#get random choice

print(random.choice(x))

#shuffle x

random.shuffle(x)

#print the shuffled x

print(x)

#print random element

print(random.random())

 

Answer the following Question

                                                                              (10 marks)

          1.Write a Program to find Simple interest and Compound interest using function

def  Simple_interest(p,n,r):

       res=(p*n*r)/100

       return(res)

def  Compound_interest(p,n,r):

       res=p*(1+(r/100))**n

       return(res-p)

#Function call

p=int(input(“Enter Principle Amount:”))

n=int(input(“Enter No.of Years:”))

r=int(input(“Enter Rate of interest:”))

print(“Simple Interest value=”,Simple_interest(p,n,r))

print(“Compound Interest value=”,Compound_interest(p,n,r))

          2.Write a program for Mathematical function

import math

#output: 3.141592653589793

print(math.pi)

#output: -1.0

print(math.cos(math.pi))

#output: 22026.465794806718

print(math.exp(10))

#output: 3.0

print(math.log10(1000))

#output: 1.1752011936438014

print(math.sinh(1))

#output:  720

print(math.factorial(6))

No comments:

Post a Comment