Last modified: Sep 20, 2023 By Alexander Williams
Python: Change Variable Value [Examples]
Example 1: Reassigning a Variable
# Initial value
x = 5
# Reassigning the variable
x = 10
# Print the updated value
print(x)
Output:
10
Example 2: Incrementing a Numeric Variable
# Initial value
count = 0
# Increment the variable
count += 1
# Print the updated value
print(count)
Output:
1
Example 3: Decrementing a Numeric Variable
# Initial value
quantity = 10
# Decrement the variable
quantity -= 2
# Print the updated value
print(quantity)
Output:
8
Example 4: Concatenating Strings
# Initial values
first_name = "John"
last_name = "Doe"
# Concatenate strings
full_name = first_name + " " + last_name
# Print the concatenated string
print(full_name)
Output:
John Doe
Example 5: Updating Lists
# Initial list
fruits = ["apple", "banana", "cherry"]
# Update the list
fruits[1] = "orange"
# Print the updated list
print(fruits)
Output:
['apple', 'orange', 'cherry']
Example 6: Modifying Dictionary Values
# Initial dictionary
student = {"name": "Alice", "age": 25}
# Modify a dictionary value
student["age"] = 26
# Print the modified dictionary
print(student)
Output:
{'name': 'Alice', 'age': 26}
Example 7: Multiplying Numeric Variables
# Initial values
length = 5
width = 3
# Multiply variables
area = length * width
# Print the calculated area
print("Area:", area)
Output:
Area: 15
Example 8: Updating Variables Conditionally
# Initial values
temperature = 25
# Check a condition and update the variable
if temperature > 30:
status = "Hot"
else:
status = "Moderate"
# Print the status
print("Temperature Status:", status)
Output:
Temperature Status: Moderate
Example 9: Swapping Variable Values
# Initial values
a = 5
b = 10
# Swap the values of variables
a, b = b, a
# Print the swapped values
print("a:", a)
print("b:", b)
Output:
a: 10
b: 5
Example 10: Updating Variables Using a Function
# Initial value
value = 5
# Define a function to update the value
def update_value(num):
return num * 2
# Update the value using the function
value = update_value(value)
# Print the updated value
print("Updated Value:", value)
Output:
Updated Value: 10
Example 11: Appending to a List
# Initial list
fruits = ["apple", "banana", "cherry"]
# Append an item to the list
fruits.append("orange")
# Print the updated list
print("Fruits:", fruits)
Output:
Fruits: ['apple', 'banana', 'cherry', 'orange']
Example 12: Removing an Item from a List
# Initial list
fruits = ["apple", "banana", "cherry"]
# Remove an item from the list
fruits.remove("banana")
# Print the updated list
print("Fruits:", fruits)
Output:
Fruits: ['apple', 'cherry']
Example 13: Updating Dictionary Values
# Initial dictionary
person = {"name": "Alice", "age": 25}
# Update a dictionary value
person["age"] += 1
# Print the updated dictionary
print("Person:", person)
Output:
Person: {'name': 'Alice', 'age': 26}
Example 14: Clearing a List
# Initial list
numbers = [1, 2, 3, 4, 5]
# Clear the list
numbers.clear()
# Print the empty list
print("Numbers:", numbers)
Output:
Numbers: []
Example 15: Deleting a Variable
# Initial value
variable_to_delete = "I will be deleted"
# Delete the variable
del variable_to_delete
# Attempt to print the deleted variable (will raise an error)
try:
print(variable_to_delete)
except NameError as e:
print("Variable deleted:", e)
Output:
Variable deleted: name 'variable_to_delete' is not defined