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

2024-02-02T17-09-04.518Z-Day-4 - Functions

Uploaded by

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

2024-02-02T17-09-04.518Z-Day-4 - Functions

Uploaded by

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

Functions

By Aksadur Rahman
aksadur@yahoo.com
Functions
Functions
Parameters/Arguments
Keyword Parameters/Arguments
Default Parameter Value
Return Statement
Lambda Function
Map and Filter function
Zip Function
Recursion
Functions
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a
function. A function can return data as a result.

Built in function User define function

print("Hello World") def my_function():


print("Hello from a function")
mylist = ["apple", "banana", "cherry"]
x = len(mylist) my_function()

x = max(5, 10)

print('Enter your name:')


x = input()
print('Hello, ' + x)
Parameters/Arguments
def my_function(fname): #Parameters #This function expects 2 arguments, and gets 2 arguments:
print("Good morning! “, fname) def my_function(fname, lname):
print(fname, " “, lname)
my_function("Emil") #Arguments
my_function("Tobias") my_function("Emil", "Refsnes")
my_function("Linus")
Keyword Parameters/Arguments

#You can also send arguments with the key = value syntax.
#This way the order of the arguments does not matter.

def my_function(child3, child2, child1):


print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")


Default Parameter Value

def my_function(country = "Norway"):


print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Return Statement

def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))
Docstring

def Add(a, b):


'''
This will take two parameter
Return value is integer
'''
return a + b

print(Add(10, 20))
print(Add.__doc__)
Lambda Function
Python Lambda Functions are anonymous function means that the function is without a name.

(lambda parameters: expression) (arguments)


print((lambda a : a + 10) (5))

x = lambda a : a + 10
print(x(5))

x = lambda a, b, c : a + b + c
print(x(5, 6, 2))

def cube(y):
return y * y * y

lambda_cube = lambda y: y * y * y

print(cube(5))

# using the lambda function


print(lambda_cube(5))
Map and Filter function

def myfunc(a): def square(x):


return len(a) return x*x

x = map(myfunc, ('apple', 'banana', 'cherry')) num = [1, 2, 3, 4, 5]


print(list(x)) result = list(map(square, num))

print(result)
ages = [5, 12, 17, 18, 24, 32]
num = [5, 12, 17, 18, 25, 32]
def myFunc(x):
if x < 18: result = list(filter(lambda x: x%2==0, num))
return False
else: print(result)
return True print(num)

adults = list(filter(myFunc, ages))

print(adults)

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