Funtions with Outputs
def my_function():
result = 3*2
return result
when call my_function()
my_function() is replaced by the return
then we can save this result as another variable
like:
output = my_function
Return tells the computer it is the end and won't execute later.
The return keyword will exit the function and prevent the rest of the code from being executed.
if True:
the following will be executed;
if False:
the follwing will not be executed
Docstrings
"""(3)"""(3)
it is used to documente the things you want
def format_name(f_name, l_name):
"""Take a first and last name and format it to return the title case version of the name"""
Also,you can use comment instead.But it can not work like docstrings
How to create a dictionary at the same time adding multiple keys and values in it?
operation={
"love"=A * B ,
"Trust"= A ** B
}
operations={
"+":add,
"-":subtract,
"*":multiply,
"/":divide
}
# This dictionary will act as a mean to call the operation
#How can we call this funtion?
# 1)pick the key
# like this: opertions["+"]
# and we can store it into a variable ,for example we can call it function
# So 👉 function = operations["+"]
# Then we can call it funtin(n1,n2)
# 👆That partly explains why we just store *add/ subtract* in the value part
Print Vs. Return
*BUT there is a question:
What if we wanted to take the output that comes from calling this function and instead of just storing it inside a variable and printed it out? What if we wanted to pass it to another function as an input?