Python Data Types
Python supports several built-in data types, each designed to handle specific kinds of information. Here’s an overview:
Data Type | Class(es) | Description |
---|---|---|
Numeric | int , float , complex | Stores numeric values, including integers, decimals, and complex numbers. |
String | str | Holds sequences of characters. |
Sequence | list , tuple , range , bool , set , frozenset | Stores collections of items, including ordered (like lists, tuples) and unordered (like sets) collections. |
Mapping | dict | Maintains data in key-value pairs. |
None Type | NoneType | Represents the absence of a value or null. |
Boolean | bool | Represents logical values: True or False . |
Set | set , frozenset | Stores a collection of unique items. |
In Python, each data type is implemented as a class. These classes provide built-in methods and functionalities that allow you to interact with and manipulate data efficiently.
To write efficient programs, it’s essential to choose the right data type. Let’s explore the various data types available in Python.
1. Numeric Data Types
-
Integer (
int
): Whole numbers (positive or negative), without a decimal point.marks = 20 Rno = 12 x = -4
-
Float (
float
): Numbers with decimals. Even an integer stored as a float is represented with a decimal (e.g.,9.0
).weight = 56.8 y = -10.5
-
Complex (
complex
): Numbers with a real and imaginary part.distance = 2 + 1j
2. Boolean Data Type
The Boolean type represents truth values: True
or False
. You can also use 1
for True
and 0
for False
.
state = False
print(type(state)) # Output: <class 'bool'>
3. Dictionary Data Type
A dictionary is an unordered set of key-value pairs, defined using curly braces {}
. Each key must be unique, while values can repeat.
medals = {'TeamA': 25, 'TeamB': 32, 'TeamC': 14}
4. Sequence Data Types
-
String (
str
): Holds any type of characters, enclosed in single (' '
) or double quotes (" "
).Houseno = 'B-19' name = "Neer"
-
List (
list
): A compound data type holding a group of values, defined using square brackets[ ]
. Lists are mutable.student = [12, 'Niyasa', 60.5] vowels = ['a', 'e', 'i', 'o', 'u'] marks = [21, 20, 17, 24]
-
Tuple (
tuple
): Similar to lists but immutable, defined using round brackets( )
.days = ('Mon', 'Tue', 'Wed')