Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Variables, Data Types, and Operators

Learn how Python stores information, labels what kind of value it is, and combines values into useful results.

This is the first detailed notebook in the fundamentals sequence. It turns the high-level map from the section overview into concrete syntax you will use in almost every Python program.

Python programs become useful as soon as they can remember values, understand what kind of values they hold, and combine or compare those values with operators.

That is why variables, data types, and operators appear everywhere: revenue calculations, discount checks, customer labels, performance metrics, and model thresholds all depend on them.

Why This Matters

Business analytics
Machine learning
Software thinking

Variables store measures like sales, costs, or conversion rates. Operators help calculate totals, margins, and growth.

Concept Map

Core Explanation

A variable is a name attached to a value. The data type tells Python how that value should behave. Operators then let you transform, compare, or update the value.

IdeaExampleWhy it matters
Variablesales = 25000stores a value for reuse
Stringcompany = "Bright Retail"stores text labels
Integercustomers = 120stores whole numbers
Floattax_rate = 0.18stores decimal values
Booleanis_profitable = Truestores yes/no style logic
Operatorrevenue = units * pricecreates new results from values
Reading the output
  • int means a whole number

  • float means a decimal number

  • str means text

  • bool means a value such as True or False


Operators in Action

The same values can be combined in different ways depending on the operator you use.

Operator familyExamplesTypical use
Arithmetic+, -, *, /totals, averages, differences, revenue
Comparison>, <, ==, !=checking targets or thresholds
Assignment=, +=, -=storing or updating a value

Worked Example

Suppose a business sells 180 units of a product at a price of 45 each and wants to know whether revenue beats a weekly target of 7000.

Why this matters

This example combines four fundamentals at once:

  • storing values in variables

  • choosing numeric data types

  • using arithmetic operators to compute a result

  • using a comparison operator to ask a business question

Quick Quiz

Which value is most likely a string in Python?

42This is an integer.
"Quarterly Report"Correct. Text inside quotation marks is a string.
3.14This is a float.
TrueThis is a boolean value.
sales = 32000
costs = 21000
profit = sales - costs

print("Initial profit:", profit)

refund = 1500
profit = profit - refund

print("Profit after refund:", profit)
Initial profit: 11000
Profit after refund: 9500
price = 12.5
quantity = 40
revenue = price * quantity
target = 450

print("Revenue:", revenue)
print("Revenue meets target:", revenue >= target)
Revenue: 500.0
Revenue meets target: True

Exercises

  1. Create variables for product name, price, and quantity, then print a readable summary.

  2. Store a boolean value that represents whether a customer is active.

  3. Compute total cost using multiplication and compare two values using > or <.

Quick Summary
  • variables store values

  • data types describe the kind of value

  • operators help compute and compare results

  • these ideas appear in almost every Python program

The next notebook builds on this by teaching Python how to make decisions using conditions.

8. Interactive Code

Expected output
RetailCo
42
0.18
Expected output
str
bool
float

9. Guided Practice

Which data type is typically used for `230.5`?

intIntegers do not store decimal values.
floatCorrect. Decimal numbers are commonly stored as floats.
boolBooleans hold `True` or `False`.
listA list stores a collection of values.

Which function helps inspect a variable's type?

len()`len()` measures size for many containers.
type()Correct. `type()` shows the class of an object.
input()`input()` reads text from the user.
range()`range()` generates numeric sequences.