0% found this document useful (0 votes)
21 views170 pages

Python In Operator Complexity Explained

Uploaded by

Nguyen Van Kien
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)
21 views170 pages

Python In Operator Complexity Explained

Uploaded by

Nguyen Van Kien
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

Business Analytics

Python Basics

Phan Xuân Hiếu • VNU-UET • hieupx@[Link]


Lecture objectives

§ Introduce primitive data types in Python and their characteristics.


§ Give an overview of the Python environments like IPython, Jupyter notebooks ...
§ Introduce control flows like if-elif-else, for loops, while loops, ...
§ Introduce important built-in data structures like tuple, list, dict, set ...
§ Explain how to handle errors and exceptions in Python.
§ Explain the Python coding conventions
§ Introduce essential libraries for analytics like numpy, pandas, matplotlib, seaborn ...

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 2
Contents

1. IPython and Jupyter Notebooks


2. Python Language Basics
3. Data Structures and Sequences
4. Errors and Exception Handling
5. Files and the Operating System
6. Python Coding Conventions
7. Essential Python Libraries for Data Analysis

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 3
IPython and Jupyter Notebooks
– The Python Interpreter
– IPython Basics
– Jupyter Notebooks

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 4


The Python interpreter
§ Python is an interpreted language
– The Python interpreter runs a program by executing on statement at a time

§ Invoke Python interpreter on the command line

§ To exit the Python interpreter:


– Type exit(), quit(), or press Ctrl-D (works on Linux and macOS only)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 5


Running a Python program
§ Calling python with a .py file § Run it by executing the command:
§ Suppose a simple program named
hello_world.py with the following
contents:
§ Note: the hello_world.py file must be
in your current working directory

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 6


Other ways to run Python code
§ Data analysts and data scientists normally use
– IPython: an enhanced Python interpreter, or
– Jupyter notebooks: web-based code notebooks

§ When using the %run command, IPython executes the code in the specified file in
the same process

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 7


10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 8
Running the Jupyter Notebook
§ Jupyter notebook:
• A type of interactive document for
code, text (including Markdown),
data visualizations, and other
outputs
• The Jupyter notebook interacts
with kernels ...
§ The Python Jupyter kernel uses the
IPython system for its underlying
behaviors
§ Jupyter will automatically open
your default web browser (unless
starting it with --no-browser)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 9


Jupyter notebook on web browser

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 10


Jupyter notebook file
§ Save the current document → save to a .ipynb file
§ .ipynb: a self-contained file format containing
– Python code
– Text (e.g., Markdown)
– Any evaluated code outputs
§ Different ways to work with Jupyter notebooks
– Local computer
– Deploy Jupyter kernel on servers and access remotely

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 11


Tab completion in IPython and Jupyter Notebook

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 12


Introspection using a question mark (?) before or after

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 13


Function or method introspection: showing the docstring

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 14


Introspection with wildcard (*)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 15


Python Language Basics
– Language Semantics
– Standard Python Scalar Types
– Control Flows

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 16


Language semantics

§ Indentation, not braces § Dynamic references with different types


§ Everything is object § Attributes and methods
§ Comments in Python code § Duck typing
§ Function & object method calls § Python modules
§ Variable and reference § Binary operators and comparisons
§ Passing objects as arguments § Mutable and immutable objects
§ Variable binding and scope

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 17


Indentation, not braces

§ Recommended: four spaces


– And replacing tabs with four spaces
§ IPython and Jupyter notebooks
– Automatically insert four spaces for indentation, and
– Replace tabs by four spaces.
§ Semicolons are not required to terminate a statement
– Use them to separate multiple statements: a = 5; b = 6; c = 7 use them
– But putting multiple statements on a single line is generally discouraged

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 18


Everything is an object

§ Everything in Python is an object, called Python object


– Primitive data types like numbers, strings are objects
§ Modules are objects
§ Functions are objects

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 19


Comments in Python code

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 20


Function and object method calls

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 21


Variable and reference
§ Assigning a variable (or name) in Python is creating a reference.

§ In some languages, the assignment will create a copy

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 22


Passing objects as arguments

§ When passing an object as an argument to a function,


