Python Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, categorized into several groups.
1. Text Type: str
String literals in Python are surrounded by either single quotation marks, or double quotation marks.
x = "Hello World"
y = 'Hello World'
Strings are arrays of bytes representing Unicode characters, allowing you to iterate over them and access individual characters using indexing.
2. Numeric Types: int, float, complex
There are three distinct numeric types in Python:
int(Integer): A whole number, positive or negative, without decimals, of unlimited length.x = 20 y = -3255522float(Floating point number): A number, positive or negative, containing one or more decimals.x = 20.5 y = -35.59complex(Complex number): Written with a “j” as the imaginary part.x = 1j y = 3+5j
3. Sequence Types: list, tuple, range
Sequences allow you to store multiple values in an organized and efficient way.
list: A collection which is ordered and changeable (mutable). Allows duplicate members. Created with square brackets[].x = ["apple", "banana", "cherry"]tuple: A collection which is ordered and unchangeable (immutable). Allows duplicate members. Created with parentheses().x = ("apple", "banana", "cherry")range: Represents an immutable sequence of numbers and is commonly used for looping a specific number of times inforloops.x = range(6) # Represents numbers 0, 1, 2, 3, 4, 5
4. Mapping Type: dict
A dictionary is a collection which is ordered (as of Python 3.7), changeable, and does not allow duplicate keys. Dictionaries are used to store data values in key:value pairs.
x = {"name": "John", "age": 36}
# Accessing a value using its key
print(x["name"]) # Outputs: John
5. Set Types: set, frozenset
set: A collection which is unordered, unchangeable (though you can add/remove items), and unindexed. No duplicate members. Created with curly brackets{}.x = {"apple", "banana", "cherry"}frozenset: Similar to a set, except that its elements are entirely unchangeable (immutable) after creation.x = frozenset({"apple", "banana", "cherry"})
6. Boolean Type: bool
Booleans represent one of two values: True or False. They are essential for conditional statements and logic.
x = True
y = False
# Expressions evaluated as boolean
print(10 > 9) # True
7. Binary Types: bytes, bytearray
These types are used to manipulate binary data (raw bytes).
bytes: Immutable sequences of single bytes.x = b"Hello"bytearray: Mutable counterpart tobytesobjects.x = bytearray(5)
8. Type Checking: type() and isinstance()
You can easily get the data type of any object by using the type() function:
x = 5
print(type(x)) # <class 'int'>
y = "Hello"
print(type(y)) # <class 'str'>
While type() is useful for quick checks, the recommended way to test if an object is of a specific type (especially in production code) is using the isinstance() built-in function. isinstance() takes into account class inheritance, making it more robust.
x = 5
print(isinstance(x, int)) # True
y = [1, 2, 3]
print(isinstance(y, list)) # True
print(isinstance(y, tuple)) # False
Getting the Specific Type
If you need to set the data type specifically, you can use the constructor functions:
x = str("Hello World")
y = int(20)
z = list(("apple", "banana", "cherry"))
Discussion
Loading comments...