Short Introduction To Python Basics: Geared Towards Data Analysis
Short Introduction To Python Basics: Geared Towards Data Analysis
Short Introduction To Python Basics: Geared Towards Data Analysis
1
Getting Started with Python
Programming
• Early 1990s: Guido van Rossum
– invented the Python programming language
• Python is a high-level, general-purpose
programming language for solving problems on
modern computer systems
• Useful resources at www.python.org
• Extensive Python documentation at python.org/doc/
• Python as component of the Anaconda package:
https://www.anaconda.com/download/
The programming language for this course.
Note that we are using Python 3, not Python 2.
2
Running Code in the Interactive Shell
• Python is an interpreted language
• Simple Python expressions and statements can
be run in the shell
– Easiest way to open a Python shell is to launch
Jupyter QtConsole (formerly IPython)
(part of Anaconda).
– or better: use the Jupyter Notebook.
You can easily scroll up and down and additional
notes between the code!
– Shell/Jupyter Notebook is useful for:
» Experimenting with short expressions or statements
• Do this! You can’t break the computer by doing so!
» Consulting the documentation 3
Jupyter Notebook
• Powerful tool for interactively developing and
presenting data science projects.
• Two types of cells: code and markdown.
• Some of the most important commands:
– Toggle between edit and command mode with Esc and
Enter, respectively.
– Once in command mode:
» Scroll up and down your cells with your Up and Down keys.
» Press A or B to insert a new cell above or below the active cell.
» M will transform the active cell to a Markdown cell.
» Y will set the active cell to a code cell.
» D + D (D twice) will delete the active cell.
– https://www.dataquest.io/blog/jupyter-notebook-tutorial/ 4
Quick Comparison
Compare Pascal
To Python
Number = 10
5
Numbers
# Basic Calculations
1+2
5/6
# Numbers
a = 123.1
print(a)
b = 10
print(b)
a + b
c = a + b
print(c)
6
Boolean and None
Multiple Assignments
# Boolean
a = True
b = False
print(a, b)
# No value
a = None
print(a)
# Multiple Assignments
a, b, c = 1, 2, 3
print(a, b, c)
7
Strings
# Strings
data = 'hello world'
print(data)
8
Functions
# Sum function
def mysum(x, y):
return x + y-----------specify here what want to do
variables defined abve
# Be very careful with the correct indentation!
# Recommendation: use 4 spaces.
locals()
# will give you a dictionary of all local variables.
9
Loops
# For-Loop
myrange = range(10) gives the domain/data we looking at
for i in myrange:
print(i)
# While-Loop
i = 0
while i < 10: this is the condition set for valu of I,
print(i) give result
i += 1
means
I = I + 1 use this condition otw will keep doing 0
10
If Statements
# If Statement (Boolean)
is_customer = True
if is_customer:
print("Yes")
11
Additional Readings
• Python for Data Analysis: Data Wrangling with Pandas, NumPy, and
IPython by Wes McKinney (pub. yr. 2017). Chapter 2 and 3.
• Machine Learning Mastery with Python by Jason Brownlee (pub. yr.
2017). Chapter 3.
• https://www.analyticsvidhya.com/blog/2016/01/complete-tutorial-learn-
data-science-python-scratch-2/
• DataCamp:
– Course: Intro to Python for Data Science
» Chapter: Python Basics
12
Appendix:
Arithmetic Expressions
• An arithmetic expression consists of operands
and operators combined in a manner that is
already familiar to you from learning algebra
13
Arithmetic Expressions (continued)
• Precedence rules:
– ** has the highest precedence and is evaluated first
– Unary negation is evaluated next
– *, /, and % are evaluated before + and -
– + and - are evaluated before =
– With two exceptions, operations of equal precedence
are left associative, so they are evaluated from left to
right
» ** and = are right associative
– You can use () to change the order of evaluation
14
Arithmetic Expressions (continued)
15
Mathematical Constants
# e, pi
import math
math.e
math.pi
16
17
18
19
20
21
22
23
24
25
26
27
28