– Python does not create a copy of the object,
– A new local variable is created and referencing the same (original) object in memory

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 23
Variable binding and scope

§ If you reassign a variable inside a


function (i.e., bind it to a new object),
• It does not affect a variable with the
same name in the outer scope.
§ This is because Python creates a
new local variable instead of
modifying the outer variable.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 24
Dynamic references with different types

§ Variables in Python have no inherent type


associated with them
§ A variable can refer to a different type
of object simply by doing an assignment.
§ I.e., variables can hold different data
types over their lifetime.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 25
Python – a typed language

§ Variables are names for objects within a particular namespace.


§ The type information is stored in the object itself.
§ Somes might hastily conclude that Python is not a “typed language.”

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 26
Python – a typed language (2)
§ In some languages (e.g., PHP), the string
“5” is implicitly converted to an integer:
• I.e., “5” + 5 = 5 + 5 = 10

§ In some other languages (e.g., JavaScript),


the integer 5 might be cast to a string:
• I.e., “5” + 5 = “5” + “5” = “55”

§ In Python, such implicit casts are not


allowed.
§ Implicit conversions will occur only in
some certain cases:

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 27
Knowing the type of an object is important

§ Useful to write functions that can


handle different kinds of input
§ Checking the type of objects using the
isinstance function

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 28
Attributes and methods

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 29


Duck typing

§ Duck typing is a concept in


dynamic typing where an object’s
suitability for a task is determined
by its methods and properties,
rather than its explicit type.
§ This follows the principle: “If it
looks like a duck, swims like a
duck, and quacks like a duck,
then it probably is a duck.”

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 30
Python modules

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 31


Import a module and its components

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 32


Binary
operators
and
comparisons

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 33
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 34
Mutable and immutable objects

§ Many objects in Python are mutable


– List, dictionary, NumPy array, and
– Most user-defined types (classes)
§ Some others are immutable
– String
– Tuple

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 35


Examples of mutable and immutable objects

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 36


Standard Python scalar types
§ Python has a small set of built-in types for handling
– Numerical data, strings, Boolean (True or False) values, ...
§ These “single value” types are sometimes called scalar types.

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 37


Numeric types

§ The primary Python types for numbers are int and float.
§ An int can store arbitrarily large numbers:

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 38
Floating-point numbers

§ Floating-point numbers are


represented with the float type.
§ Under the hood, each one is a
double-precision value.
§ They can also be expressed with
scientific notation.

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 39


Strings
§ Many people use Python for its built-in string handling capabilities.
§ String literals using either single quotes (‘hello’) or double quotes (“hello”)
§ Double quotes are generally favored.

§ The Python string type is str.


§ Use triple quotes ’’’ or ””” for multiline strings with line breaks

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 40
Python strings are immutable

§ You cannot modify a string

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 41


If still need to “modify” a string?
§ Using the replace method:

§ The variable a is unmodified.

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 42


Convert to string using the function str

§ Many Python objects can be


converted to a string using the str
function:

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 43


Strings are sequences

§ A string is a sequence of Unicode


characters
§ Strings can be treated like other
sequences, such as lists and tuples

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 44


Escape character
§ The backslash character \ is an escape character
§ Used to specify special characters like newline \n or Unicode chars

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 45


Write a string with the backslash character \

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 46


Bytes and Unicode

§ In modern Python (i.e., Python 3.0 and up)


– Unicode has become the first-class string type to enable more consistent handling
of ASCII and non-ASCII text.

§ In older versions of Python, strings were all bytes without any explicit
Unicode encoding.
– You could convert to Unicode assuming you knew the character encoding.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 47
Unicode and encoding conversion

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 48


Booleans

§ The two Boolean values in Python


are written as True and False.
§ Comparisons and other conditional
expressions evaluate to either True
or False.
§ Boolean values are combined with
the and and or keywords

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 49


Type casting

§ The str, bool, int, and float


types are also functions that can be
used to cast values to those types

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 50


None
None is also a common default value
None is the Python null value type for function arguments

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 51


Dates and times

§ The built-in Python


datetime module
provides datetime,
date, and time types.
§ The datetime type
combines the information
stored in date and time
and is the most
commonly used:

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 52


