Python Expressions: Beginner

NASA's Jet Propulsion Laboratory uses Python to command Mars rovers and process terabytes of telescope data, and one of the core reasons they chose it is the clean, unambiguous syntax that lets dozens of scientists and engineers collaborate on mission-critical code without misreading each other's work. When a line of code could trigger a command 140 million miles away, there is no room for a misplaced bracket or an indentation error that changes meaning silently. Python's strict indentation rules and readable structure force programs to look exactly like their logic, making bugs visible at a glance rather than hidden in syntactic noise. The syntax fundamentals you practice in this lesson are the same foundation that JPL engineers rely on when the cost of a misunderstood line of code is a 2.5-billion-dollar spacecraft.

How Computers Store Data

Daily Life
Interviews

Understand how values live in memory

Before we can understand variables, we need to understand memory. Your computer has two main types of storage:
Hard Drive / SSD
Hard Drive / SSD
Permanent storage that keeps data even when power is off. This is where your files live.
RAM (Memory)
RAM (Memory)
Temporary, ultra-fast storage used while programs run. Data disappears when the program ends.
When you run a Python program, it uses RAM to store all the data it needs. Think of RAM as a massive grid of tiny storage slots, each with a unique address (like a house number on a street).
Each slot can hold a small piece of data. When you create a variable in Python, you're essentially telling the computer: "Reserve some of these memory slots for me, and let me refer to them by this name."
Fill in the Blank

> A variable name must follow Python rules. Pick the name that is valid Python and see what happens.

 = 42
print()

Python enforces naming rules at parse time, so a bad variable name causes an immediate SyntaxError before any code runs.

Memory and variables are the foundation of every program you will ever write. Every piece of data your program works with lives in a named memory slot.
TIP
Use descriptive names from the very start. "count" is fine; "c" is not. Future you will thank present you.

Variables and Naming

Daily Life
Interviews

Name variables clearly and correctly

A variable is a named container for storing data in your computer's memory. The name "variable" comes from the fact that the value it holds can vary (change) during the program.

The Labeled Box Analogy

Imagine you have a warehouse full of boxes. Each box can hold something, and each box has a label on the front. The label is the variable name. The contents inside the box is the value.
When you want to find something, you look for the label. When you want to change what's stored, you replace the contents of that labeled box. You don't need to know exactly where in the warehouse the box is located; the label handles that for you.
1user_name = "Maya"
2user_age = 28
3
4print(user_name)
5print(user_age)
>>>Output
Maya
28

What Happens in Memory

When Python executes the line user_name = "Maya", several things happen:

01
Find memory
Python finds available memory slots large enough to store the value "Maya".
02
Write the data
It writes the characters M, a, y, a into those memory slots.
03
Create a reference
It creates a link from the name "user_name" to that memory location.
04
Look up by name
Whenever you use user_name, Python knows where to find the data.
You don't need to manage any of this yourself. Python handles all the memory details automatically. That's one reason Python is considered beginner-friendly compared to languages like C or C++.

Python Naming Conventions

Variable names in Python have rules (requirements) and conventions (recommendations). Breaking rules causes errors. Ignoring conventions makes code hard to read.
Rules (Must Follow)
  • Must start with a letter or underscore (_)
  • Can only contain letters, numbers, and underscores
  • Cannot be a Python keyword (like if, for, while)
  • Case-sensitive: "Name" and "name" are different variables
Valid variable names:
1user_name = "Maya"
2_private_data = 42
3score2 = 100
4firstName = "Alex"
5
6print(user_name, _private_data, score2, firstName)
>>>Output
Maya 42 100 Alex
Invalid names that cause errors:
12nd_place = "silver"
2user-name = "Maya"
3my variable = 10
4for = 5

These fail because: names can't start with numbers, can't use hyphens, can't have spaces, and can't use reserved keywords like for.

Python uses snake_case for variable names: all lowercase letters with underscores separating words. This is a convention, not a rule, but virtually all Python programmers follow it.

