Lecture-1 Intro To Python
Lecture-1 Intro To Python
Python programming
language
Evrad Kamtchoum
Lecture 1
Introduction to Python programming
LOGIK
Control Structures
Selection
Loops
Functions
Summary
Evrad Kamtchoum
Centre for Cybersecurity and Mathematical Cryptology
The University of Bamenda
1.1
Introduction to
Content Python programming
language
Evrad Kamtchoum
Selection Loops
Loops Functions
Summary
4 Functions
1.2
Introduction to
Course Content Python programming
language
Evrad Kamtchoum
LOGIK
Summary
1.3
Introduction to
Course Objectives Python programming
language
Evrad Kamtchoum
LOGIK
Functions
Summary
1.4
Introduction to
Prerequisites Python programming
language
Evrad Kamtchoum
LOGIK
Functions
Summary
1.5
Introduction to
Objectives of this lecture Python programming
language
Evrad Kamtchoum
LOGIK
Control Structures
Selection
4 Identify problems for which Python is a good alternative Loops
Functions
Summary
1.6
Introduction to
Overview of Python Python programming
language
Evrad Kamtchoum
LOGIK
Definition (Python...)
Summary
1.7
Introduction to
Some history Python programming
language
"Over six years ago, in December 1989, I was looking for a "hobby"
Evrad Kamtchoum
programming project that would keep me occupied during the week
around Christmas... I chose Python as a working title for the project,
being in a slightly irreverent mood (and a big fan of Monty Python’s LOGIK
Flying Circus)."
-Python creator Guido Van Rossum, from the foreward to Programming Python (1st ed.)
Control Structures
Selection
Loops
Functions
Summary
Copyright by Google
1.8
Introduction to
What sort of language is Python? Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
Copyright by Bob Dowling What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.9
Introduction to
What sort of language is Python? Python programming
language
Evrad Kamtchoum
Objectives and
Content
What is Python?
• Clearly, a lot less work is done to get a program to start running Operators
Control Structures
compared with compiled languages! Selection
Functions
can be efficiently run by the Python interpreter.
Summary
• The interpreter itself is written in C and is a compiled program.
1.10
Introduction to
Comparison Python programming
language
Evrad Kamtchoum
Interpreted Compiled
• Faster development • Longer development LOGIK
1.11
Introduction to
Who uses Python? Python programming
language
Pyton is used for a variety of task:
Evrad Kamtchoum
What is Python?
Control Structures
Selection
Loops
Functions
Summary
Copyright by Statista
1.12
Introduction to
Installing Python... Python programming
language
Evrad Kamtchoum
• [Link]
• [Link]
Objectives and
• [Link] Content
tools Selection
Loops
1.13
Introduction to
Python 2 Vs Python 3 Python programming
language
Evrad Kamtchoum
What is Python?
• Py3 is not completely compatible with Py2
Python Data Types
• For learning Python these differences are almost negligible Variables
Text
Numbers and Boolean
Values
• If your research group / advisor / boss / friends all use one Control Structures
Selection
version that’s probably the best one for you to choose Loops
• If you have a compelling reason to focus on one vs the other Functions
• Otherwise just choose Py3. This is where the language Summary
development is happening!
1.14
Introduction to
What Python can do? Python programming
language
Evrad Kamtchoum
LOGIK
Tasks Library
Scientific SciPy, NumPy, Pandas, IPython, Mat-
Objectives and
plotlib Content
Summary
1.15
Introduction to
Pip Python programming
language
Evrad Kamtchoum
LOGIK
Control Structures
• Listing installed packages Selection
Loops
• $ pip list
Functions
• $ pip freeze # output file can be uses by ’pip install -r’
Summary
1.16
Introduction to
Hello World program in Python Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
print(’Hello, World!’)
Python Data Types
Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators
Control Structures
Selection
Loops
Functions
Summary
1.17
Introduction to
Try Python as a calculator Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
Control Structures
do? Selection
Loops
Functions
Summary
1.18
Introduction to
Variables Python programming
language
Evrad Kamtchoum
= operator
time Operators
Control Structures
Selection
Loops
• Variable type is not specified Functions
Summary
1.19
Introduction to
Variables cont’d Python programming
language
Evrad Kamtchoum
Functions
Summary
• Multiple assignments can be done on one line:
• x, y , z = 1, 2.39,0 cat 0
1.20
Introduction to
Variable Data Types Python programming
language
Evrad Kamtchoum
context
Objectives and
• The type is identified when the program runs, called Content
• Compare with compiled languages like C++ or Fortran, Python Data Types
Variables
Control Structures
• Run-time typing is very convenient and helps with rapid Selection
Summary
• The larger the program, the more significant the burden this
is!!!
1.21
Introduction to
Variable Data Types Python programming
language
Evrad Kamtchoum
What is Python?
• Strings, using double or single quotes: ”cat”, 0 dog 0
Python Data Types
Variables
Summary
1.22
Introduction to
Check a type Python programming
language
Evrad Kamtchoum
LOGIK
Summary
1.23
Introduction to
Variables and Memory Locations Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
• y = x does not mean "make a
Python Data Types
copy of the list x and assign it to Variables
Text
y" it means "make a copy of the Numbers and Boolean
Values
memory location in x and assign Other Data Types
it to y" Operators
Control Structures
Selection
Loops
• x is not the list it’s just a Functions
reference to it Summary
1.24
Introduction to
Data Types in Python Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
Copyright by Phondanai Khanti
Summary
1.25
Introduction to
Strings Python programming
language
Evrad Kamtchoum
LOGIK
Functions
• Strings have many built-in functions...
Summary
1.26
Introduction to
String functions Python programming
language
Evrad Kamtchoum
LOGIK
• In the Python console, create a string variable called mystr Objectives and
Content
What is Python?
Summary
1.27
Introduction to
The len() function Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
• It’ll return the length of any Python variable that contains. Python Data Types
Variables
some sort of countable thing. Text
Numbers and Boolean
Values
Other Data Types
Operators
Control Structures
• In the case of strings it is the number of characters in the Selection
Loops
string. Functions
Summary
1.28
Introduction to
Numbers Python programming
language
Evrad Kamtchoum
What is Python?
• The reason is that numbers can be integers or floating Python Data Types
Variables
1.29
Introduction to
Boolean Values Python programming
language
Evrad Kamtchoum
Boolean
• a True or False value, corresponding to the machine’s LOGIK
• In Python the name Boolean is shortened to the type bool Numbers and Boolean
Values
Other Data Types
Operators
Functions
Summary
1.30
Introduction to
Lists Python programming
language
Evrad Kamtchoum
Objectives and
• Lots of things in Python act like lists or use list-style Content
Control Structures
Selection
• Lists can change in size: elements can be added or Loops
removed Functions
Summary
1.31
Introduction to
Making a list and checking it twice... Python programming
language
Evrad Kamtchoum
LOGIK
Control Structures
Selection
Functions
elements Summary
1.32
Introduction to
List functions Python programming
language
Evrad Kamtchoum
LOGIK
• Try dir(list1)
Objectives and
Content
• Like strings, lists have a number of What is Python?
Control Structures
Summary
1.33
Introduction to
Accessing List Elements Python programming
language
Evrad Kamtchoum
What is Python?
• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] Python Data Types
Variables
Text
Numbers and Boolean
• First element: x[0] Values
Other Data Types
Operators
Control Structures
• First element: x[2] Selection
Loops
Functions
• Next-to-last: x[−2]
1.34
Introduction to
Accessing List Elements Python programming
language
Evrad Kamtchoum
Functions
• Last element: x[-1] → ’e’
Summary
1.35
Introduction to
List Slicing Python programming
language
• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] Evrad Kamtchoum
What is Python?
list
Python Data Types
Variables
Text
• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] Numbers and Boolean
Values
Other Data Types
Operators
• First element: x[0:1] → [’a’] Control Structures
Selection
Loops
Summary
Evrad Kamtchoum
LOGIK
What is Python?
Control Structures
Selection
• del x[-1] → x is now [-3.14, ’b’, ’c’, ’d’] Loops
Functions
Summary
1.37
Introduction to
More on Lists and Variables Python programming
language
Evrad Kamtchoum
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.38
Introduction to
Copying Lists Python programming
language
Evrad Kamtchoum
LOGIK
Control Structures
Selection
Loops
Functions
Summary
1.39
Introduction to
Tuples Python programming
language
Evrad Kamtchoum
LOGIK
Functions
Summary
1.40
Introduction to
Return multiple values from a function Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
Control Structures
• Python syntax can Selection
Loops
1.41
Introduction to
Dictionaries Python programming
language
Evrad Kamtchoum
About dictionnaries
Objectives and
Content
• Dictionaries are another basic Python data type that are
What is Python?
tremendously useful Python Data Types
Variables
Text
Control Structures
• Dictionaries store values and are indexed with keys Selection
Loops
Functions
1.42
Introduction to
Dictionaries Python programming
language
Evrad Kamtchoum
• Keys can be primitive types (numbers), strings, tuples, and Objectives and
Content
some custom data types
What is Python?
• Basically, any data type that is immutable Python Data Types
Variables
Text
Numbers and Boolean
• Lists and dictionaries cannot be keys but they can stored Values
Other Data Types
as values Operators
Control Structures
Selection
Functions
• x[’a_key’] → 55
Summary
• x[100] → ’a_value’
1.43
Introduction to
Try Out Dictionaries Python programming
language
Evrad Kamtchoum
Control Structures
• Try [Link]() and [Link]() Selection
Loops
Functions
• Try: del x[valid_key] → Summary
1.44
Introduction to
Operators Python programming
language
Python supports a wide variety of operators which act like func- Evrad Kamtchoum
• Arithmetic: +, -, *, /,
Objectives and
Content
• Logical: and or not
What is Python?
• Assignment: = Operators
Control Structures
Selection
Loops
Summary
Evrad Kamtchoum
What is Python?
* Multiplication Operators
Control Structures
/ Division (Note: 3 / 4 is 0.75!) Selection
Functions
** Exponentiation
Summary
== Equals
1.46
Introduction to
Variable modifying operators Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
Operator Effect Equivalent to... Python Data Types
x += y Add the value of y to x x=x+y Variables
Text
x -= y Subtract the value of y from x x=x-y Numbers and Boolean
Values
x *= y Multiply the value of x by y x=x*y Other Data Types
Control Structures
Selection
Loops
Summary
1.47
Introduction to
String operators Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
Control Structures
Selection
Functions
Summary
1.48
Introduction to
String operators Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
• Python strings are immutable, i.e. they can’t be changed. Summary
1.49
Introduction to
String Substitutions Python programming
language
Evrad Kamtchoum
Control Structures
• For more than one: Selection
Loops
Functions
Summary
• Variables are listed in the substitution order inside ()
1.50
Introduction to
Variables with operators Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
Control Structures
editor. Click the green triangle to Selection
Summary
1.51
Introduction to
More operators Python programming
language
Evrad Kamtchoum
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.52
Introduction to
Comments Python programming
language
Evrad Kamtchoum
LOGIK
Control Structures
• An editor like Spyder makes it Selection
Loops
Edit menu
1.53
Introduction to
Line Continuation Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
• You might prefer to send part of the code to the next line. Python Data Types
Variables
So, 2.0 times 1.5 plus 5 could be written in two lines, and Text
Numbers and Boolean
the machine could still read it as one command Values
Other Data Types
Operators
Control Structures
• This could be achieved by putting a back slash where you Selection
Loops
would like the end of the first line to be
Functions
Summary
1.54
Introduction to
Control Structures in Python Python programming
language
Evrad Kamtchoum
• Control flow refers to the sequence a program will follow What is Python?
Summary
executing a certain piece of a code block
1.55
Introduction to
If / Else Python programming
language
Evrad Kamtchoum
Objectives and
• Syntax: Content
What is Python?
Control Structures
Selection
Loops
Functions
• elif and else are not required - used to chain together Summary
1.56
Introduction to
If / Else Python programming
language
Evrad Kamtchoum
LOGIK
What is Python?
console? Text
Numbers and Boolean
Values
Other Data Types
Operators
• Try using an elif or else statement by
Control Structures
itself without a preceding if. What error Selection
Loops
message comes up? Functions
Summary
1.57
Introduction to
Indentation of code... easier on the eyes! Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
1.58
Introduction to
The Use of Indentation Python programming
language
Evrad Kamtchoum
• Code blocks are logical groupings of commands. They are Objectives and
Content
always preceded by a colon :
What is Python?
Control Structures
Summary
1.59
Introduction to
If / Else code blocks Python programming
language
Evrad Kamtchoum
LOGIK
Summary
1.60
Introduction to
File vs. Console Code Blocks Python programming
language
Evrad Kamtchoum
indentation is removed
Objectives and
• EXCEPT when typing code into the Python console. There Content
What is Python?
an empty line indicates the end of a code block
Python Data Types
Variables
Text
• This sometimes causes problems when pasting code into Control Structures
Selection
Functions
Summary
1.61
Introduction to
Match - Case Python programming
language
Evrad Kamtchoum
statements, you would’ve had to use the elif keyword like Python Data Types
Variables
this: Text
Numbers and Boolean
Values
Other Data Types
Operators
Control Structures
Selection
Loops
Functions
Summary
1.62
Introduction to
Match - Case Python programming
language
Evrad Kamtchoum
• You can implement this feature with the match and case Objectives and
Content
keywords:
What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.63
Introduction to
Match - Case Example Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.64
Introduction to
While Loops Python programming
language
Evrad Kamtchoum
• The indentation
indicates what’s in the Objectives and
Content
while loop
• The loop runs until the What is Python?
1.65
Introduction to
For Loops Python programming
language
Evrad Kamtchoum
through a collection of
things
Objectives and
Content
1.66
Introduction to
Processing lists element-by-element Python programming
language
Evrad Kamtchoum
a list
Objectives and
Content
• There are several ways:
What is Python?
• Loop over the list elements Python Data Types
• Loop over a list of index values and access the list by index Variables
Text
• Do both at the same time Numbers and Boolean
Values
• Use a shorthand syntax called a list comprehension Other Data Types
Operators
Control Structures
Functions
Summary
• Let’s look at code samples for each of these
1.67
Introduction to
The range() function Python programming
language
Evrad Kamtchoum
Objectives and
• Syntax: range(start, exclusive end, increment) Content
What is Python?
Control Structures
Selection
Loops
• range(4,-3,-2) → 4,2,0,-2
Functions
Summary
1.68
Introduction to
Defining a Function in Python Python programming
language
Evrad Kamtchoum
Objectives and
Content
What is Python?
Control Structures
Selection
Loops
• To tell the computer you are about to create a function, just write def at the beginning of the line. Def is neither a
command nor a function. It is a keyword. Functions
• Then, you can type the name of the function you will use. Then you can add a pair of parentheses. Summary
• Technically, within these parentheses, you could place the parameters of the function if it requires you to have any. It
is no problem to have a function with zero parameters.
• To proceed, don’t miss to put a colon after the name of the function.
1.69
Introduction to
Creating a Function with a Parameter Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
Summary
1.70
Introduction to
Creating a Function with a Parameter Python programming
language
Evrad Kamtchoum
Functions
People often confuse print and return, and the type of situations Summary
1.71
Introduction to
Creating a Function with a Parameter Python programming
language
Evrad Kamtchoum
Functions
each of the two keywords does.
Summary
This will help you a great deal
when working with functions.
1.72
Introduction to
Using a Function in Another Function Python programming
language
Evrad Kamtchoum
LOGIK
Objectives and
Content
What is Python?
• It isn’t a secret we can have a function within the function Control Structures
Selection
• In with_bonus(w_hours), you can return directly the wage Loops
Functions
with working hours as an output, which would be the value
Summary
obtained after the wage function has been run, plus 50.
1.73
Introduction to
Creating Functions Containing a Few Arguments Python programming
language
Evrad Kamtchoum
LOGIK
You can work with more than one parameter in a function. The
way this is done in Python is by enlisting all the arguments within
Objectives and
the parentheses, separated by a comma. Content
What is Python?
Control Structures
Selection
Loops
Functions
Summary
1.74
Introduction to
Creating Functions Containing a Few Arguments Python programming
language
Evrad Kamtchoum
LOGIK
Summary
1.75
Introduction to
Notable Built-In Functions in Python Python programming
language
Evrad Kamtchoum
When you install Python on your computer, you are also in- LOGIK
stalling some of its built-in functions. This means you won’t need
to type their code every time you use them - these functions are
already on your computer and can be applied directly Objectives and
Content
str() transforms its argument in a string data type Numbers and Boolean
Values
max() Returns the highest value from a sequence of numbers Other Data Types
min() Returns the lowest value from a sequence of numbers Operators
abs() Allows you to obtain the absolute value of its argument Control Structures
sum() Calculates the sum of all the elements in a list designated as an argument Selection
returns the float of its argument (x), rounded to Loops
round(x,y)
a specified number of digits (y) after the decimal point
Functions
pow(x,y) returns x to the power of y
len() returns the number of elements in an object Summary
1.76
Introduction to
Summary Python programming
language
programming language
2 Python’s popularity lies on two main pillars. One is that it is an LOGIK
4 Basic Python data types include integers, floating points, Numbers and Boolean
Values
complex numbers, strings, and booleans. Compound data Other Data Types
Operators
types include lists, tuples, and dictionaries Control Structures