Control flows
§ if, elif, and else
§ for loops
§ while loops
§ The keyword pass
§ The range function

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 53
if, elif, and else

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 54


Compound condition – evaluated from left to right

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 55
for loops

for loops are for iterating over a sequence or collection (like list or tuple)

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 56
for loops with continue and break

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 57


Variable unpacking in for loops

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 58
while loops

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 59


The keyword pass

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 60


The range function

The range function generates a sequence of evenly spaced integers

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 61


Using range in for loop

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 62


The range function vs. Iterator

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 63


The range function vs. Generator

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 64


Data Structures and Sequences
– Tuple
– List
– Dictionary
– Set
– Built-in Sequence Functions
– List, Set, and Dictionary Comprehensions

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 65


tuple – a fixed-length, immutable sequence of objects

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 66
Accessing tuple elements

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 67
Trying to modify tuple’s content

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 68
Concatenate tuples and more ...

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 69
Unpacking tuples

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 70
Swapping two variables

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 71
Variable unpacking when iterating over a sequence of tuples

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 72
Unpacking and only selecting several first elements

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 73
Tuple’s methods

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 74
List
§ Like a string, a list is a sequence of values.
§ In a list, values can be any type.
§ A list can contain values of different types (i.e., heterogeneous)
§ The values in a list are called elements.
§ Lists are mutable, i.e., can be modified.
§ Lists can be nested, i.e., a list can contain other lists

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 75
Several ways to create Python lists

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 76
List – adding, changing, and removing elements

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 77


Check elements in a list

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 78


Access elements in a list using index

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 79


Concatenating and combining lists

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 80
Note: combining is more expensive than extending

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 81
Sorting elements in a list

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 82
Sorting elements in a list with user-defined functions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 83
Illustration of Python slicing conventions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 84
Examples of list slicing

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 85
Methods of Python lists

§ append() § pop()
§ count() § sort()
§ insert() § copy()
§ reverse() § index()
§ clear() § remove()
§ extend()

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 86
Dictionary

§ The dictionary or dict may be the most important built-in Python data
structure.

§ In other programming languages, dictionaries are sometimes called hash maps or


associative arrays.

§ A dictionary stores a collection of key-value pairs, where key and value are
Python objects.

§ Each key is associated with a value so that a value can be conveniently


retrieved, inserted, modified, or deleted given a particular key.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 87
Different ways to create a dictionary

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 88


Different ways to create a dictionary (2)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 89


Different ways to create a dictionary (3)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 90


Working with dictionary

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 91


Working with dictionary (2)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 92


Working with dictionary (3)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 93


Creating dictionaries from sequences

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 94


Default values

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 95
Building a dict of〈key → group of objects〉

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 96


Building a dict of〈key → group of objects〉using setdefault

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 97


Building a dict of〈key → group of objects〉using defaultdict

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 98


Valid dictionary key types

§ Values of a dict can be any Python object


§ Keys generally have to be immutable objects
– Scalar types: int, float, str
– Tuples (all the objects in the tuple need to be immutable, too)
§ The technical term here is hashability.
§ Checking whether an object is hashable with the hash function.

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 99


10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 100
Set

§ A set is an unordered collection of unique elements.


§ A set can be created in two ways:
– Via the set function, or
– Via a set literal with curly braces.
§ Sets support mathematical set operations like
– Union,
– Intersection,
– Difference, and
– Symmetric difference

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 101
Examples of set creation and operations

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 102
Examples of subset and superset checking

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 103
Set operations

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 104
In-place set operations (useful for very large sets)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 105
Set elements must be immutable

§ Set elements must be hashable.


§ If need to store list-like elements in a set, convert them to tuples:

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 106
Built-in sequence functions
§ enumerate
§ sorted
§ zip
§ reversed

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 107
enumerate function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 108
The use case of the enumerate function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 109
sorted function

sorted returns a new sorted list from the elements of any sequences:

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 110
Examples of the sorted function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 111
zip function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 112
Examples of the zip function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 113
A common use of the zip function

A common use of zip is simultaneously iterating over multiple sequences,


possibly also combined with enumerate:

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 114
reversed function

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 115
List, set, dictionary comprehensions

§ List comprehensions are a convenient and widely used feature.


§ Allow to concisely form a new list by filtering the elements of a collection.
§ The basic form:
[expr for item in iterable if condition]
§ This is equivalent to the following for loop:
result = []
for item in collection:
if condition:
[Link](expr)
10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 116
Examples of list comprehensions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 117
More complex examples of list comprehensions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 118
Dictionary and set comprehensions

§ Dictionary comprehensions:
{key: value for item in iterable if condition}
§ Set comprehensions:
{expr for item in iterable if condition}

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 119
Examples of dictionary comprehensions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 120
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 121
Examples of set comprehensions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 122
Errors and Exception Handling

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 123
Types of errors in Python

§ Error and exception handling is essential for writing robust Python programs.

§ Python has different types of errors. Some errors are:


– SyntaxError – incorrect Python syntax
– NameError – using a variable that has not been defined
– TypeError – performing an invalid operation on a data type
– IndexError – accessing an invalid list index
– KeyError – accessing a non-existent key in a dictionary
– ValueError – passing an incorrect value to a function
– ZeroDivisionError – dividing by zero

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 124
Exception handling with try-except-else-finally

§ try:
– This block will test the expected errors to occur

§ except:
– Here you can handle the error(s)
– We can have multiple except blocks or one except block can handle multiple exceptions

§ else:
– If there is no exception then this block will be executed

§ finally:
– This block always gets executed either exception is generated or not

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 125
Python try-except-else-finally syntax
try: try:
# some code # some code
except Error1: except (Error1, Error2, ...):
# handling exception # handling exceptions
except Error2: else:
# handling exception # execute if no exception
... finally:
else: # some code... (always executed)
# execute if no exception
finally:
# some code... (always executed)

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 126
Handling exceptions with try-except

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 127
Catching multiple exceptions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 128
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 129
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 130
Using else with try-except

§ The else block runs only


if no exceptions occur.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 131
Using finally block (always executed)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 132
Files and the Operating System

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 133
Open a file in Python
Syntax: f = open(filename, mode, encoding, ...)

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 134
Open and read the entire file

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 135
Open and read a file line by line

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 136
Open a file and read all lines at once

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 137
Open and read all lines of a file in one or two code lines

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 138
Open a file and read all lines with list comprehensions

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 139
Open a file with the “with” statement

The file will be closed automatically when exiting the with block

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 140
Reading a file in text and binary modes

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 141
Seeking and reading data in the text and binary modes

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 142
Writing data to a file in the text mode

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 143
Writing data to a file in the text mode with writelines

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 144
Checking the default encoding in the system

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 145
Listing available encodings in the system

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 146
Python Coding Conventions
– The Zen of Python
– Python Enhancement Proposal 8 (PEP8)

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 147
The Zen of Python

§ The Zen of Python is a collection of 19 guiding principles for writing


clean, readable, and beautiful Python code.
§ It was written by Tim Peters and can be accessed in Python by running:
import this

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 148
🌸 Code should be elegant and aesthetically pleasing.

🍄🟫 Code should be clear and unambigous.


🌸
🍄🟫 🐤 Prefer simple solutions whenever possible.
🐤
👍 Some problems require complexity, but avoid
👍
unnecessary complications
🦊
🦊 Avoid deeply nested structures for better readability.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 149
🤵 Use whitespace and clear formatting to improve readability.
🦕 Code is read more often than written, so prioritize clearity.

🐼 Stick to consistent rules instead of making exceptions.


👌 Real-world needs sometimes strict adherence to rules.

🤵 🍓 Handle errors explicitly instead of ignoring them.


🦕
🐼
👌
🍓 🤐 If you must silence an error, do it intentionally.
🤐

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 150
🐎 Code should be deterministic and predictable.
🐍 Python encourages clarity and consistency in problem-solving.

🌷 A playful reference to Python’s creator, Guido van Rossum,


Who is Dutch.
🐆 Don’t overthink, start coding

🍺 Avoid rushed and bad decisons, some things are


better left undone.
🐎
🐍
🌷
🐆
🍺

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 151
🐵 Code should be self-explanatory.
🐙 If you can clearly describe how it works,
it is likely a good design
🍿 Use modules, classes, and functions to
organize code effectively.

