Datatypes in Python

Introduction

Like in all the programming languages data types are assigned to the variable to accept values of a single data type.

Let us go over the important aspects of data types we have on python

Note: we do not need to declare the data type anywhere in the program

Background

One way to think about data types is to consider the different types of data that we use in the real world. An example of data in the real world are numbers: we may use whole numbers (0, 1, 2, …), integers (…, -1, 0, 1, …), and irrational numbers (π), for example.

Usually, in math, we can combine numbers from different types, and get some kind of an answer. We may want to add 5 to π, for example:

5 + π

We can either keep the equation as the answer to account for the irrational number, or round π to a number with a brief number of decimal places, and then add the numbers together:

5 + π = 5 + 3.14 = 8.14

But, if we start to try to evaluate numbers with another data type, such as words, things start to make less sense. How would we solve for the following equation?

sky + 8

For computers, each data type can be thought of as being quite different, like words and numbers, so we will have to be careful about how we use them to assign values and how we manipulate them through operations.

Numbers

Any number entered in python without decimal places will be considered as an integer by python.
Note: Like in mathematics, integers in python can have values that are positive, negative or zero.

Any number containing decimals .i.e. Fractional values of considered to be a float value.
We can perform basic arithmetic operations on float values.

Integers

Like in math, integers in computer programming are whole numbers that can be positive, negative, or 0 (…, -1, 0, 1, …). An integer can also be known as an int. As with other programming languages, you should not use commas in numbers of four digits or more, so when you write 1,000 in your program, write it as 1000.

We can print out an integer in a simple way like this:

print(-25)

Output

-25

Or, we can declare a variable, which in this case is essentially a symbol of the number we are using or manipulating, like so:

my_int = -25
print(my_int)

Output
-25

We can do math with integers in Python, too:

int_ans = 116 - 68
print(int_ans)

Output
48

Integers can be used in many ways within Python programs, and as you continue to learn more about the language you will have a lot of opportunities to work with integers and understand more about this data type.

Floating-Point Numbers

A floating-point number or a float is a real number, meaning that it can be either a rational or an irrational number. Because of this, floating-point numbers can be numbers that can contain a fractional part, such as 9.0 or -116.42. Simply speaking, for the purposes of thinking of a float in a Python program, it is a number that contains a decimal point.

Like we did with the integer, we can print out a floating-point number in a simple way like this:

print(17.3)

Output
17.3

We can also declare a variable that stands in for a float, like so:

my_flt = 17.3
print(my_flt)
Output
17.3

And, just like with integers, we can do math with floats in Python, too:

flt_ans = 564.0 + 365.24
print(flt_ans)
Output
929.24

With integers and floating-point numbers, it is important to keep in mind that 3 ≠ 3.0, as 3 refers to an integer while 3.0 refers to a float.

Booleans

Booleans data type is capable of holding two values
True, which represents a logical 1
False, which represents a logical 0

Booleans are mainly used in conditional statements to make decision in the program.

Strings

A string is a sequence or combination of more than one characters. In python strings exist in single quotes or double quotes

We can print strings, store the values in variables and also refer to the string by calling the variable

'This is a string in single quotes.'
"This is a string in double quotes."

You can choose to use either single quotes or double quotes, but whichever you decide on you should be consistent within a program.

The simple program “Hello, World!” demonstrates how a string can be used in computer programming, as the characters that make up the phrase Hello, World! are a string.

print("Hello, World!")

As with other data types, we can store strings in variables:

hw = "Hello, World!"

And print out the string by calling the variable:

print(hw)
Output
Hello, World!

Like numbers, there are many operations that we can perform on strings within our programs in order to manipulate them to achieve the results we are seeking. Strings are important for communicating information to the user, and for the user to communicate information back to the program.

Lists

A list is a sequence of elements represented by a single variable. Every element inside a list is referred as an item. Elements are declared in between square brackets and separated by commas.

We can consider lists to be a flexible data type as the elements can be added, removed and changed.

A list of integers looks like this:

[-3, -2, -1, 0, 1, 2, 3]

A list of floats looks like this:

[3.14, 9.23, 111.11, 312.12, 1.05]

A list of strings:

['shark', 'cuttlefish', 'squid', 'mantis shrimp']

If we define our string list as sea_creatures:

sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp']

We can print them out by calling the variable:

print(sea_creatures)

And we see that the output looks exactly like the list that we created:

Output
['shark', 'cuttlefish', 'squid', 'mantis shrimp']

Lists are a very flexible data type because they are mutable in that they can have values added, removed, and changed. There is a data type that is similar to lists but that can’t be changed, and that is called a tuple.

Try

Tuples

Tuples are lists that are immutable. The values in the tuple cannot be changed after the declaration.

A tuple is declared using a parenthesis instead of square brackets.

A tuple looks like this:

('blue coral', 'staghorn coral', 'pillar coral')

We can store a tuple in a variable and print it out:

coral = ('blue coral', 'staghorn coral', 'pillar coral')
print(coral)
Output
('blue coral', 'staghorn coral', 'pillar coral')

Like in the other data types, Python prints out the tuple just as we had typed it, with parentheses containing a sequence of values.

Dictionaries

Dictionaries are pythons built-in maps. All the data in dictionaries is stored in data-value pairs. A dictionary is declared by using curly braces.

Each element can be referred using the value of the key.

Typically used to hold data that are related, such as the information contained in an ID, a dictionary looks like this:

{'name': 'Sammy', 'animal': 'shark', 'color': 'blue', 'location': 'ocean'}

You will notice that in addition to the curly braces, there are also colons throughout the dictionary. The words to the left of the colons are the keys. Keys can be made up of any immutable data type. The keys in the dictionary above are: 'name', 'animal', 'color', 'location'.

The words to the right of the colons are the values. Values can be comprised of any data type. The values in the dictionary above are: 'Sammy', 'shark', 'blue', 'ocean'.

Like the other data types, let’s store the dictionary inside a variable, and print it out:

sammy = {'name': 'Sammy', 'animal': 'shark', 'color': 'blue',  'location': 'ocean'}
print(sammy)

Output {'color': 'blue', 'animal': 'shark', 'name': 'Sammy', 'location': 'ocean'}

If we want to isolate Sammy’s color, we can do so by calling sammy['color']. Let’s print that out:

print(sammy['color'])
Output
blue

As dictionaries offer key-value pairs for storing data, they can be important elements in your Python program.

Try

Conclusion

At this point, you should have a better understanding of some of the major data types that are available for you to use in Python. Each of these data types will become important as you develop programming projects in the Python language.