Basic Variables

Variables make it easy to use values. Let’s change our print to use a variable that we define.

Code

msg = 'Hello World'
print(msg)

Analysis

  • We define a variable
  • The variable name is msg
  • The variable value is Hello World
  • The value has a type of string
  • Other common basic types: integer, float, boolean, None
  • We generally use single-quotes in Python for defining strings
  • We use a single = as the assignment operator (== is an equality operator)
  • Using a non-existing variable causes an exception

Exercises

  • Assign and print another string
  • Same for an integer
  • Same for a decimal (float)
  • Same for False
  • Same for None
  • Print the value of an undefined variable

Quiz

  • What are some common types of values?
  • Do you assign a variable with a single or double equals-sign?
  • When assigning a variable, the left-hand-side of the equals is that what, and the right-hand-side the what?
  • What happens if you reference an undefined variable?