If you’re new to Python or programming in general, welcome—you’re in the right place. This chapter is where we start turning ideas into code. Just like learning a new language starts with learning a few key words and phrases, learning Python begins with understanding how to work with basic building blocks: numbers, text, and data containers.
Why is this important? Because whether you’re analyzing customer data, building a machine learning model, or automating a task at work, you’ll rely on these foundational concepts every single time you write code. Think of this chapter as your toolbox—it may seem simple now, but you’ll keep coming back to these tools throughout your data science journey.
And don’t worry if it doesn’t all click right away. Everyone struggles with syntax or logic at first. Learning to code is more like learning to solve puzzles than memorizing rules. Take your time, experiment, and remember: errors are part of the process (even experienced coders Google stuff constantly).
By the end of this chapter, you’ll be able to:
Identify and use Python’s basic data types (numbers, strings, booleans)
Store and retrieve values using variables
Use comparison operators to make logical expressions
Write basic code that performs operations on individual values and collections of data
Let’s dive in.
Note📓 Follow Along in Colab!
As you read through this chapter, we encourage you to follow along using the companion notebook in Google Colab (or other editor of choice). This interactive notebook lets you run the exact same code examples covered in the chapter—and experiment with your own ideas.
Let’s write a few lines of Python code. Run the following in a Python environment (like Google Colab, Jupyter Notebook, or VS Code) and see what happens:
# Store your name as a stringname ="Taylor"# Store your age as a numberage =22# Print a personalized messageprint("Hi "+ name +"! You are "+str(age) +" years old.")# Calculate how many years until you turn 30years_to_30 =30- ageprint("You'll turn 30 in "+str(years_to_30) +" years.")
Hi Taylor! You are 22 years old.
You'll turn 30 in 8 years.
What just happened?
You stored information using variables
You worked with different data types (a string and a number)
You used basic math and printed a personalized message
Don’t worry if this isn’t totally clear yet—that’s what this chapter is for. We’ll break it all down step by step.
3.2 Python Data Types
Everything in Python is an object, and every object has a type. A data type tells Python what kind of value you’re working with—whether it’s a number, a piece of text, or a simple True/False flag. Understanding Python’s core data types is a fundamental step toward writing useful programs.
Let’s walk through a quick introduction to the most common data types you’ll encounter early on. As we progress through this class and book, you’ll explore many more ways to work with and manipulate these data types.
Numeric Types
Python has two main types of numbers:
int (integers): whole numbers like 5, 42, or -100
float (floating-point): numbers with decimals like 3.14, 0.5, or -2.718
You can do all kinds of math with numbers in Python using simple operators:
10+3.5# Addition
13.5
10*3.5# Multiplication
35.0
Go ahead and run these lines of code in your notebook:
# Basic operations10-3.5# Subtraction10*3.5# Multiplication10/3.5# Division (always returns a float)# More math10**3.5# Exponentiation (10^2 = 100)10//3.5# Integer division (result is an int)10%3.5# Modulus (remainder)
WarningHeads Up: Python Math Isn’t Always Perfect
Sometimes, Python (like all programming languages) can give results that look a little… off. This usually happens because computers represent decimal numbers using floating-point approximation, which isn’t always exact.
For example, you’d expect the following to equal 0.3 but instead…
0.1+0.1+0.1
0.30000000000000004
This tiny difference is due to how numbers like 0.1 are stored in computer memory. It’s a common issue when doing calculations with decimal values, especially in financial or scientific applications. As we progress, you’ll learn best practices to handle imperfections like this. For now, you could just round it.
round(0.1+0.1+0.1, ndigits=2)
0.3
Strings
A string is a sequence of characters, enclosed in single or double quotes - whichever you use is personal preference but the output in the Python environment will contain single quotes:
"Hello Taylor!"
'Hello Taylor!'
Strings can be combined and manipulated in many ways:
Go ahead and run these lines of code in your notebook:
# String repetition"ha"*3# 'hahaha'# Get first letter (starts at 0)"Taylor"[0] # 'T'# Get first three letters"Taylor"[:3] # 'Tay'
Strings are extremely common—you’ll use them to label, format, and present data in readable ways.
In fact, as organizations increasingly rely on data-driven decision-making, a significant portion of their data comes in the form of text. Examples include product descriptions, customer feedback, social media posts, and even log files. Understanding how to work with strings is essential for extracting insights and making sense of this unstructured data.
Booleans
A Boolean is a special data type with only two values: True and False.
is_raining =Truehas_umbrella =False
In Python, True and False are capitalized. Writing true or false (lowercase) will result in a NameError. Always ensure proper capitalization when working with booleans.
You’ll mostly use booleans when you write logical conditions, such as comparing values or checking if something is true. Don’t worry, we’ll discuss comparison operators (i.e. >, ==) in a moment.
5>3# True - 5 is greater than 3
True
5==3# False - 5 does not equal 3
False
Go ahead and run these lines of code in your notebook:
10<910>910<=910>=910==10
Type Checking and Conversion
You can check the type of any object using the type() function:
When working in a Jupyter notebook, Python will automatically display the result of the last line in a cell if it’s a value (like a string or number). For example:
type(10) type("hello") # will only show the output of this last line
str
But in most real Python programs—like scripts, functions, or when running multiple lines—you need to use print() to explicitly display something to the user:
print(type(10)) # will print print out this output...print(type("hello")) # and this output
<class 'int'>
<class 'str'>
Using print()` is a way to say, “Hey Python, show this to me in the output.” You’ll use it all the time for debugging, building interfaces, or just (as in this case) just to tell Python to display the output of each line of code.
You can also convert between types using built-in functions. For example, the following converts the integer value of 5 to a string '5'.
str(5) # '5' (string to int)
'5'
And the following converts a string '5' to an integer 5:
int('5') # 5 (int to string)
5
Go ahead and run these lines of code in your notebook:
print(type(3.5)) # <class 'float'>print(type(True)) # <class 'bool'>print(str(3.14)) # '3.14' (convert decimal to string)print(bool(0)) # False (0 is treated as False)print(float(2)) # 2.0 (convert integer to decimal)
These conversions come in handy when working with user input or cleaning messy data.
Knowledge check
Work through the following tasks in your notebook.
None🍕 What’s the best deal?
A 12-inch pizza costs $8. Use the formula for the area of a circle (\(A = \pi × r^2\)) to calculate the cost per square inch of the pizza.
Hints:
Radius (\(r\)) is half the diameter
Use 3.14159 as your approximation for \(\pi\)
Divide the price by the area to get cost per square inch
Now repeate for a 15-inch pizza that costs $12. Which is a better deal?
NonePlay with ‘strings’
First, guess what each line of code will result in. Then run them in your notebook. Were the results what you expected?
print("Python"+"Rocks")print("ha"*5)print("banana"[1])print("banana"[::-1]) # Can you guess what this does?
Extra challenge: Can you use slicing to print just the word "ana" from "banana"?
None🕵🏻♂️ Data type detective
Before you run the following, what do you think the data types are for each line? Then, run the code in your notebook to check your answers. Were your predictions correct?
print(type("True"))print(type(True))
3.3 Variables and the Assignment Operator
In the last section, we did a lot of math using the same numbers — 10 and 3.5 — over and over again:
# Basic operations10-3.510*3.510/3.5
That works fine for short examples, but imagine writing a program that needs to use the same values in dozens of different places. What if you want to change one of those values later? You’d have to find and update every instance in your code.
That’s where variables come in.
What Is a Variable?
A variable is a name that refers to a value. You can think of it like labeling a container that holds something useful—like a number, a word, or even a list of things.
Here’s how we could rewrite the examples above using variables:
x =10y =3.5print(x - y)print(x * y)print(x / y)
Much cleaner, right?
ImportantWhy Use Variables?
Variables help you:
Avoid repeating values
Make your code easier to read and maintain
Update values in one place instead of many
And when your programs get more complex, variables become essential for storing user input, results from calculations, or intermediate steps in a data analysis.
The Assignment Operator: =
To create a variable, we use the assignment operator (=). This tells Python to take the value on the right and assign it to the name on the left:
greeting ="Hello, world!"
This means - store the string "Hello, world!" in a variable called greeting
You can then reuse that variable:
print(greeting)
Hello, world!
In Python, = does not mean “equal to” like in math. It’s an instruction: assign the value.
Naming Variables
Here are basic rules for naming variables in Python:
✅ Must start with a letter (or an underscore _ as in _name, though that’s typically reserved for special cases—so avoid starting with _ unless you know what you’re doing)
Use descriptive names when possible—it makes your code easier for others (and future-you) to understand.
Reassigning Variables
Variables can change! When you assign a new value to an existing variable, it overwrites the old one:
x =5x = x +1# Now x is 6print(x)
6
Python always uses the most recent value.
You Can Store Any Type of Value
You can assign any data type to a variable—numbers, strings, booleans, and more:
name ="Taylor"# stringgpa =3.85# floatis_honors_student =True# boolean
And Python is flexible — you can even change what type a variable holds:
gpa ="3.85"# Now it's a string!
This is called dynamic typing, and it’s part of what makes Python beginner-friendly.
Knowledge check
NoneTry It!
Let’s return to our earlier example from the start of the chapter:
# Store your name and agename ="Taylor"age =22# Print a custom messageprint("Hi "+ name +"! You are "+str(age) +" years old.")# Do a little mathprint("You'll turn 30 in "+str(30- age) +" years.")
Try updating the values of name and age to reflect your info. Then tweak the message to include your graduation year or your major. Play around; don’t worry you won’t break anything!
3.4 Comparison Operators
In the previous section on booleans, we saw that comparison expressions—like 5 > 3 — evaluate to either True or False. These expressions are powered by comparison operators, which are used to compare values in Python.
You’ll use comparison operators all the time when writing conditions, checking data, filtering results, or writing logic into your programs.
Here’s a quick cheat sheet of the most common ones:
Operator
Description
Example
Result
==
Equal to
5 == 5
True
!=
Not equal to
5 != 3
True
>
Greater than
10 > 7
True
<
Less than
4 < 2
False
>=
Greater than or equal to
3 >= 3
True
<=
Less than or equal to
8 <= 6
False
All of these expressions return a boolean value: True or False. Try running the following lines of code in your notebook:
print(10>3) # Trueprint(2<1) # Falseprint(4==4.0) # True (int and float are treated as equal in value)print(4!=5) # Trueprint(6>=7) # Falseprint(5<=5) # True
= is the assignment operator (used to assign a value to a variable)
== is the comparison operator (used to check if two values are equal)
x =5# assignmentprint(x ==5) # comparison → True
Knowledge check
NoneWhich Pizza is the Better Deal?
Let’s build on a problem you saw earlier. This time, we’ll:
use variables to store the cost per square inch of two pizzas and
then use a comparison operator to see which one is the better deal.
The Setup
A 12-inch pizza costs $8
A 15-inch pizza costs $12
Use the formula for the area of a circle:
\(A = \pi \times r^2\)
Use 3.14159 for π
Your Task
Compute the cost per square inch for each pizza
Store the results in two variables: small_pizza and large_pizza
Use a comparison operator to check if the smaller pizza is a better or equal deal
Here’s a starting point:
# Calculate cost per square inch for each pizzasmall_pizza =8/ (3.14159* (12/2) **2)large_pizza =12/ (3.14159* (15/2) **2)# Compare them (insert proper comparison operator in the blanks)print(small_pizza __ large_pizza)
What does the output of the comparison tell you? Try printing both values first to see how they compare. Which pizza gives you more for your money?
print(small_pizza)print(large_pizza)
3.5 Putting It All Together: Basic Python in Action
Now that you’ve learned about Python’s core data types, how to assign values to variables, and how to make comparisons, let’s put it all together into a small real-world example.
Imagine you’re helping manage event registration for a student club. You want to:
Store the number of attendees and the cost per ticket
Calculate total revenue from the event
Set a goal for how much you wanted to make
Print a basic summary report
Use comparison logic to see if you met your goal
Here’s how you might write that in Python:
# Number of attendees and ticket priceattendees =48ticket_price =12.50# Calculate total revenuetotal_revenue = attendees * ticket_price# Set a revenue goalrevenue_goal =600# Print a summary messageprint("You sold "+str(attendees) +" tickets at $"+str(ticket_price) +" each.")print("Total revenue: $"+str(total_revenue))# Compare to revenue goalgoal_met = total_revenue >= revenue_goalprint("Did we meet our revenue goal: "+str(goal_met))
You sold 48 tickets at $12.5 each.
Total revenue: $600.0
Did we meet our revenue goal: True
What’s going on here?
In these 8 lines of code we’ve combined everything we learned across this chapter:
We used variables to store numbers and reused them in calculations
We performed basic math operations using multiplication
We used print() to display helpful messages, combining strings and numeric values
We used str() to convert numeric and boolean data types to strings
We used a comparison operator (>=) to return True or False based on whether our total revenue met the goal
Try it Yourself!
Change the number of attendees or the ticket price. What happens to total revenue?
Try changing the revenue goal and see if the result of the comparison changes.
Stretch: Use a GenAI tool like ChatGPT, Claude, or Copilot and ask it to expand upon your code so that it prints ‘Yaaah!’ if we met the revenue goal, or ‘Booo!’ if we didn’t. Then copy the AI’s suggestion into your notebook and test it out. Can you understand what it did? Does the code work the way you expected? If not—can you fix it?
Remember: GenAI tools are great helpers, but not always correct. Always try to understand the code they give you!
This is your first step toward building programs that do real work. It may not seem fancy now—but you’ve already written a script that stores, processes, and evaluates real-world data.
3.6 Summary and What’s Next
In this chapter, you learned how to:
Identify and use Python’s most common data types—numbers, strings, and booleans
Use the assignment operator (=) to store and reuse values with variables
Write and evaluate comparison expressions that return True or False
Use print statements to combine and display information
Start thinking like a programmer by working through simple real-world examples
These are the essential tools that will support everything you do moving forward—whether you’re analyzing a spreadsheet, building a model, or writing a script to automate a task.
What’s Next: From Basics to Real Data
Now that you’ve learned how to work with individual values and variables, it’s time to start thinking bigger—about how we structure and analyze real-world data. In the next module, we’ll go deeper into three key topics:
Jupyter Notebooks: You’ve already seen Jupyter Notebooks in action, but now we’ll explore just how powerful they are. Mastering Jupyter is about more than writing code—it’s about communicating insights clearly. You’ll learn how to:
Combine text and code in the same document using Markdown
Format your notebook with headers, bullets, and even equations to make your work easier to understand
Structure your notebooks as professional, reproducible reports, just like a real data scientist would
Python Data Structures: So far, you’ve worked with individual values like one number or one string. But in data science, we almost never work with just one thing—we work with collections of data. Understanding data structures is essential as we start to clean, transform, and analyze datasets so we’ll cover:
Storing and accessing data using lists (ordered sequences of items)
Organizing key-value pairs using dictionaries (think of them like labeled data bins)
Looping through and manipulate these collections efficiently
Importing Real-World Datasets with Pandas: You’ll also take your first step into real data science work—bringing in external datasets and exploring them with the Pandas library. Pandas is one of the most important tools in any data scientist’s toolbox. It makes it easy to:
Read data from CSV files, Excel, databases, and more
View, clean, and filter your data
Begin asking real questions and discovering patterns
You’ve built a strong foundation. Next, we’ll build on it and start working with data the way real analysts and scientists do. Let’s keep going!
3.7 Exercise: Build a Simple Event Summary
NoneThe Scenario
Your student club is hosting an event and you’re in charge of summarizing registration data. Use what you’ve learned in this chapter to answer the following questions using Python.
Write all your code from scratch—no copy-pasting. Try to reason through the logic before typing.
Tickets sold: 56
Ticket price: $10.50
Revenue goal: $600
Event name: “Python for Everyone”
NoneYour Tasks
Create Variables: Assign appropriate values to variables for:
Event name
Tickets sold
Ticket price
Revenue goal
Calculate Total Revenue: Use math operations to calculate the total revenue earned from ticket sales.
Print a Summary Report: Use print() and string concatenation to display a message like:
The event "Python for Everyone" sold 56 tickets at $10.50 each.
Total revenue: $588.0
Met or exceeded goal: False
Stretch Task (Optional): Add a comparison that checks whether your total revenue met or exceeded the revenue goal and prints:
"Yaaah! We met our goal!" if the goal was met
"Booo! We missed our goal." if it was not
Hint: Ask a GenAI tool to help you construct the logic! If you can’t get it, don’t worry as we will talk about this later in the course.