Python Foundations and Tooling
Python Foundations and Tooling
2
Kickoff Survey
https://forms.gle/TWUGUAcxUBcZ3F2d9
3
Python foundations
Data types
Operators
Functions
Control flow and iterators
Finding help Data Science Workflow
Python
Tooling
Installation
Visual Studio Code
Jupyter Notebooks
Python Packages
Virtual Environments
4
Agenda (preliminary)
Day 1
Installation
Primitive Types
Using Python as a Calculator
Python Packages and Virtual Environments
Day 2
Functions
List, Tuple, Dictionary and Set
Control Flow
Day 3
List and Dict Comprehensions
Useful Iterators
Developing our own quiz application
5
Python Setup and Tooling
6
Recommended Setup
7
Python Installation Guide
8
Which Python distribution?
Distributions CPython distribution Anaconda distribution
https://www.python.org https://anaconda.org/
Jupyterlab/Jupyter
Visual Studio Code PyCharm Notebook (via Spyder
Browser)
Jupyter Notebooks
✓ ✓ ✓ x
(.ipynb)
Git ✓ ✓ (✓) x
10
Visual Studio Code
File Explorer
Extensions
Command Palette
Ctrl + Shift + P
Fast access to everything
Settings
11
Packages
Python
12
Virtual Environments
13
Virtual Environments in VS Code
14
Python script vs Jupyter Notebook
Script (.py) Jupyter Notebook (.ipynb)
15
Python scripts
Text file (editable in any text editor), with file ending .py
Use cases:
Focus on code
Concise documents
Re-use code elsewhere by importing the python file
Production setting
Comments are marked by # symbol. Comments won‘t be executed. All
other lines will.
Execution modes in VS Code: interactive (selection, current line) or script
mode (entire file)
16
Python scripts: execution modes
Run selection/line
Shift + Enter
17
Jupyter Notebooks
18
Jupyter Notebooks
Content aspects:
Code, output, text, formulas, images, etc. all in one file. Contents are formatted via
Markdown
Julia, Python, and R code
Use cases: exploration, presentation, documentation, books
Technical aspects:
Files with JSON structure and file ending.ipynb (IPython Notebook)
Web-based
Editable in browser via Jupyterlab/Jupyer Notebook, and via IDEs such as VS Code
Convertible into html, pdf, or python scripts
19
Jupyter Notebooks
Disadvantages
Editing functionalities are limited compared to Python scripts
Working productively requires that you remember many shortcuts
Not useful if you want to re-use code
Not ideal for version control due to JSON format
20
Markdown
21
Python Foundations
22
Data types Manipulating data
Primitive Types create | subset | edit | delete
List
Tuple
Dictionary Operators
Set [2, 3] Math | comparison |logical
Python
Functions Control flow and Iterators
Foundations
Using functions if, else and loops
Writing functions List and dict comprehensions
Lambda functions Generator expressions
Generator functions
Concepts / Paradigms
Mutability
Object Oriented Programming
Functional programming
23
Built-In Data Types
Integer 20
Numbers
Float 37.5
Primitive
Immutable
Complex 1+3j
Boolean True/False
Sequences
Tuple (3, 4.5, ˈbˈ)
Collections
List [2, ˈaˈ, 5.7]
Mutable
Dictionary {1:ˈaˈ, 2:ˈbˈ}
Set {2, 4, 6}
24
Math Operators
25
Functions
Formatting:
Consistent indentation
Docstring: function help
Type Hints (optional)
Default values (optional)
A function may
carry out some operations
return objects
print()
26
Methods
27
Strings
Create strings:
'single quotes'
"double quotes"
"""triple quotes""" (for multiline strings)
Subsetting strings
[start:stop:step]
zero-based
start is inclusive, stop is exclusive
negative indexing allowed
28
Strings
String Methods
str.upper()
str.lower()
str.reverse()
…
29
Comparison and Membership Operators
30
Assignment, names, values
Assignment: x = 2337
Variable x is a name that stores the reference to value 2337
Consequences:
Dynamic typing: We can change the type of x, by
pointing x to another object
Aliasing: Multiple names can point to the same object
Side effects: If a mutable object is changed, this
effects all aliases
31
Mutability
Mutable objects can be changed, which means that the change occurs in-
place (without altering the memory address)
Use copy method to avoid side effects between two names
a a b a b
2 3 4 2 3 4 2 3 5
#123456 #123456 #123456
32
List and Tuple
33
If - elif - else loop
34
While condition
35
For loop
36
Iterables
tuple
list (2, 4, 6)
[2, 4, 6] 2 4 6 string
2 4 6 ˈdataˈ
d a t a
for <var> in <iterable>:
# do something #
dictionary
range
dict = {1:ˈAnnaˈ, 2:ˈMaxˈ}
range(0,10,2)
0 2 4 6 8
dict.keys() dict.values()
1 2 ˈAnnaˈ ˈMaxˈ
37
Iterables
39
List comprehension
For loop:
squares = []
for x in [1, 3, 5]: [1, 9, 25]
squares.append(x**2)
List comprehension:
40
Dictionary comprehension
squares = {}
for x in [1, 3, 5]: {1: 1, 3: 9, 5: 25}
squares.update({x: x**2})
Dict comprehension:
41
Conventions
42