Common Data Types in Python
Python is a versatile programming language that supports a wide range of data types. These data types allow you to store and manipulate different kinds of information in your programs. Here are the most common data types in Python:
1. Numeric Data Types
Python has three main numeric data types:
-
Integers (int): Integers are whole numbers, both positive and negative, without any decimal places. For example:
42
,-17
,0
. -
Floating-Point Numbers (float): Floating-point numbers are numbers with decimal places. They can represent both integers and non-integer values. For example:
3.14
,-2.5
,0.0
. -
Complex Numbers (complex): Complex numbers are numbers with both real and imaginary parts. They are represented using the form
a + bj
, wherea
is the real part andb
is the imaginary part. For example:2 + 3j
,-1 + 0j
.
2. Boolean Data Type
The boolean data type, represented by the keywords True
and False
, is used to represent logical values. Booleans are often used in conditional statements and logical operations.
3. Text Data Types
Python has two main text data types:
-
Strings (str): Strings are sequences of characters, which can include letters, numbers, and special characters. Strings are enclosed in single quotes (
'
), double quotes ("
), or triple quotes ('''
or"""
). For example:'Hello, world!'
,"Python is awesome"
,'''This is a multiline string.'''
. -
Bytes and Byte Arrays (bytes and bytearray): Bytes and byte arrays are used to represent binary data, such as images, audio files, or network packets. Bytes are immutable, while byte arrays are mutable.
4. Sequence Data Types
Python has several sequence data types, which are used to store collections of items:
-
Lists (list): Lists are ordered collections of items, which can be of different data types. Lists are enclosed in square brackets (
[]
). For example:[1, 2, 3]
,['apple', 'banana', 'cherry']
. -
Tuples (tuple): Tuples are also ordered collections of items, but they are immutable, meaning their elements cannot be modified after creation. Tuples are enclosed in parentheses (
()
). For example:(1, 2, 3)
,('red', 'green', 'blue')
. -
Ranges (range): Ranges are used to represent a sequence of numbers. They are commonly used in
for
loops and other iterative operations. For example:range(5)
(represents the numbers 0, 1, 2, 3, 4).
5. Mapping Data Types
Python's main mapping data type is the Dictionary (dict). Dictionaries are unordered collections of key-value pairs, where each key must be unique. Dictionaries are enclosed in curly braces ({}
). For example: {'name': 'John', 'age': 30, 'city': 'New York'}
.
6. Set Data Types
Python has two set data types:
-
Sets (set): Sets are unordered collections of unique elements. Sets are enclosed in curly braces (
{}
), similar to dictionaries, but without key-value pairs. For example:{1, 2, 3}
,{'apple', 'banana', 'cherry'}
. -
Frozen Sets (frozenset): Frozen sets are immutable versions of sets, meaning their elements cannot be modified after creation.
These are the most common data types in Python. Each data type has its own characteristics and use cases, and understanding them is crucial for writing effective and efficient Python code.