Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Introduction-to-Python-Chapter-1-1-Python-Basics

The document serves as an introduction to Python for machine learning, highlighting its general-purpose nature, simplicity, and extensive libraries. It provides instructions on running Python code, including using the Python interpreter, scripts, Google Colaboratory, and Jupyter Notebook. Additionally, it covers variables, data types, input/output operations, and Python operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction-to-Python-Chapter-1-1-Python-Basics

The document serves as an introduction to Python for machine learning, highlighting its general-purpose nature, simplicity, and extensive libraries. It provides instructions on running Python code, including using the Python interpreter, scripts, Google Colaboratory, and Jupyter Notebook. Additionally, it covers variables, data types, input/output operations, and Python operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Introduction to

Python
for Machine
Learning
Hello Python!
INTRODUCTION TO PYTHON

Data Scientist at DataCamp


Why Python?
• General purpose: build anything
• Simplicity and Readability
• Open source! Free!
• Extensive Libraries and Frameworks
• Large and Supportive Community
• #1 Programming Language for ML, DS, AI, DL
How to
run
Python
Codes
Running Python Code using Python Interpreter/Shell

Step 1: Open the Command Prompt or Terminal or Anaconda Prompt

Step 2: type ‘python’

Step 3: type print(“Hello, World!”)


Running Python Scripts
Step 1: Write a Python Script

Open a text editor like Notepad and type:


Name = “Mong”
print(“Hello” + Name)

Step 2: Save the file with a .py extension, for example, hello.py, in a known
directory (e.g. Desktop)).

Step 3: Open the Command Prompt or Terminal or Anaconda Prompt

Step 4: Navigate to Your Script:

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

#1 Calculate the sum of two numbers

123 + 321

#2 Print: Hello Python!

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'

• Different type = different behavior!


Questions?
Python Input/Output (I/O) Operations

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

The input() function allows the user to enter data.


It reads the input as a string.

name = input("Enter your name: ")


print("Hello, " + name + "!")

number = int(input("Enter an integer number: "))


print("The number you entered is:", number)
Python Input/Output (I/O) Operations

Output

The print() function is used to display information to the user.

print("Welcome to Python Basics Tutorial!")


Printing Methods in Python
name = “John”
age = 30

1 Direct Printing
print(name)
print(age)

2 Concatenation – using ‘+’ operator


print("Name: " + name + ", Age: " + str(age))
# Output: Name: John, Age: 30

3 Comma Separation – using ‘,’


print("Name:", name, "Age:", age)
# Output: Name: John Age: 30
Printing Methods in Python
name = “John”
age = 30

4 ‘%’ Operator (Old style)


print("Name: %s, Age: %d" % (name, age))
# Output: Name: John, Age: 30

5 String Formatting (‘format()’)


print("Name: {}, Age: {}".format(name, age))
# Output: Name: John, Age: 30

6 f-strings (Literal String Interpolation)


print(f"Name: {name}, Age: {age}")
# Output: Name: John, Age: 30
Python Data Types

Data types represent the kind of value that tells Python what operations can be
performed on a given data.

The basic types include:


Integers
Floats
Strings
booleans
Python Operators

Operators are used to perform operations on variables and values.

Python divides the operators into the following groups:

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?

Data Scientist at DataCamp

You might also like