Introduction-to-Python-Chapter-1-1-Python-Basics
Introduction-to-Python-Chapter-1-1-Python-Basics
Python
for Machine
Learning
Hello Python!
INTRODUCTION TO PYTHON
Step 2: Save the file with a .py extension, for example, hello.py, in a known
directory (e.g. Desktop)).
Use the cd command to navigate to the directory where your Python file is
saved. For example: cd .\Desktop\
Running Python Scripts
Step 5: In the Command Prompt, type python filename.py and press Enter.
Replace filename.py with the actual name of your Python file.
e.g.
python hello.py
Google Colaboratory (Online)
Colab is a hosted Jupyter Notebook service that requires no setup
to use and provides free access to computing resources, including
GPUs and TPUs.
Colab is especially well suited to machine learning, data science,
and education.
https://colab.research.google.com/
Jupyter Notebook (Offline)
an interactive web-based computational environment widely
used for data analysis, scientific computing, and machine
learning.
It allows you to create and share documents that contain live
code, equations, visualizations, and narrative text.
https://www.anaconda.com/download
Run a simple Python Code
123 + 321
print(“Hello Python!”)
Tip:
Use your Notebooks to code along
Let’s start coding!
Variables and Types
INTRODUCTION TO PYTHON
Hugo Bowne-
Data Scientist at DataCamp
Anderson
Variable
• Specific, case-sensitive name
• Call up value through variable name
• 1.79 m - 68.7 kg
height = 1.79
weight = 68.7
height
1.79
Calculate BMI
height = 1.79 68.7 / 1.79 ** 2
weight = 68.7
21.4413
height
1.79 weight / height ** 2
21.4413
𝑤ⅇⅈ𝑔ℎ𝑡
𝐵𝑀𝐼 = bmi = weight / height ** 2
ℎⅇⅈ𝑔ℎ𝑡 2
bmi
21.4413
Reproducibility
height = 1.79
weight = 68.7
bmi = weight / height ** 2
print(bmi)
21.4413
Tip:
in Jupyter, the last line of code in cell a block gets displayed automagically
Reproducibility
height = 1.79
weight = 74.2 # <-
bmi = weight / height ** 2
print(bmi)
23.1578
Python Types
type(bmi)
float
day_of_week = 5
type(day_of_week)
int
Python Types (2)
x = "body mass index"
y = 'this works too’
type(y)
str
z = True
type(z)
bool
Python Types (3)
2 + 3
'ab' + 'cd'
'abcd'
I/O operations in Python involve interacting with the user (input) or displaying
results (output).
The most common functions are input() for receiving input and print() for
displaying output.
Python Input/Output (I/O) Operations
Input
Output
1 Direct Printing
print(name)
print(age)
Data types represent the kind of value that tells Python what operations can be
performed on a given data.
arithmetic,
assignment,
comparison,
logical,
identity,
membership,
and bitwise.
Python Operators
Python Operators
Python Operators
Python Operators
Python Operators
Python Operators
Python Operators
Python Operators
Questions?