Lesson 3: Data Types | Learn and Practice with HolyPython.com (original) (raw)

Although there are many different data types in Python, some are much more common than others. Some of the basic and most common data types in Python are:

  1. int : this type of data consists of numbers and particularly integers.
  2. float : this type of data refers to numbers with decimals
  3. str : standing for string, an str type of variable keeps data as a text string.

This lesson has relatively more reading parts. If you are a CS major or if you have programming experience you might want to skim it or skip through. Otherwise if you don’t have programming exposure it’s a great opportunity to grasp some of the most fundamental programming concepts (and in particular Python) which you will use while coding in future.

Function : type()

type function can be used to find out data type of data in Python.

Used Where?

Syntax do(s)

  1. Be aware of your data types their formats, usage areas and their capabilities.

Syntax don't(s)

  1. str values are always in quotes while int, float and bool values are not. Make sure you don't type bool values in quotes while assigning it to a variable. Same goes for int and float variables.

Example 1: String and Integer

>>> myCars_number = 3
>>> myCars_color = “Vermont Bronze, Deep Forest Green, Graphite Black”
>>> print (myCars_number)
>>> print (myCars_color)

3
Vermont Bronze, Deep Forest Green, Graphite Black

In this example we created 2 types of variables and then we printed them. myCars_number is an integer (int) and myCars_color is a string (str).

Also, nice colors no? Maybe a bit masculine. Which color would you like your car(s) to be? 🤔

** Let’s look at the execution order of the code above and see what happens exactly:

  1. By line 1, myCars_number variable gets created and 4 is assigned to it. (Note though, you don’t see anything as this is an internal process. It all happens in the kitchen and unless you print this variable you won’t see any output.)
  2. By line 2, another variable gets created and assigned a value.
  3. By line 3 and 4 variable that were created earlier get printed on the display.

Why Python Needs Data Types?

It’s not only Python but pretty much any programming language that makes use of data types. Here are some of the reasons why with a few interesting analogies.

So why does Python need data types at all? This question may arise to the beginner programmer.

1- When you think about the kitchen of your computer, memory makes allocation and cpu makes executions. In other words memory stores data and cpu processes them (cooks, cleans, chops and prepares a delicious meal. Well, sometimes delicious.) So if it’s going to help you remember think of memory as the fridge and CPU as the oven.

Thanks to the data types, your computer knows where data should be stored, how much it should allocate and what kind of operations it should expect concerning data processes.

2- So if variables (previous Python lesson) are containers for raw data data type is more like the fundamental categories that has sections in the fridge like veggies, fruits, meat and diary.

3- So, just as you know if it’s a fruit in the container, refrigeration might not be needed, if it’s meat, you might have to freeze it for long-term use etc. computer figures out certain treatments and processes based on data type. If it’s an integer it won’t be required to have decimals, if it’s a string math operations won’t be performed on them etc. It’s a beautiful system that works in harmony and makes everything more efficient and tidier.

Tips & Roadmap

In this lesson we will focus on the most fundamental data types which can be seen as building blocks in Python. These are:

Now let’s create a float example.

Example 2: Float Data Type

>>> Miami_temp_today = 103.40
>>> print(Miami_temp_today)

In this example we created a float variable and then printed it on the output screen / programming console.

As long as there are decimals involved it’s a float type in Python and not integer. Even if value is 100.0, decimal hints us that it’s a float type data. Also, you can always double check with the type function. There will be more examples regarding that in the next lesson.

Advanced Concepts (Optional)

As we mentioned, there are other types of data in Python and programming in general, for example:

  1. bytearray
  2. datetime

We will keep it simple for now and not get into those. You will know it when you need them and it won’t be too complicated once you have a solid programming base.

You can also use type function when you aren’t sure of a data’s data type. It will return a string specifying data type accordingly.

Example 3: Using type function

>>> Miami_temp_today = “103.40”
>>> print(type(Miami_temp_today))

Our data is in quotes which signifies strings in Python. So, type function returned string class. Just a fancy way of saying it’s a string in Python.

Now let’s see a simple boolean (bool) example. Remember, it can only take two values ever: True or False, and not both of them at the same time. (At least in traditional computing, soon consumer quantum computers will introduce qubits which can hold True and False or 1 and 0 simultaneously which is kind of mind blowing. So, take it easy 🙂. Here is a nice definition by Computer Hope: Quantum Computer)

Okay, let’s not deviate from our target. Here is a simple boolean example:

Example 4: Boolean data type

>>> hungry_or_not = True
>>> print(hungry_or_not)

* bool type in Python shouldn’t be confused with string “True” which is a string that consists of 4 letters.

** bool type is not a string it doesn’t have 4 letters in Python it only has meaning of True or False. If you are new to programming this may seem abstract but later one realizes how powerful (and useful) the concept is.

From manufacturing to telecommunication, reproduction to nature events bool types can be observed everywhere in life and business.

A couple more words on boolean (bool in Python)

Boolean is a relatively common but slightly more complicated data type. Although it’s a very simple concept, we normally don’t realize it directly in real life which makes it a bit hard to comprehend sometimes.

You can think of boolean type as your light switch. It’s always either on or off. In Python it’s either True or False.

Note that, boolean type True or False is never in quotes which separates it from strings. So in short, True and “True” are two different data types in Python, latter is a str type while former is a bool.

In intermediate and advanced concepts there are many situations where booleans will prove to be very valuable and we will use them in the upcoming lessons.

Example 5: Multiple booleans

>>> switch1 = True
>>> switch2 = False

>>> print(switch1 * switch0)

* This may seem confusing but you can do math operations on bool type data. True will be treated as 1 and False as 0. This is accurate with the bit representation on very low level programming which deals with computer on the hardware level.

** Imagine if you have two switches going to the same lightbulb. if one is off it won’t turn on.

Well, that about does it for Python data types. We hope you enjoyed this lesson and found it useful. Don’t forget to check out our online and interactive Data Type Python Exercises and next lesson: Python Type Conversions which will build on Python Data Types.

“Have you installed Python yet?”

Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.