Python from zero to data engineering Lesson 2 / 110

Variables and numbers: teach your program to remember

Understand assignment, calculate a ticket total, choose clear names, and discover why updating a value does not rewrite the past.

Our cinema can announce a screening. Unfortunately, it has no idea how many tickets it has sold. The projector has declined to handle accounting.

You need: lesson one’s save-and-run workflow and print(). By the end: you can name values, calculate a total, and trace how values change. Allow 25–35 minutes with the exercises.

Give a value a name

Create tickets.py and run this complete program:

ticket_price = 8
ticket_count = 3
total = ticket_price * ticket_count

print("Total in euros:")
print(total)

Expected output:

Total in euros:
24

ticket_price is a variable name. The statement ticket_price = 8 assigns the integer value 8 to that name. An integer, or int, is a whole number, including zero and negative whole numbers.

Read = as “assign,” not “is mathematically equal to.” Python evaluates the expression on the right, then binds the name on the left to the result. An expression is code that produces a value: 8, ticket_count, and ticket_price * ticket_count are expressions.

In total = ticket_price * ticket_count, Python looks up 8 and 3, multiplies them, and assigns 24 to total. The * symbol means multiplication. We print total without quotes because we want its value. print("total") would print the word total.

Names are not live spreadsheet formulas

Predict the two numbers before running this separate example:

ticket_price = 8
ticket_count = 3
total = ticket_price * ticket_count

ticket_count = 4
print(total)

total = ticket_price * ticket_count
print(total)

The output is 24, then 32. Assignment stores the result of an expression at that moment; it does not install a spreadsheet formula that recalculates whenever an input changes.

After this statementticket_counttotal
total = ticket_price * ticket_count324
ticket_count = 4424
total = ticket_price * ticket_count again432

To increase a value, we can read its old value and assign a new one:

ticket_count = 3
ticket_count = ticket_count + 1
print(ticket_count)

This prints 4. The right side is evaluated first, so there is no contradiction. Later, you will also see the shorthand ticket_count += 1.

Arithmetic you can actually use

Run these in a new file:

print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(2 ** 3)

They print 13, 7, 30, approximately 3.3333333333333335, 3, 1, and 8, on separate lines.

OperatorPurposeA cinema use
+, -, *Add, subtract, multiplySold tickets × price
/Division, producing a floating-point numberAverage tickets per booking
//Floor division: round the quotient downFull rows filled by a positive seat count
%RemainderPeople left after making equal groups
**Raise to a powerSquared measurements or growth calculations

For negative numbers, “down” means toward negative infinity: -7 // 3 is -3, not -2. We will use positive counts for now.

Multiplication and division happen before addition and subtraction. 2 + 3 * 4 is 14; (2 + 3) * 4 is 20. Parentheses make the intended grouping explicit. You do not get extra points for making the next reader solve a puzzle.

Whole numbers, decimal approximations, and money

8 is an int. 8.5 is a float, Python’s usual type for approximate numbers with a fractional part. Most decimal fractions cannot be represented exactly as binary floating-point numbers. Try:

print(0.1 + 0.2)

You will normally see 0.30000000000000004. This is a representation issue, not random arithmetic. Floats are useful for many measurements, but we will keep ticket prices in integer cents so our small project’s additions and multiplications remain exact:

ticket_price_cents = 850
ticket_count = 3
total_cents = ticket_price_cents * ticket_count
print(total_cents)

This prints 2550. Later we will cover the decimal library and explicit rounding rules. Integer cents are a deliberate choice for this exercise, not a complete treatment of every currency or accounting rule.

Name things for the next reader

Use descriptive lowercase names with underscores: ticket_count, not tc or Thing. Names can contain letters, digits, and underscores, but cannot start with a digit. Avoid spaces and Python keywords such as if.

Python distinguishes uppercase and lowercase: ticket_count and Ticket_count are different names. Reading a name before assigning it causes a NameError.

Use # for a comment. Python ignores the rest of that line:

# Keep money in whole cents for exact ticket arithmetic.
ticket_price_cents = 850

The comment explains why we chose cents. A comment saying “set price to 850” would merely repeat the code. Also avoid naming your own values print, input, or int: those names already provide useful built-in functions.

Your turn: reserve the back row

The cinema has 48 seats and has sold 17 tickets. A group books another 6. Write a program that prints the remaining seat count. Use names for capacity, sold tickets, and the group size. Then change only the group size to 10 and rerun.

Hint

Update the sold count before calculating the remaining seats. Otherwise you will calculate from the old value.

Show one solution
capacity = 48
sold_tickets = 17
group_size = 6

sold_tickets = sold_tickets + group_size
remaining_seats = capacity - sold_tickets
print(remaining_seats)

The answer is 25. With a group of 10 it becomes 21. This program does not yet stop an oversized booking: that requires a decision, which we will learn in lesson four.

Checkpoint: explain the difference between print("total") and print(total), and why changing ticket_count does not automatically change total. If you can predict those, you understand assignment rather than merely recognising its syntax.

Search