🐵
🐙
🍿
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 152
What is Python Enhancement Proposal 8 (PEP8)?

§ PEP 8 (Python Enhancement Proposal 8) is the official style guide for writing
clean, readable, and consistent Python code.

§ It was created by Guido van Rossum, Barry Warsaw, and Nick Coghlan to promote
best practices in Python development.

§ PEP 8 ensures code readability and maintainability, making it easier for


teams to collaborate.

§ URL: [Link]

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 153
Whitespace
1) Use spaces instead of tabs for indentation.
2) Use four spaces for each level of syntactically significant indenting.
3) Lines should be 79 characters in length or less.
4) Continuations of long expressions onto additional lines should be indented by
four extra spaces from their normal indentation level.
5) In a file, functions and classes should be separated by two blank lines.
6) In a class, methods should be separated by one blank line.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 154
Whitespace (2)
7) In a dictionary, put no whitespace between each key and colon; put a single space
before the corresponding value if it fits on the same line.
8) Put one – and only one – space before and after the = operator in a variable
assignment.
9) For type annotations, ensure that there is no separation between the variable
name and the colon, and use a space before the type information.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 155
Naming
§ Functions, variables, and attributes should be in lowercase_underscore format.
§ Protected instance attributes should be in _leading_underscore format.
§ Private instance attributes should be in __double_leading_underscore format.
§ Classes (including exceptions) should be in CapitalizedWord format.
§ Module-level constants should be in ALL_CAPS format.
§ Instance methods in classes should use self, which refers to the object, as the name of
the first parameter.
§ Class methods should use cls, which refers to the class, as the name of the first
parameter.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 156
Expressions and statements
§ Use inline negation (if a is not b) instead of negation of positive expressions (if not a
is b).
§ Don’t check for empty containers or sequences (like [] or "") by comparing the length to zero
(if len(somelist) == 0). Use if not somelist and assume that empty values will
implicitly evaluate to False.
§ The same thing goes for non-empty containers or sequences (like [1] or "hi"). The statement
if somelist is implicitly True for non-empty values.
§ Avoid single-line if statements, for and while loops, and except compound statements.
Spread these over multiple lines for clarity.
§ If you can’t fit an expression on one line, surround it with parentheses and add line breaks and
indentation to make it easier to read.
§ Prefer surrounding multiline expressions with parentheses over using the \ line continuation
character.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 157
Imports

§ Always put import statements (including from x import y) at the top of a


file.
§ Always use absolute names for modules when importing them, not names relative
to the current module’s own path. For example, to import the foo module from
within the bar package, you should use from bar import foo, not just
import foo.
§ If you must do relative imports, use the explicit syntax from . import foo.
§ Imports should be in sections in the following order: standard library modules,
third-party modules, your own modules. Each subsection should have
imports in alphabetical order.
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 158
Things to remember

§ Always follow the Python Enhancement Proposal #8 (PEP 8) style guide when
writing Python code.
§ Sharing a common style with the larger Python community facilitates
collaboration with others.
§ Using a consistent style makes it easier to modify your own code later.
§ Community tools like black and pylint can automate compliance with PEP 8,
making it easy to keep your source code in good style.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 159
Essential Python Libraries for Data Analysis
– NumPy
– pandas
– matplotlib
– SciPy
– scikit-learn
– statsmodels

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 160
NumPy – Numerical Python

§ A cornerstone of numerical computing in Python.


§ Providing data structures, algorithms, and libraries for most scientific
applications involving numerical data in Python.
§ Libraries written in a low-level language, such as C or FORTRAN.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 161
NumPy contains, among other things:
§ A fast and efficient multidimensional array object ndarray
§ Functions for performing element-wise computations with arrays or
mathematical operations between arrays.
§ Tools for reading and writing array-based datasets to disk
§ Linear algebra operations, Fourier transform, and random number generation
§ A mature C API to enable Python extensions and native C or C++ code to
access NumPy’s data structures and computational facilities

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 162
pandas

§ Provides high-level data structures and functions designed to make working


with structured or tabular data intuitive and flexible.
§ Primary objects in pandas: Series and DataFrame.
– Series: a one-dimensional labeled array object.
– DataFrame: a tabular, column-oriented data structure with row and column labels.

