Every value in Python has a data type, which determines what kind of data it represents and what operations can be performed on it. Some common data types in Python are:
Table of Contents
-
Numeric Data Types
- 1.1 Integer (
int
) - 1.2 Floating-Point (
float
) - 1.3 Complex (
complex
)
- 1.1 Integer (
-
Textual Data Types
- 2.1 String (
str
) - 2.2 String Operations
- 2.1 String (
-
Sequence Data Types
- 3.1 List (
list
) - 3.2 Tuple (
tuple
) - 3.3 Range (
range
)
- 3.1 List (
-
Mapping Data Type
- 4.1 Dictionary (
dict
)
- 4.1 Dictionary (
-
Set Data Type
- 5.1 Set (
set
) - 5.2 Frozenset (
frozenset
)
- 5.1 Set (
-
Boolean Data Type
- 6.1 bool: Boolean Values
-
Binary Types
- 7.1 bytes: Bytes
- 7.2 bytearray: Byte Arrays
-
NoneType
- 8.1 None: The None Object
- Type Conversion (Casting)
- User-Defined Classes
- Checking Data Types
1. Numeric Data Types
1.1 Integer (int
)
Integers represent whole numbers without decimal points. They can be positive or negative.
x = 5
y = -10
1.2 Floating-Point (float
)
Floating-point numbers are numbers with decimal points. They are used to represent real numbers.
pi = 3.14159
temperature = -2.5
1.3 Complex (complex
)
Complex numbers consist of a real part and an imaginary part.
z = 2 + 3j
2. Textual Data Types
2.1 String (str
)
Strings are sequences of characters enclosed in single or double quotes.
name = "Akash"
message = 'Hello, world!'
2.2 String Operations
Strings support various operations like concatenation, slicing, and formatting.
greeting = "Hello"
name = "Akash"
full_greeting = greeting + ", " + name # Concatenation
substring = full_greeting[7:12] # Slicing
formatted_message = f"{greeting}, {name}" # String formatting
3. Sequence Data Types
3.1 List (list
)
Lists are ordered collections that can contain elements of different types.
fruits = ["apple", "banana", "orange"]
3.2 Tuple (tuple
)
Tuples are similar to lists but are immutable.
coordinates = (3, 5)
3.3 Range (range
)
Ranges represent sequences of numbers.
numbers = range(1, 6) # Creates a range from 1 to 5
4. Mapping Data Type
4.1 Dictionary (dict
)
Dictionaries store key-value pairs and allow fast lookup.
person = {
"name": "Akash",
"age": 23,
"city": "Pune"
}
5. Set Data Type
5.1 Set (set
)
Sets are unordered collections of unique elements.
colors = {"red", "green", "blue"}
5.2 Frozenset (frozenset
)
Frozensets are like sets but are immutable.
immutable_colors = frozenset({"red", "green", "blue"})
6. Boolean Data Type
6.1 bool: Boolean Values
Boolean values represent truth (True) or falsehood (False).
is_valid = True
7. Binary Types
7.1 bytes: Bytes
Bytes represent sequences of bytes (integers from 0 to 255).
data = b"hello"
7.2 bytearray: Byte Arrays
Byte arrays are mutable sequences of bytes.
mutable_data = bytearray(data)
8. None Type
8.1 None: The None Object
The None object represents the absence of a value.
result = None
9. User-Defined Classes
You can define your own data types using classes.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
10. Type Conversion (Casting)
Python allows you to convert between different data types using type casting functions.
x = "5"
y = int(x) # Casting to integer
11. Checking Data Types
Python provides the type()
function to determine the data type of a value. This function returns the class type of the argument. Here's how you can use it:
print(type(age)) # Output: <class 'int'>
print(type(temperature)) # Output: <class 'float'>
print(type(name)) # Output: <class 'str'>
print(type(is_student)) # Output: <class 'bool'>
print(type(fruits)) # Output: <class 'list'>
print(type(coordinates)) # Output: <class 'tuple'>
print(type(person)) # Output: <class 'dict'>
print(type(unique_numbers)) # Output: <class 'set'>
Top comments (0)