You are on page 1of 3

Date: 06-Nov-21

Functions:

They are basically a set of codes that do one thing but instead of the codes being written as part
of a program and having to rewrite the code multiple times to redo it or having to restart the
program to run the code again we can set it as a single separate entity that whenever called will
run the function it is written to do.
 Functions in python are indicated with a KEY word “def”
KEY WORDS CANNOT BE USED AS VARIBALES THEY REPERESENT A SET OF CODES WITHIN THE
LANGUAGE ITSELF
 The convention used for naming functions is to separate multiple word with a _ i.e., def
user_name():
 Functions must have their codes intended otherwise it will not be considered part of the
function by the interpreter
 Functions do not run automatically when the program is run. They need to be called for it
to be executed i.e., user_name() The calling is done using the function name and a set of
parenthesis containing the data that the function requires to run if needed
 Functions are similar to variables they are case sensitive and any change in format will not
recognize the function
 For functions taking in data to use/parameters it retains the same format however the
function initialization as well as the call need the data to be entered i.e., def
user_name(name) with an indented print(“Hello “ + name) and a call function
user_name(“Mike”) will return Hello Mike
 For functions taking parameters multiple paraments are sepearated by a comma on both
the initialization as well as call i.e., def user_name(-,-) and user_name(-,-)

Return:

 Return is a key word


 The return statement is part of functions. Usually, functions can simply be called and they
will run their code and move on to the next part of the program however sometimes after
executing the code we need some information back and the return statement will allow
us to get the value back from the function and use it else where rather than just run the
statement and continue forward
def cube(num)
num*num*num
print(cube(3))
will print out None since the cube functions has no value that it outputs out it simply executes
the code and end it
def cube(num)
return num*num*num
print(cube(3))
will print out 27 since the return statement gives the function an output value
The return statement will end the function it basically breaks a set of codes out of the
function
RETURN FUNCTIONS WILL END THE FUNCTION AND THERFORE MUST ONLY BE USED AT THE END OF
THE CODE

“if”, “or”, “and”, “elif”, “not” and “else” functions:

 if is a key word
 It is used to check data against a condition to run a set of codes and if false to run other
codes or exit out and not run a set of codes
 It can be used to check multiple conditions and default to a final set a codes
 if function work with a colon i.e., if x==10: and a new line indented like in a function will
be the code executed if the statement is true
 For Booleans if statements run the code if the statement is True. The condition is true i.e.,
male = True and if male: with indented print(“Hi dude”) will return Hi dude since male is
true however if male was set as false then the function will not print anything
 A if function that does not continue to other conditions or a default line will end the code
after the if test and code that is indented
 To set a default value for failure we use the else statement
Male = False
if male:
print(“Hey dude”)
else:
print(“Hey lady”)
will return
Hey lady
 Remember that the format is important IF or If or iF will not work it must be if and else
 The else statement is used to run a set of codes in case all the test conditions fail
 To test multiple conditions on the same line we use the or code for this condition if at least
one of the conditions are true then the code will run. It will only fail only when both
conditions are false
 For multiple conditions on the same line we can also use the and code. This code works
similar to the or code but for the code for the truth condition to execute both of the
conditions must be true if either one or both are false then the test fails
 There also exists the elif condition. This condition is used to test for conditions on new line so
that a new of single or compound conditions may be tested for various uses and its own
separate line of code.
 The not function is a negative test condition to test for a false condition and must contain the
test condition in brackets (-) i.e., elif not(is_sane):

Is_male = True
Is_tall = False

if Is_male and Is_tall:


print(“Hi tall dude”)
elif not(Is_male) and Is_tall:
print(“Hey tall lady”)
elif not Is_male and not(Is_tal)l:
print(“hey short lady”)
else:
print(“Hey shot dude”)

You might also like