Variables

Variable is a name (identifier) associated with a value

Variables can be assigned different values during program execution.
Values are assigned to variables using the assignment operator =.

num = 10 # variable n is assigned the value 5

  • The right side of the assignment is evaluated first, then the result is assigned to the variable on the left.

num = num + 1 # (10 + 1)


  • Variables may also be assigned to the value of another variable
  • Variables num and k are both associated with the same literal value 10 in memory. One way to see this is by use of built-in function id().
  • The id() function produces a unique number identifying a specific value (object) in memory.
  • Variables refer to immutable values; changing one variable does not affect the other if they reference immutable values.
  • If no other variable references the memory location of the original value, the memory location is deallocated (that is, it is made available for reuse).
    Finally, in Python, the same variable can be associated with values of different type during program execution.

All input is returned by the input function as a string type. Built-in functions int() and float() can be used to convert a string to a numeric type.