Data Types

Data Types

We explored the numeric data types when we talked about numbers in Python. However, there is a whole range of data types that we can use in Python, and in this blog, we’ll explore some of the most commonly used data types.

What exactly is a data type? It refers to the different kinds of values that we can use in a programming language, and these include numbers, decimals, complex numbers, texts, characters, Boolean, and collections of items.

In Python these are the following data types we can use:

 

SERIAL NO. CATEGORY DATA TYPE
1. Text str
2. Numeric int, float, complex
3. Sequence list, tuple, range
4. Mapping dict
5. Boolean bool
6. Binary bytes, bytearray, memoryview
7. Set set, frozenset

 

Don’t worry we won’t overload ourselves, but we will venture into some of the most crucial and frequently used data types.

Text

“str” is a short form for String and can contain strings (texts), or single letter characters. The syntax for creating a variable of type ‘str’ is 

  • variable = “–your text–”

With str, when assigning a value to a variable, we can use single quotes (‘’) or double quotes (“ ”) interchangeably, while there exists a specific use case for triple quotes (‘‘‘ ’’’). 

# printing line_1 and line_2 should give the same output
line_1 = 'Lorem ipsum dolor sit amet'  # single quotes
line_2 = "Lorem ipsum dolor sit amet "  # double quotes

When to use triple quotes? If we have some text that spans over multiple lines, using triple quotes is the best approach. While it is possible to use single/double quotes with a larger text, it can open doors for code that is messy and has potential errors, consider the following:

# using triple quotes
line_3 = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam elementum consequat nibh.'''

# using double quotes
line_4 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " \
"Etiam elementum consequat nibh."

Notice how we need to add the respective quotation mark at the start and end of a line? We also need to add a ‘\’ which tells Python the string is continued in the next line.

Tip: We can check the data type of a variable, using the type() function.

name = "Christopher"

print(name)
print(type(name))

Numeric

We have different numeric types with Python such as integers, decimals, and complex numbers. There exists a vast majority of libraries in Python that we can use for more advanced mathematical computations. We covered the foundations before and you can find the previous blog here.

Sequence

The purpose of the sequence category is to provide a way to store multiple values in one variable, and we can do this in three ways. Try printing the examples given for each:

  • List: It allows us to generate a collection of items, that is indexed and alterable. A list is contained within a pair of square brackets ‘[]’.
list_variable = ["Verification Master", "Python", "Technical Writing"]
  • Tuple: It allows us to generate a collection of items, that is indexed and unalterable. A tuple is contained within a pair of circular brackets ‘()’.
tuple_variable = ("Red skies", "Sunny mornings", "Warm days")
  • Range: It allows us to generate a sequence of numbers based on the arguments we provide.
    • range(start, stop, step)
      • start – the first number of the sequence (optional)
      • stop – the last number of the sequence (mandatory)
        • If the last number is ‘n’, Python will stop at ‘n-1’
      • step – the incremental value (optional; default is 1)
range_variable = range(1, 9, 2)

for x in range_variable:
    print(x)

You should see the following:

Don’t worry about the part which says “for x in”, we will cover it later but for now. just understand it as a way to access each element inside a collection of items.

As you can see the first item is 1, as we gave 1 as the argument for the start parameter, it then displays the next value which is incremented by 2, as we gave 2 as the argument for the step parameter and finally it ends with 7 instead of 9, as we gave 9 as the argument for the stop parameter.

Let’s consider the Set type. It provides a way to store a collection of items, that is indexed but not modifiable. We can either add to it or remove from it, but we cannot modify its contents. A set is contained within a pair of curly braces ‘{}’.

set_variable = {"Lions", "Tigers", "Leopards"}

Mapping

Here we have “dict”, which stands for dictionary. Dictionaries hold “key-value” pairs, i.e., they provide a way to access a certain value by referencing its key. Confused? Let’s consider this example.

dictionary_variable = {
    "name": "Robert",
    "age": 31,
    "weight": 75.5,
    "profession": "actor"
}




print(dictionary_variable)

The output would be:

To exclusively access one value, all we need to do is state the required key value.

print(dictionary_variable["name"])

None Keyword

Certain terms are off-limits for programmers, as these words have been reserved to be used only by Python. We have seen a few of them, such as “print”, “for”, “range”, “in”, “input”, “def”, “return”, and there are so many more. If you try to use one of these words as a name for a variable or a function Python will consider it as a syntax error.

Let’s talk about the “None” keyword. We assign it to a variable when we want it to have no value (referred to as ‘null value’ in programming) and its data type is “Nonetype”.

random_variable = None

print(random_variable)
print(type(random_variable))

Let’s say we are creating a data sheet for students, and one of the columns requires the name of the company they got a job offer from. Students who haven’t been placed yet, cannot fill that column, so they can enter None, which would convey that a student hasn’t been placed yet.

What have we learned?

  • What are the different types of data in Python?
  • When creating a text variable what is the significance of using triple quotes?
  • How can we check the type of a variable?
  • What is a keyword?
  • What is the use case of a None keyword?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments