0% found this document useful (0 votes)
3 views77 pages

Lecture-1 Intro To Python

Uploaded by

yunweneric
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views77 pages

Lecture-1 Intro To Python

Uploaded by

yunweneric
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to

Python programming
language

Evrad Kamtchoum

Lecture 1
Introduction to Python programming
LOGIK

language Objectives and


Content
Data types, control structures, and functions
What is Python?

Python Data Types


Application Development with Python of April 9, 2026 Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

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

1 What is Python? LOGIK

2 Python Data Types


Objectives and
Variables Content
Text What is Python?

Numbers and Boolean Values Python Data Types


Variables
Other Data Types Text

Operators Numbers and Boolean


Values
Other Data Types
Operators

3 Control Structures Control Structures


Selection

Selection Loops

Loops Functions

Summary

4 Functions

1.2
Introduction to
Course Content Python programming
language

Evrad Kamtchoum

LOGIK

# Lecture Topic Time


1 Introduction to Python 5h
Objectives and
2 Object-Oriented Programming in Python 5h Content

3 Introduction to Web Programming (Web dev, 5h What is Python?

Python frameworks, basic security) Python Data Types


Variables
4 Secure Web Programming (Validation, Sanitiza- 5h Text

tion, Session Management) Numbers and Boolean


Values

5 Authentication and Authorization 3h Other Data Types


Operators
6 Application Security Testing 2h
Control Structures
7 Program Versioning (Git) 2h Selection
Loops
8 System Programming in Python 3h
Functions

Summary

1.3
Introduction to
Course Objectives Python programming
language

Evrad Kamtchoum

LOGIK

• Understand the fundamentals of Python programming


• Apply object-oriented programming concepts in Python Objectives and
Content

• Develop basic web applications using Python frameworks What is Python?

Python Data Types


• Implement secure coding practices in web development Variables
Text
• Understand authentication and authorization mechanisms Numbers and Boolean
Values

• Perform basic application security testing Other Data Types


Operators

• Use version control systems for collaborative development Control Structures


Selection
• Explore system-level programming with Python Loops

Functions

Summary

1.4
Introduction to
Prerequisites Python programming
language

Evrad Kamtchoum

LOGIK

• Basic knowledge of programming concepts (variables,


Objectives and
conditions, loops) Content

• Familiarity with algorithms and problem-solving What is Python?

Python Data Types


• Basic understanding of computer systems and operating Variables
Text
systems Numbers and Boolean
Values
• Introduction to networking concepts (recommended) Other Data Types
Operators

• Motivation to learn secure and modern software Control Structures

development practices Selection


Loops

Functions

Summary

1.5
Introduction to
Objectives of this lecture Python programming
language

Evrad Kamtchoum

LOGIK

1 Understand the basics of Python programming language


Objectives and
Content

2 Discover and master Python data types, operators, What is Python?

Python Data Types


variables, and control structures Variables
Text
Numbers and Boolean
Values

3 Write functions in Python Other Data Types


Operators

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...)

• ... is a general purpose interpreted programming Objectives and


Content
language
What is Python?
• ... is a language that supports multiple approaches to Python Data Types
software design, principally structured and Variables
Text
object-oriented programming Numbers and Boolean
Values

• ... provides automatic memory management and Other Data Types


Operators

garbage collection Control Structures


Selection
• ... is extensible Loops

• ... is dynamically typed. Functions

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.)

• Goals: Objectives and


Content
• An easy and intuitive language just as powerful as major What is Python?
competitors Python Data Types
• Open source, so anyone can contribute to its development Variables
Text
• Code that is as understandable as plain English Numbers and Boolean
Values
• Suitability for everyday tasks, allowing for short Other Data Types

development times Operators

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?

Python Data Types


Compiled Languages (ex. C++ or Fortran) Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

Copyright by Brian Gregor

1.9
Introduction to
What sort of language is Python? Python programming
language

Evrad Kamtchoum

Interpreted Languages (ex. Python or R) LOGIK

Objectives and
Content

What is Python?

Python Data Types


Variables
Copyright by Brian Gregor Text
Numbers and Boolean
Values
Other Data Types

• Clearly, a lot less work is done to get a program to start running Operators

Control Structures
compared with compiled languages! Selection

• Bytecodes are an internal representation of the text program that Loops

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

• Edit / compile / test cycle