§ Provides convenient indexing functionality to enable you to reshape, slice


and dice, perform aggregations, and select subsets of data.
§ Helped enable Python to be a powerful and productive data analysis
environment.
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 163
matplotlib

§ matplotlib is the most popular Python library for producing plots and other two-
dimensional data visualizations.
§ Originally created by John D. Hunter, and is now maintained by a large team of
developers.
§ It is designed for creating plots suitable for publication.
§ While there are other visualization libraries available to Python programmers,
matplotlib is still widely used and integrates reasonably well with the rest of the
ecosystem.
§ matplotlib is a safe choice as a default visualization tool.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 164
SciPy

§ SciPy is a collection of packages addressing a number of foundational problems in


scientific computing.
§ Here are some of the tools it contains in its various modules:
– [Link] – integration routines & differential equation solvers.
– [Link] – linear algebra routines and matrix decompositions ...
– [Link] – function optimizers (minimizers) and root finding algorithms.
– [Link] – signal processing tools.
– [Link] – implementing many common mathematical functions ...
– [Link] – standard continuous and discrete probability distributions (density functions,
samplers, continuous distribution functions), various statistical tests, ...

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 165
scikit-learn

§ The premier general-purpose machine learning toolkit for Python programmers.

§ More than two thousand different individuals have contributed code to the
project. It includes submodules for such models as:
– Classification: SVM, nearest neighbors, random forest, logistic regression, etc.
– Regression: Lasso, ridge regression, etc.
– Clustering: k-means, spectral clustering, etc.
– Dimensionality reduction: PCA, feature selection, matrix factorization, etc.
– Model selection: Grid search, cross-validation, metrics
– Preprocessing: Feature extraction, normalization

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 166
statsmodels

§ A statistical analysis package that was seeded by work from Stanford University
statistics professor Jonathan Taylor, who implemented a number of regression
analysis models popular in the R programming language.
§ Skipper Seabold and Josef Perktold formally created the new statsmodels project
in 2010 and since then have grown the project to a critical mass of engaged users
and contributors.
§ Nathaniel Smith developed the Patsy project, which provides a formula or model
specification framework for statsmodels inspired by R’s formula system.
§ Compared with scikit-learn, statsmodels contains algorithms for classical
(primarily frequentist) statistics and econometrics.
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 167
Primary submodules in statsmodels
§ Regression models: linear regression, generalized linear models, robust linear
models, linear mixed effects models, etc.
§ Analysis of variance (ANOVA).
§ Time series analysis: AR, ARMA, ARIMA, VAR, and other models.
§ Nonparametric methods: Kernel density estimation, kernel regression.
§ Visualization of statistical model results.
statsmodels is more focused on statistical inference, providing uncertainty
estimates and p-values for parameters. scikit-learn, by contrast, is more prediction
focused.

March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 168
Lecture summary
§ Introduced primitive data types in Python and their characteristics.
§ Gave an overview of the Python environments like IPython, Jupyter notebooks ...
§ Introduced control flows like if-elif-else, for loops, while loops, ...
§ Introduced important built-in data structures like tuple, list, dict, set ... and their
operations (e.g., tuple / list / dict / set comprehensions)
§ Explained how to handle errors and exceptions in Python.
§ Explained the Python coding conventions: The Zen of Python and PEP-8
§ Introduced essential libraries for analytics like numpy, pandas, matplotlib, scikit-
learn, statsmodels ...
10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 169
Lecture materials

§ Allen B. Downey. Think Python: How to Think Like a Computer Scientist. O’Reilly Media
Inc., 2024.
§ Luciano Ramalho. Fluent Python, O’Reilly Media Inc., 2015.
§ Brett Slatkin. Effective Python: 125 Specific Ways to Write Better Python, Pearson
Education, Inc., 2025.
§ Wes McKinney. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and
Jupyter, O’Reilly Media Inc., 2022.
§ Guido van Rossum, Barry Warsaw, and Alyssa Coghlan. Python Enhancement Proposal
8 (PEP 8) – Style Guide for Python Code, 2001. URL: [Link]

10 March 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 170

You might also like