Foundation Course in
Python
Loops:
FOR LOOP
For Loop:
A for loop is used for iterating over a sequence (that is
either a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once
for each item in a list, tuple, set etc.
Range () Function:
The range() function returns a sequence of numbers, starting
from 0 by default, and increments by 1 (by default), and
ends at a specified number.
User Input:
Input (_) Function
Functions
Functions:
● Creating clean repeatable code is a key part of becoming
an effective programmer.
● Functions allow us to create blocks of code that can be
easily executed many times, without needing to
constantly rewrite the entire block of code.
Functions:
● Functions will be a huge leap forward in your
capabilities as a Python programmer.
Functions:
● It is very important to get practice combining everything
you’ve learned so far (control flow, loops, etc.) with
functions to become an effective programmer.
How to create Function:
def name_of_function():
Keyword telling Python
this is a function. A colon indicates an upcoming
indented block. Everything indented is
then “inside” the function
How to create Function:
def name_of_function():
’’’
Docstring explains function.
’’’
Optional: Multi-line string to describe
function.
How to create Function:
def name_of_function():
’’’
Docstring explains function.
’’’
print(“Hello”)
Code then goes inside the
function.
How to create Function:
def name_of_function():
’’’
Docstring explains function.
’’’
print(“Hello”)
>> name_of_function() Function can then be
>> Hello executed/called to see the
result.
How to create Function:
def name_of_function(name):
’’’
Docstring explains function.
’’’
print(“Hello ”+name)
>> name_of_function(“Jose”)
>> Hello Jose Functions can accept
arguments to be passed by
the user.
How to create Function:
● Typically we use the return keyword to send back
the result of the function, instead of just printing it
out.
● return allows us to assign the output of the
function to a new variable.
How to create Function:
def add_function(num1,num2):
return num1+num2
>> result = add_function(1,2) Return allows to save the
result to a variable.
>>
>> print(result)
>> 3