Practicing Python 3
Mosky
Python?
Websites
➤ Pinkoi
➤ Google search engine
➤ Instagram
➤ MAU: 700M @ 2017/5
➤ Uber
➤ Pinterest
3
Desktop Applications
➤ Dropbox
➤ Disney
➤ For animation studio tools.
➤ Blender
➤ A 3D graphics software.
4
Science
➤ NASA
➤ LIGO
➤ Gravitational waves @ 2016
➤ LHC
➤ Higgs boson @ 2013
➤ MMTK
➤ Molecular Modelling Toolkit
5
Embedded System
➤ iRobot uses Python.
➤ Raspberry Pi supports Python.
➤ Linux has built-in Python.
6
Why?
➤ Python is slower than C, Java,
➤ But much faster to write,
➤ And easy to speed up.
➤ Numba | Cython
➤ Has the rich libraries.
➤ Emphasizes code readability.
➤ Easier to learn.
➤ Easier to co-work.
➤ “Time is money.”
7
Showcases
A Website in a Minute
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
9
Symbolic Mathematics
from sympy import symbols
from sympy import diff

x = symbols('x')
x**2
diff(x**2)
10
Data Visualization
import pandas as pd
import numpy as np
ts = pd.Series(
np.random.randn(1000),
index=pd.date_range(
'1/1/2000', periods=1000
)
)
ts = ts.cumsum()
ts.plot()
11
Mosky
➤ Python Charmer at Pinkoi.
➤ Has spoken at
➤ PyCons in 

TW, MY, KR, JP, SG, HK,

COSCUPs, and TEDx, etc.
➤ Countless hours 

on teaching Python.
➤ Own the Python packages:
➤ ZIPCodeTW, 

MoSQL, Clime, etc.
➤ http://mosky.tw/
12
➤ The Foundation Part:
➤ Primitives
➤ If & While
➤ Composites
➤ For, Try, Def
➤ Common Functions
➤ Input & Output
➤ Command Line
Arguments
➤ The Fascinating Part:
➤ Yield
➤ Comprehensions
➤ Functional Tricks
➤ Import Antigravity***
➤ Module & Package
➤ Class
➤ And the checkpoints!
The Outline
13
Our Toolbox
(a terminal) Master the machine.
Python 3 Not Python 2.
Jupyter Notebook Learn Python with browsers.
Visual Studio Code A full-featured source code editor.
(other libs) Will be introduced in this slides.
We Will Use
15
➤ Open a terminal:
➤ Spotlight (Cmd-Space) / “terminal”
➤ Install Homebrew by executing the command:
➤ $ /usr/bin/ruby -e "$(curl -fsSL https://
raw.githubusercontent.com/Homebrew/install/master/
install)"
➤ Execute the commands:
➤ $ brew install python3
➤ $ pip3 install jupyter numpy scipy sympy matplotlib
ipython pandas flask beautifulsoup4 requests seaborn
statsmodels scikit-learn
➤ Note the above commands are just a single line.
On Mac
16
➤ Open a terminal:
➤ Spotlight (Cmd-Space) / “terminal”
➤ Install Homebrew by executing the command:
➤ $ /usr/bin/ruby -e "$(curl -fsSL https://
raw.githubusercontent.com/Homebrew/install/master/
install)"
➤ Execute the commands:
➤ $ brew install python3
➤ $ pip3 install jupyter numpy scipy sympy matplotlib
ipython pandas flask beautifulsoup4 requests seaborn
statsmodels scikit-learn
➤ Note the above commands are just a single line.
Hint to talk to macOS. Enter
the command without $.
On Mac
17
➤ Install Python 3 with Miniconda:
➤ http://conda.pydata.org/miniconda.html
➤ Python 3.6 / Windows / 64-bit (exe installer)
➤ Open Anaconda's terminal:
➤ Start Menu / Search / Type “Anaconda Prompt”
➤ Right-click the item and choose “Run as administrator”.
➤ Execute the commands:
➤ > conda install jupyter numpy scipy sympy matplotlib
ipython pandas flask beautifulsoup4 requests seaborn
statsmodels scikit-learn
➤ Note the above commands are just a single line.
On Windows
18
➤ Install Python 3 with Miniconda:
➤ http://conda.pydata.org/miniconda.html
➤ Python 3.6 / Windows / 64-bit (exe installer)
➤ Open Anaconda's terminal:
➤ Start Menu / Search / Type “Anaconda Prompt”
➤ Right-click the item and choose “Run as administrator”.
➤ Execute the commands:
➤ > conda install jupyter numpy scipy sympy matplotlib
ipython pandas flask beautifulsoup4 requests seaborn
statsmodels scikit-learn
➤ Note the above commands are just a single line.
Hint to talk to Windows. Enter
the command without >.
On Windows
19
If You Already Have Python
➤ Try to install the packages:
➤ The Jupyter Notebook
➤ jupyter
➤ The SciPy Stack
➤ numpy scipy sympy matplotlib ipython pandas
➤ The web-related libs
➤ flask beautifulsoup4 requests
➤ The data-related libs
➤ seaborn statsmodels scikit-learn
➤ If fail, clean uninstall Python, and follow the previous slides.
20
Common MS-DOS Commands
21
C: Go C: drive.
cd PATH
Change directory to PATH.

PATH can be /Users/python/, python/, etc.
dir Display the contents of a directory.
cls Clear the terminal screen.
python PATH
python3 PATH
Execute a Python program.
exit Exit the current command processor.
Common Unix-like (Mac) Commands
22
cd PATH
Change Directory to PATH.

PATH can be /Users/python/, python/, etc.
ls List the contents of a directory.
<Ctrl-L> Clear the terminal screen.
python PATH

