xxxxxxxxxx
def function():
print('This is a basic function')
function()
// Returns 'This is a basic function'
def add(numA, numB):
print(numA+numB)
add(1,2)
// Returns 3
def define(value):
return value
example = define('Lorem ipsum')
print(example)
// Returns 'Lorem ipsum'
xxxxxxxxxx
def myFunction(say): #you can add variables to the function
print(say)
myFunction("Hello")
age = input("How old are you?")
myFunction("You are {} years old!".format(age))
#this is what you get:
Hello
How old are you?
>>11 #lol my real age actually
You are 11 years old!
xxxxxxxxxx
#plz suscribe my youtube channel --> https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
# Parameter of the function
# |
def greetings(Name):
#Content inside the function
print("Hello",Name)
print("How are you",Name)
print(greetings("Python devloper"))
# ^
# |
# Argument of the function
xxxxxxxxxx
#Letters, Nums,
#& underscores
#only, can't =7
#start w/ num | _ 5 is the default value if no
# | | | value is passed
# v v v
def funcName(param1, param2=5): #The colon terminates func header
str(param1) #Typecasting is temparary
strVar = str(param1) # To make typecast perma, assign to var
param1 = param2 # 5 is passed to param1
return param1 # 5 is returned
# ^ ^
# | |_ return value is optional, if no return value, func
#Optional will end returning to func call
#return
x = funcName(7)
print(x) # prints 5
xxxxxxxxxx
# first we have to write 'def'
# then our function name followed by ()
# and a ':' abd defining block of code
def multiply(): # naming convention could be same as variable for functions
product = 10.5 * 4
return product
product = multiply()
print(product)
xxxxxxxxxx
# basic function
def funcOne():
print("Hello Wordl")
funcOne()
# function with params
def funcTwo(x, y):
print(x + y)
funcTwo(2, 1)
# function with returning value
def funcThree():
return "Max Cavalera"
print(funcThree())
# function with default params
def funcFour(v="Jamal Cavalera"):
return v
print(funcFour())
# function with args as tuple
def funcFive(*args):
return args
five = funcFive(1, 2, 3, 4, 5)
print(five)
# function with dictionary
def funcSix(name, age):
return "My name is " + name + " and my age is " + str(age)
six = funcSix(name="jamal", age=20)
print(six)
# function with args as dictionary
def funcSeven(**args):
return args
seven = funcSeven(name="jamal", age=20, hobby="programming")
print(seven)
# function ignore params
def funcEight(n, /):
print(n)
funcEight(10)
# function params only args dictionary
def funcNeen(*, n):
print(n)
funcNeen(n=21)
# function recursive with params
def funcTeen(n):
if n == 0:
return 0
else:
res = funcTeen(n - 1)
print(res)
return n
funcTeen(10)
xxxxxxxxxx
# Functions with more than one input
def function_name(parameter1, parameter2):
print(f"This is {parameter1}")
print(f"This is {parameter2}")
#These arguments are called positional argument since the code will check the order of the parameters
function_name("Argument1", "Argument2")
# Keyword argument to check the position add the parameter name to the argument so the order will not matter )
function_name(parameter2="Argument2", parameter1="Argument1")
xxxxxxxxxx
# Example 1 : Function without args or kwargs
def func_name(param1, param2=1):
"""
Function Definition like find the power of 2 values with respect to each other.
Args:
param1 (int): A numeric value.
param2 (int): A numeric value.
Returns:
new_tuple (tuple): a tuple of 2 values.
""" # Function definition Docstring
new_value1 = param1 ** param2
new_value2 = param2 ** param1
new_tuple = (new_value1, new_value2) # Binding results in a tuple
return new_tuple # Returning multiple results
func_name(2,3) # (8,9)
# Example 2 : Function with *args (a tuple of parameters)
def add_all(*args):
"""Sum all values in *args together."""
sum_all = 0
for num in args: # iterate over args tuple
sum_all += num
return sum_all
add_all(5, 10, 15, 20) # 50
# Example 3 : Function with **kwargs (a dictionary of parameters)
def print_all(**kwargs):
"""Print out key-value pairs in **kwargs."""
for key, value in kwargs.items(): # iterate over kwargs dictionary
print(f"key = {key}, value = {value}")
print_all(name="dumbledore") # key = name, value = dumbledore
# Example 4 : Function as return value
def get_function():
def print_me(s):
print(s)
return print_me
new_func = get_function() # print_me is now referenced by new_func
# calling new_func will now call print_me
new_func('This is a sentence.')
# See function definition
print(inspect.getsource(func_name))
# See documentation
print(func_name.__doc__)
print(help(func_name))
# Change docstring
func_name.__doc__ = "New Function Definition"
xxxxxxxxxx
def add(number):
equation = 5 + number
print(equation)
add(10)
output:
15