TL ; DR:
In Python, not all data is treated the same. There are different data types, such as:
-
int- represents integer numbers like3and-12 -
float- represents floating point numbers like3.14 -
str- represents a string (sequence of characters) like"John Doe" -
bool- represents a Boolean, data that can have one of two values:TrueorFalse -
None- represents a null value, or no value at all
Full lesson:
We've talked about data and variables, which are containers to store that data. But not all data is treated the same.
For example, Python distinguishes between numbers, such as the number 12, and strings, such as "12" or "dog", which are collections of characters.
The most basic and used data types in Python are:
-
int- represents integer numbers like3and-12 -
float- represents floating point numbers like3.14 -
str- represents a string (sequence of characters) like "John Doe" or 'dog' -
bool- represents a Boolean, data that can have one of two values: True or False -
None- represents a null value, or no value at all
type() function
Python knows what type everything is. We can ask Python what type something is by using the type() function:
x = 1
print(type(x)) # Output: <class 'int'>
y = 3.14
print(type(y)) # Output: <class 'float'>
print(type("butter")) # Output: <class 'str'>
z = True
print(type(z)) # Output: <class 'bool'>
temp = 23.0
print(type(temp)) # Output: <class 'float'>
Choosing what operation to perform:
One instruction can have several distinct meanings depending on the data types it's working with.
For example, suppose you see this line of code somewhere in a program:
print(a + b)
Can you tell for sure what it does without knowing anything about the variables a and b?
It might add two numbers, if a and b are both numbers:
a = 10
b = -5
print(a + b) # Output: 5
But it also might concatenate two strings, if a and b are both strings:
a = "Hello "
b = "world!"
print(a + b) # Output: Hello world!
Of course, addition and string concatenation are two completely different operations and Python needs to figure out which one to do.
And this is exactly why Python assesses the data type of every value you use in your program. In order to be able to make these decisions when needed.
In our example, when it reads a + b, Python checks the data type of a and b. If both are string, it performs string concatenation. If both are numbers, it performs addition.
Invalid operations:
Asessing data types is also helpful for Python to make sure that what you do with the data actually makes sense.
For example, computers can perform mathematical operations on numbers, but not on strings. Of course, nobody can stop you from writing a program like this:
a = "peanut"
b = "butter"
print(a * b) # TypeError
But when you try to run this program, Python will throw a TypeError: can't multiply sequence by non-int of type 'str'.
This is the way of Python telling you: "Hey, I'm a little confused. How do you multiply two strings? I've never heard of that."
Assignment
Follow the Coding Tutorial and let's practice with data types!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.
