0% found this document useful (0 votes)
4 views26 pages

Chapter1 - Dive Into Python

This document is an introduction to data science using Python, covering essential topics such as writing and executing Python code, loading data, and creating visualizations. It explains the use of modules like pandas and matplotlib, variable creation, and common errors in Python programming. Additionally, it outlines the structure of functions and their components, including positional and keyword arguments.

Uploaded by

ikhwan.ayat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Chapter1 - Dive Into Python

This document is an introduction to data science using Python, covering essential topics such as writing and executing Python code, loading data, and creating visualizations. It explains the use of modules like pandas and matplotlib, variable creation, and common errors in Python programming. Additionally, it outlines the structure of functions and their components, including positional and keyword arguments.

Uploaded by

ikhwan.ayat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dive into Python

I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N

Hillary Green-Lerman
Lead Data Scientist, Looker
What you'll learn
How to write and execute Python code with DataCamp
How to load data from a spreadsheet

How to turn data into beautiful plots

INTRODUCTION TO DATA SCIENCE IN PYTHON


Solving a mystery with data

INTRODUCTION TO DATA SCIENCE IN PYTHON


Using the IPython shell

INTRODUCTION TO DATA SCIENCE IN PYTHON


Using the script editor

INTRODUCTION TO DATA SCIENCE IN PYTHON


What is a module?
Groups related tools together
Makes it easy to know where to look for a particular tool

Common examples:
matplotlib

pandas

scikit-learn

scipy

nltk

INTRODUCTION TO DATA SCIENCE IN PYTHON


Importing pandas and matplotlib
import pandas as pd
from matplotlib import pyplot as plt

# Pandas loads our data


df = pd.read_csv('[Link]')

# Matplotlib plots and displays


[Link]([Link], [Link])
[Link]()

INTRODUCTION TO DATA SCIENCE IN PYTHON


Importing a module
Importing a Module

import pandas

Importing a module with an alias

import pandas as pd

INTRODUCTION TO DATA SCIENCE IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N
Creating variables
I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N

Hillary Green-Lerman
Lead Data Scientist, Looker
Filing a missing puppy report
name = "Bayes"
height = 24
weight = 75.5

INTRODUCTION TO DATA SCIENCE IN PYTHON


Rules for variable names
Must start with a letter No spaces or special
(usually lowercase) characters

After first letter, can use Case sensitive ( my_var is


letters/numbers/underscores different from MY_VAR )

# Valid Variables # Invalid Variables


bayes_weight bayes-height
b bayes!
bayes42 42bayes

INTRODUCTION TO DATA SCIENCE IN PYTHON


Error messages
bayes-height = 3

File "<stdin>", line 1


bayes-height = 3
^
SyntaxError: can't assign to operator

INTRODUCTION TO DATA SCIENCE IN PYTHON


Floats and strings
float: represents an integer or decimal number

height = 24
weight = 75.5

string: represents text; can contain letters, numbers, spaces,


and special characters

name = 'Bayes'
breed = "Golden Retriever"

INTRODUCTION TO DATA SCIENCE IN PYTHON


Common string mistakes
Without quotes, you'll get a name error.

owner = DataCamp

File "<stdin>", line 1, in <module>


owner = DataCamp
NameError: name 'DataCamp' is not defined

If you use different quotation marks, you'll get a syntax error.

fur_color = "blonde'

File "<stdin>", line 1


fur_color = "blonde'
^
SyntaxError: EOL while scanning string literal

INTRODUCTION TO DATA SCIENCE IN PYTHON


Displaying variables
name = "Bayes"
height = 24
weight = 75

print(height)

24

INTRODUCTION TO DATA SCIENCE IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N
What is a function?
I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N

Hillary Green-Lerman
Lead Data Scientist, Looker
A function is an action

INTRODUCTION TO DATA SCIENCE IN PYTHON


Functions in code
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('letter_frequency.csv')

[Link](df.letter_index, [Link], label='Ransom')


[Link]()

Functions perform actions:

pd.read_csv() turns a csv file into a table in Python

[Link]() turns data into a line plot

[Link]() displays plot in a new window

INTRODUCTION TO DATA SCIENCE IN PYTHON


INTRODUCTION TO DATA SCIENCE IN PYTHON
Anatomy of a function: function name

Function Name:

Starts with the module that the function "lives" in ( plt )

Followed by the name of the function ( plot )

Function name is always followed by parentheses ()

INTRODUCTION TO DATA SCIENCE IN PYTHON


Anatomy of a function: positional arguments

Positional Arguments:

These are inputs to a function; they tell the function how to


do its job

Order matters!

INTRODUCTION TO DATA SCIENCE IN PYTHON


Anatomy of a function: keyword arguments

Keyword Arguments:

Must come after positional arguments

Start with the name of the argument ( label ), then an equals


sign ( = )

Followed by the argument ( Ransom )

INTRODUCTION TO DATA SCIENCE IN PYTHON


Common function errors
Missing commas between arguments

Missing closed parenthesis

INTRODUCTION TO DATA SCIENCE IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA S C I E N C E I N P Y T H O N

You might also like