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:

  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.

Function : n/a

No new function will be introduced in this lesson.

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

>>> 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).

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

  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.

Tips

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.

Example 2

>>> 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.

Advanced Concepts (Optional)

There are many other types of data in Python, for example:

  1. bytes
  2. complex
  3. bool
There are many other data types in Python under different domains, such as date, time, gps location etc. In future you may find it beneficial to take a look at those if you work on an advanced project. We will keep it simple for now.

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.

Example 3

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

True

Next Lesson:

Type Conversions

051ZH1406_7SHDG


Andreas Rickenbacher

Schnee und eis auf dem Silsersee

Schnee und eis auf dem Silsersee by Andreas Rickenbacher

“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.
Sandra Files