CHAPTER 03 · BEGINNER4 min read

Variables and types

Use names to describe values and reason about assignment, reassignment, and conversion.

Python 3.12+Free to readModule 1 of 13
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Trace assignments without assuming names update automatically.
  • Distinguish integers, floats, strings, booleans, and None.
  • Choose meaningful names and explicit conversions.

01Names refer to values

A variable is a name bound to an object, such as a number or a piece of text. Assignment uses one equals sign: Python evaluates the expression on the right, then binds the name on the left to its result. Read tickets = 3 as 'let tickets refer to the integer three.' A name is useful because it describes the meaning of a value in your problem.

An assignment is an action performed at that moment, not a formula that stays connected to other names. In this example, total is calculated using the current values of tickets and price. Reassigning tickets later does not go back and recalculate total. To get a new total, execute the multiplication again. This distinction becomes essential when tracking a longer program.

ticket_total.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02A type tells you what operations mean

Python values have types. int represents integers, float represents floating-point numbers, str represents text, and bool represents True or False. None is a separate value used to represent the absence of a result or an intentionally missing value. It is different from zero, an empty string, and the string 'None'. You will meet useful examples of None when writing functions.

The type function helps inspect a value. For a compact display, this example prints the type object's __name__ attribute; you do not need to memorize that spelling yet. Notice that 7 and '7' look related to a person but support different operations. Numbers add arithmetically; strings joined with + concatenate. Predict each result using the value's type, not only its appearance.

inspect_types.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Convert when your problem calls for it

Use int, float, or str to request a conversion. Converting numeric text can make it suitable for calculation, while converting a number to text can help construct a message. Conversion does not alter the original object; it produces a value you can store under a new name or assign back to an existing name.

A conversion has rules. int('12') works, but int('12.5') raises ValueError because that string is not an integer representation. int(12.9) truncates toward zero rather than rounding. Do not use conversion to conceal an unclear requirement: decide whether your problem allows fractions before discarding them. For now use known valid inputs; a later lesson teaches how to handle invalid ones explicitly.

convert_values.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Choose names that explain the work

Prefer names such as minutes_per_session to vague names such as x when the meaning is known. Python names are case-sensitive: score and Score are different names. Use letters, digits, and underscores, with no digit at the start; ordinary course code uses lowercase words separated by underscores. Reserved keywords such as for cannot be variable names.

Avoid naming a variable print, str, or list because those names already identify useful built-in tools. Reusing them hides the original meaning in the current scope. Before running a program, trace the names in a small table on paper: after each assignment, record the current value. If you can explain exactly when a name changes, you are ready to combine variables into larger calculations.

LET IT CLICK

The ideas worth keeping.

  • Assignment evaluates a result now and binds a name to it.
  • Types determine which operations make sense for a value.
  • Explicit conversion creates a new value; meaningful names make changes easier to trace.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Trace a changing score

Start with score = 10. Store doubled = score * 2, then set score = 15. Print doubled and a newly calculated doubled score on separate lines.

your_turn_1.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.pyPython
score = 10doubled = score * 2score = 15print(doubled)print(score * 2)

The output is 20 then 30. doubled retains its earlier result because assignment does not create an automatically updating formula.

02

Turn a label into a quantity

Given boxes_text = '8' and items_per_box = 6, convert the text into an integer and print the total item count. Also print boxes_text afterward to confirm it remains text.

your_turn_2.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.pyPython
boxes_text = "8"items_per_box = 6boxes = int(boxes_text)print(boxes * items_per_box)print(type(boxes_text).__name__)

The total is 48 and the original variable still has type str. int returns a new numeric value; it does not rewrite the original string.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01After a = 4; b = a + 1; a = 9, what is b?
02Which expression produces the integer 23?
03Why is list = 5 an unhelpful variable assignment?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Trace a changing score

Start with score = 10. Store doubled = score * 2, then set score = 15. Print doubled and a newly calculated doubled score on separate lines.

EXPECTED OUTPUT
20
30
challenge.pyPython 3
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this chapter, when you want a second angle or the full details.

One chapter closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock chapter completion.