• Easier debugging
is longer!
• Debugging can stop
anywhere, swap in new
• Harder to debug Objectives and
Content
code, more control over • Usually requires a
What is Python?
state of program special compilation Python Data Types

• (almost always) takes less • (almost always) takes Variables


Text

code to more code to get things Numbers and Boolean


Values

• Slower programs done Other Data Types


Operators

• Sometimes as fast as • Faster Control Structures

compiled, rarely faster • Compiled code runs Selection


Loops

• Less control over program directly on CPU Functions


• Can communicate Summary
behavior get things done
directly with hardware
• More control over program
behavior

1.11
Introduction to
Who uses Python? Python programming
language
Pyton is used for a variety of task:
Evrad Kamtchoum

• On-line games • Science


• Web services • Instrument control LOGIK

• Applications • Embedded systems


visit: [Link]/wiki/List_of_Python_software
Objectives and
Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

Copyright by Statista

1.12
Introduction to
Installing Python... Python programming
language

Evrad Kamtchoum

There are many ways to install Python on your laptop/PC/etc. LOGIK

• [Link]
• [Link]
Objectives and
• [Link] Content

distribution/ What is Python?

Python Data Types


• [Link] Variables
Text
Most popular option: Anaconda Numbers and Boolean
Values

• Anaconda is a packaged set of programs including the Other Data Types


Operators

Python language, a huge number of libraries, and several Control Structures

tools Selection
Loops

• These include the Spyder development environment and Functions

Jupyter notebooks Summary

• Anaconda can be used on the SCC, with some caveats.

1.13
Introduction to
Python 2 Vs Python 3 Python programming
language

Evrad Kamtchoum

• Python 2: released in 2000, Python 3 released in 2008 LOGIK

• Python 2 is in "maintenance mode" - no new features are


expected
Objectives and
Content

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

• Which one to learn? Other Data Types


Operators

• 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

Machine Learning, NLTK,SciKit-Learn,OpenCV, Tensor- What is Python?

AI Flow Python Data Types


Variables
Web Development, Django, Flask**, Pyramid, Twisted, Text
Numbers and Boolean
Network Program- BeautifulSoup, Requests Values
Other Data Types
ming Operators

DevOps Ansible, SaltStack Control Structures


Selection
3D & Game Blender, PyGame Loops

Mobile App Kivy Functions

Summary

1.15
Introduction to
Pip Python programming
language

Evrad Kamtchoum

LOGIK

• Package manager for Python

• Install Objectives and


Content
• $ pip install Flask
What is Python?
• $ pip install numpy pandas tensorflow==2.0.0
• $ pip install -r [Link] # install list of package file. Python Data Types
Variables
Text

• Uninstall Numbers and Boolean


Values
Other Data Types
• $ pip uninstall Flask Operators

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

• Go to the Python prompt Objectives and


Content

What is Python?

• Try out some arithmetic Python Data Types


Variables
operators: +, −, ∗, /, %, ∗∗, ==, () Text
Numbers and Boolean
Values
Other Data Types

• Can you identify what they all Operators

Control Structures
do? Selection
Loops

Functions

Summary

1.18
Introduction to
Variables Python programming
language

Evrad Kamtchoum

• Variables are assigned values using the LOGIK

= operator

• In the Python console, typing the name Objectives and


Content
of a variable prints its value What is Python?
• Not true in a script! Python Data Types
Variables
Text
Numbers and Boolean
• Variables can be reassigned at any Values
Other Data Types

time Operators

Control Structures
Selection
Loops
• Variable type is not specified Functions

Summary

• Types can be changed with a


reassignment

1.19
Introduction to
Variables cont’d Python programming
language

Evrad Kamtchoum

• Variables refer to a value stored in memory and are LOGIK

created when first assigned

• Variable names: Objectives and


Content
• Must begin with a letter (a - z, A - B) or underscore _ What is Python?
• Other characters can be letters, numbers or _ Python Data Types
• Are case sensitive: capitalization counts! Variables
Text
• Can be any reasonable length Numbers and Boolean
Values
Other Data Types
Operators

• Assignment can be done en masse: Control Structures


Selection
• x =y =z=1 Loops

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

• Python determines data types for variables based on the LOGIK

context

Objectives and
• The type is identified when the program runs, called Content

dynamic typing What is Python?

• Compare with compiled languages like C++ or Fortran, Python Data Types
Variables

