Functions
Python Functions is a block of statements that return the specific task.
Example: A function that checks even and odd number.
def check(n):
if n%2==0:
return "Even"
else:
return "Odd"
print(check(5))
Types of Function Arguments
1. Keyword Argument
- No need to remember the order of argument.
- Use argument name and provide in any order.
>>> def fun(first, second):
... return first+second
...
>>> fun(second="banana", first="apple")
'applebanana'
2. Default Argument
- A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.
>>> def fun(a,b=5):
... return a+b
...
>>> fun(4)
9
3. Variable Length Arguments
>>> def fun(*args, **kwargs):
... for arg in args:
... print(arg)
... for key, value in kwargs.items():
... print(f"{key} - {value}")
...
>>> fun(1,2,3, four=4, five=5)
1
2
3
four - 4
five - 5
4. Required Argument
- Required arguments are those that are required to be passed to the function at the time of function call.
>>> def fun(a, b):
... return a+b
...
>>> fun(1,2)
3
>>> fun(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: fun() missing 1 required positional argument: 'b'
Lambda Function
A lambda function is typically used as a small, anonymous function.
square = lambda x: x**2
square(2)
numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
print(list(squares)) # Output: [1, 4, 9, 16, 25]