Chapter 1 of ?
python 11 min read

Python for Data Science — Chapter 3: Expressions and Variables

Module 1 · Chapter 3

Expressions and Variables

Learn about arithmetic operators, order of operations (PEMDAS), variable assignment & reassignment, inter-variable calculations, and clean naming conventions.

3.1 Expressions & Operators

An expression in Python is a combination of numbers and mathematical symbols (operators) that Python evaluates to produce a single final value.

Python Arithmetic Operators EXPRESSION RESULT DESCRIPTION 43 + 60 + 16 + 41 160 Addition combines values into a sum 30 - 45 -15 Subtraction — result can be negative 5 * 5 25 Multiplication 25 / 6 ≈ 4.167 Division always returns a float in Python 3 25 // 6 4 Integer division — rounds down, not to nearest

Division deserves a special note: in Python 3, / always returns a float, even when the numbers divide evenly (e.g. 10 / 5 gives 5.0, not 5). Use // when you want a whole-number result rounded down.

Interactive Expression Calculator

Enter numbers for Operand A and Operand B, choose an operator, and watch Python compute the result in real time.

In [3]:
25 + 6
Out[3]:
31
Addition combines the operands.

3.2 Order of Operations

Python follows standard mathematical operator precedence conventions (PEMDAS): multiplication and division happen before addition and subtraction, and anything inside parentheses is evaluated first.

Order of Operations Python follows standard math conventions — multiplication before addition, parentheses first Without parentheses: 30 + 20 * 6 ↓ 20 × 6 = 120 (multiplication happens first) 30 + 120 = 150 With parentheses — always evaluated first: (12 + 20) * 60 ↓ (12 + 20) = 32 (parentheses first) 32 * 60 = 1920

3.3 Variables

A variable stores a value so you can use it again later. Use the assignment operator (=) to give a variable a value, and just type its name to use that value elsewhere in your code.

my_variable = 1
print(my_variable)
OutputPython Output
1

You can assign a new value to an existing variable at any time. The old value simply isn't important anymore — Python only remembers what the variable holds right now.

Variables: Assign and Reassign A variable always holds its most recent value STEP 1 my_variable my_variable = 1 1 my_variable = 10 STEP 2 my_variable my_variable = 10 10 Reassigning a variable overwrites whatever it held before — the old value (1) is simply gone.

3.4 Building Expressions with Variables

Variables can store the result of an expression, and you can use one variable to compute another. You can even reassign a variable using its own current value.

x = 2 + 3 + 3        # x is 8
y = x / 3            # y is 2.666...
x = x / 3            # x is now 2.666... too

You can also check a variable's type just like any other value, with type(x).

3.5 Naming Variables Well

It's good practice to use meaningful variable names, so you don't have to keep track of what a variable is doing from memory. It's common to use an underscore to join words (total_min) or camelCase (totalMin).

Say you have a music dataset with a song's length in minutes, and you want the length in hours instead:

total_min = 876
total_hr = total_min / 60
print(total_hr)
OutputPython Output
14.6
Variables Can Depend on Each Other Change one variable, rerun, and anything calculated from it updates automatically Before total_min = 876 ÷ 60 total_hr = 14.6 edit + rerun After total_min = 900 ÷ 60 total_hr = 15.0 ✓ updated automatically total_hr didn't need to change — it recalculates from total_min every time the cell runs.
Interactive Minutes → Hours Converter

Change total_min input below and observe total_hr recalculate instantly.

In [4]:
total_hr = total_min / 60
Out[4]:
total_hr = 14.6

Key Takeaways

  • An expression combines operands with an operator (+ - * / //).
  • / always returns a float in Python 3; // floors down to a whole number.
  • Python evaluates parentheses first, then multiplication/division, then addition/subtraction.
  • A variable stores a value with =; reassigning it overwrites the old value completely.
  • Variables can depend on each other — change one and rerun, and anything calculated from it updates automatically.

Practice Exercises

Chapter 3 Quiz — Test Your Knowledge

1. What is the result of evaluating 30 + 20 * 6 in Python?

2. What happens when you execute: my_variable = 1; my_variable = 10; print(my_variable)?

3. What is the return data type of the standard division operator / in Python (e.g. 10 / 2)?

Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *