Section 4 Python-Loop
Section 4 Python-Loop
Started
Python For Loops
Python While Loops
Python Functions
Python Lambda Agenda
Python Classes and Objects
Python Inheritance
Python For Loops
“ Python For Loops”
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.
“ Python For Loops”
Note that range(6) is not the values of 0 to 6, but
The range() Function the values 0 to 5.
To loop through a set of code a specified number of The range() function defaults to 0 as a starting
times, we can use the range() function, value, however it is possible to specify the starting
The range() function returns a sequence of value by adding a parameter: range(2, 6), which
means values from 2 to 6 (but not including 6):
numbers, starting from 0 by default, and increments
by 1 (by default), and ends at a specified number.
Calling a Function
To call a function, use the function name followed
by parenthesis:
“ Python Functions”
Parameters or Arguments? Number of Arguments
By default, a function must be called with the correct number
The terms parameter and argument can be of arguments.
used for the same thing: information that are Meaning that if your function expects 2 arguments, you have to
passed into a function. call the function with 2 arguments, not more, and not less.
Kid ={
‘fmane’: ‘tobie’ ,
‘lname’: ‘Refen’
}
“ Python Functions”
Passing a List as an Argument Return Values
You can send any data types of argument to a To let a function return a value, use the return statement:
function (string, number, list, dictionary etc.),
and it will be treated as the same data type
inside the function.
E.g. if you send a List as an argument, it will still
be a List when it reaches the function:
“ Python Functions”
The pass Statement
function definitions cannot be empty, but if you for some
reason have a function definition with no content, put in the
pass statement to avoid getting an error.
Recursion
“ Python Functions”
Python also accepts function recursion, which means a defined function can call itself.
Recursion is a common mathematical and programming concept. It means that a function calls itself.
This has the benefit of meaning that you can loop through data to reach a result.
The developer should be very careful with recursion as it can be quite easy to slip into writing a function
which never terminates, or one that uses excess amounts of memory or processor power. However,
when written correctly recursion can be a very efficient and mathematically-elegant approach to
programming.
In this example, tri_recursion() is a function that we have defined to call itself ("recurse"). We use
the k variable as the data, which decrements (-1) every time we recurse. The recursion ends when the
condition is not greater than 0 (i.e. when it is 0).
To a new developer it can take some time to work out how exactly this works, best way to find out is by
testing and modifying it.
k tri_recursion(k - 1) result
6 6+
5 5+
4 4+
3
2 1
1 0 1+0=1
Python Lambda
“ Python Lambda”
A lambda function is a small anonymous function.
Lambda functions can take any number of
arguments:
A lambda function can take any number of arguments,
but can only have one expression.
Syntax
“ Python Lambda”
Why Use Lambda Functions?
The power of lambda is better shown when you
use them as an anonymous function inside another
function.
Say you have a function definition that takes one
argument, and that argument will be multiplied
with an unknown number:
“ Python Lambda”
Python Classes and Objects
“ Python Classes”
Python Classes/Objects The __init__() Function
Python is an object oriented programming language. The examples above are classes and objects in their
simplest form, and are not really useful in real life
Almost everything in Python is an object, with its applications.
properties and methods. To understand the meaning of classes we have to
understand the built-in __init__() function.
A Class is like an object constructor, or a "blueprint" for All classes have a function called __init__(), which is
creating objects. always executed when the class is being initiated.
Use the __init__() function to assign values to object
Create a Class properties, or other operations that are necessary to do
To create a class, use the keyword class: when the object is being created:
“ Python Classes”
Note: The __init__() function is called automatically every Note: The self parameter is a reference to the current instance
time the class is being used to create a new object. of the class, and is used to access variables that belong to the
class.
Object Methods The self Parameter
Objects can also contain methods. The self parameter is a reference to the current instance of the
Methods in objects are functions that belong to class, and is used to access variables that belongs to the class.
the object.
Let us create a method in the Person class: It does not have to be named self , you can call it whatever you
like, but it has to be the first parameter of any function in the
class:
“ Python Classes”
Delete Object Properties
Modify Object Properties
You can delete properties on objects by using the del keyword:
You can modify properties on objects like this:
“ Python Classes”
Delete Objects The pass Statement
You can delete objects by using the del keyword: class definitions cannot be empty, but if you for some reason
have a class definition with no content, put in the pass
statement to avoid getting an error.
“ Python Classes”
Python Inheritance
Inheritance allows us to define a class that
inherits all the methods and properties from
another class.
Parent class is the class being inherited from,
also called base class.
Child class is the class that inherits from
another class, also called derived class.
Create a Child Class
“ Python Classes”
Add the __init__() Function
To create a class that inherits the functionality
So far we have created a child class that inherits the properties
from another class, send the parent class as a
and methods from its parent.
parameter when creating the child class:
We want to add the __init__() function to the child class
(instead of the pass keyword).
Note: The __init__() function is called automatically every time
Note: Use the pass keyword when you do not want to add the class is being used to create a new object.
any other properties or methods to the class.
By using the super() function, you do not have to use the name of the parent element, it will automatically
inherit the methods and properties from its parent.
Thanks!
Any questions?