Get students paying attention to types in Python; although type declarations aren’t explicitly required in Python, they still exist and are crucial to understanding how Python manages data.

  • Use the mantra, "Everything in Python has a name, a type and a value"
  • Ask students about things in Python according to the Mantra:
    • What is the name?
    • What is the type?
    • What is the value?
  • This concept can be difficult.
    • Consider the fact that functions are just things that have a name, type, and value.
      • Use python tutor to show how things of different types have names, types and values.
    • For instance, in the example below there are three different names, types, and values!
      • Python script defining a function, an array and an intData representation of a function, an array and an int
      • Point out that the variables functX, arr, and num are defined in the global space.
      • The image on the right (containing the Frames and Objects) shows the environment after all of the code has been run.
        • Describe this content as what "Python knows."
  • Students struggle to pay attention to types (or connect types to Python code at all) because Python is implicitly typed.
  • Remind students frequently to think about types in Python.
  • Many coding errors result from students not understanding types. When you help students debug their code, ask "what is the type of x?", and as a follow on, "what operations do objects of this type support?"
  • The opinions expressed in the above three points are those of the Suzanne Matthews and not necessarily the position of the Army or DoD

More about this tip

External Source

Interview with Owen Astrachan.

Interview with Suzanne Matthews