Lesson 3: Python Data Types
- ESTIMATED TIME: 15 mins.
- DIFFICULTY: Beginner
- FUNCTIONS: print()
- 4 EXERCISES
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:
- int : this type of data consists of numbers and particularly integers.
- float : this type of data refers to numbers with decimals
- str : standing for string, an str type of variable keeps data as a text string.
Function : type()
type function can be used to find out data type of data in Python.
Used Where?
- Data is used at every point of programming and it can be important to understand what type of data you’re dealing with and the implications that come with it. There are many data types but only a few of them are commonly used by every programmer.
- int is used where numbers are needed to be stored, float is used when decimals are needed. str stores data in a string format and can include any letters, symbols or numbers but int and float can not include symbols or letters and they are consisted of numbers.
- bool type on the other hand consists of only two values: True or False. It’s important to note that while bool type is being initiated or assigned True or False values won’t be in quotation marks. String values, on the other hand, are always assigned in quotes.
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:
- 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.)
- By line 2, another variable gets created and assigned a value.
- 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:
- integer, string and float (in Python named int, str and float).
- boolean (named bool in Python) is another fundamental type and it’s usage usually increases after intermediate topics like conditional statements and for loops.
- These are very basic data types and also most commonly used ones but there are many other data types such as complex, byte memoryview etc. which are much less commonly used.
- Another point we will see in next lesson is data type conversions. When it’s sensible it’s possible to convert between data.
- For example “1” is a string but it can also be converted to integer 1.
- 2.65 is a float but it can also be converted to integer, decimals will be dropped and it will be 2.
- However, “Hello World!” can’t be converted to integer nor float. It doesn’t make any sense.
Now let’s create a float example.
Example 2: Float Data Type
>>> Miami_temp_today = 103.40
>>> print(Miami_temp_today)
103.40
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:
- bytearray
- datetime
- If you are working on advanced mathematics, encryption, network engineering etc. niche domains you might take advantage of subtle yet significant benefits another data type such as bytearray offers.
- If you are working with time series which is critical in some fields such as finance and energy industry you would get familiar with datetime data type and use it regularly. If you are familiar with cell format in Excel different type of dates are one of the options. So, it’s somewhat similar. With correct datetime format you can sort dates as ascending or descending or extract days, months, weekdays, years etc.
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))
class “string”
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)
True
* 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)
0
* 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.
Next Lesson: Type Conversions