Professional Documents
Culture Documents
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:
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