Pythonic
  • user_count
  • total_revenue
  • is_logged_in
  • max_retry_attempts
Not Pythonic
  • userCount (camelCase)
  • TotalRevenue (PascalCase)
  • USERCOUNT (SCREAMING)
  • u, x, temp1 (unclear)
TIP
Good variable names describe WHAT the data represents. "customer_count" is better than "c" or "num" because anyone reading the code instantly understands what it holds.
Debug Challenge

> This variable name breaks Python naming rules. Remove the invalid tile to fix the syntax error.

SyntaxError: invalid syntax

Hyphens are arithmetic operators in Python, so "my-name" is parsed as subtraction, not a variable name. Underscores are the correct separator for multi-word names.
The snake_case convention makes Python code immediately recognizable. When you follow it, your code looks professional and integrates seamlessly with the standard library.

Assignment vs. Equality

Daily Life
Interviews

Distinguish setting values from comparing them

This is one of the most common points of confusion for beginners. In Python (and most programming languages), the = symbol does NOT mean "equals" in the mathematical sense. It means assignment.

Math Class
  • x = 5 means "x equals 5"
  • 5 = x also valid (same thing)
  • States a fact about equality
Python
  • x = 5 means "assign 5 to x"
  • 5 = x is an ERROR (invalid)
  • Performs an action (stores data)

Read x = 5 as "x gets the value 5" or "assign 5 to x". The value on the right flows into the variable on the left.

1score = 100
2score = score + 1
3print(score)
>>>Output
101
After this, score is 101. This would be nonsense in math (100 = 101?), but in programming it makes perfect sense: "Take the current value of score, add 1, store the result back in score."

So How Do I Check Equality?

To actually check if two things are equal, Python uses == (two equals signs). This is called the equality operator.

Assignment stores a value:
1x = 5

Comparison asks a question and returns True or False:

1x = 5
2print(x == 5)
3print(x == 10)
>>>Output
True
False
The first returns True ("Is x equal to 5?" Yes!). The second returns False.
Using the correct operator in conditions:
1x = 5
2if x == 5:
3 print("x is five")
>>>Output
x is five
Debug Challenge

> This code tries to assign 5 to x but has an extra equals sign. Remove the extra tile to fix it.

SyntaxError: invalid syntax

Confusing = with == is one of the most common beginner bugs. In Python, assignment always flows right to left: the value on the right is stored into the variable on the left.
Every time you write a condition in an if-statement or while-loop, you need == to compare. Every time you store a value, you need =. With practice, this distinction becomes automatic.
TIP
Read = aloud as "gets" or "is assigned". Read == as "equals". That mental habit prevents most assignment-vs-equality bugs before you make them.

Data Types and Strings

Daily Life
Interviews

Handle text alongside numbers and booleans

Every piece of data in your program has a type. The type determines what you can do with that data and how the computer stores it in memory.
Think about the number "5" and the text "5". They look the same, but they're fundamentally different:
1number_five = 5
2text_five = "5"
They behave completely differently:
1number_five = 5
2text_five = "5"
3
4print(number_five + 10)
5print(text_five + "10")
>>>Output
15
510
The first gives 15 (mathematical addition). The second gives "510" (text concatenation). Trying text_five + 10 causes an error because you can't add text and a number directly.
Data types exist because computers need to know how to interpret the 1s and 0s stored in memory. The same binary pattern could represent a number, a letter, or something else entirely, depending on its type.

The Four Fundamental Types

Python has many data types, but four are fundamental. Almost every program uses these:
intfloatstrbool
int
Whole numbers
Counting and indexing
float
Decimal values
Measurements, percentages
str
Text strings
Names, messages, any text
bool
True or False
Conditions, flags, logic
TIP
Floats have limited precision. Very small decimal differences can occur in calculations. For money, many professionals use integers representing cents instead of floats representing dollars.

Checking Data Types

You can check any value's type using the type() function. This is useful when debugging or when you're unsure what type a variable holds.

