Variables and types
Use names to describe values and reason about assignment, reassignment, and conversion.
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.
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.
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.
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.
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.
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.
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.
Explore a worked solution
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.
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.
Explore a worked solution
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.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
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.
20 30
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.