Variable
Code python
password = 1745 #variable_name ➼ password
'=' is called assignment operator who stores the right sides value into left side name. Here password is a variable who store a data(1745).
Remember three points when creating a variable name.
- Variable name should be alphanumerical.
- Variable name can contain underscore_ .
- Varaible name shouldn't be started with a number.
Different assignment operations -
- Assigning/Initianlizing single value to multiple variables :
- Assigning/Initianlizing multiple value to multiple variables :
- Destructuring a list :
Code pyhton
x = y = z = "John" # All three variables(x,y,z) stores same value
Code pyhton
x,y = "John" , "Bob" # x stores John and y stores Bob
Code pyhton
name = [ "John" , "Bob" ]
x,y = name # x stores "John" and y stores "Bob"
(Note : Number of values should be equal to number of varables. If there would be single variable then it stores whole values(list/tuples etc ..) )
Assignment order -
Python evaluates expressions from right to left. So when you assign multiple values to variable/s then right most evaluates first. For example -
Code pyhton
x = "Alice"
y = "Bob"
a, b = y, x # x, y = "Alice", "Bob" #right evaluate first
print(a,b) # output : Bob Alice