where types are identified by the programmer and by the Text


Numbers and Boolean
compiler before the program is run Values
Other Data Types
Operators

Control Structures
• Run-time typing is very convenient and helps with rapid Selection

code development... but requires the programmer to do Loops

more code testing for reliability. Functions

Summary
• The larger the program, the more significant the burden this
is!!!

1.21
Introduction to
Variable Data Types Python programming
language

Evrad Kamtchoum

• Available basic types: LOGIK

• Numbers: Integers and floating point (64-bit)

• Complex numbers: x = complex(3, 1) or x = 3 + 1j Objectives and


Content

What is Python?
• Strings, using double or single quotes: ”cat”, 0 dog 0
Python Data Types
Variables

• Boolean: True and False Text


Numbers and Boolean
Values
Other Data Types
• Lists, dictionaries, and tuples Operators

• These hold collections of variables Control Structures


Selection
Loops
• Specialty types: files, network connections, objects
Functions

Summary

• Custom types can be defined

1.22
Introduction to
Check a type Python programming
language

Evrad Kamtchoum

LOGIK

• A built-in function, type(), returns


Objectives and
the type of the data assigned to a Content
variable What is Python?

• It’s unusual to need to use this Python Data Types


Variables
in a program, but it’s available if Text

you need it! Numbers and Boolean


Values
Other Data Types
Operators

• Try this out in Python - do some Control Structures


Selection

assignments and reassignments Loops

and see what type() returns Functions

Summary

1.23
Introduction to
Variables and Memory Locations Python programming
language

Evrad Kamtchoum

LOGIK

• Variables refer to a value stored


in memory
Objectives and
Content

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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions
Copyright by Phondanai Khanti

Summary

1.25
Introduction to
Strings Python programming
language

Evrad Kamtchoum

LOGIK

• Strings are a basic data type in Python


Objectives and
Content
• Indicated using pairs of single ” or
What is Python?
double "" quotes Python Data Types
Variables
Text

• Multiline strings use a triple set of Numbers and Boolean


Values
Other Data Types
quotes (single or double) to start and Operators

end them Control Structures


Selection

Copyright by Brian Gregor


Loops

Functions
• Strings have many built-in functions...
Summary

1.26
Introduction to
String functions Python programming
language

Evrad Kamtchoum

LOGIK

About string functions...

• In the Python console, create a string variable called mystr Objectives and
Content

What is Python?

• type: dir(mystr) Python Data Types


Variables
Text
Numbers and Boolean
Values
• Try out some functions: len(mystr), [Link](), Other Data Types
Operators
[Link](), [Link](), help([Link]) Control Structures
Selection
Loops

• Need help? Try: help([Link]) Functions

Summary

1.27
Introduction to
The len() function Python programming
language

Evrad Kamtchoum

LOGIK

• The len() function is not a string specific function


Objectives and
Content

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

About numbers in Python


LOGIK

• When programming, not only in Python, if you say that a


variable has a numeric value, you are being ambiguous Objectives and
Content

What is Python?

• The reason is that numbers can be integers or floating Python Data Types
Variables

points, also called floats, for instance. Text


Numbers and Boolean
Values
Other Data Types
Operators
Term Definition
Positive or negative whole numbers without a decimal point Control Structures
Integer Selection
Example: 5, 10, -3, -15
Loops
Real numbers. Hence, they have a decimal point
Floating point (float)
Example: 4.75, -5.50, 11.0 Functions
Positive or negative whole numbers without a decimal point Summary
Complex numbers
Example: (1.0 + 2.0j)

1.29
Introduction to
Boolean Values Python programming
language

Evrad Kamtchoum
Boolean
• a True or False value, corresponding to the machine’s LOGIK

logic of understanding 1s and 0s, on or off, right or wrong,


true or false
Objectives and
Content

• Example: True, False What is Python?

Python Data Types


Variables
Text

• In Python the name Boolean is shortened to the type bool Numbers and Boolean
Values
Other Data Types
Operators

• Any object can be converted to Boolean (type bool) Control Structures


Selection
Loops

Functions

Summary

1.30
Introduction to
Lists Python programming
language

Evrad Kamtchoum

• A Python list is a general purpose 1-dimensional container


for variables. LOGIK

• i.e. it is a row, column, or vector of things

Objectives and
• Lots of things in Python act like lists or use list-style Content

notation. What is Python?

Python Data Types


