Python Mod1
Python Mod1
Module 1: Programming
fundamentals for data analytics
Python for Data Analytics
Sean Barnes
Our philosophy
💡 LLMs are: 📚 In this course, you’ll:
● A complement to skills, not a ● Learn fundamentals of coding and
replacement LLM collaboration
● Excellent coding companions for ● Build code interpretation skills
coding and fix bugs ● Develop intuition about what tasks
LLMs excel at and can go wrong
🧠 You’ll get the most from LLMs if you ● Use Coursera’s built-in lab chatbot
know how to code
to completement code writing
Sean Barnes
LLMs in this course
● New models and features
🗓 Demonstrates the most up-to-date constantly released
capabilities as of 2025
Sean Barnes
LLM options
Sean Barnes
Programming fundamentals
for data analytics
Module 1 introduction
Module 1 outline Library revenue Safety inspection scores
Fundamentals
Role of programming Control flow
of Python
Use Jupyter
notebook Variables Loops
environment
Lists
Modify your code
Functions using an LLM
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Computer programming
What is computer programming?
🧠 Your role:
● Tell computers very precisely what
you want them to do
💻 Computer’s role:
● Carry out commands to the letter
● If you don’t tell them exactly how to do
what you want, they might:
⛔ Make a mistake
⛔ Only do some of your commands
Sean
SeanBarnes
Barnes
Why computer programming?
● Automates thinking work like:
● Math
● Data analysis
Intern
Sean
SeanBarnes
Barnes
Efficiency Compute average
Completing tasks with minimal time daily sales for:
and resources
A particular item
📱 Intern: 20 seconds
1 💻 Python: < 0.001 seconds
200 products
📱 Intern: > 1 hour
100 stores
📱 Intern: > 2 weeks
Sean
SeanBarnes
Barnes
Traceability Your averages look too low:
The ability to track how tasks are completed.
Sean
SeanBarnes
Barnes
Repeatability
Doing the same task in the same way many times.
Sean
SeanBarnes
Barnes
Quick note about Python
📊 Visualizations
⚙ Automation
🌐 Web development
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
What code does
🔢 Numbers 📁 Folder of images Program needs to expect
Input
📄 Dataset 📊 Something else data in that format
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Python or a spreadsheet?
Interface
Spreadsheets
● Manually managing the data
● Easily format data with colors and fonts
● Store data in one format
Sean
SeanBarnes
Barnes
Interface
Spreadsheets
● Manually managing the data
● Easily format data with colors and fonts
● Store data in one format
Python
● Processing happens in background
● Inspect data, but won’t be managing
it as directly
● Format the markdown
Sean
SeanBarnes
Barnes
Interface
Spreadsheets
● Manually managing the data
● Easily format data with colors and fonts
● Stores data in one format
Python
● Processing happens in background
● Inspect data, but won’t be managing
it as directly
● Format the markdown
Sean
SeanBarnes
Barnes
Complexity of analysis
Spreadsheets
● Sorting
● Filtering
● Hypothesis testing
Sean
SeanBarnes
Barnes
Visualization
❌ Not possible to create
boxplot in Google Sheets
Spreadsheets
● Sorting
● Filtering
● Hypothesis testing
Source: Google Docs Editor Help
Python
● Expands those capabilities ✅ Customize every
● Perform sophisticated statistics aspect of plot
✅ Apply unique color
● Run simulations
schemes
● Create machine learning models ✅ Create several plots at
● A more custom analysis once
Sean
SeanBarnes
Barnes
Traceability & Repeatability
?
Spreadsheets
● You can use version history raw_data.csv organized_data.csv
Sean
SeanBarnes
Barnes
Summary
Spreadsheets Python
If you’re looking to: If you’re looking for:
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
Data types in action
You
Data Analyst
Sean
SeanBarnes
Barnes
Summary
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Storing information:
variables
Variables Create a variable:
income_2023 = 830696
💾 Used to store values you expect
to change or reuse
1⃣ Declare 2⃣ Assign
only once
📦 A box that can store some
information
830696
830000 Overwriting:
income_2023
income_2023 = 830000
830000
830696
● Don’t need the previous value
income_2023
● Otherwise, create a new variable
Sean
SeanBarnes
Barnes
Variables
income_2023 = 830696
print(income_2023
print(35602)
830696 - 794398)
35602
830696
income_2023
803696
income_2023
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
The coder’s mindset
💪 Be resilient
Key strategies:
💬 Ask an LLM
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Creating lists
Lists [737487, 740967, 756155, 763415, 830696]
● A type of sequence
● A collection of values that has an order
incomes
● Values inside a list are also called items
Declares a variable
and assigns value Brackets create a list
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
List operations
Recap length of list - 1
0 1 2 3 4
Name Index
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Taking action:
calling functions
Functions
Input you provide Processes it Produce output
Sean
SeanBarnes
Barnes
Functions
Inputs to a function: arguments
sum() .append()
sum(incomes) [Link](837404)
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
State
State
📸 Computer's "state" is like a
snapshot of all current info
Sean
SeanBarnes
Barnes
Hidden state from helper_functions import get_list
incomes = get_list("Total Income")
print(incomes)
✅ 100% reliability: don’t always
need to see intermediate steps
● Can’t actually see what values are in list
✅ Makes calculations fasts ● Calculate without seeing data itself:
○ max()
○ min()
Problems:
○ sum() / len()
⛔ Think variable has a value, but
it has a different one ● Use print() to:
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Control flow
Control flow Sequential control flow
● Order in which individual lines of ● Default way code runs
code are executed
● Lines of code will execute in order
● Determines the path computer
takes through the code
Sean
SeanBarnes
Barnes
Conditional control flow
● Dataset: Libraries from Start
all around the world
SUMIF
Add 1 to total Nothing
Sean
SeanBarnes
Barnes
Repetitive control flow
● Data: A 100 items in a list items = [item0, item1, item2,..., item99]
item[0] = item[0] / 2
● Goal: Divide them all by 2 item[1] = item[1] / 2
item[2] = item[2] / 2
…
● Allows you to write a few lines of item[99] = item[99] / 2
code that repeat an action
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Comparison
Yes No
Scenario 90 or above
Inspection Yes No
A 80 to 89
Inspection Yes No
Give score from 0 to 100 based on
B
● 70 to 79
its sanitation
● Raw score are analyzed to: Inspection
Sean
SeanBarnes
Barnes
Comparison operators
● Answers question about values
>= ● Two possible answers: True False Type: Boolean
● = is taken by the
assignment operator == !=
● If you’re checking for Equals Not equals
equality, always write ==
Sean
SeanBarnes
Barnes
Comparison operators The
Beverly Pasta Melrose Modern Alfred’s
Falafel Roma Shrimp Eats Coffee
A B C Failure
Question Inequality
Did Modern Eats fail their inspection? scores[3] < 70 or scores[3] <= 69
Sean
SeanBarnes
Barnes
Knowledge check
1. Which of these expressions answers the question
“Did Beverly Falafel score a 100?”?
scores[0] == 100
scores[0] > 99
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
Score: 100 Score: 73
print("Start
bf_score = 96of code") # 1 print("Start
bf_score = 96of code") # 1
a =score
if 90 #>=minimum
90: score for an A a =score
if 90 #>=minimum
90: score for an A
print("A") # 2 print("A") # 2
# minimum score for an A # minimum score for an A
else: else:
print(bf_score >= a) print(bf_score >= a)
print("Less than A") # 3 print("Less than A") # 3
print("End of code") # 4 print("End of code") # 4
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
print("Starting loop")
Sean
SeanBarnes
Barnes
Getting Started with Python
Indentation
Indentation
Tells Python which lines belong with
which control structure scores = [96, 91, 79, 93, 86]
Outside
print("Starting loop")
if score >= A:
print("You got an A!") for score in scores:
if score >= a:
for score in scores: print("A")
Inside
print(score)
else:
Sean
SeanBarnes
Barnes
Recap: Indentation
● Conditionals and loops needs an if score >= A:
indented block of code print("You got an A!")
● Python will throw an error if you don’t
include it for score in scores:
print(score)
● Indentation sets aside the code you
want to execute when:
● Can cause silent errors:
✅ Condition is true
💬 Try debugging with LLM
🔄 Inside the loop
🖨 Print statements
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
elif statement if score >= A:
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Sean
SeanBarnes
Barnes
Recap: range If you need to:
1. range()
⬜ Access to a single list:
Creates list of numbers, from 0 to
number you specify Use for score in scores
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
Execution order
1 A = 90
2 B = 80
3 C = 70
4 scores = [96, 91, 79, 93, 86, …] Variable Value
5
6 num_As = 0 num_As 2
3
0
1
7 num_Bs = 0
8 num_Cs = 0 num_Bs 1
0
9
10 for score in scores: num_Cs 1
0
11 if score >= A:
12 num_As += 1
score 91
79
96
93
86
13 elif score >= B:
14 num_Bs += 1
15 elif score >= C:
16 num_Cs += 1
17
18 num_scores = len(scores)
Sean
SeanBarnes
Barnes
Programming fundamentals
for data analytics
○ Test your code as many times ### START CODE HERE ###
as you like before you submit
num_observations = None
💡 To make sure you get best grade:
### END CODE HERE ###
○ Use provided variables names
○ Replace instances of “None”
○ Only need modify cells that
include comment “Graded cell”
Sean
SeanBarnes
Barnes