Data Types¶
Introduction¶
Whenever you create a variable in Python, it has a value with a corresponding data type. There are many different data types, such as integers, floats, booleans, and strings, all of which we'll cover in this lesson. (This is just a small subset of the available data types -- there are also dictionaries, sets, lists, tuples, and much more.)
Data types are important, because they determine what kinds of actions you can do with them. For instance, you can divide two floats, but you cannot divide two strings. For instance, 12.0/2.0
makes sense, but "cat"/"dog"
does not.
To avoid errors, we need to make sure that the actions match the data types that we have.
Integers¶
Integers are numbers without any fractional part and can be positive (1
, 2
, 3
, ...), negative (-1
, -2
, -3
, ...), or zero (0
).
In the code cell below, we set a variable x
to an integer. We then verify the data type with type()
, and need only pass the variable name into the parentheses.
x = 14
print(x)
print(type(x))
14 <class 'int'>
In the output above, <class 'int'>
refers to the integer data type.
Floats¶
Floats are numbers with fractional parts. They can have many numbers after decimal.
nearly_pi = 3.141592653589793238462643383279502884197169399375105820974944
print(nearly_pi)
print(type(nearly_pi))
3.141592653589793 <class 'float'>
We can also specify a float with a fraction.
almost_pi = 22/7
print(almost_pi)
print(type(almost_pi))
3.142857142857143 <class 'float'>
One function that is particularly useful for fractions is the round()
function. It lets you round a number to a specified number of decimal places.
# Round to 5 decimal places
rounded_pi = round(almost_pi, 5)
print(rounded_pi)
print(type(rounded_pi))
3.14286 <class 'float'>
Whenever you write an number with a decimal point, Python recognizes it as a float data type.
For instance, 1.
(or 1.0
, 1.00
, etc) will be recognized as a float. This is the case, even though these numbers technically have no fractional part!
y_float = 1.
print(y_float)
print(type(y_float))
1.0 <class 'float'>
Booleans¶
Booleans represent one of two values: True
or False
. In the code cell below, z_one
is set to a boolean with value True
.
z_one = True
print(z_one)
print(type(z_one))
True <class 'bool'>
Next, z_two
is set to a boolean with value False
.
z_two = False
print(z_two)
print(type(z_two))
False <class 'bool'>
Booleans are used to represent the truth value of an expression. Since 1 < 2
is a true statement, z_three
takes on a value of True
.
z_three = (1 < 2)
print(z_three)
print(type(z_three))
True <class 'bool'>
Similarly, since 5 < 3
is a false statement, z_four
takes on a value of False
.
z_four = (5 < 3)
print(z_four)
print(type(z_four))
False <class 'bool'>
We can switch the value of a boolean by using not
. So, not True
is equivalent to False
, and not False
becomes True
.
z_five = not z_four
print(z_five)
print(type(z_five))
True <class 'bool'>
Booleans will be important in the next lesson, when you learn about conditions and conditional statements.
Strings¶
The string data type is a collection of characters (like alphabet letters, punctuation, numerical digits, or symbols) contained in quotation marks. Strings are commonly used to represent text.
w = "Hello, Python!"
print(w)
print(type(w))
Hello, Python! <class 'str'>
You can get the length of a string with len()
. "Hello, Python!"
has length 14, because it has 14 characters, including the space, comma, and exclamation mark. Note that the quotation marks are not included when calculating the length.
print(len(w))
14
One special type of string is the empty string, which has length zero.
shortest_string = ""
print(type(shortest_string))
print(len(shortest_string))
<class 'str'> 0
If you put a number in quotation marks, it has a string data type.
my_number = "1.12321"
print(my_number)
print(type(my_number))
1.12321 <class 'str'>
If we have a string that is convertible to a float, we can use float()
.
This won't always work! For instance, we can convert "10.43430"
and "3"
to floats, but we cannot convert "Hello, Python!"
to a float.
also_my_number = float(my_number)
print(also_my_number)
print(type(also_my_number))
1.12321 <class 'float'>
Just like you can add two numbers (floats or integers), you can also add two strings. It results in a longer string that combines the two original strings by concatenating them.
new_string = "abc" + "def"
print(new_string)
print(type(new_string))
abcdef <class 'str'>
Note that it's not possible to do subtraction or division with two strings. You also can't multiply two strings, but you can multiply a string by an integer. This again results in a string that's just the original string concatenated with itself a specified number of times.
newest_string = "abc" * 3
print(newest_string)
print(type(newest_string))
abcabcabc <class 'str'>
Note that you cannot multiply a string by a float! Trying to do so will return an error.
will_not_work = "abc" * 3.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_19/2386798361.py in <module> ----> 1 will_not_work = "abc" * 3. TypeError: can't multiply sequence by non-int of type 'float'
In the error, the "sequence" is the string "abc"
, and the "non-int of type 'float'" is the float (3.
). So, the error message can be reworded to say "can't multiply string by float".
Your turn¶
Write your own code to explore different data types.
Have questions or comments? Visit the course discussion forum to chat with other learners.