1print(type(42))
2print(type(3.14))
3print(type("hello"))
4print(type(True))
>>>Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Fill in the Blank

> Each value in Python has a type. Pick different values to see what type Python assigns.

x = 
print(type(x))

Working with Strings

Strings are one of the most commonly used data types. Python lets you use single quotes, double quotes, or triple quotes:
1name = 'Maya'
2name = "Maya"
3message = "She said 'hello'"
4paragraph = """This is a long string that spans multiple lines."""

What is Concatenation?

Concatenation means joining strings together. When you use the + operator with strings, instead of adding numbers, it glues the strings end-to-end.

1first_name = "Maya"
2last_name = "Johnson"
3
4full_name = first_name + " " + last_name
5greeting = "Hello, " + first_name + "!"
6
7print(full_name)
8print(greeting)
>>>Output
Maya Johnson
Hello, Maya!
You can only concatenate strings with strings. Trying to concatenate a string with a number causes an error. You'll learn how to convert between types later.
Fill in the Blank

> The + operator behaves differently with numbers versus strings. Pick values to see how Python handles each combination.

a = 
b = 
print(a + b)
Python is strongly typed, meaning it never silently converts between types. This strict behavior prevents subtle bugs that occur in languages that allow mixed-type operations.
Understanding type errors early saves hours of debugging later. When Python raises a TypeError, it is telling you exactly where you mixed incompatible types.

Operators and Readability

Daily Life
Interviews

Write expressions that others can follow

Python provides operators for mathematical calculations. Most work exactly like you'd expect from math class, but a few have special behaviors.
01
+ Addition
5 + 3 equals 8. Adds two values together.
02
- Subtraction
10 - 4 equals 6. Subtracts right from left.
03
* Multiplication
7 * 6 equals 42. Multiplies two values.
04
/ Division
15 / 4 equals 3.75. Always returns a float.
1total = 100 + 50
2difference = 100 - 30
3product = 25 * 4
4quotient = 100 / 8
5
6print(total)
7print(difference)
8print(product)
9print(quotient)
>>>Output
150
70
100
12.5

Special Operators

Python has three additional arithmetic operators that are incredibly useful:
//  Floor division
// Floor division
Divides and rounds DOWN to the nearest integer. 15 // 4 equals 3.
%  Modulo (remainder)
% Modulo (remainder)
Returns the remainder after division. 15 % 4 equals 3.
**  Exponent (power)
** Exponent (power)
Raises a number to a power. 2 ** 10 equals 1024.
Floor division for whole numbers:
1hours = 125 // 60
2pages = 45 // 10
3
4print(hours)
5print(pages)
>>>Output
2
4
125 minutes = 2 complete hours. 45 items = 4 complete pages of 10.
Modulo for remainders:
1remainder = 125 % 60
2print(remainder)
3
4number = 8
5is_even = number % 2 == 0
6print(is_even)
>>>Output
5
True

Order of Operations

Python follows the standard mathematical order of operations (PEMDAS): Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
1result = 2 + 3 * 4
2print(result)
>>>Output
14
Result is 14, not 20. Multiplication happens first (3 * 4 = 12), then addition (2 + 12 = 14). When in doubt, use parentheses for clarity.
Fill in the Blank

> Python has three division-related operators. Pick one to see what 7 divided by 2 gives you.

result = 7  2
print(result)

Comments and Readable Code

Comments are notes in your code that Python completely ignores. They exist only for humans reading the code. Writing good comments is a skill that separates amateur code from professional code.
1# Track users
2user_count = 150
Comments explain WHY, not WHAT. Bad: "# Add 1 to x". Good: "# Compensate for zero-based indexing".
Do
  • customer_total = price * quantity
  • is_valid = "@" in email
  • days_left = deadline - today
Don't
  • ct = p * q
  • v = "@" in e
  • d = dl - t