Variables
Text
• Variables in a list can be of any type at any location, Numbers and Boolean
Values

including other lists. Other Data Types


Operators

Control Structures
Selection
• Lists can change in size: elements can be added or Loops

removed Functions

Summary

• Lists are not meant for high performance numerical


computing!

1.31
Introduction to
Making a list and checking it twice... Python programming
language

Evrad Kamtchoum

LOGIK

• Make a list with [] brackets


Objectives and
Content
• Append with the append()
What is Python?
function Python Data Types
Variables
Text
Numbers and Boolean
• Create a list with some initial Values
Other Data Types
elements Operators

Control Structures
Selection

• Create a list with N repeated Loops

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?

built-in functions Python Data Types


Variables
Text
Numbers and Boolean

• Let’s try out a few... Values


Other Data Types
Operators

Control Structures

• Also try the len() function to see how Selection


Loops

many things are in the list: len(list1) Functions

Summary

1.33
Introduction to
Accessing List Elements Python programming
language

Evrad Kamtchoum

• Lists are accessed by index


• All of this applies to accessing strings by index as well! LOGIK

• Index numbers start at 0


Objectives and
Content

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

• Last element: x[−1] Summary

• Next-to-last: x[−2]

1.34
Introduction to
Accessing List Elements Python programming
language

Evrad Kamtchoum

• Elements in a list are accessed by an index number


LOGIK

• Index numbers start at 0


Objectives and
Content

• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] What is Python?

Python Data Types


Variables
Text
• First element: x[0] → ’a’ Numbers and Boolean
Values
Other Data Types
Operators

• First element: x[2] → ’c’ Control Structures


Selection
Loops

Functions
• Last element: x[-1] → ’e’
Summary

• Next-to-last: x[-2] → ’d’

1.35
Introduction to
List Slicing Python programming
language
• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] Evrad Kamtchoum

• Slice syntax: x[start : end : step] LOGIK

• The start value is inclusive, the end value is exclusive


• Step is optional and defaults to 1
• Leaving out the end value means "go to the end" Objectives and
• Slicing always returns a new list copied from the existing Content

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

• First element: x[0:2] → [’a’,’b’] Functions

Summary

• Last element: x[-3:] → [’c’,’d’,’e’] # Third from the end to


the end

• Next-to-last: x[2:5:2] → [’c’,’e’] 1.36


Introduction to
List assignments and deletions Python programming
language

Evrad Kamtchoum

LOGIK

• Lists can have their elements overwritten or deleted (with


the del) command Objectives and
Content

What is Python?

• List: x=[’a’, ’b’, ’c’, ’d’ ,’e’] Python Data Types


Variables
Text
Numbers and Boolean
Values
• x[0] = -3.14 → x is now [-3.14, ’b’, ’c’, ’d’, ’e’] Other Data Types
Operators

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

What do you think will be printed? LOGIK

Objectives and
Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

1.38
Introduction to
Copying Lists Python programming
language

Evrad Kamtchoum

LOGIK

• How to copy (2 ways... there are more!):


Objectives and
Content

• y = x[:] or y = list(x) What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

1.39
Introduction to
Tuples Python programming
language

Evrad Kamtchoum

LOGIK

• Tuples are lists whose


Objectives and
elements can’t be Content
changed. What is Python?
• Like strings they are Python Data Types
Variables
immutable Text
Numbers and Boolean
Values
Other Data Types
• Indexing (including slice Operators

notation) is the same as Control Structures


Selection

with lists Loops

Functions

Summary

1.40
Introduction to
Return multiple values from a function Python programming
language

Evrad Kamtchoum

LOGIK

• Tuples are more useful


than they might seem at
Objectives and
first glance Content

What is Python?

Python Data Types


• They can be easily used to Variables
Text
return multiple values from Numbers and Boolean
Values
a function Other Data Types
Operators

Control Structures
• Python syntax can Selection
Loops

automatically unpack a Functions


tuple return value Summary

1.41
Introduction to
Dictionaries Python programming
language

Evrad Kamtchoum

x = {’a_key’:55, 100:’a_value’, 4.1:[5,6,7]} LOGIK

About dictionnaries
Objectives and
Content
• Dictionaries are another basic Python data type that are
What is Python?
tremendously useful Python Data Types
Variables
Text

• Create a dictionary with a pair of curly braces: x = {} Numbers and Boolean


Values
Other Data Types
Operators

Control Structures
• Dictionaries store values and are indexed with keys Selection
Loops

Functions

• Create a dictionary with some initial values: Summary

1.42
Introduction to
Dictionaries Python programming
language

Evrad Kamtchoum

• Values can be any Python thing


LOGIK

• 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

• Index dictionaries via keys: Loops

Functions
• x[’a_key’] → 55
Summary
• x[100] → ’a_value’

1.43
Introduction to
Try Out Dictionaries Python programming
language

Evrad Kamtchoum

• Create a dictionary in the LOGIK

Python console or Spyder


editor
Objectives and
Content
• Add some values to it just What is Python?

by using a new key as an Python Data Types


Variables
index. Can you overwrite a Text
Numbers and Boolean
value? Values
Other Data Types
Operators

Control Structures
• Try [Link]() and [Link]() Selection
Loops

Functions
• Try: del x[valid_key] → Summary

deletes a key/value pair


from the dictionary.

1.44
Introduction to
Operators Python programming
language

Python supports a wide variety of operators which act like func- Evrad Kamtchoum

tions, i.e. they do something and return a value:


LOGIK

• Arithmetic: +, -, *, /,
Objectives and
Content
• Logical: and or not
What is Python?

Python Data Types


Variables
• Comparison: >, <, >=, <=, !=, == Text
Numbers and Boolean
Values
Other Data Types

• Assignment: = Operators

Control Structures
Selection
Loops

• Bitwise: &, p, ∧, ∼, ,  Functions

Summary

• Identity: is, is not

• Membership: in, not in


1.45
Introduction to
Try Python as a calculator 2 Python programming
language

Evrad Kamtchoum

• Go to the Python prompt LOGIK

• Try out some arithmetic operators: +, −, ∗, /, %, ∗∗, ==, () Objectives and


Content

What is Python?

Python Data Types


Operator Function Variables
Text
+ Addition Numbers and Boolean
Values
- Subtraction Other Data Types

* Multiplication Operators

Control Structures
/ Division (Note: 3 / 4 is 0.75!) Selection

% Remainder (aka modulus) Loops

Functions
** Exponentiation
Summary
== Equals

1.46
Introduction to
Variable modifying operators Python programming
language

Evrad Kamtchoum

LOGIK

Some additional arithmetic operators that modify variable val-


ues:
Objectives and
Content

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

x /= y Divide the value of x by y x=x=x/y Operators

Control Structures
Selection
Loops

The += operator is by far the most commonly used of these! Functions

Summary

1.47
Introduction to
String operators Python programming
language

Evrad Kamtchoum

LOGIK

• Try using the + and += operators with strings in the Python


console. Objectives and
Content

What is Python?

• + concatenates strings. Python Data Types


Variables
Text
Numbers and Boolean

• += appends strings. Values


Other Data Types
Operators

Control Structures
Selection

• Index strings using square brackets, starting at 0. Loops

Functions

Summary

1.48
Introduction to
String operators Python programming
language

Evrad Kamtchoum

LOGIK

• Changing elements of a string by an index is not allowed:

Objectives and
Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

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

• Python provides an easy way to stick variable values into


LOGIK

strings called substitutions


Objectives and
Content
• Syntax for one variable: What is Python?

Python Data Types


Variables

• %s means sub in value Text


Numbers and Boolean
• variable name comes after a % Values
Other Data Types
Operators

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

• Operators can be combined


freely with variables and variable Objectives and
assignment Content

What is Python?

Python Data Types

• Try some out again! Variables


Text
Numbers and Boolean
Values
Other Data Types
• This time type them into the Operators

Control Structures
editor. Click the green triangle to Selection

run the file. Save the file and it Loops

will run. Functions

Summary

1.51
Introduction to
More operators Python programming
language

Evrad Kamtchoum

• Try some comparisons and Boolean operators. True and LOGIK

False are the keywords indicating those values:

Objectives and
Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

1.52
Introduction to
Comments Python programming
language

Evrad Kamtchoum

LOGIK

• # is the Python comment


character. On any line everything
Objectives and
after the # character is ignored Content
by Python What is Python?

Python Data Types


Variables

• There is no multi-line comment Text


Numbers and Boolean
Values
character as in C or C++ Other Data Types
Operators

Control Structures
• An editor like Spyder makes it Selection
Loops

very easy to comment blocks of Functions


code or viceversa. Check the Summary

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

• It indicates you will continue the same command on a new


line.

1.54
Introduction to
Control Structures in Python Python programming
language

Evrad Kamtchoum

• All programming languages contain a pre-included set of LOGIK

control structures that enable these control flows to


execute, which makes it conceivable
Objectives and
Content

• Control flow refers to the sequence a program will follow What is Python?

Python Data Types


during its execution Variables
Text
Numbers and Boolean
Values
• There are three types of control structures in Python: Other Data Types
Operators
• Sequential: the default working of a program
Control Structures
• Selection: this structure is used for making decisions by Selection
Loops
checking conditions and branching
• Repetition: this structure is used for looping, i.e., repeatedly Functions

Summary
executing a certain piece of a code block

1.55
Introduction to
If / Else Python programming
language

Evrad Kamtchoum

• If, elif, and else statements are used to implement LOGIK

conditional program behavior

Objectives and
• Syntax: Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

• elif and else are not required - used to chain together Summary

multiple conditional statements or provide a default case

1.56
Introduction to
If / Else Python programming
language

Evrad Kamtchoum

LOGIK

• Try out something like this in the


Spyder editor Objectives and
Content

What is Python?

• Do you get any error messages in the Python Data Types


Variables

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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Unknown Author Summary

1.58
Introduction to
The Use of Indentation Python programming
language

Evrad Kamtchoum

• Python uses whitespace (spaces or tabs) to define code


blocks LOGIK

• Code blocks are logical groupings of commands. They are Objectives and
Content
always preceded by a colon :
What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures

• This is due to an emphasis on code readability Selection


Loops

• Fewer characters to type and easier on the eyes! Functions

Summary

• Spaces or tabs can be mixed in a file but not within a code


block

1.59
Introduction to
If / Else code blocks Python programming
language

Evrad Kamtchoum

LOGIK

• Python knows a code


block has ended when the Objectives and
Content
indentation is removed
What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
• Code blocks can be Values

Unknown Author Other Data Types


nested inside others Operators

therefore if-elif-else • Note the lack of "end if", Control Structures


Selection
statements can be freely "end", curly braces, etc Loops

nested within others Functions

Summary

1.60
Introduction to
File vs. Console Code Blocks Python programming
language

Evrad Kamtchoum

• Python knows a code block has ended when the LOGIK

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

• Let’s try this out in Spyder Numbers and Boolean


Values
Other Data Types
Operators

• This sometimes causes problems when pasting code into Control Structures
Selection

the console Loops

Functions

Summary

• This issue is something the IPython console helps with

1.61
Introduction to
Match - Case Python programming
language

Evrad Kamtchoum

• Until version 3.10, Python never had a feature that LOGIK

implemented what the switch statement does in other


programming languages
Objectives and
Content
• So, if you wanted to execute multiple conditional What is Python?

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

• From version 3.10 upwards, Python has implemented a


switch case feature called "structural pattern matching" LOGIK

• You can implement this feature with the match and case Objectives and
Content
keywords:
What is Python?

Python Data Types


Variables
• To write switch statements with the structural pattern Text
Numbers and Boolean
matching feature, you can use the syntax below: Values
Other Data Types
Operators

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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

1.64
Introduction to
While Loops Python programming
language

Evrad Kamtchoum

• While loops have a


condition and a code block LOGIK

• The indentation
indicates what’s in the Objectives and
Content
while loop
• The loop runs until the What is Python?

Python Data Types


condition is false Variables
Text
Numbers and Boolean
Values
• The break keyword will Other Data Types
Operators
stop a while loop running Control Structures
Selection
Loops

• In the Spyder edit enter in Functions

some loops like these. Summary

Save and run them one at


a time. What happens with
the first loop?

1.65
Introduction to
For Loops Python programming
language

Evrad Kamtchoum

• for loops are a little


different. They loop LOGIK

through a collection of
things
Objectives and
Content

• The for loop syntax has a What is Python?

collection and a code block Python Data Types


Variables
Text
Numbers and Boolean
• Each element in the Values
Other Data Types
collection is accessed in Operators

order by a reference Control Structures


variable Selection
Loops
• Each element can be
Functions
used in the code block Summary

• The break keyword can be


used in for loops too

1.66
Introduction to
Processing lists element-by-element Python programming
language

Evrad Kamtchoum

