Definition and Defining Functions
A function represents some part of the code which can be executed only when required .In python, user defines the function by using def.
def sum(a,b):print(a+b)def sum(a,b): print(a+b)def sum(a,b): print(a+b)
Enter fullscreen mode Exit fullscreen mode
in this example we have performed a sum of two integers a,b. so.. whenever we require to sum two numbers, we can directly use the function sum(a,b).
Calling a Function
In Python, calling a function means executing the code defined inside a function block by using the function’s name followed by parentheses. Here is an example for how calling is done in python:
def sum(a,b):print(a+b)sum(1,3)def sum(a,b): print(a+b) sum(1,3)def sum(a,b): print(a+b) sum(1,3)
Enter fullscreen mode Exit fullscreen mode
here,
- a and b are the parameters in the function definition.
- 1, 3 are the arguments passed to the function.
- sum is the name of the function
Generally, there are 4 types of Arguments:
1.Required Arguments
Required arguments are the parameters that a function must receive when it is called. If you do not provide the required arguments in a function call, Python will raise a TypeError
because the function will not have the necessary information to execute.
sum(a,b):print(a+b)sum(1,3)sum()sum(a,b): print(a+b) sum(1,3) sum()sum(a,b): print(a+b) sum(1,3) sum()
Enter fullscreen mode Exit fullscreen mode
Output: 4
TypeError
Here:
-
a
andb
are required arguments because they do not have a default value. - So, when you call
sum
without an argument, Python raises an error.
2. Keyword Arguments
Keyword arguments allow you to pass arguments to a function by explicitly specifying the parameter names.
new_print(a,b):print("{a} is a friend of {b}")new_print(b="Alice" ,a="Bob")new_print(a,b): print("{a} is a friend of {b}") new_print(b="Alice" ,a="Bob")new_print(a,b): print("{a} is a friend of {b}") new_print(b="Alice" ,a="Bob")
Enter fullscreen mode Exit fullscreen mode
Output: Bob is a friend of Alice
Here:
- Since the Arguments are mentioned beforehand, in spite of wrong order, this code will produce this kind of result.
3. Default Arguments
Default arguments in Python allow you to define a function with default values for certain parameters. If a value for a parameter with a default is not provided during the function call, Python automatically assigns the default value. This makes functions more flexible and reduces the need to specify every parameter explicitly.
sum(a=0, b=0):print(a+b)sum()sum(1,3)sum(a=0, b=0): print(a+b) sum() sum(1,3)sum(a=0, b=0): print(a+b) sum() sum(1,3)
Enter fullscreen mode Exit fullscreen mode
Output: 0\n 4
In this example, even though, we have not assigned any variable to a, b since the a and b are assigned to a default value, it will give an output 0
4. Variable Length Arguments
Python provides a way to define functions that can accept a variable number of arguments. Variable-length arguments are handled using:
- *args (for non-keyword arguments)
- **kwargs (for keyword arguments)
1. *args:
*args allows a function to accept any number of positional arguments. Inside the function, these arguments are accessible as a tuple.
sum (*numbers):sum=0for i in numbers:sum+=iprint(sum)sum(1,2,3,4)sum (*numbers): sum=0 for i in numbers: sum+=i print(sum) sum(1,2,3,4)sum (*numbers): sum=0 for i in numbers: sum+=i print(sum) sum(1,2,3,4)
Enter fullscreen mode Exit fullscreen mode
Output: 10
2. **Kwargs:
**kwargs allows a function to accept any number of keyword arguments (arguments passed as key=value pairs). These are stored in a dictionary.
sum(**numbers):sum=0for keys,values in numbers:print("{keys}={numbers}")sum+= valuessum(**numbers): sum=0 for keys,values in numbers: print("{keys}={numbers}") sum+= valuessum(**numbers): sum=0 for keys,values in numbers: print("{keys}={numbers}") sum+= values
Enter fullscreen mode Exit fullscreen mode
Trick of the Day: 4 ways to swap the numbers
way 1:
P=5Q=4temp = PP = QQ = tempP=5 Q=4 temp = P P = Q Q = tempP=5 Q=4 temp = P P = Q Q = temp
Enter fullscreen mode Exit fullscreen mode
way 2:
P=5Q=4P,Q=Q,PP=5 Q=4 P,Q=Q,PP=5 Q=4 P,Q=Q,P
Enter fullscreen mode Exit fullscreen mode
way 3:
P=5Q=4P = P ^ QQ = P ^ QP = P ^ QP=5 Q=4 P = P ^ Q Q = P ^ Q P = P ^ QP=5 Q=4 P = P ^ Q Q = P ^ Q P = P ^ Q
Enter fullscreen mode Exit fullscreen mode
way 4:
P=5Q=4P = P + QQ = P - QP = P - QP=5 Q=4 P = P + Q Q = P - Q P = P - QP=5 Q=4 P = P + Q Q = P - Q P = P - Q
Enter fullscreen mode Exit fullscreen mode
原文链接:Day 4 :Everything You Need to Know About Functions in Python
暂无评论内容