FUNCTIONS in Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

FUNCTIONS

Function is a group of related statements that


perform a specific task.

Syntax: Def function_name(parameters):

Example 1-
def hello():
print(“how are you”)
hello()

Example 2-
def greet(name):
print(“hello”,name)
print(“how are you”)
greet(“Rose”)

Passing more than one arguments to the function


Example 1-
def add(n1,n2):
sum=n1+n2
print(“the sum is”,sum)
def dif(n1,n2):
dif=n2-n1

number_a=5
number_b=6
add(number_a,number_b)

example 2-
def add_numbers(n1,n2):
sum=n1+n2
return sum
number1=5
number2=10
result=add_numbers(number1,number2)
print(“the sum is”,result)

BUILT IN FUNCTIONS
1. Float(),int(),input(),type()
2. Len()
Example- marks=[55,64,50,45]
Length=len(marks)
Print(“length is ”,length)
3. sum()
a=sum(marks)
print(“total marks”,a)

4. round()
eg:-

print(round(15))
print(round(51.6))
print(round(54.3))

print(round(2.665,2) ) - 2.67
print(round(2.676,2)) -2.68
print(round(2.592,2)) -2.60

One function call within another function.


def fun2():
print ("iam fine")
def fun1(): # function definition
print ("how are you")
fun2() # calling fun2() from fun1()
fun1() # calling a function

example 2
def calc(a, b):
sum(a, b)
diff(a, b)
mult(a,b)
div(a,b)
def sum(x, y):
c=x+ y
print (“sum= ",c)

def diff(x, y):


c=x-y
print("d= ",c)

a=int(input())
b=int(input())
calc(a,b)

Example 3
import math
def pri_Twice(bruce):
print(bruce, bruce)

pri_Twice('Spam')
pri_Twice(5)
pri_Twice(3.14159)
pri_Twice('Spam'*4)
pri_Twice(math.cos(math.pi))

output
Spam Spam
55
3.14159 3.14159
SpamSpamSpamSpam SpamSpamSpamSpam
-1.0 -1.0

Recursive functions
To find the factorial of a number using recursion-

def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))

num =int(input("enter the number"))


print("The factorial of", num, "is", factorial(num))
What is recursion? Write a python program to calculate nCr.
Use a recursive function fact( ) to find the factorial of a number.
[nCr=n!/(r! x (n-r)!)]

def nCr(n, r):

return (fact(n) / (fact(r)


* fact(n - r)))

def fact(n):

res = 1

for i in range(2, n+1):


res = res * i

return res

n = int(input("enter n"))
r = int(input("enter r"))
print(int(nCr(n, r)))

Differentiate between int () and round() functions in python


Write the output of the following program fragment.
def check(x,y):
if y==0:
print (“error” )
return
else:
return(x/y)

a,b=2,4
print (check(a,b))

Output
0.5

Write a Python code using function to find the quadrant of a given


point (x, y)

5,0
def quadrant(x, y):
if (x > 0 and y > 0):
print ("lies in First quadrant")

elif (x < 0 and y > 0):


print ("lies in Second quadrant")

elif (x < 0 and y < 0):


print ("lies in Third quadrant")

elif (x > 0 and y < 0):


print ("lies in Fourth quadrant")
elif (x == 0 and y > 0):
print ("lies at positive y axis")

elif (x == 0 and y < 0):


print ("lies at negative y axis")

elif (y == 0 and x < 0):


print ("lies at negative x axis")

elif (y == 0 and x > 0):


print ("lies at positive x axis")

else:
print ("lies at origin")

x = int(input())
y = int(input())
quadrant(x, y)

Write a menu driven Python program to input a number and


implement the following operations. Use separate functions to
implement each operation. i) check whether the number is odd or
even ii) check whether the number is positive, negative or zero
iii) generate factors of the number
def odd_even(x)
input
if(x%2==0)
print(“the number is even”)
else
def pos_neg(x)

a=int(input()
y= int (input(“press 1 to find whether the no is odd or even\n press 2
”)
if y==1
def odd_even(a)
elif y==2
def
else
def

10-1,10,2,5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy