Python In Operator Complexity Explained
Python In Operator Complexity Explained
Python Basics
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 2
Contents
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
§ When using the %run command, IPython executes the code in the specified file in
the same process
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 23
Variable binding and scope
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 24
Dynamic references with different types
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 25
Python – 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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 27
Knowing the type of an object is important
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 28
Attributes and methods
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 30
Python modules
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
§ 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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 40
Python strings are immutable
§ 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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 53
if, elif, and else
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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 58
while loops
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
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.
§ A dictionary stores a collection of key-value pairs, where key and value are
Python objects.
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 87
Different ways to create a dictionary
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] 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
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
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
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.
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
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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 148
🌸 Code should be elegant and aesthetically pleasing.
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.
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.
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.
§ 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 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
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
§ 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
March 10, 2025 Python Basics • Phan Xuân Hiếu • VNU-UET • hieupx@[Link] 165
scikit-learn
§ 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