← Python from Scratch Lesson 7 of 78 · 10 XP

Variables

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A variable is a name for a value. The = sign doesn't mean "equals" — it means "put the value on the right into the name on the left":

age = 20
city = "Almaty"
age = age + 1      # now age = 21

Rules for names:

  • letters, digits and _, but not starting with a digit: user1 is fine, 1user is not;
  • case matters: name and Name are different variables;
  • write clearly, with underscores: total_price, not tp.

Python lets you assign several variables at once and swap values without a temporary variable:

a, b = 1, 2
a, b = b, a        # now a = 2, b = 1

Task. Read two words (each on its own line), swap the values of the variables and print them separated by a space: the second word first, then the first.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.