How to Check if Two Variables Store the Same Value in Python?

Estimated read time 1 min read

To check if two variables store the same value in Python, you can use the equality operator (==). The equality operator compares the values of two variables and returns True if they are equal, and False otherwise.

Here’s an example code snippet that demonstrates how to check if two variables have the same value:

variable1 = 10
variable2 = 10

if variable1 == variable2:
    print("variable1 and variable2 have the same value.")
else:
    print("variable1 and variable2 do not have the same value.")

In this example, the if statement checks if variable1 is equal to variable2 using the equality operator (==). If the values of the two variables are equal, the code inside the if block is executed, and the message “variable1 and variable2 have the same value” is printed. If the values are not equal, the code inside the else block is executed, and the message “variable1 and variable2 do not have the same value” is printed.

You can modify the values of variable1 and variable2 to match your specific use case. The comparison can be done on variables of any data type, including integers, strings, lists, etc.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply