Objects and Variables

Type Object type in Python Example
Integer number int 123
Decimal number (floating point) float 3.14
Logic value bool True, False
Text string "Hello World!"

Variables

  • Variables are names we set to refer to objects.
    • A not-so-good metaphor: variables are containers for objects
    • A better metaphor: objects are houses, variables are addresses of these houses
x = 123  # a variable x that contains the integer value 123
x = x + 1  # x is updated with the value of x + 1, becoming 124...
hello = "Hello World!"  # a variable that contains the string "Hello World!"
is_done = True  # a variable is_done with the logic value True

Objects x variables

  • It is very important to differentiate!
  • Which of the following are variables and which are objects?
"hello"

hello

132

var_1

truev

True

Variable naming conventions in Python

  • They MUST start with a letter or with _ (underline)
  • They are case sensitive (‘C’ is different from ‘c’)
  • They can’t contain: { ( + - * / \ ; . , ?
  • They can’t have names of words already reserved for other purposes in Python:

  • What happens if?
True = 123
"Hello" = world
1stcar = 2000