Like religion, kids, and how to hang the toilet paper, data types are a conversation you can only put off for so long. If you are relatively new to Python, this will give you a good enough understanding to get started with. It won’t be exhaustive, but it will keep you from shooting yourself in the foot.
The big four
Most of our variables in Python are either
whole numbers like 2, 300, 6 million, 0, or -7
floating point numbers (
numbers with a decimal place, like 6.4, -.9, .0001, or 500.005
strings of characters (
like "Bob", "abracadabra", "xvCd5Kp6q", "%#*$", "z", and can even include emojis
either True or False
Find out a variable's type
There's a convenient built-in function
that can tell you the type of a variable.
>>> a = 8 >>> print(type(a)) <class 'int'>
This is also great for checking whether a variable is of a particular type.
>>> print(type(a) == int) True
Cast a variable from one type to another
Python also has the power to transform variables from one type
to another, whenever there's a reasonable way to do it. The
casting functions are named after the types:
# Start with a str >>> b = "5.0" >>> print(b) 5.0 >>> print(type(b)) <class 'str'> # Cast a str to a float >>> c = float(b) >>> print(c) 5.0 >>> print(type(c)) <class 'float'> # Cast a float to an int >>> d = int(c) >>> print(d) 5 >>> print(type(d)) <class 'int'>
Four secrets to keeping track of your variables' types
1. Different types can mean the same thing
Mathematically, we know that the integer 1
and the floating point number 1.0 mean the same thing.
Inside a computer this is no longer true. Different types are
as different as foreign languages. The words "one" and
"один" represent the same underlying concept but are otherwise
entirely different. Likewise, an integer
and a floating point
1.0 have entirely different
representations on the computer,
different patterns and even different numbers of bits.
A bit by bit comparison would show they are not even close.
2. Different types can look the same
Sometimes different types can look identical when you put them in a print statement. The number three is an abstract thing, but it can be represented by a character, the numeral "3". To show a number, a computer first has to translate it into a sequence of characters. Minus seventeen becomes "-17". One and a half becomes "1.5". After this translation happens, you can't tell whether the "1.5" you see on the screen was originally the floating point representation of one and a half or the character string "1", ".", and "5". They end up looking identical on the screen. This makes it easy to forget that they might actually have originated as different types and might mean something different.
>>> a = "1.5" >>> print(type(a)) <class 'str'> >>> b = 1.5 >>> print(type(b)) <class 'float'> >>> print(a, b) 1.5 1.5
3. Python doesn't check types
def add(a, b): return a + b
Nowhere does it specify whether
should be integers, floats, or strings. A lot of other languages
require this, but Python doesn't. All it cares is that whatever
you pass in has a plus operator defined for it. The nerd word
for this is duck typing, as in "if it walks like a
duck and talks lke a duck (and in our case, adds like a duck),
then we'll call it a duck". As long as the variables can do whatever
is asked of them, Python doesn't care what they are. Never mind
that the plus operator adds integers and concatenates strings.
As long is the code can be carried out on the arguments, then
that's good enough for Python.
>>> add(4, 7) 11 >>> add ("some", "strings") 'somestrings'
This flexibility can take some of the pressure off to have
your functions know exactly what types they need to handle, and
it can allow your functions to handle several types seamlessly,
but it can also cause trouble. If you are thinking that
b are integers when they are actually
add() function will be happy to let you
go on believing that, rather than throw a helpful error.
4. Python can automatically change variables' types
In an effort to be helpful, Python also does some automatic type conversion without you even asking, called implicit casting. It's really helpful if you're trying to add 7, an integer, to 3.3, a float. Integers and floating point numbers are apples and oranges. They can't be meaningfully added. Fortunately, Python has some built-in logic that interprets this as a request to add the floating point number 7 to 3.3, and gives that intuitive result 10.3.
>>> m = 7 >>> print(type(m)) <class 'int'> >>> n = 3.3 >>> print(type(n)) <class 'float'> >>> p = m + n >>> print(p) 10.3 >>> print(type(p)) <class 'float'>
This all happens without any pedantic error messages and is part of Python's appeal as a first programming language. It also makes it easy to forget that ints and floats are very different things, and that not all operations are so convenient to interpret. After performing a few lines of computation with our variables, it's tough to keep track of which have been up-cast from integer to float. It's not a fatal flaw, but it can bite you in the butt occassionally.
Python does everything in its power to let you ignore types. But once you have a clear mental picture of your variables types and how Python is interpreting them, you'll be able to avoid some extremely painful debugging and walk more confidently through your coding projects.