Lesson 3: Data Types
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 : n/a
No new function will be introduced in this lesson.
- 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.
1) Be aware of your data types their formats, usage areas and their capabilities.
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.
>>> myCars_number = 3
>>> myCars_color = “Vermont Bronze, Deep Forest Green, Graphite Black”
>>> print (myCars_number)
>>> print (myCars_color)
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).
** Let’s look at the execution order of the above code and what exactly happens:
- 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.
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. Thanks to the data types, your computer knows how much memory should be allocated and what kind of operations should be expected concerning a particular data.
2- A good metaphor is containers for your groceries… You can think of data types as different containers. You can put fresh food in some and cans in the other, soaps in another one etc.
3- On the other hand, if your data is an integer, computer doesn’t worry about converting it to upper or lower case or finding the vowels in it. If your data is a string, then arithmetic operations, square roots, base conversions etc. will be irrelevant.
Now let’s create a float example.
>>> Miami_temp_today = 103.40
In this example we created a float variable and then printed it on the output screen.
Advanced Concepts (Optional)
There are many other types of data in Python, for example:
Let’s check out boolean (bool) type. It’s also a relatively common but slightly more complicated data type. Although it’s a very simple concept, we don’t observe 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.
In below example, although it might resemble to a string the value True without quotes makes the variable a boolean and it’s different than str type. Bool can only take two value: True or False.
>>> hungry_or_not = True
Schnee und eis auf dem Silsersee by Andreas Rickenbacher