python3 PATH
Execute a Python program.
<Ctrl-D> Exit the current terminal.
Common Terminal Shortcuts
23
<Tab> Complete the command.
<Up> Show the last command.
<Down> Show the next command.
<Ctrl-C>
Enter new command, or
interrupt a running program.
Start Jupyter Notebook
➤ Mac:
➤ $ jupyter notebook
➤ Windows:
➤ Search / 

“Jupyter Notebook”
24
Install Visual Studio Code
➤ If you already have a source code editor, it's okay to skip.
➤ Install:
➤ https://code.visualstudio.com/download
➤ Open an integrated terminal:
➤ Visual Studio Code / View / Integrated Terminal
➤ Execute a Python program:
➤ $ cd PROJECT_DIR
➤ $ python hello.py
➤ or
➤ $ python3 hello.py
25
Hello, Python!
Checkpoint: Say Hi to Python
print('Hello, Python!')
27
Checkpoint: Say Hi to Python – on a Notebook
➤ Type the code into a notebook's cell.
➤ Press <Ctrl-Enter>.
➤ The output should be “Hello, Python!”
28
Checkpoint: Say Hi to Python – on an Editor
➤ Save a “hello.py” file into your project folder.
➤ Write the code into the file.
➤ Open up a new terminal, or use the integrated terminal.
➤ Change directory ($ cd ...) to your project folder.
➤ Execute ($ python ... or $ python3 ...) the file.
➤ The output should be “Hello, Python!”
29
Common Jupyter Notebook Shortcuts
30
Esc Edit mode → command mode.
Ctrl-Enter Run the cell.
B Insert cell below.
D, D Delete the current cell.
M To Markdown cell.
Cmd-/ Comment the code.
H Show keyboard shortcuts.
P Open the command palette.
Common Markdown Syntax
31
# Header 1 Header 1
## Header 2 Header 2.
> Block quote Block quote.
* Item 1

* Item 2
Unordered list.
1. Item 1

2. Item 2
Ordered list.
*emphasis* Emphasis.
**strong emp** Strong emphasis.
Markdown Cheatsheet | markdown.tw
How Jupyter Notebook Works?
32
1. Connect to a Python kernel.
2. Calculate and return the output.
3. Store the output.
A. When kernel halts or restart,
notebook remembers the outputs.
B. “Run All” 

to refresh the new kernel.
But when connect to a new kernel,

kernel remember nothing.
The Numbers
➤ In [n]
➤ n is the execution order,

like the line number.
➤ It may be:
➤ 1, 2, 3, 4
➤ 3, 5, 2, 4
➤ Depends on how you run it.
➤ “Restart & Run All” 

to reorder the numbers.
34
Primitives
Data Types
Programming?
➤ Programming 

is abstracting.
➤ Abstract the world with:
➤ Data types: 1.
➤ Operations: 1+1.
➤ Control flow: if ... else.
➤ They are from:
➤ Libraries.
➤ Your own code.
36
The Common Primitives
37
int Integer: 3
float Floating-point numbers: 3.14
str Text Sequence: '3.14'
bytes Binary Sequence: b'xe4xb8xadxe6x96x87'
bool True or False
None Represents the absence of a value.
Variables
➤ Points to an “object”.
➤ Everything in Python is an
object, including class.
➤ The object:
➤ Has a (data) type.
➤ Supports an operation set.
➤ Lives in the memory.
38
“01_data_types_primitives.ipynb
-The Notebook Time
39
➤ del x
➤ Now the x points to nothing.
➤ The object will be collected if no variable points to it.
Delete the “Pointing”
40
Checkpoint: Calculate BMR
➤ The Mifflin St Jeor Equation:
➤
➤ Where:
➤ P: kcal / day
➤ m: weight in kg
➤ h: height in cm
➤ a: age in year
➤ s: +5 for males, -161 for females
➤ Just simply calculate it in notebook.
41
P = 10m + 6.25h − 5a + s
If & While
Control Flow
If & While
➤ if <condition>: ...
➤ Run inner block if true.
➤ while <condition>: ...
➤ The while-loop.
➤ Run inner block while true.
➤ I.e. run until false.
43
“02_control_flow_if_while.ipynb
-The Notebook Time
44
Keep Learning
The Domains
➤ Web
➤ Django Girls Tutorial
➤ The Taipei Version
➤ Data / Data Visualization
➤ Seaborn Tutorial
➤ The Python Graph Gallery
➤ Matplotlib Gallery
➤ Data / Machine Learning
➤ Scikit-learn Tutorials
➤ Standford CS229
➤ Hsuan-Tien Lin
➤ Data / Deep Learning
➤ TensorFlow Getting Started
➤ Standford CS231n
➤ Standford CS224n
➤ Data / Statistics
➤ Good handout + Google
➤ Handbook of Biological
Statistics
➤ scipy.stats + StatsModels
➤ Not so good in Python.
➤ Ask or google 

for your own domain!
46
The Language Itself
➤ All the “Dig More”.
➤ Understand the built-in batteries and their details:
➤ The Python Standard Library – Python Documentation
➤ Understand the language:
➤ The Python Language Reference – Python Documentation
➤ Data model
47
If You Need a Book
➤ “Introducing Python – 

Modern Computing in Simple
Packages”
➤ “精通Python:

運⽤用簡單的套件進⾏行行現代運算”
48
The Learning Tips
➤ Problem → project → learn things!
➤ Make it work, make it right, and finally make it fast!
➤ Learn from the great people in our communities:
➤ Taipei.py
➤ PyHUG
➤ PyCon TW
➤ Python Taiwan
➤ And sleep well, seriously.
➤ Anyway, have fun with Python!
49
Composites
Data Types
The Common Composites
51
list Contains objects.
tuple
A compound objects has an unique meaning,

e.g., a point: (3, 1).
dict Maps an object to another object.
set Contains non-repeated objects.
“03_data_types_composites.ipynb
-The Notebook Time
52
Recap
➤ list []
➤ tuple ()
➤ dict {:}
➤ set {}
53
For
Control Flow
For & Loop Control Statements
➤ for
➤ The for-loop.
➤ In each iteration, 

