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:
- Integer (int): Whole numbers like
10
,-5
, or1000
. - Float: Numbers with a decimal point like
3.14
,-0.5
, or100.99
. - String (str): Text enclosed in quotes, like
"Hello"
or'Python'
. - 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
- Integer: Used for whole numbers.
- Float: Used for decimal numbers.
- String: Used for text enclosed in quotes.
- Boolean: Represents True or False values.
Summary
Data types are an important part of Python programming. They help us store and manage different kinds of data.