Execute Program

Python for Programmers: Type

Welcome to the Type lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • A previous lesson showed the isinstance function. We checked values' types by passing int, str, tuple, etc. as isinstance's second argument.

  • >
    isinstance(100, int)
    Result:
    TruePass Icon
  • We've also used int, str, and tuple as functions. They convert values into integers, strings, and tuples.

  • >
    int("100")
    Result:
    100Pass Icon
  • >
    isinstance("100", int)
    Result:
    FalsePass Icon
  • >
    isinstance(int("100"), int)
    Result:
    TruePass Icon
  • int, str, tuple, etc. aren't special syntax in Python. They're values that we can pass to isinstance as regular function arguments. So what are these values?

  • We can think of int as representing the idea of "integerness" and str as representing the idea of "stringness". Likewise for tuple, list, dict, and many others. We'll refer to them as "type objects" or simply "types".

  • It's very useful to have a value that represents "stringness" or "listness". It binds a lot of ideas together, as we already saw above: we can use the same value to ask "is this an integer?" and to convert other data types into integers.

  • Sometimes we have a value and want to get its type object. The built-in type function does that: type(0) gives us int.

  • (When a code example returns the int type object, you can answer with int, without any quotes around it. They're not strings; they're type objects!)

  • >
    type(10)
    Result:
    intPass Icon
  • Note that if you try type(10) in an interactive Python shell, you may see a different result. Different environments print type objects differently. But it's still the same type object! It's like the difference between "Amir" and 'Amir': they're two different ways to print the same value.

  • Now we've seen two closely-related tools: type asks "what is the type of this value?" and isinstance asks "is this a value of this type?"

  • We've also seen that type objects like int are regular Python values. That means that we can store them inside variables. Then we can pass those variables to functions like isinstance.

  • >
    some_type = type(0)
    # `some_type` holds `int` here!
    isinstance(0, some_type)
    Result:
    TruePass Icon
  • >
    some_type = type(0)
    isinstance(77, some_type)
    Result:
    TruePass Icon
  • >
    some_type = type("some string")
    isinstance("a different string", some_type)
    Result:
    TruePass Icon
  • >
    some_type = type("some string")
    isinstance(77, some_type)
    Result:
    FalsePass Icon
  • Here's a quick rundown of common Python type objects:

    • Booleans are bools.
    • Integers are ints.
    • Floating point numbers are floats.
    • Strings are strs.
    • Lists are lists.
    • Tuples are tuples.
    • Dictionaries are dicts.
  • >
    type(True)
    Result:
    boolPass Icon
  • >
    type("Welcome")
    Result:
    strPass Icon
  • >
    type([1, 2, None])
    Result:
    listPass Icon
  • >
    type(2.5)
    Result:
    floatPass Icon
  • >
    type((1, 2, 3))
    Result:
    tuplePass Icon
  • All type objects work with isinstance, all of them can be returned by type, and all of them convert values when called as functions.