0% found this document useful (0 votes)
8 views5 pages

Python Function Examples

Uploaded by

yashkarande2121
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)
8 views5 pages

Python Function Examples

Uploaded by

yashkarande2121
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

Def Function Examples

Name : Yash Karande


Roll No : 08
EX – 1)
def greet():

print("Hello! Welcome to Python.")

greet()

OUTPUT

Hello! Welcome to Python.

EX – 2)
def square(n):

print("Square:", n * n)

square(5)

OUTPUT

Square: 25

EX – 3)
def cube(n):

return n ** 3

print("Cube:", cube(3))

OUTPUT

Cube: 27

EX – 4)
def add(a, b):
return a + b

print("Addition:", add(10, 20))

OUTPUT

Addition: 30

EX – 5)
def welcome(name="Guest"):

print("Welcome", name)

welcome()

welcome("Yash")

OUTPUT

Welcome Guest

Welcome Yash

EX – 6)
def info(name, age):

print("Name:", name, "| Age:", age)

info(age=21, name="Alex")

OUTPUT

Name: Alex | Age: 21

EX – 7)
def calc(a, b):

return a+b, a-b, a*b

s, d, p = calc(10, 5)

print("Sum:", s, "Diff:", d, "Product:", p)

OUTPUT

Sum: 15 Diff: 5 Product: 50


EX – 8)
def total(*nums):

return sum(nums)

print("Total:", total(2, 4, 6, 8))

OUTPUT

Total: 20

EX – 9)
def fact(n):

if n == 0 or n == 1:

return 1

return n * fact(n-1)

print("Factorial:", fact(5))

OUTPUT

Factorial: 120

EX – 10)
def outer():

def inner():

return "Inner Function Called"

return inner()

print(outer())

OUTPUT

Inner Function Called


EX – 11)
def apply(func, value):

return func(value)

print(apply(lambda x: x**2, 6))

OUTPUT

36

EX – 12)
def multiplier(n):

return lambda x: x * n

double = multiplier(2)

print("Double:", double(10))

OUTPUT

Double: 20

EX – 13)
def average(marks):

return sum(marks)/len(marks)

print("Average:", average([80, 90, 70, 60]))

OUTPUT

Average: 75.0

EX – 14)
def reverse_string(s):

return s[::-1]

print("Reversed:", reverse_string("Python"))

OUTPUT

Reversed: nohtyP
EX – 15)

def list_sum(lst):

return sum(lst)

print("Sum of list =", list_sum([1,2,3,4,5]))

OUTPUT

Sum of list = 15

You might also like