Short Introduction To Python Basics: Geared Towards Data Analysis

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

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

var Number: integer;


Number := 10;

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)

print(len(data)) – length of data – 12 characters

print(data[0]) – gives 1st letter in the word - h

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.

# Test sum function


result = mysum(1, 3)--- could also put variables
print(result)

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)

My range could series of numbers (1,2,3,4,5

# 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")

# If Statement with else branch


is_customer = "No"
if (is_customer == "No"): use == when using text
print("No")
else:
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

You might also like