Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    15 Quizzes
  2. Python: Control Flow
    12 Quizzes
  3. Python: Errors
    5 Quizzes
Lesson 1, Topic 8
In Progress

Python: Updating Variables

Yasin Cakal 4 Oct 2021
Lesson Progress
0% Complete

Variables with numeric values can be treated identically to numbers themselves. Without Python distinguishing between variables and literals, two variables can be added together, divided by two, and multiplied by a third variable. Arithmetic on variables does not affect the variable; the only way to update a variable is to use the = sign.

We define two variables and assign them numeric values. Then we calculate them. This does not cause the variables to be updated! When we update the variable’s value and rerun the calculations, the variables’ new values are used as shown.

# Variable x and y created and assigned initial values.
x = 10
y = 5

# Variable z is initialized to the sum of x and y.
z = x + y
print(z) # prints 15

# Now, it is reinitialized to the product of x and y.
z = x * y
print(z) # prints 50
%d bloggers like this: