Store information and combine values with Python expressions.
Variables, values, and expressions
Learning objectives
- Distinguish a variable name from its value.
- Use strings, integers, floats, and booleans.
- Combine values with arithmetic and f-strings.
Example
student = "Mina" # string
projects_completed = 3 # integer
average_score = 91.5 # float
is_ready = True # boolean
print(f"{student} completed {projects_completed} projects.")
print(f"Average score: {average_score}%")
print(f"Ready for the next lesson: {is_ready}")
The value on the right is assigned to the name on the left. Python determines the type from the value. Use type(value) while learning to inspect a value.
Expressions
robot_distance = 12
additional_distance = 5
total_distance = robot_distance + additional_distance
remaining_distance = 30 - total_distance
print(total_distance, remaining_distance)
Use parentheses when they make the order of operations clearer. Keep units in variable names when confusion is possible, such as distance_cm and distance_m.
Practice
- Create variables for a robot’s battery percentage and name.
- Calculate the battery percentage after using 18 points.
- Print a sentence describing the result.
- Inspect the types of all three values.
Common mistakes
score = "91"is text, not a number.Scoreandscoreare different names.- A name should describe what it stores.
- Do not overwrite a useful value unless the change is intentional.
Checkpoint
Explain the difference between 5, 5.0, and "5". Then predict the output before running a short example.