Editor settings

General settings

Save settings

Learn to program with Python

Reassign a value to an existing variable #

You can reassign a value to a variable simply by using the assignment operator = followed by the new value that you want to assign. This will overwrite the previous value of the variable with the new value.

Here's an example:

x = 10
print(x)  # Output: 10

x = 20
print(x)  # Output: 20

In this example, we first assign the value 10 to the variable x, and then print its value. Next, we reassign the value 20 to the same variable x, which overwrites its previous value. Finally, we print the new value of x, which is 20.


Exercise #

  • On the third line of the code, reassign the value 20 to the variable x. You must not change the first line of the code.
Output will be displayed here