Python Datatypes 🐍

Python Datatypes 🐍

What are they and how do they work?

So, you are slowly getting started with Python but have no idea what all the datatypes are and how you can use them? We’ve got you covered in this article.

We’ll go over the most common datatypes in this article. Not the datatypes you’ll rarely use, like Complex.


Table of Contents


Text

This is probably one of the most common datatypes if you are making an application. A text variable is called a string. You can easily make one by simply putting a sentence between quotes (“/’)

a_string = "Hello, world!"
print(a_string) # Hello, world!

Awesome! You’ve create a string now. You can do a lot with string. Here’s a list of what you can do:

  • Make them full CAPS or lower case.
  • Print them!
  • Split them up into c h a r a c t e r s
  • Too much, check this page from W3Schools

Numeric

This is also a very common datatype. We use numbers everywhere in programming. To store the age of a person or to simply count up in a loop.

There are two common numeric datatypes:

  • int (integer)
  • float (floating point number)

Integers

An integer is just a plain numer like the number 7. It doesn’t have a decimal and can be as high or as low as you want (ofcourse, there are limits but you’ll rarely hit them).

number = 7  # It doesn't have quotes, it's just a plain number
print(number) # 7

Float

A float is a more complex type of number. It has a decimal in it. An example of a float is 6.9. It’s close to 7 but not quite yet.

floating_number = 6.9  # Again, no quotes! But it needs a .
print(floating_number) # 6.9

Why do we use them? We use them when we need more precision than a whole integer.

What can you do with int and float? Calculate things!

Booleans

True, Talse. Those are boolean values! It’s a way to express that something is a thing (True) or that it isn’t (False).

boolean = True  # Setting the variable
print(boolean)  # True

One of it’s main purposes is the use in if, elif, else statement (more on that in a future article). A quick example:

beautiful_weather = True
if beautiful_weather == True: # beautiful_weather equals True! 
    print("The weather is beautiful!") # <--- This get printed
else:  #This will get executed it beautiful_weather was False
    print("It's raining, noooo!") # <--- This doesn't get printed

As you can see, the variable is set to True. This means that the if is True so it will print it in your console.

Sequences

A sequence is a datatype that has an order. You can see this as a shopping list, you put this on a list in an order. In Python we have 2 types of sequences. In this part of the article we’ll go over both of them.

Tuples

Let’s start with the one with the strangest name 😊. A so-called Tuple is a collection of data that usually belongs to eachother. Let’s give you an example:

person_tuple = ('Daniel', 18, 'M')  # I store a name, age and gender in this tuple
print(person_tuple)                 # ('Daniel', 18, 'M')

As you can see, the data belongs to each other. The data is stored in order and will not get shuffled around like in sets (that we’ll talk about later in this article.). It’s also an immutable data type which means that you cannot change it.

Lists

A list is familiar to a Tuple but the key difference is that it’s a very dynamic data type. You can add, remove, re-order or do whatever you want with the data that’s inside of it. It looks the same but it only uses difference brackets.

fruits = ["Apple", "Banana", "Orange"]
print(fruits) # ["Apple", "Banana", "Orange"]

Dictionaries

A dictionary is a collection of data. It’s like a phone number book where you store information about a person linked by their name, for example.

It’s an easy to use and understand data type so let’s dive into an example:

persons = {
    "Daniel": {
        "age": 18,
        "school": "Hogeschool Leiden",
        "study": "Software Engineering"
    }
}

# Let's get my age, you can simply use:
print(persons["Daniel"]["age"]) # 18

Sets

A set is an unorder data type that doesn’t have indexes. This means that you can’t use things like [1] to get the second item from the collection.

fruits = {"Apple", "Banana", "Orange"}
print(fruits) # {"Banana", "Apple", "Orange"}

You can use loops to go through them and use them to simply store data. If you want more information on what you can exactly do with them check this page from W3Schools

That’s it for todays article! See you in the next one 👋


Originally posted on 2020-09-23T11:30:08.000Z over at Krossor, that’s what together.codes was called before.

Original author “Daniel”


Share this article!