Variables

Variables

A variable is a name that stores a value.

age = 36

This line creates a variable named age and stores the value 36 in it. The = sign assigns the value on the right to the name on the left.

Once a variable exists, its name stands for its value:

age = 36
next_year = age + 1

next_year now holds 37.

Naming rules

Variable names are lowercase. Words are separated by underscores: first_name, items_sold. A name cannot contain spaces and cannot start with a digit.

Text values are written between quotes and are called strings: "Melbourne". Numbers are written without quotes: 5400000. A number in quotes is a string, not a number.

Exercise

Create three variables:

  1. city — the string "Melbourne"
  2. population — the number 5400000
  3. country — the string "Australia"

Then print the three variables.

Press Submit to check your work.

Tests

import unittest


class TestVariables(unittest.TestCase):
    def test_city_is_the_string(self):
        self.assertEqual(city, "Melbourne")

    def test_population_is_the_number(self):
        self.assertEqual(population, 5400000)

    def test_country_is_the_string(self):
        self.assertEqual(country, "Australia")
city = "Melbourne"
population = 5400000
country = "Australia"
Solution hidden. Give it a real try first.

Sign in to join the discussion.

No comments yet. Be the first to ask a question.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.