Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Section 4 Python-Loop

Uploaded by

Magy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Section 4 Python-Loop

Uploaded by

Magy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Python Getting

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.

The range() function defaults to increment the


sequence by 1, however it is possible to specify
the increment value by adding a third
parameter: range(2, 30, 3):
“ Python For Loops”
The break Statement
With the break statement we can stop the loop
before it has looped through all the items:
“ Python For Loops”
The continue Statement Else in For Loop
With the continue statement we can stop The else keyword in a for loop specifies a block of code to be
the current iteration of the loop, and executed when the loop is finished:
continue with the next:
“ Python For Loops”
Nested Loops The pass Statement
A nested loop is a loop inside a loop. for loops cannot be empty, but if you for some reason have a
The "inner loop" will be executed one time for loop with no content, put in the pass statement to avoid
for each iteration of the "outer loop": getting an error.
Python While Loops
“ Python While Loops”
The while Loop The else Statement
With the while loop we can execute a set of With the else statement we can run a block of
statements as long as a condition is true. code once when the condition no longer is true:

Note: remember to increment i, or else the


loop will continue forever.
The while loop requires relevant variables to be
ready, in this example we need to define an indexing
variable, i, which we set to 1.
“ Python While Loops”
The break Statement The continue Statement
With the break statement we can stop the loop With the continue statement we can stop the
even if the while condition is true: current iteration, and continue with the next:
Python Functions
“ Python Functions”
A function is a block of code which only runs when it is Arguments
called. Information can be passed into functions as arguments.
You can pass data, known as parameters, into a function. Arguments are specified after the function name, inside the
A function can return data as a result. parentheses. You can add as many arguments as you want, just
Creating a Function separate them with a comma.
In Python a function is defined using the def keyword: The following example has a function with one argument
(fname). When the function is called, we pass along a first
name, which is used inside the function to print the full name:

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.

From a function's perspective:

A parameter is the variable listed inside the


parentheses in the function definition.

An argument is the value that is sent to the


function when it is called.
“ Python Functions”
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed Keyword Arguments
into your function, add a * before the parameter name in You can also send arguments with
the function definition. the key = value syntax.
This way the function will receive a tuple of arguments, and This way the order of the arguments does not
can access the items accordingly: matter.
“ Python Functions”
Arbitrary Keyword Arguments, **kwargs Default Parameter Value
If you do not know how many keyword arguments that will be The following example shows how to use a default
passed into your function, add two asterisk: ** before the parameter value.
parameter name in the function definition. If we call the function without argument, it uses
the default value:
This way the function will receive a dictionary of arguments,
and can access the items accordingly:

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.

Now the Student class has the same properties and


methods as the Person class. When you add the __init__() function, the child class will no
longer inherit the parent's __init__() function.
Note: The child's __init__() function overrides the inheritance
of the parent's __init__() function.
“ Python Classes”
To keep the inheritance of the parent's __init__() function, Add Properties
add a call to the parent's __init__() function:

Now we have successfully added the __init__() function, and


kept the inheritance of the parent class, and we are ready to
add functionality in the __init__() function.
Use the super() Function “ Python Classes”
Python also has a super() function that will make the child class inherit all the methods and properties
from its parent:

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?

You might also like