In this post we’ll talk about python’s types, how to use them, how they’re treated, and what we can do with typing.

This is aimed at beginners who have heard the words “Python” and “Types” but haven’t quite nailed down what they have to do with each other.

This is Part 1 of a multi-part series on Python Typing.

Python’s Native Types

The best place to start is just by listing each of the types that Python uses natively so that way we can work through examples later. As with most core-python functionality, the best place to learn more is the documentation, specifically this page.

  • Boolean – bool
  • Integer – int
  • Float – float
  • Complex
  • Iterator
  • Generator
  • List – list
  • Tuple – tuple
  • Range – range
  • String – str
  • Bytes
  • Byte Array
  • Memory View
  • Set – set
  • Frozenset – frozenset
  • Dictionary – dict
  • Context Manager
  • Modules
  • Functions
  • Methods
  • Code Objects
  • Type Objects
  • Null – None
  • Ellipses
  • NotImplemented

Whew, that’s a ton. Let’s focus on the more important ones for now.

Booleans

Truth values, these can take on one of two values – True or False.

Integers

Basic numeric type, these are whole numbers.

Floats

Another numeric type, these are decimal numbers.

Lists

An ordered collection of items. These items don’t have to be the same type, and there’s no limit on the length of the
list. Lists are mutable, which we’ll talk about later.

Tuples

Another ordered collection of items, which also don’t have to be the same type, and there’s also no limit on the length.
However, the big difference between lists and tuples is that tuples are not mutable.

Ranges

Ranges are ordered sequences of numbers created with the range keyword.

Strings

Strings are an ordered collection of “characters”.

Sets

Sets are an unordered collection of items. These are similar to lists, but are not mutable and not ordered.

Dictionaries

Dictionaries are mappings of Key to Value pairings. The keys can be any immutable thing, and the values can be
anything.

None

The Null type, the type for something that doesn’t exist is called None.

Mutability

We mentioned mutability a lot, but didn’t nail down what that means. It’s a essentially just whether or not a variable
is “changeable”. For instance, with a mutable object (like a list) we can change the values in place.

However an immutable object cannot be changed.

Conclusion

Everything object in Python has a type, but above we went over the most basic and necessary ones.

In the next post we’ll talk about the types provided by the typing module in Python 3 and how they can be used.

3 Comments

  1. Pingback: Python Type Hinting – The Data Leek

  2. Pingback: The Python Typing Module – The Data Leek

Leave a Reply

Your email address will not be published. Required fields are marked *