Data Types Overview

Acadestine

What is a Data Type?

If you have a storage box in your garage, you probably wouldn't store liquid soup in a cardboard box, and you wouldn't store heavy car parts in a paper envelope. Different items require different kinds of containers.

The same rule applies in programming. Inside your computer's memory, Python doesn't just store "data" it categorizes every piece of information into a specific Data Type.

A data type tells Python two critical things: 1. What kind of value is stored inside the variable (e.g., text, a whole number, a decimal, or a true/false condition). 2. What you are allowed to do with that data. (For example, you can multiply two numbers together, but you cannot multiply two sentences of text!)

Python is smart enough to look at whatever data you assign to a variable and figure out its type automatically.

Numbers: Integers and Floats

When working with numeric data, Python splits numbers into two distinct data types depending on whether they have a decimal point:

  1. Integers (int): These represent whole numbers both positive and negative without any fractions or decimals. Examples include 42, -7, 0, and 1000000.
  2. Floats (float): These represent numbers that contain a decimal point. Examples include 3.14, -0.05, and 99.99.

Even if a float ends in zero (like 5.0), Python treats it as a float rather than an integer because of the decimal point. This distinction matters because computers handle whole numbers and decimals using different math hardware in your processor.

Let's look at how Python stores and handles numeric data types:

Console

        

Strings (Text Data)

We have already used strings extensively when printing messages to the terminal, but let's officially define them.

A String (str) is a data type used to represent human-readable text. Any sequence of characters wrapped inside single (') or double (") quotation marks is treated as a string by Python.

A common beginner trap is confusing text numbers with actual math numbers:

Console

        

Even though both variables represent the concept of "five," Python treats them completely differently because of the quotation marks. You can perform math on integers and floats, but trying to do math on a string will cause an error!

Booleans (True or False)

Sometimes data isn't a number or a piece of text it is simply a condition: Yes or No, On or Off, True or False.

In Python, this data type is called a Boolean (bool). A boolean can only ever hold one of two values: True or False.

Notice the capitalization: In Python, True and False must always start with an uppercase letter. If you write true or false in lowercase, Python will throw a NameError because it thinks you are typing a variable name.

Booleans are essential for controlling the logic of your programs (like checking if a user is logged in or if a game is over):

Console

        

The Special None Type

What happens when you want to create a variable, but you don't have any data to put inside it yet? You can't leave it completely blank, so Python provides a special data type called None (belonging to the NoneType class).

None represents the intentional absence of a value or an empty placeholder container. It means: "This variable exists, but right now it holds absolutely nothing."

Console

        

Lesson Summary

You have explored the fundamental building blocks of Python data! Let's review what you learned:

  • Data Types: Python categorizes every piece of information into a specific type to determine what can be stored and what operations can be performed.
  • Integers (int): Whole numbers without decimals (positive, negative, or zero).
  • Floats (float): Numbers containing a decimal point.
  • Strings (str): Text characters wrapped in quotation marks. Remember that numbers inside quotes are treated as text, not math numbers!
  • Booleans (bool): Binary status flags that can only be True or False (always capitalized).
  • NoneType (None): A special placeholder representing the intentional absence of a value.

Understanding data types is crucial because sending the wrong type of data into a function or math operation will cause your program to crash.