*Memos:
- My post explains positional-only parameters in function.
- My post explains keyword-only parameters in function.
- My post explains positional-only parameters and keyword-only parameters together in function.
- My post explains Lambda.
- My post explains variable assignment.
A function:
- can do a specific task.
- can reuse and reduce code.
- must have a
def
, identifier and():
. - can have zero or more parameters.
- can have one or more return statements to return a value.
- can have one or more pass statements,
return
statements and values. - must have code.
- can be indirectly assigned to a variable but not directly.
Code is more if not using a function as shown below:
num1 = 3
num2 = 5
print(num1+num2) # 8
num1 = 7
num2 = 2
print(num1+num2) # 9
num1 = 1
num2 = 4
print(num1+num2) # 5
Code is less if using a function as shown below:
def func(num1, num2):
return num1+num2
print(func(3, 5)) # 8
print(func(7, 2)) # 9
print(func(1, 4)) # 5
Zero or more parameters can be set as shown below:
def func():
print("No param")
func() # No param
def func(p1):
print(p1)
func("p1") # p1
def func(p1, p2):
print(p1, p2)
func("p1", "p2") # p1 p2
def func(p1, p2, p3):
print(p1, p2, p3)
func("p1", "p2", "p3") # p1 p2 p3
One or more return
statements can be used as shown below:
def func():
return "Hello"
print(func()) # Hello
def func():
return [0, 1, 2]
print(func()) # [0, 1, 2]
def func():
return "Hello"
return [0, 1, 2]
print(func()) # Hello
def func():
return [0, 1, 2]
return "Hello"
print(func()) # [0, 1, 2]
def func():
return
print(func()) # None
You can use one or more pass
statements, return
statements or values to do nothing in a function, returning None
as shown below:
*Memos:
- Conventionally, a
pass
statement is used for the function to do nothing. - The function with no code gets error.
def func():
pass
print(func()) # None
def func():
pass
pass
print(func()) # None
def func():
return
print(func()) # None
def func():
return
return
print(func()) # None
def func():
"Hello"
print(func()) # None
def func():
"Hello"
"World"
print(func()) # None
def func():
100
print(func()) # None
def func():
100
200
print(func()) # None
def func(): # SyntaxError: incomplete input
# No code
You can pass a class and function to a function as shown below:
class mycls:
v = "Hello"
def myfn():
return "World"
def func(cls, fn):
print(cls.v, fn())
func(mycls, myfn) # Hello World
You can define a class and function in a function as shown below:
def func():
class mycls:
v = "Hello"
def myfn():
return "World"
print(mycls.v, myfn())
func() # Hello World
You can write a function in one line as shown below:
def func(num1, num2): return num1+num2
print(func(3, 5)) # 8
def func(): return
print(func()) # None
def func(): pass
print(func()) # None
You can indirectly assign a function to a variable but you cannot directly assign a function to a variable like JavaScript except Lambda as shown below:
def func(num1, num2):
return num1+num2
v = func
print(v(3, 5)) # 8
v = def func(num1, num2):
return num1+num2
# SyntaxError: invalid syntax
v = func(num1, num2):
return num1+num2
# SyntaxError: invalid syntax
v = lambda num1, num2: num1+num2
print(v(3, 5)) 8
Top comments (0)