PythonLesson 2 of 16

Variables in Python

A variable is a name for a value, like a label on a box. Put a song in the box, and from then on you use the label.

Key points

  • name = value creates a variable
  • Assigning again replaces the old value
  • Use lowercase words joined by underscores: total_price
  • Case matters: song and Song are different names
  • type() tells you the kind of value: str is text, int is a whole number

Example

song = "Blue Sky"artist = "Nova"plays = 3print("Now playing:", song, "by", artist)plays = plays + 1print("Plays:", plays)print(type(song), type(plays))

plays grows by one: Python reads the old value on the right and stores the new one on the left. type() reveals what each box holds.

Task

How to use the editor⌘ Enter or Ctrl Enter runs the code, ⇧ ⌘ Enter checks the task.Tab and Shift Tab indent the line or the selection; Esc, then Tab leaves the editor.The check compares what the program printed with the task; spaces at line ends do not matter.After two failed checks you may open the solution. Your code is kept in this browser.

The player skipped to the next track. Set song to "Night Drive" and print the Now playing line again with the new song.

Output

What your program prints appears here.