Variables and Data Types

Reading
Audio Reader Ready

Remember from Level 1 that variables are like labeled containers or boxes where we can store information. We give each box a name (the variable name) so we can easily find and use the information inside the box.

For example:

Imagine you have a box labeled "daily sales" and you put the number 520 inside. In Python, this would look like:

Python
daily_sales = 520

Here, daily_sales is the variable name, and 520 is the value stored in it.

Interactive Exercise 1:

Think of three things you would want to store in labeled boxes in your house. What would you label those boxes, and what would you put inside the boxes? Share your ideas! (No coding is required)

---

Data Types: What Kind of Information Are We Storing?

In the real world, there are different kinds of items (such as apples, books, colors, etc.), Python also has different data types to represent different kinds of information. Let's look at some of the most common ones:

Integers (int): Whole Numbers

These are positive and negative whole numbers, that is numbers without any decimal points. They are simply the counting numbers.

Examples: -100, -50, -10, 0, 1, 25, 1000, etc.

Think of items like: the number of students in a class, your age in years, the number of apples in a box, the score in a game.

Let’s assign an integer value to a variable in our Python playground as follows:

What is your age?

Python
my_age = 22
print(my_age)

Floats (float): Numbers with Decimal Points

These are numbers that can have a decimal point. They are used to represent measurements or values that might not result in whole numbers.

Examples: 3.14, -2.5, 0.0, 10.99

Think of items that may result in fractions such as the price of an item, your height in meters, the temperature outside, etc. If you think that an item may result in a fractional value, assign a float data type.

Let’s try assigning a float value to a variable:

What is the price of the English textbook?

Python
book_price = 8.55
print(book_price)

Strings (str): Text

Strings are used for sequences of characters (such as letters, numbers, symbols) enclosed in quotation marks. You can use either single ' ' or double " " quotes. Either is used to represent text.

Examples: "Hello Nigeria", 'I am a Python Enthusiast', "My favorite food is plantain.", "255".

Note the following:

  • When you enclose a number in a quote, it becomes a string (text).
  • When you input a value using the input() function, the value becomes a string.

Natural string values include your name, a message you want to display, the title of a book. Think of more.

Let’s assign a string value to a variable:

What is the name of your dog?

Python
dog_name = "Lion"
print(dog_name)

Booleans (bool): True or False

In Python, we use Booleans to represent truth values. They are values that can only be one of two: True or False, Yes or No. They are very useful for making decisions in our code. (Remember how it was used in our guessing game in level 1).

Examples: True, False

The answer to the following questions will result in a Boolean:

  • Is the light switch on? (True or False)
  • Did you finish your homework? (True or False)
  • Are you playing the game? (True or False)

Whenever a value results in Yes or No, we use the Boolean data type to represent it.

Let’s assign a boolean value to a variable:

Is it raining in your area?

Python
raining = True
print(raining)