Python 3 offers a rich set of data types that are used to store various kinds of data. Here’s an overview of the fundamental data types in Python 3:
1. Numeric Types
int
(Integer)- Represents whole numbers, positive or negative, without a fractional component.
- Example:
5
,-42
,0
float
(Floating-Point Number)- Represents real numbers with a fractional component, stored as a decimal point.
- Example:
3.14
,-0.001
,2.0
complex
- Represents complex numbers, which are numbers with a real and imaginary part.
- Example:
3 + 5j
, where3
is the real part and5j
is the imaginary part.
2. Sequence Types
str
(String)- Represents a sequence of characters (text). Strings are immutable, meaning they cannot be changed after they are created.
- Example:
"Hello, World!"
,'Python 3'
list
- Represents an ordered collection of items, which can be of different data types. Lists are mutable, meaning their content can be changed.
- Example:
[1, 2, 3]
,["apple", "banana", "cherry"]
tuple
- Similar to a list, but tuples are immutable. Once a tuple is created, its content cannot be modified.
- Example:
(1, 2, 3)
,("apple", "banana", "cherry")
range
- Represents an immutable sequence of numbers, typically used for looping a specific number of times.
- Example:
range(5)
generates the numbers0, 1, 2, 3, 4
.
3. Set Types
set
- Represents an unordered collection of unique items. Sets are mutable.
- Example:
{1, 2, 3}
,{"apple", "banana", "cherry"}
frozenset
- Represents an immutable version of a set. Once created, the items in a frozenset cannot be modified.
- Example:
frozenset([1, 2, 3])
,frozenset({"apple", "banana", "cherry"})
4. Mapping Types
dict
(Dictionary)- Represents a collection of key-value pairs, where each key is unique. Dictionaries are mutable.
- Example:
{"name": "Alice", "age": 25}
,{1: "one", 2: "two", 3: "three"}
5. Boolean Type
bool
- Represents a logical value, either
True
orFalse
. - Example:
True
,False
- Represents a logical value, either
6. Binary Types
bytes
- Represents an immutable sequence of bytes (8-bit values).
- Example:
b"Hello, World!"
bytearray
- Represents a mutable sequence of bytes.
- Example:
bytearray(b"Hello, World!")
memoryview
- Provides a view of another binary sequence, such as
bytes
orbytearray
, without copying the data. - Example:
memoryview(b"Hello, World!")
- Provides a view of another binary sequence, such as
7. None Type
NoneType
- Represents the absence of a value or a null value. There is only one value of this type, which is
None
. - Example:
None
- Represents the absence of a value or a null value. There is only one value of this type, which is
8. User-Defined Types
- Classes: In Python, you can define your own data types using classes. These custom types can be as simple or complex as needed for your application.
Type Checking and Conversion
- Type Checking: You can check the type of a variable using the
type()
function.- Example:
type(5)
returns<class 'int'>
.
- Example:
- Type Conversion: Python provides functions to convert between data types.
- Example:
int("123")
converts a string to an integer,str(123)
converts an integer to a string.
- Example:
Data Type | Description | Example |
int | Integer (whole number) | x = 10 |
float | Floating-point number (decimal) | y = 10.5 |
complex | Complex number | z = 3 + 5j |
bool | Boolean (True or False) | is_valid = True |
str | String (text) | name = “Alice” |
list | List (ordered, mutable collection) | fruits = [“apple”, “banana”] |
tuple | Tuple (ordered, immutable collection) | coordinates = (10, 20, 30) |
dict | Dictionary (key-value pairs) | person = {“name”: “Alice”} |
set | Set (unordered, unique items) | unique_numbers = {1, 2, 3} |
frozenset | Immutable version of a set | frozen_numbers = frozenset([1, 2]) |
bytes | Immutable sequence of bytes | byte_data = b”Hello” |
bytearray | Mutable sequence of bytes | byte_array_data = bytearray(5) |
memoryview | View on binary data | memory_view = memoryview(b”Hello”) |
NoneType | Absence of value | value = None |
These are the fundamental data types in Python 3. Understanding them is crucial for writing efficient and effective Python code.
You made certain fine points there. I did a search on the matter and found a good number of people will have the same opinion with your blog.