Python Basics for Data Analytics
Python Basics for Data Analytics
There are many open source software and libraries that use Python
and data analysis tools built on them.
What is Python?
Python is a general, high-level programming language designed for
code readability and simplicity.
Python is available for free as open source and has a large community
supporting its development and associated tools.
Python was developed by Guido van Rossum and first released in
1991. Python 2.0 was released in 2000 (latest version 2.7), and a
backwards-incompatible release Python 3 was in 2008.
• Our coding style will be Python 3 but most code will also work for Python 2.
• Name does refer to Monty Python.
DATA 301: Data Analytics (4)
Some Quotes
"If you can't write it down in English, you can't code it."
-- Peter Halpern
Introduction to Programming
An algorithm is a precise sequence of steps to produce a result. A
program is an encoding of an algorithm in a language to solve a
particular problem.
Comments
Comments are used by the programmer to document and explain the
code. Comments are ignored by the computer. Two types:
• 1) One line comment: put “#” before the comment and any characters to the end
of line are ignored by the computer.
• 2) Multiple line comment: put “"""” at the start of the comment and “"""” at
the end of the comment. The computer ignores everything between the start
and end comment indicators.
Example: # Single line comment
print (1) # Comment at end of line
""" This is a
multiple line
comment """
DATA 301: Data Analytics (9)
Python Programming
A Python program, like a book, is read left to right and top to bottom.
Each command is on its own line.
Python code
Program output
DATA 301: Data Analytics (12)
print("Hello World!")
Python Question
Question: How many of the following statements are TRUE?
1) Python is case-sensitive.
2) A command in Python is terminated by a semi-colon.
3) Indentation does not matter in Python.
4) A single line comment starts with """.
5) The print command prints to standard input.
A) 0 B) 1 C) 2 D) 3 E) 4
DATA 301: Data Analytics (15)
Variables
A variable is a name that refers to a location that stores a data value.
Variable Assignment
Assignment using an = sets the value of a variable.
num
Example:
num = 10 10
message = "Hello world!"
message
Hello world!
DATA 301: Data Analytics (17)
Python Variables
To create a variable in Python, you must only provide a name.
• A variable type is dynamic. It can store numbers, strings, or Boolean at any time.
Example:
val = 5
val = "Hello"
Boolean values can be either True or False. Note case matters.
isAwesome = True
isAwesome = False
DATA 301: Data Analytics (18)
Variable Rules
Variables are a name that must begin with a letter and cannot contain
spaces.
Variables are created when they are first used. There is no special
syntax to declare (create) a variable.
Variable names ARE case-sensitive. Numbers are allowed (but not at
the start). Only other symbol allowed is underscore ('_');
A programmer picks the names for variables, but try to make the
names meaningful and explain their purpose.
Avoid naming variables as reserved words. A reserved word has
special meaning in the language.
• e.g. if, for, else
DATA 301: Data Analytics (19)
1) name
2) string2
3) 2cool
4) under_score
5) spaces name
6) else
A) 0 B) 1 C) 2 D) 3 E) 4
DATA 301: Data Analytics (20)
Modulus % 9%4
(answer is 1)
Exponent ** 5 ** 2
(answer is 25)
DATA 301: Data Analytics (21)
8 ** 2 + 12 / 4 * (3 – 1) % 5
A) 69 B) 65 C) 36 D) 16 E) 0
DATA 301: Data Analytics (23)
Question 3: Write a program that has a variable called name with the
value of your name and a variable called age storing your age. Print
out your name and age using these variables.
DATA 301: Data Analytics (24)
Strings
Strings are sequences of characters that are surrounded by either
single or double quotes.
• Use \ to escape ' E.g. There\'s
• Can use triple double quotes """ for a string that spans multiple lines.
Example:
name = "Joe Jones"
storeName = 'Joe\'s Store'
print("""String that is really long
with multiple lines
and spaces is perfectly fine""")
DATA 301: Data Analytics (25)
Example:
str = "Hello"
print(str[1]) # e
print("ABCD"[0]) # A
print(str[-1]) # o
# Negative values start at end and go backward
DATA 301: Data Analytics (26)
1) ""
2) ''
3) "a"
4) " "
5) """
6) "Joe\' Smith\""
A) 1 B) 2 C) 3 D) 4 E) 5
DATA 301: Data Analytics (28)
A) Error
B) Hello5World!
C) Hello5 World!
D) Hello 5 World!
DATA 301: Data Analytics (31)
Substring
The substring function will return a range of characters from a string.
Syntax:
st[start:end] # start is included, end is not
# first character is index 0
Examples:
st = "Fantastic"
print(st[1]) # a
print(st[0:6]) # Fantas
print(st[4:]) # astic
print(st[:5]) # Fanta
print(st[-6:-2]) # tast
DATA 301: Data Analytics (32)
Substring Question
Question: What is the output of this code?
st = "ABCDEFG"
print(st[1] + st[2:4] + st[3:] + st[:4])
A) ABCDCDEFGABCD
B) ABCDEFGABC
C) ACDDEFGABCD
D) BCDDEFGABCD
E) BCDECDEFGABC
DATA 301: Data Analytics (33)
Split
The split function will divide a string based on a separator.
Examples:
st = "Awesome coding! Very good!"
print([Link]())
# ['Awesome', 'coding!', 'Very', 'good!']
print([Link]("!"))
# ['Awesome coding', ' Very good', '']
st = 'data,csv,100,50,,25,"use split",99'
print([Link](","))
# ['data', 'csv', '100', '50', '', '25',
# '"use split"', '99']
DATA 301: Data Analytics (34)
Question 2: Write a Python program that prints out the first name and
last name of Steve Smith like below. You must use substring.
• Bonus challenge: Use find() function so that it would work with any name.
First Name: Steve
Last Name: Smith
DATA 301: Data Analytics (35)
Print Formatting
The print method can accept parameters for formatting.
print("Hi", "Amy", ", your age is", 21)
print("Hi {}, your age is {}".format("Amy",21))
Python Clock
Python time() function returns the current time in seconds:
import time
startTime = [Link]()
print("Start time:", startTime)
print("How long will this take?")
endTime = [Link]()
print("End time:", endTime)
print("Time elapsed:", endTime-startTime)
DATA 301: Data Analytics (38)
Python Input
To read from the keyboard (standard input), use the method input:
Comparisons
A comparison operator compares two values. Examples:
• 5 < 10
• N > 5 # N is a variable. Answer depends on what is N.
Condition Examples
n = 5
v = 8
print(n > 5) # False
print(n == v) # False
print(n != v) # True
print(n == v and n+4>v) # False
print(n == v or n+4>v) # True
print(n+1 == v-2 or not v>4) # True
DATA 301: Data Analytics (43)
A) 0 B) 1 C) 2 D) 3 E) 4
DATA 301: Data Analytics (44)
Decisions
Decisions allow the program to perform different actions based on
conditions. Python decision syntax:
if condition: if condition:
Done if condition
statement is True
statement
else:
statement Done if condition
is False
• The statement after the if condition is only performed if the condition is True.
• If there is an else, the statement after the else is done if condition is False.
• Indentation is important! Remember the colon!
DATA 301: Data Analytics (45)
if condition: if n == 1:
statement print("one")
elif condition: elif n == 2:
statement print("two")
elif condition: elif n == 3:
statement print("three")
else: else:
statement print("Too big!")
print("Done!")
DATA 301: Data Analytics (46)
Question: Decisions
Question: What is the output of the following code?
n = 3
if n < 1:
print("one")
elif n > 2:
print("two")
elif n == 3:
print("three")
Question 2: Write a Python program that asks the user for a number
between 1 and 5 and prints out the word for that number (e.g. 1 is
one). If the number is not in that range, print out error.
DATA 301: Data Analytics (52)
Example: n = 1
while n <= 5: Question: What does this print?
print(n)
n = n + 1 # Shorthand: n += 1
DATA 301: Data Analytics (54)
n = 4
while n >= 0:
n = n - 1
print(n)
n = 1
while n <= 5:
print(n)
n = n + 1
for i in range(1,6):
print(i)
Starting number
DATA 301: Data Analytics (57)
Using range
The basic form of range is:
range(start,end)
• start is inclusive, end is not inclusive
• default increment is 1
i=0
while i < 10: for i in range(0, 10, 1):
print(i) print(i)
i += 1
DATA 301: Data Analytics (59)
Example:
• This loop was supposed to print 0 to 10, but it does not.
for i in range(0,10):
print(i)
Question: How can we fix this code to print 0 to 10?
DATA 301: Data Analytics (61)
for i in range(1,10):
print(i)
A) 0 B) 9 C) 10 D) 11 E) error
DATA 301: Data Analytics (62)
for i in range(11,0):
print(i)
A) 0 B) 9 C) 10 D) 11 E) error
DATA 301: Data Analytics (63)
Question 3: Write a program that asks the user for 5 numbers and
prints the maximum, sum, and average of the numbers.
DATA 301: Data Analytics (64)
Lists Overview
A list is a collection of data items that are referenced by index.
• Lists in Python are similar to arrays in other programming languages
A list allows multiple data items to be referenced by one name and
retrieved by index.
Python list:
data = [100, 200, 300, 'one', 'two', 600]
0 1 2 3 4 5
list variable
name Indexes
DATA 301: Data Analytics (65)
List Operations
data = [1, 2, 3, 5]
lst = []
Operation Syntax Examples Output
Add item [Link](val) [Link](1) [1, 2, 3, 5, 1]
Insert item [Link](idx,val) [Link](3,4) [1, 2, 3, 4, 5]
Remove item [Link](val) [Link](5) [1, 2, 3]
Update item list[idx]=val lst[0]=10 [10]
Length of list len(list) len(data) 4
Slice of list list[x:y] data[0:3] [1, 2, 3]
Find index [Link](val) [Link](5) 3
Sort list [Link]() [Link]() [1, 2, 3, 5]
DATA 301: Data Analytics (67)
List Details
If you provide an index outside of the valid range, Python will return
an index error.
Equivalent to:
evenNums100 = []
for n in range(101):
if n%2==0:
[Link](n)
DATA 301: Data Analytics (69)
Example:
data = list(range(1,11))
print(data) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(data[1:8:2]) # [2, 4, 6, 8]
print(data[1::3]) # [2, 5, 8]
DATA 301: Data Analytics (70)
Question: List
Question: At what index is item with value 3?
data = [1, 2, 3, 4, 5]
[Link](3)
[Link](1, 3)
[Link](2)
[Link]()
data = data[1:4]
A) 0 B) 1 C) 2 D) 3 E) not there
DATA 301: Data Analytics (71)
Python Dictionary
A dictionary is a collection of key-value pairs that are manipulated
using the key.
dict = {1:'one', 2:'two', 3:'three'}
print(dict[1]) # one
print(dict['one']) # error - key not found
if 2 in dict: # Use in to test for key
print(dict[2]) # two
dict[4] = 'four' # Add 4:'four'
del dict[1] # Remove key 1
[Link]() # Returns keys
[Link]() # Returns values
DATA 301: Data Analytics (73)
Question: Dictionary
Question: What is the value printed?
A) 7 B) 0 C) 10 D) 6 E) error
DATA 301: Data Analytics (74)
We use functions so that we do not have to type the same code over
and over. We can also use functions that are built-in to the language
or written by others.
Defining and Calling DATA 301: Data Analytics (76)
def doubleNum(num):
num = num * 2
print("Num: "+num)
return num
Call function by Argument
name
n = doubleNum(5) # 10
print(str(doubleNum(n))) # ??
DATA 301: Data Analytics (78)
print(doFunc(doubleNum, 10)) # 20
print(doFunc(lambda x: x * 3, 5)) # 15
DATA 301: Data Analytics (82)
Question: Functions
Question: What is the value printed?
def triple(num):
return num * 3
n = 5
print(triple(n)+triple(2))
A) 0 B) 6 C) 15 D) 21 E) error
DATA 301: Data Analytics (83)
Conclusion
Python is a general, high-level programming language designed for
code readability and simplicity.
Programming concepts covered:
• variables, assignment, expressions, strings, string functions
• making decisions with conditions and if/elif/else
• repeating statements (loops) using for and while loops
• reading input with input() and printing with print()
• data structures including lists and dictionaries
• creating and calling functions, using built-in functions (math, random)
Python is a powerful tool for data analysis and automation.
DATA 301: Data Analytics (85)
Objectives
• Explain what is Python and note the difference between Python 2 and 3
• Define: algorithm, program, language, programming
• Follow Python basic syntax rules including indentation
• Define and use variables and assignment
• Apply Python variable naming rules
• Perform math expressions and understand operator precedence
• Use strings, character indexing, string functions
• String functions: split, substr, concatenation
• Use Python datetime and clock functions
• Read input from standard input (keyboard)
DATA 301: Data Analytics (86)
Objectives (2)
• Create comparisons and use them for decisions with if
• Combine conditions with and, or, not
• Use if/elif/else syntax
• Looping with for and while
• Create and use lists and list functions
• Advanced: list comprehensions, list slicing
• Create and use dictionaries
• Create and use Python functions
• Use built-in functions in math library
• Create random numbers
• Advanced: passing functions, lambda functions