01_Python_fundamentals_and_Jupyter_Notebooks
01_Python_fundamentals_and_Jupyter_Notebooks
Variables
How to use
Loops How to
it
use it
Modules, Scripts
Operators
Packages
Scripts
Programming language: Python
Source: Statista
Why python and what can I do with python ?
Method 1: Interactive Python interface Method 2: Create Python script files (*.py)
• Where to write code:
− Use simple Text Editors
− Use integrated development environments (IDE)
(for example, Visual Studio Code)
Three different ways to use Python
• A notebook integrates code and its output into a single document that combines
visualizations, narrative text, mathematical equations and other rich media
Discussion
Python 3.10:
• https://www.python.org/downloads/
Jupyter Notebook:
• https://jupyter.org/install
Documentation:
https://docs.python.org/3/tutorial/index.html
https://www.w3schools.com/python/default.asp
Markup language
# Strings
s1 = “Hello World“ Use lowercase letters with underscores for variables,
s2 = “Hello World“
functions, methods, packages and modules.
Use capital letters with underscores for constants.
Documentation:
https://docs.python.org/3/tutorial/introduction.html
Python - Operation overview
% Modulo 5 % 2 == 1
Documentation:
https://docs.python.org/3/tutorial/introduction.html
Python - Conditions
Python Python
i=1 # Exercise 3
# Exercise 1 x = 20
if i<1: y = 15
print("1")
if i==1: if x > y:
print("2") print("x is greater than y")
else: if x > 25:
print("3") 1)3 print("x is greater than 25")
2)2
3)x is greater than y if x > 30:
# Exercise 2 x is less than or equal to 25 print("x is greater than 30")
if i<1: else:
print("1") print("x is less than or equal to 30")
else: else:
if i==1: print("x is less than or equal to 25")
print("2") else:
print("3") print("x is less than or equal to y")
Mini Task: Solution
Python Python
i=1 # Exercise 3
# Exercise 1 x = 20
if i<1: y = 15
print("1")
if i==1: if x > y:
print("2") print("x is greater than y")
else: if x > 25:
print("3") print("x is greater than 25")
if x > 30:
# Exercise 2 print("x is greater than 30")
if i<1: else:
print("1") print("x is less than or equal to 30")
else: else:
if i==1: print("x is less than or equal to 25")
print("2") else:
print("3") print("x is less than or equal to y")
Mini Task: Solution
Python Python
i=1 # Exercise 3
# Exercise 1 x = 20
if i<1: y = 15
print("1")
if i==1: if x > y:
print("2") print("x is greater than y")
else: if x > 25:
print("3") print("x is greater than 25")
if x > 30:
# Exercise 2 print("x is greater than 30")
if i<1: else:
print("1") print("x is less than or equal to 30")
else: else:
if i==1: print("x is less than or equal to 25")
print("2") else:
print("3") print("x is less than or equal to y")
Python - Loops
Python • For-Loops:
# Basic approach − Iterate over a list of values
sum = 0
sum += 1
− Common lists can be created ad-hoc
print(sum)
sum += 2 • While-Loops:
print(sum)
… − Iterate over a list of values
sum += 100 − Useful when the number of iterations is uncertain
print(sum)
# While-Loop
sum = 0 Use for-loops for efficient and compact iteration.
index = 1
while index <= 100: Use while-loops cautiously to avoid infinite loops.
sum += index
print(sum) Documentation:
index += 1
https://docs.python.org/3/tutorial/controlflow.html
Python - Functions
Documentation:
https://docs.python.org/3/tutorial/controlflow.html
Scripts, modules, packages, and libraries
− Module: Python file that’s intended to be imported into scripts or other modules
• These are not strict definitions but give you more understanding of how the Python community works
Installing and importing libraries