Names, values, and types
Understand assignment by following which value each name refers to.
What you’ll learn
- Trace assignment and reassignment
- Distinguish numbers from numeric text
- Choose descriptive variable names
A name refers to a value
Imagine a program recording visits to a park. The statement visitors = 12 gives a name to an integer value. You can later print visitors, compare it with another count, or pass it into a function. Assignment is not an equation to solve: Python evaluates the expression on the right and makes the name on the left refer to its result. Reading a program this way helps you predict what each statement will do.
Reassignment changes the reference
If the next statement is visitors = visitors + 3, Python first reads the current value through visitors, adds three, and then assigns the resulting value to visitors. The old integer has not turned into a different integer. This distinction becomes especially useful with collections: assigning one list name to another does not copy its contents. Two names can refer to the same list, so a change made through one name can be visible through the other.
visitors = 12
visitors = visitors + 3
label = str(visitors)
print(visitors, type(visitors).__name__) # 15 int
print(label, type(label).__name__) # 15 strTypes describe available operations
The value 12 is an integer, while the value "12" is text. Their printed appearance may be similar, but addition means different things for them. Adding two integers performs arithmetic; adding two strings joins text. Python does not automatically decide that numeric-looking text should become a number for arithmetic. Use type() while exploring unfamiliar values, and make conversions explicit at the point where text enters your program. True and False are boolean values used in decisions.
Convert where the meaning changes
A response read with input() is a string. If it represents a whole-number count, int() can convert valid numeric text into an integer. Conversion can fail, so a usable program needs a plan for invalid input rather than assuming every response is correct. Keep the original text when you need it for a helpful message. Do not use floating-point values for every numeric problem: an integer is a clearer representation for a count, and exact financial calculations may need a decimal representation.
Make the program explain itself
Choose names that describe the meaning of a value, such as visitor_count or park_name. A name like data1 gives the next reader little help. Avoid replacing built-in names such as str or list with your own variables. Then check your understanding with a small trace: write the value and type associated with each name after every assignment. Include zero, empty text, and a failed conversion in your experiments. These cases reveal assumptions that an ordinary positive number can hide.
Try it yourself
Trace count = 4, count = count + 2, and label = str(count). State each final value and type.
Show a possible solution
count refers to the integer 6. label refers to the string "6". str() creates a text representation and does not change the integer referred to by count.
Check your understanding
Try 14 questions across python and practical programming. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.