Data Types in Python

When we use variables in Python, the data we store inside them can be of different types. For example, a number, some text, or a True/False value. These are called data types.

Why are Data Types Important?

Data types help Python understand how to handle the information stored in a variable. For example, you can add numbers together, but you can’t add text and numbers directly.

Common Data Types in Python

Here are the most commonly used data types in Python:

  1. Integer (int): Whole numbers like 10, -5, or 1000.
  2. Float: Numbers with a decimal point like 3.14, -0.5, or 100.99.
  3. String (str): Text enclosed in quotes, like "Hello" or 'Python'.
  4. Boolean (bool): True or False values.

Examples of Data Types

Here are some examples of how to use these data types:

# Integer
age = 25
print(age)  # Output: 25

# Float
height = 5.9
print(height)  # Output: 5.9

# String
name = "John"
print(name)  # Output: John

# Boolean
is_student = True
print(is_student)  # Output: True
  

Checking the Data Type

In Python, you can use the type() function to check the type of any variable. For example:

age = 25
print(type(age))  # Output: <class 'int'>   

name = "John"
print(type(name)) # Output: <class 'str'>

  

Key Points to Remember

Summary

Data types are an important part of Python programming. They help us store and manage different kinds of data.