Basic data types
In Python, data types define the kind of information you can store and work with. Different tasks require different types of data, such as text, numbers, or simple yes-or-no values.
This lesson introduces Python’s most common basic data types and their uses.
Common data types
1. Strings str
str
A string is a sequence of characters used to represent text. Strings are created by enclosing text in either single '
"
Examples:
name = "Alice"
greeting = 'Hello, world!'
Usage: Strings are ideal for handling any text-based data, such as names, messages, or sentences.
2. Integers int
int
An integer is a whole number—positive, negative, or zero—without a decimal point. Python automatically recognizes integers when you assign them.
Examples:
age = 25
score = -10
Usage: Integers are used for counts, ages, IDs, or any data that does not require fractions or decimals.
3. Floats float
float
A float is a number that includes a decimal point. These are useful for representing values that require precision, such as measurements or financial data.
Examples:
price = 19.99
pi = 3.14159
Usage: Floats are used for weights, temperatures, prices, or any calculations that involve non-integer values.
4. Booleans bool
bool
A Boolean represents one of two possible values: True
False
Examples:
is_student = True
is_admin = False
Usage: Booleans are used to check conditions, control program flow, and represent simple yes-or-no states, like whether a user is logged in or a switch is turned on.
Example code
Here’s how these data types look and function in Python:
# String
name = "Alice"
print(name) # Output: Alice
# Integer
age = 30
print(age) # Output: 30
# Float
height = 5.9
print(height) # Output: 5.9
# Boolean
is_member = True
print(is_member) # Output: True
Checking a data type
Python provides the type()
print(type(name)) # Output:
print(type(age)) # Output:
print(type(height)) # Output:
print(type(is_member)) # Output:
Summary of data types
Type | Example | Description | Common Uses |
---|---|---|---|
String | "Alice" |
Text-based data, enclosed in quotes | Names, messages, or sentences |
Integer | 25 , -10 |
Whole numbers, positive or negative | Counts, ages, or IDs |
Float | 19.99 , 3.14159 |
Numbers with a decimal point | Measurements, prices, or scientific data |
Boolean | , False |
Logical values representing yes/no conditions | Conditions, control flow, or flags |
Why are data types important?
Data types help Python understand how to store and manipulate your information. For example:
- Text needs different operations than numbers (e.g., concatenation vs. addition).
- Floats enable precision in calculations that integers can’t handle.
- Booleans simplify decision-making in your programs.
Conclusion
Understanding these basic data types is essential to writing effective Python code. By choosing the right data type for your needs, you make your programs more efficient and easier to maintain.
Start experimenting with strings, integers, floats, and booleans to see how they work in practice!