=

Variable

Variables are a type of containers who stores some data values.

Variable declairation in python -
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.

  1. Variable name should be alphanumerical.
  2. Variable name can contain underscore_ .
  3. Varaible name shouldn't be started with a number.

Different assignment operations -


  1. Assigning/Initianlizing single value to multiple variables :
  2. Code pyhton
    x = y = z = "John"  # All three variables(x,y,z) stores same value 

  3. Assigning/Initianlizing multiple value to multiple variables :
  4. Code pyhton
    x,y = "John" , "Bob"  # x stores John and y stores Bob 

  5. Destructuring a list :
  6. 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