DEV Community

Cover image for Python Tutorial (Basic): Data Types
Nicole Ramos
Nicole Ramos

Posted on

Python Tutorial (Basic): Data Types

Introduction

In Python, data types refer to the type or category of data that a variable or expression represents. Python has several built-in data types, including:

  • Integers
  • Floats
  • Complex
  • Strings
  • Booleans
  • Lists
  • Tuples
  • Dictionaries
  • Sets

Python Banner

These data types have different properties and methods that allow you to perform different operation. It's important to choose the appropriate data type for your specific needs, as this can affect the efficiency of your code.

Let's get started!

Integers

Integer data type represents positive or negative whole numbers without decimal points.

integer1 = 10
integer2 = 85
integer3 = -120
integer4 = -6
integer5 = 0

print(type(integer1))
Enter fullscreen mode Exit fullscreen mode

Floats

Float data type represents real numbers with decimals.

float1 = 10.23
float2 = 85.87
float3 = -120.61
float4 = -6.98732
float5 = 0.4

print(type(float4))
Enter fullscreen mode Exit fullscreen mode

Complex Numbers

Complex numbers are a type of number that includes a real part and an imaginary part. Complex numbers are commonly used in scientific and engineering calculations.

complex1 = complex(2, 3j)
complex2 = complex(18, 6j)
complex3 = complex(49, 12j)
complex4 = complex(67, 1j)
complex5 = complex(9, 98j)

print(type(complex1))
Enter fullscreen mode Exit fullscreen mode

Access to the real part and the imaginary part this way:

data = complex(13.68, 98.74)

print(data)
print('Real part of data:', data.real)
print('Imaginary part of data:', data.imag)
Enter fullscreen mode Exit fullscreen mode

Strings

A string is a sequence of characters. It is a collection of alphabets, words or other characters enclosed with quotes.

Example with single quotes. ''

string1 = 'My name is Nicole and I am 22 years old.'
Enter fullscreen mode Exit fullscreen mode

Example with double quotes. ""

string2 = "I am learning the basics of Python with this tutorial."
Enter fullscreen mode Exit fullscreen mode

Example with triple quotes using double quotes. """ """

string3 = """When you use triple quotes you can write in blocks. For example, I can finish my sentence here and
then continue writting some more in a second line and not get any error, which I would get if I was using single
quotes or double quote. When I use the triple quotes I can also use "double" and 'single'quotes without any problem."""
Enter fullscreen mode Exit fullscreen mode

Example with triple quotes using single quotes. ''' '''

string4 = '''Same as the previous example, I can write blocks with triple quotes using single quotes.
If you try to write multiple lines with the double and single quotes you will be getting an error.
Try it yourself!!!'''

print(type(string2))
Enter fullscreen mode Exit fullscreen mode

Booleans

Data type that represents one of two values: "True" or "False". Booleans are used to express logical values, such as whether a condition is true or false.

x = True
y = False
Enter fullscreen mode Exit fullscreen mode

Simple as that!

Lists

A list is a data structure in Python that is a modifiable. An ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets.

list = ["Nicole", "nickramen", True, 22]

print(type(list))
Enter fullscreen mode Exit fullscreen mode

Remember that when we count the items of an array, we always start counting from 0. For example, look at the word Hello:

  • H -> 0
  • e -> 1
  • l -> 2
  • l -> 3
  • o -> 4

With that in mind, you can access to an specific item of a list this way:

print(list[2])
Enter fullscreen mode Exit fullscreen mode

Since it is possible to modify the list after it has been created, I'll show how simple it is. You first access to the item you want to change and then assign it the new value. Finally, print the list so you can see the list with the value you changed.

list[2] = False
print(list)
Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are an ordered collection of values or items. It is similar to the lists but, unlike those, tuples cannot be modified once they are created. Tuples are defined by having values between parenthesis.

tuple = ("one", "two", "three", 4, 5)

print(type(tuple))
Enter fullscreen mode Exit fullscreen mode

You can access to an specific item of the tuple same way as you do with lists, but if you try to modify it you will get an error like the following:

TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

Dictionaries

A dictionary is a data structure that allows you to store and retrieve key-value pairs. They are similar to lists and tuples because they can hold multiple values, but instead of using numeric indices to access elements (like in lists and tuples) dictionaries use keys.

dictionary = {
    'name' : 'Nicole',
    'lastname' : 'Ramos',
    'age' : 22
}

print(type(dictionary))
print(dictionary)
Enter fullscreen mode Exit fullscreen mode

Access to an item in dictionary and modify it.

print(dictionary['name'])
print(dictionary['age'])
Enter fullscreen mode Exit fullscreen mode

Adding items to the dictionary:

dictionary['nationality'] = "honduran"
dictionary.update({'student':False, 'profession':'programmer'})
print(dictionary)
Enter fullscreen mode Exit fullscreen mode

Updating items from the dictionary:

dictionary['age'] = 25
dictionary.update({'student':True,'name':'Darcy'})
print(dictionary)
Enter fullscreen mode Exit fullscreen mode

Sets

Sets are very similar to dictionaries. They store unordered collections of data and every item is unique. Items cannot be modified nor accessed by the index. Use sets when you want to store a collection you know you won't need to modify, that way you can optimize your code.

sets = {1, 2, 3, 4, 5}

print(type(sets))
print(sets)
Enter fullscreen mode Exit fullscreen mode

Let's say we have two sets:

set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}
Enter fullscreen mode Exit fullscreen mode

Union() - creates a new set that contains elements from different sets.

union_set = set_a.union(set_b)
print(union_set) # prints {1, 2, 3, 4, 5, 6, 7, 8}
Enter fullscreen mode Exit fullscreen mode

Intersection() - creates a new set that contains the elements that are common to both sets.

intersection_set = set_a.intersection(set_b)
print(intersection_set) # prints {4, 5}
Enter fullscreen mode Exit fullscreen mode

Difference() - creates a new set that contains the elements that are in set_a but not in set_b.

difference_set = set_a.difference(set_b)
print(difference_set) # prints {1, 2, 3}
Enter fullscreen mode Exit fullscreen mode

Eliminate duplicates from a list

names = ['Alice', 'Bob', 'Charlie', 'Alice', 'Dave']
unique_names = set(names)
print(unique_names)  # prints {'Alice', 'Bob', 'Charlie', 'Dave'}
Enter fullscreen mode Exit fullscreen mode

Conclusion

First of all, congrats for getting all the way to the end! Now you have plenty of knowledge of the data types in Python. If you already know other programming languages, then this was like a review for you, but if it is your first time learning to code, then you can see that it wasn't that complicated. Keep on reviewing data types until you feel comfortable.

The next thing we will be learning are variables. I will be uploading the post soon.

Follow for more content like this!!

Top comments (0)