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

PY-101 ยท Variables and data types

Data types

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

5of 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.

    โšก The 30-second answer

    A data type tells Python what kind of value something is and what you can do with it. The core types are int for whole numbers, float for decimals, str for text, bool for True or False, and None for "no value". type(x) tells you which one you have.

    • Recognise int, float, str, bool and None
    • Check a value's type with type()
    • Pick the right type for real data

    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.

    Data types, running
    1. count = 10
    2. price = 99.5
    3. name = "Python"
    4. is_active = True
    5. nothing = None
    6. print(type(count))
    7. print(type(price))
    8. print(type(name))
    9. print(type(is_active))
    10. print(type(nothing))
    11. print(0.1 + 0.2)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA form asks for your age. You type 29. The program adds 1 and shows 291. The program treated 29 as text, not as a number.
    2. The Python ideatype(value)A data type tells Python what kind of value something is and what you can do with it.
    3. In real lifeA phone number looks like a number but should be a str: you never add phone numbers, and int would drop a leading zero.

    Start with a question

    A form asks for your age. You type 29. The program adds 1 and shows 291. The program treated 29 as text, not as a number.

    Types decide what is allowed. "5" + 5 is an error but 5 + 5 is 10. Most beginner bugs, and plenty of bugs in AI-generated code, are a value with the wrong type.

    Explain it like I'm 10

    Different things need different kinds of box. Numbers you can add, words you can spell out, and yes/no answers are True or False. Python labels every value with its kind, so it knows what is allowed.

    The technical explanation

    int is a whole number of any size: 42, -7, 10_000_000. float has a decimal point: 3.14, 2.0.

    str is text in quotes: "Chennai". bool is True or False, with a capital first letter.

    None means "nothing here yet". It is the value of a variable that has been created but not filled.

    Floats are stored in binary, so some decimals are approximate: 0.1 + 0.2 prints 0.30000000000000004. For money, use whole paise or cents, or the decimal module.

    TypeExampleUse it for
    intcount = 10Counts, ages, IDs
    floatprice = 99.5Measurements, averages
    strname = "Asha"Names, messages, codes
    boolis_paid = TrueYes/no flags
    Noneresult = NoneNot set yet

    Syntax

    type(value)
    isinstance(value, int)

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

    Real-world scenario

    A phone number looks like a number but should be a str: you never add phone numbers, and int would drop a leading zero.

    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 is type(7.0)?
    2. 2. Which value is a bool?
    3. 3. Which type should a phone number like 09876543210 be?
    4. 4. What does None mean?
    5. 5. What does print(0.1 + 0.2 == 0.3) show?

    Questions people ask

    What are the basic data types in Python?

    int (whole numbers), float (decimals), str (text), bool (True/False) and None. The collection types list, tuple, dict and set build on these.

    Why is 0.1 + 0.2 not 0.3 in Python?

    Floats are stored in binary, and 0.1 cannot be represented exactly in binary, so a tiny error appears. Every language that uses IEEE 754 floats does the same. Use round() for display, or the decimal module for money.

    Interview questions

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

    beginner What are the basic data types in Python?

    int (whole numbers), float (decimals), str (text), bool (True/False) and None. The collection types list, tuple, dict and set build on these.

    beginner Why is 0.1 + 0.2 not 0.3 in Python?

    Floats are stored in binary, and 0.1 cannot be represented exactly in binary, so a tiny error appears. Every language that uses IEEE 754 floats does the same. Use round() for display, or the decimal module for money.

    intermediate What is type(7.0)?

    float. Any number with a decimal point is a float, even 7.0.

    intermediate Which value is a bool?

    True. Booleans are True and False with a capital letter. "True" in quotes is a str.

    Quick revision

    Remember

    • Recognise int, float, str, bool and None
    • Check a value's type with type()
    • Pick the right type for real data
    • When something behaves strangely, add print(type(x)). A huge share of beginner bugs are a str where you expected an int.

    Syntax

    type(value)
    isinstance(value, int)

    Official documentation