python for data analysis
Welcome! I'm thrilled to help you build a solid foundation in Python for data analysis. Before we jump into writing code, I'd love to know a bit more about your background so we can make this as relevant as possible for you.
To help me tailor our sessions, could you tell me a little about your experience with programming in general, and why you are interested in learning Python for data analysis?
I know some programming (like SQL or R) and want to learn Python's approach.
Since you already have experience with SQL and R, you are going to find Python's approach to data analysis incredibly intuitive, yet uniquely structured.
The Python Playground
Unlike SQL, which is declarative and operates on tables via database engines, or R, which was built from the ground up specifically for statisticians, Python is a general-purpose, object-oriented language. This means it is highly readable and relies heavily on to do the heavy lifting for data analytics.
Let's kick things off by looking at how Python handles basic data types and variables. In R, you assign variables using <-, but in Python, we use the simple assignment operator =.
Python dynamically infers your data types. Let's look at the four core scalar types you will use constantly when cleaning and inspecting data:
- Integers (
int): Whole numbers, like a count of active users. - Floats (
float): Decimals, used for continuous measurements like pricing. - Strings (
str): Text data, wrapped in single or double quotes. - Booleans (
bool): Binary truth values,TrueorFalse(note the capital letters!).
# Assigning data to variables in Python
user_count = 150 # This is an Integer (int)
subscription_price = 9.99 # This is a Float (float)
product_name = "SaaS Pro" # This is a String (str)
is_active = True # This is a Boolean (bool)
Notice how we don't have to declare what type of data the variable holds; Python just figures it out.
Would you like to try writing a quick line of code to test this out, or shall we continue and explore how to use Python as a calculator with basic operators?
Let's continue to using Python as a calculator
Let's treat Python as our supercharged calculator! Because you already know R and SQL, you know how crucial math and clean logical operations are when you're preparing data features.
The Python Calculator
Python uses standard symbols for basic arithmetic, but it has a couple of unique operators that are incredibly handy for data processing.
Here is a quick breakdown of how Python handles calculations:
- Addition (
+) & Subtraction (-) - Multiplication (
*) & Division (/): Division in Python 3 always returns a float, even if the numbers divide perfectly (e.g.,4 / 2returns2.0). - (
**): Instead of using^like you do in R, Python uses double asterisks for raising a number to a power. - Modulo (
%): This returns the remainder of a division. It is highly useful in data cleaning when you want to group data or filter for every -th row.
# basic arithmetic operations
revenue = 1000
cost = 250
profit = revenue - cost # Subtraction
squared_growth = 5 ** 2 # Exponentiation (5 squared = 25)
remainder = 10 % 3 # Modulo (10 divided by 3 is 3, remainder 1)
Just like standard mathematics, Python respects operator precedence—often remembered as PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). If you want to make sure your calculations happen in a specific order, just wrap them in parentheses, exactly like you would in a spreadsheet formula.
Would you like to see how Python evaluates a multi-step formula next, or should we jump straight to comparing values with comparison operators?
Continue to comparison operators and booleans
Now that we have handled basic arithmetic, let's explore how we compare values in Python. In data analysis, you will constantly use comparisons to filter datasets—like finding transactions greater than $100 or locating users who registered after a certain date.
When you compare two values in Python, the expression always evaluates to a boolean (True or False).
Here are the standard comparison operators you will use:
- Greater than (
>) and Less than (<) - Greater than or equal to (
>=) and Less than or equal to (<=) - Equal to (
==): Notice that we use a double equals sign. A single equals sign=is for assigning values to variables, whereas==is for asking "Are these two things equal?" This is a very common source of bugs for beginners! - Not equal to (
!=)
# Comparing metrics
average_order_value = 85.50
target_value = 100.00
# Is our average order value hitting the target?
is_target_met = average_order_value >= target_value # Evaluates to False
# Checking if a categorical string matches
user_role = "Admin"
is_admin = user_role == "Admin" # Evaluates to True
print(is_admin)
Just like in R, these logical checks are the bread and butter of subsetting data.
Before we move on to how we can combine multiple conditions together using logical operators, would you like to do a quick check on this to make sure the difference between = and == feels comfortable?
Let's continue to combining conditions with logical operators
Now that you can compare single values, we can look at how to combine those checks using Python's logical operators: and, or, and not. In data analysis, you will use these constantly to filter complex datasets—like finding customers who are both active and spent over $100.
Combining Conditions
Unlike some languages that use symbols like && or ||, Python keeps things clean by using for its logical operators:
and: ReturnsTrueonly if both conditions on either side are true.or: ReturnsTrueif at least one of the conditions is true.not: Reverses the boolean value that follows it (turnsTruetoFalse, and vice versa).
# Evaluating customer profiles for a promotion
is_active = True
total_spend = 120.50
has_coupon = False
# Must be active AND spent more than 100
gets_loyalty_discount = is_active and (total_spend > 100.00) # True
# Gets promo if they spent over 150 OR have a coupon
gets_promo = (total_spend > 150.00) or has_coupon # False
# Check if the user does NOT have a coupon
no_coupon_on_file = not has_coupon # True
Using parentheses around your individual comparisons isn't always strictly required by Python, but it is highly recommended. It makes your code much easier to read and ensures that the arithmetic and comparison steps happen in the exact order you expect before the logical operator ties them together.
Continue to check my understanding with a quiz on these operators
You've got it! Testing yourself is the absolute best way to lock in these fundamentals before we move on to working with actual datasets.
Let's put your understanding of operators, variable assignments, and boolean logic to the test. Carefully read the code snippet below and figure out what the final variables will evaluate to.
# Let's trace this step-by-step
score_one = 10
score_two = 3
# Calculation 1
result_math = (score_one % score_two) ** 2
# Calculation 2
is_valid = (score_one > 8) and not (score_two == 3)
What are the final values of result_math and is_valid after this script runs?
result_math = 9, is_valid = False
Spot on! Both of your evaluations are completely correct.
Control Structures: Making Decisions
To understand why result_math is 1 and is_valid is False, let's quickly trace the operations:
score_one % score_twois10 % 3, which leaves a remainder of1. Then,1 ** 2is still1.score_one > 8evaluates toTrue. However,score_two == 3is alsoTrue, sonot (score_two == 3)becomesFalse. Finally,True and Falseresolves toFalse.
Now we can use these logical results to control how our code behaves. In data analysis, you won't just calculate values; you will write scripts that make decisions based on those values. This is where like if, elif, and else come into play.
In SQL, you filter rows using a CASE WHEN statement. Python achieves this dynamic logic using blocks of code defined by indentation.
Here is how Python structured decision-making works:
if: The initial check. If this condition isTrue, Python executes the indented code block directly beneath it.elif(short for "else if"): Checked only if the previous conditions wereFalse. You can stack as many of these as you need.else: The catch-all safety net. If none of the conditions above were met, this final block executes.
# Sorting a transaction based on its value
transaction_amount = 120.00
if transaction_amount > 500.00:
status = "High Value"
elif transaction_amount >= 100.00:
status = "Medium Value"
else:
status = "Standard Value"
# Because 120.00 is >= 100, status will be set to "Medium Value"
Unlike R or SQL, which use curly braces or explicit keyword markers like END to close logical blocks, Python relies entirely on indentation (usually four spaces) to define which lines of code belong inside each condition. Missing or inconsistent indentation will throw an error!
Are you ready to see how we use loops to automate these kinds of decisions over entire datasets, or would you like to practice a quick control structure example first?
Continue to introducing loops
You are doing fantastic! Since you already understand how to make decisions using if conditions, automating those decisions over thousands of rows of data is the next natural step.
Automating with Loops
In data analysis, we rarely look at just one data point. Instead, we want to perform the same check or calculation across an entire list, table, or array. This is where come in.
Python primarily uses two types of loops to automate repetitive tasks:
forloops: Best when you know in advance how many times you want to run, such as iterating over every item in a dataset, list, or array.whileloops: Best when you want to repeat an action until a specific condition changes (e.g., pulling batches of data from an API until there are no rows left).
# Iterating over a list of daily transaction totals
daily_sales = [120.00, 450.00, 85.00, 600.00]
high_value_count = 0
# A 'for' loop checks each sale one by one
for sale in daily_sales:
if sale > 300.00:
high_value_count = high_value_count + 1
# high_value_count is now 2
Take a look at how clean that for loop syntax is! Python simply takes each number in daily_sales, assigns it to the temporary variable sale, and runs the indented code block underneath. This is much more straightforward than setting up index counters manually like in some older programming languages.
Continue to the next subtopic: Introduction to NumPy
NumPy: The Foundation of Numerical Computing
To use NumPy, we first need to import it into our Python session. By universal convention, the data community always imports it under the alias np to save keystrokes.
# Importing the library
import numpy as np