Python Types

Python has many types of data.

Type Explanation
int Integer,  whole number
str String, text data
float Floating decimal number
bool Boolean, True or False
list Collection of numbered items
dict Dictionary, collection of named items
tuple Similar to lists, but tuples are immutable (can’t be changed)
set Collection of items with no duplicates

Some of those have their own note pages on this site.

Sometimes you need to convert one type to another. For example, you have to convert numbers to a string before concatenating to another string:

# Won't work:
x = 5
print("The number is " + x)

# This will work:
print("The number is " + str(x))

The second example converts the int value to a string using str(). This allows it to be added to the other string.

You also sometimes need to convert a string to a number so you can do math:

# Won't work:
text = "53"
answer = text + 100

# This will work:
text = "53"
number = int(text)
answer = number + 100

Python has a type() function that you can use to see what type of data one of your variables contains:

z = 6/2
print(type(z))

In this case, you’d find that z is a float.

If you try to combine two different types of data, Python might give you errors.

Try these:

n = 15
print("number is " + n)

OUTPUT:

TypeError: can only concatenate str (not "int") to str

Python is telling you that you can’t add str and int values together, because they’re incompatible types.

Fixed Version:

n = 15
print("number is " + str(n))

Now it prints correctly, because we converted the int variable n into a string using the str() function.

Each of the Python types listed in the table above has a corresponding function that you can use to convert other values into that type.

Sometimes you need to convert a string (str) into a number so it can be used in math:

a = input("Type a number:")
x = a + 100
print("New number is: " + x)

You can use the int() or float() functions to convert the text stored in a to a number type:

a = input("Type a number:")
x = float(a) + 100
print("New number is: " + str(x))

We also had to convert the variable x to a string to print it.

Be careful, because you will get an error if you try to convert something to a number if it isn’t actually a number:

int('potato')

OUTPUT:

ValueError: invalid literal for int() with base 10: 'potato'

Other Examples of Mismatched Types

Adding list + str:

pets = ['cat', 'dog']  #list
pets += 'fish'         #adding string to list
print(pets)

OUTPUT:

['cat', 'dog', 'f', 'i', 's', 'h']

Python converted the string ‘fish’ to a list prior to adding it, because you can’t add a string to a list.

Python is automatically doing the following when you try to add something that’s not a list to an existing list:

list('fish')

OUTPUT:
['f', 'i', 's', 'h']

Python’s way of converting a string to a list is to make each individual letter an item in the list. That’s probably not what you had in mind.

The solution in this case is adding [square brackets], which will be explained more later in the lists section.

pets = ['cat', 'dog']
pets += ['fish']      #adding list + list
print(pets)

OUTPUT:
['cat', 'dog', 'fish']

Converting float to int:

z = int(5.74)
print(z)

OUTPUT:
5

Notice that Python rounds down when you convert a floating decimal to an integer in this way. This math operation is also known as “floor.”

Converting to Boolean

print(bool(""))
OUTPUT: False

print(bool("anything"))
OUTPUT: True

An empty String will become False. Any string that contains characters will become True. In Python lingo, we say that an empty string is “falsy” and a non-empty string is “truthy.” This becomes useful to know when you work with Boolean conditions and if/else logic, because Python automatically uses the boolean() function to convert stuff to a Boolean value when needed.