Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Graph theory
2019/2020
python History
python timeline
characteristics of Python
Introduction to variables
Introduction
Variable is a name which is used to refer memory location. Vari-
able also known as identifier and used to hold value. In Python,
we don’t need to specify the type of variable because Python is a
type infer language and smart enough to get variable type. Vari-
able names can be a group of both letters and digits, but they
have to begin with a letter or an underscore.
Types of variables
The hierarchy of types of Python
Numbers
Number stores numeric values. Python creates Number objects
when a number is assigned to a variable. For example
Assignment of numbers to variables
a=10 , b=9 #a and b are number objects
String
String
String
String
List
tuple
A tuple is similar to the list in many ways. Like lists, tuples also
contain the collection of the items of different data types. The
items of the tuple are separated with a comma (,) and enclosed
in parentheses ().
A tuple is a read-only data structure as we can’t modify the size
and value of the items of a tuple.
Here a example of the tuple:
Dictionary
Comments in Python
Comments in Python
There are two forms of comments in python:
Single Line Comment: in case user wants to specify a single
line comment, then comment must start with #.
Single line comment
#This is single line comment.
Python Operators
Arithmetic operators
Arithmetic operators
Comparison operators
Logical operators
Membership operators
Identity operators
Operator precedence
Print function
Input function
input function
Indentation in Python
The If statement
The If statement
The syntax of if statement
if expression:
statement
Example
Loops
The flow of the programs written in any programming language
is sequential by default. Sometimes we may need to alter the
flow of the program. The execution of a specific code may need
to be repeated several numbers of times.
For this purpose, The programming languages provide various
types of loops which are capable of repeating some specific code
several numbers of times. Consider the following diagram to un-
derstand the working of a loop statement. There are the following
advantages of loops in Python:
It provides code re-usability.
Using loops, we do not need to write the same code again
and again.
Using loops, we can traverse over the elements of data struc-
tures (array or linked lists).
Bennani Mohamed Taj Faculty of Science Dhar El Mahraz Fez 47/123
Graph theory
Graph theory
Section I: The python programming language
The Python language
Pass Statement
Pass Statement
Example of using pass
else clause
For and while loops also have an else clause which most of us are
unfamiliar with. The else clause executes after the loop completes
normally. This means that the loop did not encounter a break
statement. They are really useful once you understand where to
use them. I, myself, came to know about them a lot later.
Syntax
for iterating var in sequence:
statement(s)
else:
statement(s)
Function
Functions
Advantages of Python
There are the following advantages of C functions.
By using functions, we can avoid rewriting same logic/code
again and again in a program.
We can call python functions any number of times in a pro-
gram and from any place in a program.
We can track a large python program easily when it is di-
vided into multiple functions.
is the main achievement of python functions.
However, Function calling is always overhead in a python
program.
Bennani Mohamed Taj Faculty of Science Dhar El Mahraz Fez 64/123
Graph theory
Graph theory
Section I: The python programming language
The Python language
Functions
The function block is started with the colon (:) and all the same
level block statements remain at the same indentation.
A function can accept any number of parameters that must be
the same in the definition and function calling.
Syntax of function declaration
def my function():
function-suite
return expression
Functions
The scopes of the variables depend upon the location where the
variable is being declared. The variable declared in one part of
the program may not be accessible to the other parts.
In python, the variables are defined with the two types of scopes:
Global variables.
Local variables.
The variable defined outside any function is known to have a
global scope whereas the variable defined inside a function is
known to have a local scope.
Python Recursion
Definition
As stated in the introduction, recursion involves a process
calling itself in the definition. A recursive function generally has
two components:
The base case: which is a condition that determines when
the recursive function should stop.
The recursive case: The call of the function to itself.
Python Recursion
Python Recursion
Example recursion in Python
Introduction
Example:
Update of lists
Example:
Example:
Example:
Browsing a list
Browsing a list
if element in List :
do Action
Example:
Introduction
Iterating Dictionary
There is different ways to iterate a dictionary:
for loop to print all the keys of a dictionary:
Iterating Dictionary
Iterating Dictionary
Iterating Dictionary
Introduction
Introduction
Creating a matrix
Iterating a Matrix
Adding a column
Adding a raw