• A for loop is a convenient way to process every element in


LOGIK

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

• Open the file looping_lists.py Selection


Loops

Functions

Summary
• Let’s look at code samples for each of these

1.67
Introduction to
The range() function Python programming
language

Evrad Kamtchoum

• The range() function auto-generates sequences of LOGIK

numbers that can be used for indexing into lists

Objectives and
• Syntax: range(start, exclusive end, increment) Content

What is Python?

Python Data Types


• range(0,4) → produces the sequence of numbers 0,1,2,3 Variables
Text
Numbers and Boolean
Values
Other Data Types
• range(-3,15,3) → -3,0,3,6,9,12 Operators

Control Structures
Selection
Loops
• range(4,-3,-2) → 4,2,0,-2
Functions

Summary

• Try this: print(range(4))

1.68
Introduction to
Defining a Function in Python Python programming
language

Evrad Kamtchoum

Python’s functions are an invaluable tool for programmers.


LOGIK

Objectives and
Content

What is Python?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators
• Don’t forget to return a value from the function. We will need plus_ten(a) to do a specific calculation for us and not Control Structures
just print something
Selection
• Pay attention to the following. When we define a function, we specify in parentheses a parameter. In the plus_ten() Loops
function, "a" is a parameter. Later, when we call this function, it is correct to say we provide an argument, not a
parameter. So we can say "call plus_ten() with an argument of 2, call plus_ten() with an argument of 5".. Functions

Summary

1.70
Introduction to
Creating a Function with a Parameter Python programming
language

Evrad Kamtchoum

In programming, return regards LOGIK

the value of y; it just says to the


machine "after the operations
executed by the function f, return
Objectives and
to me the value of y". "Return" Content
plays a connection between the What is Python?

second and the third step of the Python Data Types

process. In other words, a function Variables


Text
can take an input of one or more Numbers and Boolean
Values
variables and return a single Other Data Types
Operators
output composed of one or more
Control Structures
values. This is why "return" can be Selection

used only once in a function. Loops

Functions

People often confuse print and return, and the type of situations Summary

when we can apply them

1.71
Introduction to
Creating a Function with a Parameter Python programming
language

Evrad Kamtchoum

"Print" takes a statement or, better, LOGIK

an object, and provides its printed


representation in the output cell. It
just makes a certain statement
Objectives and
visible to the programmer. Content
Otherwise, print does not affect the What is Python?

calculation of the output. Python Data Types

Differently, return does not Variables


Text
Numbers and Boolean
Values
visualize the output. It specifies Other Data Types
Operators
what a certain function is
Control Structures
supposed to give back. It’s Selection

important you understand what Loops

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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

• 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?

Python Data Types


Variables
Text
Numbers and Boolean
Values
Other Data Types
Operators

Control Structures
Selection
Loops

Functions

Summary

1.74
Introduction to
Creating Functions Containing a Few Arguments Python programming
language

Evrad Kamtchoum

LOGIK

You can call the function for, say,


10, 3, and 2. You will get 4. Objectives and
Just be careful with the order in Content

which you state the values. In this What is Python?

case, we assigned 10 to the Python Data Types


Variables
variable a, 3 to b, and 2 to c. Text

Otherwise, the order won’t matter if Numbers and Boolean


Values
Other Data Types
Operators
and only if you specify the names Control Structures
of the variables within the Selection
Loops
parentheses.
Functions

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

Function Description What is Python?


type() obtains the type of variable you use as an argument Python Data Types
int() transforms its argument in an integer data type Variables
float() transforms its argument in a float data type Text

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

1 Python is an open-source, general-purpose, high-level Evrad Kamtchoum

programming language
2 Python’s popularity lies on two main pillars. One is that it is an LOGIK

easy-to-learn programming language designed to be highly


readable, with a syntax quite clear and intuitive. And the second
reason is its user-friendliness does not take away from its Objectives and
Content
strength.
What is Python?
3 Python implements dynamic typing and variables names are Python Data Types
case sensitive Variables
Text

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

5 Python’s functions are an invaluable tool for programmers. Selection


Loops

6 Indentation is a key concept in python. It is used to define blocks Functions

of code and structure the code Summary

7 Most of arithmetic, logical, comparison, etc., are implemented in


Python
8 Python implements selective (IF-ELSE, MATCH-CASE) and
iterative (WHILE, FOR) control flows.
1.77

You might also like