Python Data Types Motivating Example
Suppose you are teaching a class in geography. Each of your five students picked a country, and the following table was prepared based on their reports:
Student | Country | GDP (billion USD) | Population | In Africa? |
---|---|---|---|---|
Mary | Luxembourg | 64.2 | 602005 | False |
Matthew | Eritrea | 6.856 | 4954645 | True |
Marie | None | None | None | None |
Manuel | Lesotho | 2.721 | 2203821 | True |
Malala | Poland | 614.190 | 38433600 | False |
Marie failed to complete the assignment, so her row is None
. We can analyze the data presented in the table above by importing it into one of Python’s data structures, but as an introduction to Python’s data types we will use Python to manually manipulate the data.
Introduction to Python Data Types
This tutorial will serve as an introduction to the data types used within Python programs. Python has the following built-in data types:
- Integers
- Real Numbers (floats)
- Complex Numbers
- Booleans
- None
- Strings
Additional data types can be obtained from the multitude of Python packages, however those specialized packages will be addressed in separate tutorials. In this tutorial, we’ll focus on the following Python data type topics:
Want us to make more free Python tutorials? Share this article on Facebook and Twitter! When you spread the word on social media, you’re helping us grow so we can continue to provide free tutorials like this one for years to come.
Python Integers
Integers are the set of whole numbers, positive and negative, including 0. Python interprets integer type values as whole numbers, and thus the standard set of mathematical operations can be performed:
x = 1 #Defining x to be the integer 1
print(type(x)) #Verifying x to be of type integer
> <class 'int'>
y = x + x # Adding an integer to an integer creates an integer
print(type(y))
> <class 'int'>
Note, rows of data preceded by a right angle bracket, <type 'int'>
instead of <class 'int'>
in the the example above.
If the type of a variable is unknown, the type()
function will return the data type.
int() Function
The Python int()
function is useful for converting various data types into integer data types.
x = 1.0 #Defining x to a float data type
print(type(x))
> <class 'float'>
print(type(int(x)))
> <class 'int'>
Converting float type data into integers can be particularly important when passing derived data into a counter on a for loop.
Be aware that the int()
function will truncate, not round, float numbers.
print([int(1.0), int(1.5), int(1.9999)])
> [1, 1, 1]
Note that the int()
function will NOT iteratively apply to elements within a given list.
print(int([1.0, 1.5, 1.9999]))
> TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
>
> Detailed traceback:
> File "<string>", line 1, in <module>
Converting Strings to Integers
Python can convert simple numeric strings into integers, however strings that do not represent an integer will raise an error.
print(int("409"))
> 409
print(int("409.5"))
> ValueError: invalid literal for int() with base 10: '409.5'
>
> Detailed traceback:
> File "<string>", line 1, in <module>
Converting Booleans to Integers
The int()
function will convert Boolean types True and False to 1 and 0, respectively.
print([int(True), int(False)])
> [1, 0]
Python Integer Example
Based on our classroom example above, we can analyze the population size of the given countries. The population will always be presented in whole numbers, so integers will be the ideal data type to represent them. For example, we can determine the total sum of the populations:
total_population = 602005 + 4954645 + 2203821 + 38433600
print(total_population)
> 46194071
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Python Floats
Floats are the data type used to define real-valued numbers.
print(type(1.0))
> <class 'float'>
Floats can also be input in scientific notation using the following formats
print([type(1.0e-23), type(1.0E-23)])
> [<class 'float'>, <class 'float'>]
Float numbers used in a binary operator with an integer will produce a float
x = 1.0 + int(1.0)
print(type(x))
> <class 'float'>
round() Function
Output from floats can be rounded using the round(x, d)
function, where x
is the number to be rounded and d
is the number of decimals.
print(round(1.23456789, 3))
> 1.235
Infinity & NaN
Addition to numbers, floats also accept the strings inf
, -inf
, and nan
for positive infinity, negative infinity, and Not a Number.
print([float("inf"), float("-inf"), float("nan")])
> [inf, -inf, nan]
Python Float Example
Based on our classroom example above, we can analyze the GDP of the given countries. GDP is represented as real numbers, so they can be manipulated using floats. For example, we can determine the average GDP of the countries:
total_GDP = 64.2 + 6.856 + 2.721 + 614.190
mean_GDP = total_GDP/4
print(round(mean_GDP, 2))
> 171.99
Limitations on Base-10 Output
Note: There are limitations to the level of precision that can be obtained from converting a binary number into a base-10 number. This tutorial will not go into the mathematical details of this phenomenon, however it is considered here to develop an understanding on the limitations to the number of decimals that can be considered from an output. For example, the real number 0.1 can be exactly defined in Python as
x = 0.1
However, if the number of printed decimals is extended beyond the precision range, then the output can contain spurious numbers
print("{:.30f}".format(x)) #This method will print 30 decimals of x. We will cover this later!
> 0.100000000000000005551115123126
This issue arises in any situation wherein numbers stored in binary are converted to base-10. This will not affect Python’s underlying calculations but should be remembered when outputting high-precision numbers.
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Python Complex
Complex numbers are seldom used in coding except in the most technical of situations, however no approach to Python’s data types would be complete without considering them. Complex numbers possess two parts: a real float and an imaginary float. These types can be defined using the complex(Re, Im)
function, where Re
is the real part and Im
is the imaginary part.
x = complex(1.0, 2.0)
print(x)
> (1+2j)
print(type(x))
> <class 'complex'>
Complex Number Operations
The individual parts of the complex number x can be extracted by calling the x.real
and x.imag
attributes. The complex conjugate can be determined using the x.conjugate()
method.
print(x.real)
> 1.0
print(x.imag)
> 2.0
print(x.conjugate())
> (1-2j)
Python Boolean
The Boolean logical values true and false are specified as “True” and “False”, respectively1.
print([type(True), type(False)])
> [<class 'bool'>, <class 'bool'>]
Boolean Sums
These types can be used as standard Boolean values, operations on which satisfy standard truth tables. As mentioned in the section on integers, the Boolean values are represented as 1 and 0 for True and False, respectively, in numeric operations. For example
x = True + True + False
print(x)
> 2
l = [True, True, True, False, True, False]
print(sum(l))
> 4
This can be useful when determining the number of successes in a list of trials. Note that the following example uses a loop, which we will cover in a future tutorial.
l = [1, 4, 8, 7, 2, 5, 9]
check = [x > 5 for x in l] #Check if elements are greater than 5
print(check)
> [False, False, True, True, False, False, True]
print(sum(check))
> 3
Python Boolean Example
In our classroom example, we can use the “In Africa?” logical to determine the total number of countries within the African continent:
num_africa = False + True + True + False
print(num_africa)
> 2
We can also use lists to determine the number of countries whose population is above four million:
l = [602005, 4954645, 2203821, 38433600]
check = [x > 4000000 for x in l]
print(check)
> [False, True, False, True]
print(sum(check))
> 2
Python None
The None
object is Python’s version of the NULL
or NIL
object. Assignments and operations cannot be made to None
and doing so will raise an error. Note that the None
type cannot be used with otherwise polymorphic operations (i.e. +
) to produce an output None
, unlike other languages such as R.
print(type(None))
> <class 'NoneType'>
Python None Example
In our classroom example above, we are missing data from Marie, which has been represented as None
in the data table. If we attempted to find the average GDP by blindly adding the data, we would raise an error:
total_GDP = 64.2 + 6.856 + None + 2.721 + 614.190
> TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'
>
> Detailed traceback:
> File "<string>", line 1, in <module>
However, we can screen out None
objects if we analyze the data as a list of the GDP column. Don’t worry about understanding the list operations we perform here; we will cover them in a future tutorial. Just remember that the None
object is helpful for screening out missing values.
GDP_list = [64.2, 6.856, None, 2.721, 614.19]
Screend_list = [x for x in filter(None, GDP_list)] # Filter function will screen None
mean_GDP = sum(Screend_list)/len(Screend_list)
print(round(mean_GDP, 2))
> 171.99
Python Strings
Strings have a broad range of operators and formats associated with them, but for this introduction we’ll only look at a few of the built-in features.
A string character type can be defined with an enclosed set of either single '
or double "
quotation marks. It is good form to pick either single or double quotations for the entirety of the code.
x = "abcde"
print(type(x))
> <class 'str'>
The binary operator +
when used on strings will concatenate.
x = "The" + " " + "best" + " " + "of" + " " + "times."
print(x)
> The best of times.
x += " The worst of times."
print(x)
> The best of times. The worst of times.
Note in the above example, the x += y
operator is equivalent to x = x + y
.
String Slicing
A powerful feature of Python is the string “slicing” feature, wherein characters within a string can be extracted as though they are array elements.
x = "abcdefghi"
print(x[0]) #Note Python begins indexing at 0
> a
print(x[8]) #Last element will be length - 1
> i
print(x[3:5]) #Selects a range of characters, excluding the last
> de
print(x[5:]) #Selects characters starting at index 5 to the end of string
> fghi
print(x[:3]) #Selects characters from begining of string to index 3, exclusive.
> abc
print(x[-1]) #Selects the last character
> i
print(x[-5:-3]) #Negative integers select indices relative to the last index
> ef
Converting Data Types to String
Similar to the integer int()
function, the str()
function will convert a given data type into a string.
print(str(1)) #Integer to String
> 1
print(str(1.0)) #Float to String
> 1.0
print(str(True)) #Boolean to String
> True
print(str(None)) #None to String
> None
Note that the output from the float to string conversion maintains a Python-determined number of decimal places. To change the output format of any string, the format()
method can be used. String methods will be discussed in a future chapter on string operations.
Python String Example
Based on the classroom example, we can use string operations to create strings from the table information. For example, suppose we want to list out the countries:
countries = "Luxembourg" + ", " + "Eritrea" + ", " + "Lesotho" + ", and " + "Poland."
print(countries)
> Luxembourg, Eritrea, Lesotho, and Poland.
We can also use slicing to print back out the countries:
print(countries[:10])
> Luxembourg
print(countries[12:19])
> Eritrea
print(countries[21:28])
> Lesotho
print(countries[-7:-1])
> Poland
See how the negative integers select indices relative to the last index position? All indices select characters from left to right on the number line with -1 as the last character, which for negative numbers will be in the order of most negative to least negative.
Python also uses the escape character \
to define string literals. \t
defines a tab, and \n
defines a newline or carriage return. Using these characters, we can print out a couple of table rows:
x = ("Student" + "\t" + "Country " + "\t" + "GDP (billion USD)" + "\t" "Population" + "\n"
+"Mary " + "\t" + "Luxembourg" + "\t" + "64.2 " + "\t" "602005 " + "\n" )
print(x) #String literals will only be processed in a print function
> Student Country GDP (billion USD) Population
> Mary Luxembourg 64.2 602005
Note in the example above, we can use parentheses in Python to extend our execution over multiple lines.
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Python Data Type Exercises
- Determine the output type and value of the following operations, or state that the line will raise an error.
x = 1 + 1.0
x = 1 + "1.0"
x = 1 + int("1.0")
x = "1" + "1.0"
x = 1 + True + False
x = 1 + None
x = "None" + str(None)
x = 2 + int(str(1.5))
x = int(2.5) + int(str(1))
Solution
2. Will the following two lines print identical results? Why or why not?
total_GDP_1 = 64.2 + 6.856 + 2.721 + 614.190
total_GDP_2 = "64.2" + "6.856" + "2.721" + "614.190"
Solution
3. Use string operations to print the entire table in the classroom example.
Solution
4. In the slicing operation [x:y]
, is the character at index x included or excluded? What about index y?
Solution
5. In a string of 10 characters, what is the index of the final character?
Solution
6. In a Python terminal, execute the following two lines. Is there a difference? What impact does the print()
function have on the string?
"Hello\tworld!"
print("Hello\tworld!")
Python Data Type Solutions
x = 1 + 1.0
print(x)
> 2.0
x = 1 + "1.0"
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
x = 1 + int("1.0")
> ValueError: invalid literal for int() with base 10: '1.0'
x = int("1" + "1")
print(x)
> 11
# "1" + "1" concatenates to "11", which int() converts to the integer 11
x = 1 + True + False
print(x)
> 2
x = 1 + None
> TypeError: unsupported operand type(s) for +: 'int' and 'NoneType
x = "None" + str(None)
print(x)
> NoneNone
x = 2 + int(str(1.5))
> ValueError: invalid literal for int() with base 10: '1.5'
x = int(2.5) + int(str(1))
print(x)
> 3
2. No. The first line will add each float to produce the float 687.967, and the second line will concatenate the lines into the string “64.26.8562.721614.190”
x = ("Student " + "\t" + "Country " + "\t" + "GDP (billion USD)" + "\t" "Population" + "\t" "In Africa?" + "\n"
+"Mary " + "\t" + "Luxembourg" + "\t" + "64.2 " + "\t" "602005 " + "\t" "False" + "\n"
+"Matthew " + "\t" + "Eritrea " + "\t" + "6.856 " + "\t" "4954645 " + "\t" "True" + "\n"
+"Marie " + "\t" + "None " + "\t" + "None " + "\t" "None " + "\t" "None" + "\n"
+"Manuel " + "\t" + "Lesotho " + "\t" + "2.721 " + "\t" "2203821 " + "\t" "True" + "\n"
+"Malala " + "\t" + "Poland " + "\t" + "614.190 " + "\t" "38433600 " + "\t" "False" + "\n" )
print(x)
> Student Country GDP (billion USD) Population In Africa?
> Mary Luxembourg 64.2 602005 False
> Matthew Eritrea 6.856 4954645 True
> Marie None None None None
> Manuel Lesotho 2.721 2203821 True
> Malala Poland 614.190 38433600 False
4. Character at x will be included, character at y will be excluded
6. The first line will echo the string including the string literal, and the second will print the string with the string literal’s character.
Did you find this free tutorial helpful? Share this article with your friends, classmates, and coworkers on Facebook and Twitter! When you spread the word on social media, you’re helping us grow so we can continue to provide free tutorials like this one for years to come.
-
Python is a prim and proper language, so capitalization matters. ↩