Six months from now, you'll return to code you wrote and have no memory of why you wrote it that way. Clear, readable code is a gift to your future self.
Readable code starts with good names. Fix the poorly named variable below to make the code self-documenting.
Debug Challenge

> The comment says "sum" but the code does multiplication. Remove the misleading comment tile.

The comment says "sum" but the code does multiplication

Stale comments are worse than no comments. A comment that contradicts the code actively misleads readers and creates confusion about what the code is supposed to do.
Arithmetic, clear naming, and honest comments are the three pillars of readable Python. Everything else in this course builds on these fundamentals.
TIP
When you change code, always update its comments at the same time. Treat comments as part of the code, not an afterthought.
PUTTING IT ALL TOGETHER

> You are a junior data engineer at Stripe writing your first Python script to pull daily transaction counts from a spreadsheet and print a formatted summary to the console. The script must name each metric clearly, compute derived values, validate ranges, and produce readable terminal output.

Variables store each transaction metric (total, count, average) so values are named and reusable throughout the automation script.
Arithmetic operators like * and / compute derived figures such as revenue per transaction without repeating raw spreadsheet values.
Assignment vs. equality matters when == checks that a count is within expected bounds before the script proceeds to print.
print() outputs each formatted summary line so the daily automation produces human-readable console results for the team.
KEY TAKEAWAYS
Variables are named containers that store values in memory
= means ASSIGNMENT (store a value), == means EQUALITY (compare values)
Four fundamental types: int (whole numbers), float (decimals), str (text), bool (True/False)
Data types determine what operations are allowed on values
String concatenation (+) joins text end-to-end
Use snake_case for variable names and make them descriptive
Comments explain WHY code exists, not what it does

Where every Python journey begins

Category
Python
Difficulty
beginner
Duration
27 minutes
Challenges
0 hands-on challenges

Topics covered: How Computers Store Data, Variables and Naming, Assignment vs. Equality, Data Types and Strings, Operators and Readability

Lesson Sections

  1. How Computers Store Data

    Before we can understand variables, we need to understand memory. Your computer has two main types of storage: When you run a Python program, it uses RAM to store all the data it needs. Think of RAM as a massive grid of tiny storage slots, each with a unique address (like a house number on a street). Each slot can hold a small piece of data. When you create a variable in Python, you're essentially telling the computer: "Reserve some of these memory slots for me, and let me refer to them by this

  2. Variables and Naming

    A variable is a named container for storing data in your computer's memory. The name "variable" comes from the fact that the value it holds can vary (change) during the program. The Labeled Box Analogy Imagine you have a warehouse full of boxes. Each box can hold something, and each box has a label on the front. The label is the variable name. The contents inside the box is the value. When you want to find something, you look for the label. When you want to change what's stored, you replace the

  3. Assignment vs. Equality

    After this, score is 101. This would be nonsense in math (100 = 101?), but in programming it makes perfect sense: "Take the current value of score, add 1, store the result back in score." So How Do I Check Equality? Assignment stores a value: The first returns True ("Is x equal to 5?" Yes!). The second returns False. Using the correct operator in conditions: Confusing = with == is one of the most common beginner bugs. In Python, assignment always flows right to left: the value on the right is st

  4. Data Types and Strings

    Every piece of data in your program has a type. The type determines what you can do with that data and how the computer stores it in memory. Think about the number "5" and the text "5". They look the same, but they're fundamentally different: They behave completely differently: The first gives 15 (mathematical addition). The second gives "510" (text concatenation). Trying text_five + 10 causes an error because you can't add text and a number directly. Data types exist because computers need to k

  5. Operators and Readability

    Python provides operators for mathematical calculations. Most work exactly like you'd expect from math class, but a few have special behaviors. Special Operators Python has three additional arithmetic operators that are incredibly useful: Floor division for whole numbers: 125 minutes = 2 complete hours. 45 items = 4 complete pages of 10. Modulo for remainders: Order of Operations Python follows the standard mathematical order of operations (PEMDAS): Parentheses, Exponents, Multiplication/Divisio