get the next item 

of a collection.
➤ Supports str, list, tuple,
set, and dict, etc.
➤ I.e. iterate an iterable.
➤ break
➤ Leave the loop.
➤ continue
➤ Go the next iteration.
➤ loop … else
➤ If no break happens, 

execute the else.
55
Pass
➤ pass
➤ Do nothing.
➤ The compound statements must have one statement.
➤ The if, while, for, etc.
56
“04_control_flow_for.ipynb
-The Notebook Time
57
Checkpoint: Calculate Average BMR
58
height_cm weight_kg age male
152 48 63 1
157 53 41 1
140 37 63 0
137 32 65 0
Try
Control Flow
“05_control_flow_try.ipynb
-The Notebook Time
60
Raise Your Exception
➤ raise RuntimeError('should not be here')
➤ Raise an customized exception.
➤ Use class to customize exception class.
➤ raise
➤ Re-raise the last exception.
61
The Guidelines of Using Try
➤ An exception stops the whole program.
➤ However sometimes stop is better than a bad output.
➤ Only catch the exceptions you expect.
➤ But catch everything before raise to user.
➤ And transform the exception into log, email, etc.
62
Def
Control Flow
Functions
➤ Reuse statements.
➤ def
➤ Take the inputs.
➤ return
➤ Give an output.
➤ If no return, returns None.
➤ Method
➤ Is just a function 

belonging to an object.
64
“06_control_flow_def.ipynb
-The Notebook Time
65
Recap
➤ When calling function:
➤ Keyword arguments: f(a=3.14)
➤ Unpacking argument list: f(*list_like, **dict_like)
➤ When defining function:
➤ Default values: def f(a=None): ...
➤ Arbitrary argument list: def f(*args, **kwargs): ...
➤ Using docstrings to cooperate.
66
Docs – the Web Way
➤ The Python Standard Library
➤ Tip: Search with a leading and a trailing space.
➤ “ sys ”
➤ DevDocs
➤ Tip: Set it as one of Chrome's search engine.
➤ seaborn API reference
➤ For an example of 3rd libs.
67
The Guidelines of Designing Functions
➤ Use the simplest form first.
➤ Unless the calling code looks superfluous.
68
Checkpoint: Calculate Average BMR With Functions
69
height_cm weight_kg age male
152 48 63 1
157 53 41 1
140 37 63 0
137 32 65 0
Common Functions
Libraries
“07_libraries_

common_functions.ipynb
-The Notebook Time
71
Input & Output
Libraries
Input & Output
➤ IO: input & output.
➤ Standard IOs:
➤ stdout: standard output.
➤ stderr: standard error.
➤ stdin: standard input.
➤ File IOs:
➤ Including networking.
➤ Command line arguments
➤ Always validate user's inputs.
73
“08_libraries_input_output.ipynb
-The Notebook Time
74
Checkpoint: Calculate Average BMR From the Dataset
➤ Read the dataset_howell1.csv in.
➤ Skip the first line which doesn't contain data.
➤ Transform the datatypes.
➤ Calculate the average BMR from the dataset.
75
Command Line
Arguments
Libraries
“09_libraries_

command_line_arguments.py
-The Notebook Time
77
Recap
➤ open(…) → file object for read or write.
➤ The with will close file after the suite.
➤ The Inputs:
➤ stdin → input()
➤ for line in <file object>: ...
➤ <file object>.read()
➤ The outputs:
➤ print(…) → stdout
➤ print(…, file=sys.stderr) → stderr
➤ <file object>.write(…)
78
Checkpoint: Calculate BMR From Command Line
➤ The requirement:
➤ $ python3 calc_bmr.py 152 48 63 M
➤ 1120
➤ The hints:
➤ Read the inputs from sys.argv.
➤ Transform, calculate, and print it out!
➤ Get the extra bonus:
➤ Organize code into functions by functionality.
➤ Let user see nice error message when exception raises.
➤ Refer to 09_libraries_command_line_arguments.py.
79
Yield
Control Flow
“10_control_flow_yield.ipynb
-The Notebook Time
81
“Yield” Creates a Generator
➤ If a function uses yield, it returns a generator.
➤ Save memory.
➤ Simplify code.
82
Comprehensions
Control Flow
“11_control_flow_

comprehensions.ipynb
-The Notebook Time
84
Comprehensions & Generator Expression
➤ List Comprehension []
➤ Set Comprehension {}
➤ Dict Comprehension {:}
➤ Generator Expression ()
85
Functional Tricks
Libraries
The Functional Tricks
➤ The functional programming:
➤ Is a programming paradigm.
➤ Avoids changing-state and mutable data.
➤ Sometimes makes code clean, sometimes doesn't.
➤ Use it wisely.
➤ Python is not a functional language.
➤ But provides some useful tools.
87
“12_libraries_functional_tricks.ipynb
-The Notebook Time
88
➤ Recommend:
➤ https://github.com/Suor/funcy
➤ Not so recommend:
➤ https://github.com/pytoolz/toolz
➤ https://github.com/EntilZha/PyFunctional
The Popular Functional Libs
89
Checkpoint: Calculate Average BMR With Comprehensions
➤ Either the table or the CSV is okay.
➤ Use at least one comprehension.
90
Import Antigravity
Libraries
The Useful Packages in Standard Library
➤ sys
➤ os and os.path
➤ glob
➤ math
➤ random
➤ decimal
➤ datetime
➤ collections
➤ re
➤ urllib
➤ smtplib
➤ json
➤ csv
➤ pickle
➤ gzip and many others
➤ threading
➤ multiprocessing
➤ asyncio
➤ pprint
➤ logging
➤ doctest
➤ sqlite3
92
The Useful Third-Party Packages
➤ Requests
➤ Beautiful Soup
➤ csvkit
➤ seaborn
➤ Pillow
➤ Flask
➤ Django
➤ pytest
➤ Sphinx
➤ pipenv
➤ The SciPy Stack
➤ NumPy
➤ SciPy
➤ SymPy
➤ Matplotlib
➤ IPython
➤ pandas
➤ python3wos.appspot.com
93
“13_*_libraries_

import_antigravity_*.ipynb
-The Notebook Time
94
Install Third-Party Package
➤ When using pip:
➤ pip install <package name>
➤ When using Conda:
➤ conda install <package name>
95
Checkpoint: Visualization
➤ Explore from 13_7_libraries_import_antigravity_seaborn.ipynb .
➤ Refer to Seaborn API Reference to plot different graphs.
➤ Refer to StatsModels Datasets for different datasets.
➤ Tip: “fair” in http://www.statsmodels.org/stable/datasets/
generated/fair.html is the name of the dataset.
➤ Share your interesting insights with us!
96
Module & Package
Libraries
ma.py mb.py
Import Module
➤ A Python file is just a Python module:
import ma
import mb
➤ A module has a namespace:
ma.var
mb.var
➤ The vars are different variables.
99
p/
__init__.py ma.py mb.py
Import Package
➤ A directory containing a __init__.py is just a package:
import p
➤ It executes the __init__.py.
➤ Usually import 

the common function or module of the whole package.
➤ Import modules in a package:
import p.ma
import p.mb
101
Make Them Shorter
➤ Import variables from a module:
from ma import x, y
# or
from ma import x
from ma import y
➤ Import modules from a package:
from p import ma, mb
# or
from p import ma
from p import mb
102
➤ Give an alias:
from ma import var as _var
➤ Mix them up:
from p.ma import var as _var
103
“14_libraries_moudle_and_package
-The Notebook Time
104
Class
Data Types
The Object-Oriented Programming
➤ Class makes objects.
➤ Customize your:
➤ Data type.
➤ And its operations.
➤ A powerful tool to abstract the world.
106
The Object-Oriented Terms in Python
107
object Everything in Python is an object, e.g., str, 'str'.
class Makes instances, e.g., str.
instance Made from class, e.g., 'str'.
attribute Anything an object owns.
method A function an object owns.
108
str object | class
str.__class__ object | attribute | class
str.split object | attribute | function | method
'str' object | instance
'str'.split object | attribute | function | method
“15_data_types_class.ipynb
-The Notebook Time
109
object
Google
mosky andy
object
Google
mosky andy
Yahoo
Site
Duck-Typing & Protocol
➤ Duck-Typing
➤ “If it looks like a duck and quacks like a duck, 

it must be a duck.”
➤ Avoids tests using type() or isinstance().
➤ Employs hasattr() tests or EAFP programming.
➤ EAFP: easier to ask for forgiveness than permission.
➤ Iterator Protocol
➤ __iter__() returns itself.
➤ __next__() returns the next element.
➤ The for-loops use the iterator protocol to iterate an object.
112
The Guidelines of Designing Classes
➤ Don't use class.
➤ Unless many functions have the same arguments, e.g.,:
➤ def create_user(uid, ...): ...
➤ def retrieve_user(uid, ...): ...
➤ def update_user(uid, ...): ...
➤ When design or implementation.
➤ Don't use class method, etc.
➤ Unless you're sure the method is only associate with class.
➤ Let's keep code simple!
113
The Fun Facts
➤ import this
➤ Python’s easter eggs and hidden jokes – Hacker Noon
114
Checkpoint: Classification
➤ Extend 13_8_libraries_import_antigravity_scikitlearn.ipynb .
➤ Refer to Scikit-Learn Random Forest for the arguments.
➤ Share your awesome metrics with us!
115

More Related Content

PDF
Learning Python from Data
PDF
Graph-Tool in Practice
PDF
Programming with Python - Adv.
PDF
Learning Git with Workflows
PDF
Concurrency in Python
PDF
Minimal MVC in JavaScript
PDF
Programming with Python - Basic
PDF
Introduction to Clime
Learning Python from Data
Graph-Tool in Practice
Programming with Python - Adv.
Learning Git with Workflows
Concurrency in Python
Minimal MVC in JavaScript
Programming with Python - Basic
Introduction to Clime

What's hot (20)

PDF
Reversing the dropbox client on windows
PDF
Introduction to Programming in Go
PDF
Dive into Pinkoi 2013
PDF
OSCON2014 : Quick Introduction to System Tools Programming with Go
PDF
Open source projects with python
PDF
Python入門 : 4日間コース社内トレーニング
PDF
Python教程 / Python tutorial
PDF
10 reasons to be excited about go
PDF
Clean Manifests with Puppet::Tidy
PPTX
Rapid Application Design in Financial Services
KEY
Lock? We don't need no stinkin' locks!
PDF
Go for SysAdmins - LISA 2015
PDF
Happy Go Programming Part 1
PPT
Go lang introduction
PDF
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
PDF
Go. why it goes v2
PDF
On the Edge Systems Administration with Golang
PDF
Data analytics in the cloud with Jupyter notebooks.
ODP
Vim and Python
PPTX
How to deliver a Python project
Reversing the dropbox client on windows
Introduction to Programming in Go
Dive into Pinkoi 2013
OSCON2014 : Quick Introduction to System Tools Programming with Go
Open source projects with python
Python入門 : 4日間コース社内トレーニング
Python教程 / Python tutorial
10 reasons to be excited about go
Clean Manifests with Puppet::Tidy
Rapid Application Design in Financial Services
Lock? We don't need no stinkin' locks!
Go for SysAdmins - LISA 2015
Happy Go Programming Part 1
Go lang introduction
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
Go. why it goes v2
On the Edge Systems Administration with Golang
Data analytics in the cloud with Jupyter notebooks.
Vim and Python
How to deliver a Python project
Ad

Similar to Practicing Python 3 (20)

