25
loading...
This website collects cookies to deliver better user experience
def function_name(parameters):
"""docstring"""
statement(s)
return expression
def
keyword following the function name. Parameter(s)/ Argument(s) is the only way through which we can pass data to the function. We can add a docstring to explain the use of function and its parameter(s). A function can return a value if desire. It is not compulsory to return something.odd_or_even
. To call this function, I can write like odd_or_even()
. And if this function is expecting some argument(s), I can write it like odd_or_even(2)
.None
as the value.def sum_num(num1, num2):
total = num1 + num2
print(total)
sum_num(5,10)
# OUTPUT: 15
def sum_num(num1, num2):
total = num1 + num2
print(total)
temp = sum_num(5,10)
print(temp)
"""
OUTPUT:
15
None
"""
def sum_num(num1, num2):
total = num1 + num2
return total
temp = sum_num(5,10)
print(temp)
# OUTPUT: 15
3/2
then we will write divide(3,2)
.def divide(num1, num2):
return num1/num2
ans = divide(3,2)
print(ans)
# OUTPUT: 1.5
divide(2,3)
. This error can make the whole logic wrong.def divide(num1, num2):
return num1/num2
ans = divide(2,3)
print(ans)
# OUTPUT: 0.6666666666666666
def divide(num1, num2):
return num1/num2
ans = divide(num1=3, num2=2)
print(ans)
# OUTPUT: 1.5
def divide(num1, num2):
return num1/num2
ans = divide(num2=2, num1=3)
print(ans)
# OUTPUT: 1.5
This is one of the best practices for function in python.
*args
is not the fixed name of non-keyword arguments. We can use any name. It is just a standard to use **args.*def fruit_list_operation(*fruits):
for fruit in fruits:
print(fruit, end=",")
fruit_list_operation("apple", "banana")
print()
fruit_list_operation("apple", "banana", "grape", "mango")
""" OUTPUT:
apple,banana,
apple,banana,grape,mango,
"""
*args
will create a sequence data. As you see, when we pass 2 fruits loop runs for 2 times and 4 times in the second case. print(fruit, end=",")
function is used to notify the interpreter that does not end the statement with a new line. Instead, use comma(,) to end the statement. And when we used the print()
, it just wrote a new line and moved on.def my_project(**kwargs):
for key, value in kwargs.items():
print("%s == %s" % (key, value))
my_project(frontend = "Angular", backend = "Python", database="MongoDB")
""" OUTPUT:
frontend == Angular
backend == Python
database == mongodb
"""
def division(num1, num2=2):
print(num1/num2)
division(16,4)
division(16)
""" OUTPUT:
4.0
8.0
"""
def division(num1=2, num2):
print(num1*num2)
division(16,4)
division(16)
""" OUTPUT:
File "functions.py", line 1
def division(num1=2, num2):
^^^^
SyntaxError: non-default argument follows default argument
"""
*function_name*.__doc__
. Let's see an example to understand the concept of the docstring.def is_odd(num1):
"""Function to check if the number is odd or not.
Parameters:
num1 (float): number to be checked
Returns:
is_odd (boolean): true if number is odd
"""
is_odd = num1 % 2 != 0
print(is_odd)
return is_odd
is_odd(16)
is_odd(1)
print("-"*15)
print(is_odd.__doc__)
""" OUTPUT:
False
True
---------------
Function to check if the number is odd or not.
Parameters:
num1 (float): number to be checked
Returns:
is_odd (boolean): true if number is odd
"""
print(range.__doc__)
""" OUTPUT:
Return an object that produces a sequence of integers from start (inclusive)
to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements.
When step is given, it specifies the increment (or decrement).
"""