Lesson 4 of 60
โฎPrev Nextโฏ

PY-101 ยท Variables and data types

Variables

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

4of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    Lesson quest ยท 4 min

    Help a shop app remember the cart total while it keeps changing.

    1. Watch it run
    2. Predict
    3. Drag and sort
    4. Predict
    5. Fill the gaps
    6. Watch it run
    7. Build it
    8. Boss level
    9. Badge: Box Keeper

    ๐ŸŽฎ 8 screensโšก up to 70 XPโค๏ธ 3 hearts๐Ÿซ™ Box Keeper

    โšก The 30-second answer

    A variable is a name that points to a value, such as age = 25. You create it by assigning with =, and you can assign a new value at any time. Python works out the type from the value, so you never declare it.

    • Create variables with =
    • Change a variable's value
    • Follow Python's naming rules
    • Assign several variables in one line

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Variables, running
    1. name = "Alex"
    2. age = 25
    3. age = age + 1
    4. city, country = "Chennai", "India"
    5. print(name, "is", age)
    6. print(city, country)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA shopping cart shows โ‚น0. You add a โ‚น250 book and it shows โ‚น250. Add a pen and it changes again. Where does the app keep that number while it keeps changing?
    2. The Python ideaname = valueA variable is a name that points to a value, such as age = 25.
    3. In real lifeIn a shopping app, cart_total starts at 0 and changes every time an item is added. That running total is just a variable being reassigned.

    Start with a question

    A shopping cart shows โ‚น0. You add a โ‚น250 book and it shows โ‚น250. Add a pen and it changes again. Where does the app keep that number while it keeps changing?

    Every program stores data: a user's name, a cart total, a model's score. Variables are how you give that data a name you can use later.

    Explain it like I'm 10

    A variable is a labelled box. age = 25 means: put 25 in the box labelled age. Later you can look inside the box, or swap what is in it.

    The technical explanation

    = means assign, not "equals". age = 25 means "make the name age point to 25".

    The right side runs first. In age = age + 1, Python reads the current age, adds 1, then points age at the new value.

    Names can contain letters, digits and underscores, cannot start with a digit, and are case-sensitive: Age and age are different. Python style is lower case with underscores, called snake_case: total_price.

    You cannot use keywords such as if, for or class as names.

    NameValid?Why
    total_priceYessnake_case, the Python style
    _countYesCan start with an underscore
    2nd_placeNoCannot start with a digit
    first-nameNoHyphen means minus
    classNoclass is a keyword

    Syntax

    name = value
    a, b = 1, 2

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    In a shopping app, cart_total starts at 0 and changes every time an item is added. That running total is just a variable being reassigned.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does = do in Python?
    2. 2. Which variable name is valid?
    3. 3. x = 4, then x = x * 2. What is x?
    4. 4. Are Score and score the same variable?
    5. 5. After a, b = 1, 2 what is b?

    Questions people ask

    Do you need to declare variables in Python?

    No. A variable exists the moment you assign to it. Python works out the type from the value.

    Can a Python variable change type?

    Yes. x = 5 then x = "five" is allowed, because the name just points at a new value. It works, but it makes code harder to follow, so avoid it.

    What is snake_case?

    Lower-case words joined by underscores, such as total_price. It is the naming style recommended for Python variables and functions in PEP 8.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Do you need to declare variables in Python?

    No. A variable exists the moment you assign to it. Python works out the type from the value.

    beginner Can a Python variable change type?

    Yes. x = 5 then x = "five" is allowed, because the name just points at a new value. It works, but it makes code harder to follow, so avoid it.

    beginner What is snake_case?

    Lower-case words joined by underscores, such as total_price. It is the naming style recommended for Python variables and functions in PEP 8.

    intermediate What does = do in Python?

    Assigns a value to a name. = assigns. == is the equality check.

    intermediate Which variable name is valid?

    total_price. total_price uses letters and an underscore. Names cannot start with a digit, contain a hyphen or be a keyword.

    Quick revision

    Remember

    • Create variables with =
    • Change a variable's value
    • Follow Python's naming rules
    • Assign several variables in one line
    • Python works out the right side of = first. That is why count = count + 1 works: it uses the old count to make the new one.

    Syntax

    name = value
    a, b = 1, 2

    Official documentation