PDF
05 python.pdf
PDF
Fullstack Academy - Awesome Web Dev Tips & Tricks
PDF
The Popper Experimentation Protocol and CLI tool
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
PDF
PythonBrasil[8] - CPython for dummies
PPT
python installation in different Operating Systems.ppt
PDF
python-160403194316.pdf
PPTX
Python Seminar PPT
PPTX
Python
PDF
Pycon2017 instagram keynote
PDF
Python and Pytorch tutorial and walkthrough
PPT
week1.ppt
PPTX
Python Introduction
PPTX
Advanced windows debugging
PPT
Python programming-2-2048 (30 files merged).ppt
PPT
Python programming notes all in one python ppt
PDF
Python 3.5: An agile, general-purpose development language.
PDF
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PDF
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
PDF
Multiprocessing with python
05 python.pdf
Fullstack Academy - Awesome Web Dev Tips & Tricks
The Popper Experimentation Protocol and CLI tool
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
PythonBrasil[8] - CPython for dummies
python installation in different Operating Systems.ppt
python-160403194316.pdf
Python Seminar PPT
Python
Pycon2017 instagram keynote
Python and Pytorch tutorial and walkthrough
week1.ppt
Python Introduction
Advanced windows debugging
Python programming-2-2048 (30 files merged).ppt
Python programming notes all in one python ppt
Python 3.5: An agile, general-purpose development language.
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
Multiprocessing with python
Ad

More from Mosky Liu (10)

PDF
Statistical Regression With Python
PDF
Data Science With Python
PDF
Hypothesis Testing With Python
PDF
Elegant concurrency
PDF
Boost Maintainability
PDF
Beyond the Style Guides
PDF
Simple Belief - Mosky @ TEDxNTUST 2015
PDF
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
PDF
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
PDF
MoSQL: More than SQL, but less than ORM
Statistical Regression With Python
Data Science With Python
Hypothesis Testing With Python
Elegant concurrency
Boost Maintainability
Beyond the Style Guides
Simple Belief - Mosky @ TEDxNTUST 2015
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
MoSQL: More than SQL, but less than ORM

Recently uploaded (20)

PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Auditboard EB SOX Playbook 2023 edition.
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PDF
Altius execution marketplace concept.pdf
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Introduction to MCP and A2A Protocols: Enabling Agent Communication
PPTX
Internet of Everything -Basic concepts details
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
Lung cancer patients survival prediction using outlier detection and optimize...
Advancing precision in air quality forecasting through machine learning integ...
EIS-Webinar-Regulated-Industries-2025-08.pdf
Auditboard EB SOX Playbook 2023 edition.
LMS bot: enhanced learning management systems for improved student learning e...
Altius execution marketplace concept.pdf
Data Virtualization in Action: Scaling APIs and Apps with FME
Early detection and classification of bone marrow changes in lumbar vertebrae...
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
Module 1 Introduction to Web Programming .pptx
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Introduction to MCP and A2A Protocols: Enabling Agent Communication
Internet of Everything -Basic concepts details
Connector Corner: Transform Unstructured Documents with Agentic Automation
Presentation - Principles of Instructional Design.pptx
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf

