Data Types In Python
Python Data Types and Constants: A Comprehensive Guide
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.
1. Built-in Data Types
Python has the following data types built-in by default, in these categories:
| Category | Types |
| :--- | :--- |
| Text Type | str |
| Numeric Types | int, float, complex |
| Sequence Types | list, tuple, range |
| Mapping Type | dict |
| Set Types | set, frozenset |
| Boolean Type | bool |
| Binary Types | bytes, bytearray, memoryview |
| None Type | NoneType |
2. Getting the Data Type
You can get the data type of any object by using the type() function:
x = 5
print(type(x)) # Output: <class 'int'>
3. Numeric Types
Python has three distinct numeric types: int, float, and complex.
x = 1 # int
y = 2.8 # float
z = 1j # complex
[!NOTE] Floats can also be scientific numbers with an "e" to indicate the power of 10.
x = 35e3
4. Sequence Types: List, Tuple, and Range
List
Lists are used to store multiple items in a single variable. They are ordered and changeable.
fruits = ["apple", "banana", "cherry"]
Tuple
Tuples are used to store multiple items in a single variable. They are ordered and unchangeable.
coordinates = (10.0, 20.0)
Range
The range() function returns a sequence of numbers, starting from 0 by default.
x = range(6)
5. Mapping Type: Dictionary
Dictionaries are used to store data values in key:value pairs. They are ordered (as of Python 3.7) and changeable.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
6. Boolean Type
Booleans represent one of two values: True or False.
print(10 > 9) # Output: True
is_active = True
7. Type Casting
If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Python Constants: Best Practices
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be altered later.
1. How to Define Constants
In Python, constants are usually declared and assigned in a module (a separate file).
[!IMPORTANT] Technical Note: Python does not actually have built-in "true" constants that are protected from being changed. Instead, we use a naming convention to tell other developers that a variable should be treated as a constant.
2. Naming Convention
Constants are usually written in all CAPITAL LETTERS with underscores separating words.
PI = 3.14
GRAVITY = 9.8
MAX_USERS = 100
API_KEY = "your_secret_key"
3. Using a Separate File
The best practice is to create a file named constants.py and store all your constants there.
constants.py
# constants.py
PI = 3.14
GRAVITY = 9.8
Step 2: Import in your main code
import constants
print(constants.PI)
print(constants.GRAVITY)
4. Rules for Constants
- Meaningful Names: Use names that describe the value (e.g.,
WIDTHinstead ofW). - Never Reassign: Once a constant is defined, do not change its value in the code.
- Global Scope: Constants are typically defined at the top of a file or in a dedicated module.
5. Literal Constants
Literals are the actual values assigned to variables or constants.
| Type | Example |
| :--- | :--- |
| Numeric Literals | 10, 3.14, 0b101 (binary) |
| String Literals | "Hello", 'Python' |
| Boolean Literals | True, False |
| Special Literals | None |