Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Variables

Programs need to remember values: a user's name, a price, a score in a game. For that, they use variables. A variable is like a box with a label: it has a name, and it holds a value that you can use later.

You create a variable by writing its name, an equals sign = and a value:

city = "Prague"
print(city)

This prints:

Prague

The first line creates a variable named city and stores the value "Prague" in it. The = sign stores the value on its right in the variable on its left. In programming, it doesn't mean “equals” as in math; read it as “gets”: city gets the value "Prague".

To use the value, write the name of the variable without quotes. print(city) prints the value of the variable, Prague, while print("city") prints the text city. And if you forget the quotes around text, Python reads it as the name of a variable: print(Prague) stops with the error NameError: name 'Prague' is not defined, because there is no variable called Prague.

A variable can also hold a number, and you can print it together with other values, separated by commas:

temperature = 18
print("It is", temperature, "degrees outside.")

This prints:

It is 18 degrees outside.

Rules for the names of variables:

  • A name can contain letters, digits and the underscore _, but it can't start with a digit: price2 is fine, 2price isn't.
  • A name can't contain spaces. If it has more words, write them in lowercase and join them with underscores: first_name, chocolate_price. This style is called snake_case, and Python programmers use it for the names of variables.
  • Uppercase and lowercase letters matter: age and Age are two different variables.

Choose names that say what the variable holds: price tells you more than p or x.

Instructions

  1. 1

    Create a variable first_name with the value "John".

  2. 2

    On the next line, print the value of first_name.

  3. 3

    Below, create a variable age with the value 25. It's a number, so no quotes.

  4. 4

    Finally, print this sentence. Use the variables first_name and age, and separate the values with commas:

    John is 25 years old.
Back

5/15

  1. 1. Printing
  2. 2. Arithmetic Operations
  3. 3. Quiz: Order of Operations
  4. 4. Comments
  5. 5. Variables
  6. 6. Changing a Variable's Value
  7. 7. Division and Remainder
  8. 8. Joining Text
  9. 9. Text and Numbers
  10. 10. F-strings
  11. 11. Reading Errors
  12. 12. Quiz: Spot the Mistake
  13. 13. Quiz: Changing a Variable
  14. 14. Quiz: The Order of Commands
  15. 15. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.