Practicing Python 3

  • 3. Websites ➤ Pinkoi ➤ Google search engine ➤ Instagram ➤ MAU: 700M @ 2017/5 ➤ Uber ➤ Pinterest 3
  • 4. Desktop Applications ➤ Dropbox ➤ Disney ➤ For animation studio tools. ➤ Blender ➤ A 3D graphics software. 4
  • 5. Science ➤ NASA ➤ LIGO ➤ Gravitational waves @ 2016 ➤ LHC ➤ Higgs boson @ 2013 ➤ MMTK ➤ Molecular Modelling Toolkit 5
  • 6. Embedded System ➤ iRobot uses Python. ➤ Raspberry Pi supports Python. ➤ Linux has built-in Python. 6
  • 7. Why? ➤ Python is slower than C, Java, ➤ But much faster to write, ➤ And easy to speed up. ➤ Numba | Cython ➤ Has the rich libraries. ➤ Emphasizes code readability. ➤ Easier to learn. ➤ Easier to co-work. ➤ “Time is money.” 7
  • 9. A Website in a Minute from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() 9
  • 10. Symbolic Mathematics from sympy import symbols from sympy import diff
 x = symbols('x') x**2 diff(x**2) 10
  • 11. Data Visualization import pandas as pd import numpy as np ts = pd.Series( np.random.randn(1000), index=pd.date_range( '1/1/2000', periods=1000 ) ) ts = ts.cumsum() ts.plot() 11
  • 12. Mosky ➤ Python Charmer at Pinkoi. ➤ Has spoken at ➤ PyCons in 
 TW, MY, KR, JP, SG, HK,
 COSCUPs, and TEDx, etc. ➤ Countless hours 
 on teaching Python. ➤ Own the Python packages: ➤ ZIPCodeTW, 
 MoSQL, Clime, etc. ➤ http://mosky.tw/ 12
  • 13. ➤ The Foundation Part: ➤ Primitives ➤ If & While ➤ Composites ➤ For, Try, Def ➤ Common Functions ➤ Input & Output ➤ Command Line Arguments ➤ The Fascinating Part: ➤ Yield ➤ Comprehensions ➤ Functional Tricks ➤ Import Antigravity*** ➤ Module & Package ➤ Class ➤ And the checkpoints! The Outline 13
  • 15. (a terminal) Master the machine. Python 3 Not Python 2. Jupyter Notebook Learn Python with browsers. Visual Studio Code A full-featured source code editor. (other libs) Will be introduced in this slides. We Will Use 15
  • 16. ➤ Open a terminal: ➤ Spotlight (Cmd-Space) / “terminal” ➤ Install Homebrew by executing the command: ➤ $ /usr/bin/ruby -e "$(curl -fsSL https:// raw.githubusercontent.com/Homebrew/install/master/ install)" ➤ Execute the commands: ➤ $ brew install python3 ➤ $ pip3 install jupyter numpy scipy sympy matplotlib ipython pandas flask beautifulsoup4 requests seaborn statsmodels scikit-learn ➤ Note the above commands are just a single line. On Mac 16
  • 17. ➤ Open a terminal: ➤ Spotlight (Cmd-Space) / “terminal” ➤ Install Homebrew by executing the command: ➤ $ /usr/bin/ruby -e "$(curl -fsSL https:// raw.githubusercontent.com/Homebrew/install/master/ install)" ➤ Execute the commands: ➤ $ brew install python3 ➤ $ pip3 install jupyter numpy scipy sympy matplotlib ipython pandas flask beautifulsoup4 requests seaborn statsmodels scikit-learn ➤ Note the above commands are just a single line. Hint to talk to macOS. Enter the command without $. On Mac 17
  • 18. ➤ Install Python 3 with Miniconda: ➤ http://conda.pydata.org/miniconda.html ➤ Python 3.6 / Windows / 64-bit (exe installer) ➤ Open Anaconda's terminal: ➤ Start Menu / Search / Type “Anaconda Prompt” ➤ Right-click the item and choose “Run as administrator”. ➤ Execute the commands: ➤ > conda install jupyter numpy scipy sympy matplotlib ipython pandas flask beautifulsoup4 requests seaborn statsmodels scikit-learn ➤ Note the above commands are just a single line. On Windows 18
  • 19. ➤ Install Python 3 with Miniconda: ➤ http://conda.pydata.org/miniconda.html ➤ Python 3.6 / Windows / 64-bit (exe installer) ➤ Open Anaconda's terminal: ➤ Start Menu / Search / Type “Anaconda Prompt” ➤ Right-click the item and choose “Run as administrator”. ➤ Execute the commands: ➤ > conda install jupyter numpy scipy sympy matplotlib ipython pandas flask beautifulsoup4 requests seaborn statsmodels scikit-learn ➤ Note the above commands are just a single line. Hint to talk to Windows. Enter the command without >. On Windows 19
  • 20. If You Already Have Python ➤ Try to install the packages: ➤ The Jupyter Notebook ➤ jupyter ➤ The SciPy Stack ➤ numpy scipy sympy matplotlib ipython pandas ➤ The web-related libs ➤ flask beautifulsoup4 requests ➤ The data-related libs ➤ seaborn statsmodels scikit-learn ➤ If fail, clean uninstall Python, and follow the previous slides. 20
  • 21. Common MS-DOS Commands 21 C: Go C: drive. cd PATH Change directory to PATH.
 PATH can be /Users/python/, python/, etc. dir Display the contents of a directory. cls Clear the terminal screen. python PATH python3 PATH Execute a Python program. exit Exit the current command processor.
  • 22. Common Unix-like (Mac) Commands 22 cd PATH Change Directory to PATH.
 PATH can be /Users/python/, python/, etc. ls List the contents of a directory. <Ctrl-L> Clear the terminal screen. python PATH
 python3 PATH Execute a Python program. <Ctrl-D> Exit the current terminal.
  • 23. Common Terminal Shortcuts 23 <Tab> Complete the command. <Up> Show the last command. <Down> Show the next command. <Ctrl-C> Enter new command, or interrupt a running program.
  • 24. Start Jupyter Notebook ➤ Mac: ➤ $ jupyter notebook ➤ Windows: ➤ Search / 
 “Jupyter Notebook” 24
  • 25. Install Visual Studio Code ➤ If you already have a source code editor, it's okay to skip. ➤ Install: ➤ https://code.visualstudio.com/download ➤ Open an integrated terminal: ➤ Visual Studio Code / View / Integrated Terminal ➤ Execute a Python program: ➤ $ cd PROJECT_DIR ➤ $ python hello.py ➤ or ➤ $ python3 hello.py 25
  • 27. Checkpoint: Say Hi to Python print('Hello, Python!') 27
  • 28. Checkpoint: Say Hi to Python – on a Notebook ➤ Type the code into a notebook's cell. ➤ Press <Ctrl-Enter>. ➤ The output should be “Hello, Python!” 28
  • 29. Checkpoint: Say Hi to Python – on an Editor ➤ Save a “hello.py” file into your project folder. ➤ Write the code into the file. ➤ Open up a new terminal, or use the integrated terminal. ➤ Change directory ($ cd ...) to your project folder. ➤ Execute ($ python ... or $ python3 ...) the file. ➤ The output should be “Hello, Python!” 29
  • 30. Common Jupyter Notebook Shortcuts 30 Esc Edit mode → command mode. Ctrl-Enter Run the cell. B Insert cell below. D, D Delete the current cell. M To Markdown cell. Cmd-/ Comment the code. H Show keyboard shortcuts. P Open the command palette.
  • 31. Common Markdown Syntax 31 # Header 1 Header 1 ## Header 2 Header 2. > Block quote Block quote. * Item 1
 * Item 2 Unordered list. 1. Item 1
 2. Item 2 Ordered list. *emphasis* Emphasis. **strong emp** Strong emphasis. Markdown Cheatsheet | markdown.tw
  • 32. How Jupyter Notebook Works? 32 1. Connect to a Python kernel. 2. Calculate and return the output. 3. Store the output.
  • 33. A. When kernel halts or restart, notebook remembers the outputs. B. “Run All” 
 to refresh the new kernel. But when connect to a new kernel,
 kernel remember nothing.
  • 34. The Numbers ➤ In [n] ➤ n is the execution order,
 like the line number. ➤ It may be: ➤ 1, 2, 3, 4 ➤ 3, 5, 2, 4 ➤ Depends on how you run it. ➤ “Restart & Run All” 
 to reorder the numbers. 34
  • 36. Programming? ➤ Programming 
 is abstracting. ➤ Abstract the world with: ➤ Data types: 1. ➤ Operations: 1+1. ➤ Control flow: if ... else. ➤ They are from: ➤ Libraries. ➤ Your own code. 36
  • 37. The Common Primitives 37 int Integer: 3 float Floating-point numbers: 3.14 str Text Sequence: '3.14' bytes Binary Sequence: b'xe4xb8xadxe6x96x87' bool True or False None Represents the absence of a value.
  • 38. Variables ➤ Points to an “object”. ➤ Everything in Python is an object, including class. ➤ The object: ➤ Has a (data) type. ➤ Supports an operation set. ➤ Lives in the memory. 38
  • 40. ➤ del x ➤ Now the x points to nothing. ➤ The object will be collected if no variable points to it. Delete the “Pointing” 40
  • 41. Checkpoint: Calculate BMR ➤ The Mifflin St Jeor Equation: ➤ ➤ Where: ➤ P: kcal / day ➤ m: weight in kg ➤ h: height in cm ➤ a: age in year ➤ s: +5 for males, -161 for females ➤ Just simply calculate it in notebook. 41 P = 10m + 6.25h − 5a + s
  • 43. If & While ➤ if <condition>: ... ➤ Run inner block if true. ➤ while <condition>: ... ➤ The while-loop. ➤ Run inner block while true. ➤ I.e. run until false. 43
  • 46. The Domains ➤ Web ➤ Django Girls Tutorial ➤ The Taipei Version ➤ Data / Data Visualization ➤ Seaborn Tutorial ➤ The Python Graph Gallery ➤ Matplotlib Gallery ➤ Data / Machine Learning ➤ Scikit-learn Tutorials ➤ Standford CS229 ➤ Hsuan-Tien Lin ➤ Data / Deep Learning ➤ TensorFlow Getting Started ➤ Standford CS231n ➤ Standford CS224n ➤ Data / Statistics ➤ Good handout + Google ➤ Handbook of Biological Statistics ➤ scipy.stats + StatsModels ➤ Not so good in Python. ➤ Ask or google 
 for your own domain! 46
  • 47. The Language Itself ➤ All the “Dig More”. ➤ Understand the built-in batteries and their details: ➤ The Python Standard Library – Python Documentation ➤ Understand the language: ➤ The Python Language Reference – Python Documentation ➤ Data model 47
  • 48. If You Need a Book ➤ “Introducing Python – 
 Modern Computing in Simple Packages” ➤ “精通Python:
 運⽤用簡單的套件進⾏行行現代運算” 48
  • 49. The Learning Tips ➤ Problem → project → learn things! ➤ Make it work, make it right, and finally make it fast! ➤ Learn from the great people in our communities: ➤ Taipei.py ➤ PyHUG ➤ PyCon TW ➤ Python Taiwan ➤ And sleep well, seriously. ➤ Anyway, have fun with Python! 49
  • 51. The Common Composites 51 list Contains objects. tuple A compound objects has an unique meaning,
 e.g., a point: (3, 1). dict Maps an object to another object. set Contains non-repeated objects.
  • 53. Recap ➤ list [] ➤ tuple () ➤ dict {:} ➤ set {} 53
  • 55. For & Loop Control Statements ➤ for ➤ The for-loop. ➤ In each iteration, 
 get the next item 
 of a collection. ➤ Supports str, list, tuple, set, and dict, etc. ➤ I.e. iterate an iterable. ➤ break ➤ Leave the loop. ➤ continue ➤ Go the next iteration. ➤ loop … else ➤ If no break happens, 
 execute the else. 55
  • 56. Pass ➤ pass ➤ Do nothing. ➤ The compound statements must have one statement. ➤ The if, while, for, etc. 56
  • 58. Checkpoint: Calculate Average BMR 58 height_cm weight_kg age male 152 48 63 1 157 53 41 1 140 37 63 0 137 32 65 0
  • 61. Raise Your Exception ➤ raise RuntimeError('should not be here') ➤ Raise an customized exception. ➤ Use class to customize exception class. ➤ raise ➤ Re-raise the last exception. 61
  • 62. The Guidelines of Using Try ➤ An exception stops the whole program. ➤ However sometimes stop is better than a bad output. ➤ Only catch the exceptions you expect. ➤ But catch everything before raise to user. ➤ And transform the exception into log, email, etc. 62
  • 64. Functions ➤ Reuse statements. ➤ def ➤ Take the inputs. ➤ return ➤ Give an output. ➤ If no return, returns None. ➤ Method ➤ Is just a function 
 belonging to an object. 64
  • 66. Recap ➤ When calling function: ➤ Keyword arguments: f(a=3.14) ➤ Unpacking argument list: f(*list_like, **dict_like) ➤ When defining function: ➤ Default values: def f(a=None): ... ➤ Arbitrary argument list: def f(*args, **kwargs): ... ➤ Using docstrings to cooperate. 66
  • 67. Docs – the Web Way ➤ The Python Standard Library ➤ Tip: Search with a leading and a trailing space. ➤ “ sys ” ➤ DevDocs ➤ Tip: Set it as one of Chrome's search engine. ➤ seaborn API reference ➤ For an example of 3rd libs. 67
  • 68. The Guidelines of Designing Functions ➤ Use the simplest form first. ➤ Unless the calling code looks superfluous. 68
  • 69. Checkpoint: Calculate Average BMR With Functions 69 height_cm weight_kg age male 152 48 63 1 157 53 41 1 140 37 63 0 137 32 65 0
  • 73. Input & Output ➤ IO: input & output. ➤ Standard IOs: ➤ stdout: standard output. ➤ stderr: standard error. ➤ stdin: standard input. ➤ File IOs: ➤ Including networking. ➤ Command line arguments ➤ Always validate user's inputs. 73
  • 75. Checkpoint: Calculate Average BMR From the Dataset ➤ Read the dataset_howell1.csv in. ➤ Skip the first line which doesn't contain data. ➤ Transform the datatypes. ➤ Calculate the average BMR from the dataset. 75
  • 78. Recap ➤ open(…) → file object for read or write. ➤ The with will close file after the suite. ➤ The Inputs: ➤ stdin → input() ➤ for line in <file object>: ... ➤ <file object>.read() ➤ The outputs: ➤ print(…) → stdout ➤ print(…, file=sys.stderr) → stderr ➤ <file object>.write(…) 78
  • 79. Checkpoint: Calculate BMR From Command Line ➤ The requirement: ➤ $ python3 calc_bmr.py 152 48 63 M ➤ 1120 ➤ The hints: ➤ Read the inputs from sys.argv. ➤ Transform, calculate, and print it out! ➤ Get the extra bonus: ➤ Organize code into functions by functionality. ➤ Let user see nice error message when exception raises. ➤ Refer to 09_libraries_command_line_arguments.py. 79
  • 82. “Yield” Creates a Generator ➤ If a function uses yield, it returns a generator. ➤ Save memory. ➤ Simplify code. 82
  • 85. Comprehensions & Generator Expression ➤ List Comprehension [] ➤ Set Comprehension {} ➤ Dict Comprehension {:} ➤ Generator Expression () 85
  • 87. The Functional Tricks ➤ The functional programming: ➤ Is a programming paradigm. ➤ Avoids changing-state and mutable data. ➤ Sometimes makes code clean, sometimes doesn't. ➤ Use it wisely. ➤ Python is not a functional language. ➤ But provides some useful tools. 87
  • 89. ➤ Recommend: ➤ https://github.com/Suor/funcy ➤ Not so recommend: ➤ https://github.com/pytoolz/toolz ➤ https://github.com/EntilZha/PyFunctional The Popular Functional Libs 89
  • 90. Checkpoint: Calculate Average BMR With Comprehensions ➤ Either the table or the CSV is okay. ➤ Use at least one comprehension. 90
  • 92. The Useful Packages in Standard Library ➤ sys ➤ os and os.path ➤ glob ➤ math ➤ random ➤ decimal ➤ datetime ➤ collections ➤ re ➤ urllib ➤ smtplib ➤ json ➤ csv ➤ pickle ➤ gzip and many others ➤ threading ➤ multiprocessing ➤ asyncio ➤ pprint ➤ logging ➤ doctest ➤ sqlite3 92
  • 93. The Useful Third-Party Packages ➤ Requests ➤ Beautiful Soup ➤ csvkit ➤ seaborn ➤ Pillow ➤ Flask ➤ Django ➤ pytest ➤ Sphinx ➤ pipenv ➤ The SciPy Stack ➤ NumPy ➤ SciPy ➤ SymPy ➤ Matplotlib ➤ IPython ➤ pandas ➤ python3wos.appspot.com 93
  • 95. Install Third-Party Package ➤ When using pip: ➤ pip install <package name> ➤ When using Conda: ➤ conda install <package name> 95
  • 96. Checkpoint: Visualization ➤ Explore from 13_7_libraries_import_antigravity_seaborn.ipynb . ➤ Refer to Seaborn API Reference to plot different graphs. ➤ Refer to StatsModels Datasets for different datasets. ➤ Tip: “fair” in http://www.statsmodels.org/stable/datasets/ generated/fair.html is the name of the dataset. ➤ Share your interesting insights with us! 96
  • 99. Import Module ➤ A Python file is just a Python module: import ma import mb ➤ A module has a namespace: ma.var mb.var ➤ The vars are different variables. 99
  • 101. Import Package ➤ A directory containing a __init__.py is just a package: import p ➤ It executes the __init__.py. ➤ Usually import 
 the common function or module of the whole package. ➤ Import modules in a package: import p.ma import p.mb 101
  • 102. Make Them Shorter ➤ Import variables from a module: from ma import x, y # or from ma import x from ma import y ➤ Import modules from a package: from p import ma, mb # or from p import ma from p import mb 102
  • 103. ➤ Give an alias: from ma import var as _var ➤ Mix them up: from p.ma import var as _var 103
  • 106. The Object-Oriented Programming ➤ Class makes objects. ➤ Customize your: ➤ Data type. ➤ And its operations. ➤ A powerful tool to abstract the world. 106
  • 107. The Object-Oriented Terms in Python 107 object Everything in Python is an object, e.g., str, 'str'. class Makes instances, e.g., str. instance Made from class, e.g., 'str'. attribute Anything an object owns. method A function an object owns.
  • 108. 108 str object | class str.__class__ object | attribute | class str.split object | attribute | function | method 'str' object | instance 'str'.split object | attribute | function | method
  • 112. Duck-Typing & Protocol ➤ Duck-Typing ➤ “If it looks like a duck and quacks like a duck, 
 it must be a duck.” ➤ Avoids tests using type() or isinstance(). ➤ Employs hasattr() tests or EAFP programming. ➤ EAFP: easier to ask for forgiveness than permission. ➤ Iterator Protocol ➤ __iter__() returns itself. ➤ __next__() returns the next element. ➤ The for-loops use the iterator protocol to iterate an object. 112
  • 113. The Guidelines of Designing Classes ➤ Don't use class. ➤ Unless many functions have the same arguments, e.g.,: ➤ def create_user(uid, ...): ... ➤ def retrieve_user(uid, ...): ... ➤ def update_user(uid, ...): ... ➤ When design or implementation. ➤ Don't use class method, etc. ➤ Unless you're sure the method is only associate with class. ➤ Let's keep code simple! 113
  • 114. The Fun Facts ➤ import this ➤ Python’s easter eggs and hidden jokes – Hacker Noon 114
  • 115. Checkpoint: Classification ➤ Extend 13_8_libraries_import_antigravity_scikitlearn.ipynb . ➤ Refer to Scikit-Learn Random Forest for the arguments. ➤ Share your awesome metrics with us! 115