pdf1
pdf1
Here are detailed examples for each of the Python data types:
1. Numeric Types
a. Integers (int)
Integers are whole numbers, either positive, negative, or zero.
# Example of integer
x = 10
print(type(x)) # Output: <class 'int'>
# Example of float
y = 10.5
print(type(y)) # Output: <class 'float'>
2. Text Type
a. Strings (str)
Strings are sequences of characters enclosed in quotes.
# Example of string
name = "John Doe"
print(type(name)) # Output: <class 'str'>
3. Sequence Types
a. Lists (list)
Lists are ordered collections of items enclosed in square brackets.
# Example of list
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # Output: <class 'list'>
b. Tuples (tuple)
Tuples are ordered, immutable collections enclosed in parentheses.
# Example of tuple
colors = ("red", "green", "blue")
print(type(colors)) # Output: <class 'tuple'>
c. Ranges (range)
Ranges are sequences of numbers.
# Example of range
numbers = range(1, 6)
print(type(numbers)) # Output: <class 'range'>
4. Mapping Type
a. Dictionaries (dict)
Dictionaries are unordered collections of key-value pairs enclosed in curly brackets.
# Example of dictionary
person = {"name": "Jane", "age": 30}
print(type(person)) # Output: <class 'dict'>
5. Set Types
a. Sets (set)
Sets are unordered collections of unique items enclosed in curly brackets.
# Example of set
unique_fruits = {"apple", "banana", "cherry"}
print(type(unique_fruits)) # Output: <class 'set'>
b. Frozen Sets (frozenset)
Frozen sets are immutable sets.
# Example of frozenset
immutable_fruits = frozenset({"apple", "banana", "cherry"})
print(type(immutable_fruits)) # Output: <class 'frozenset'>
6. Boolean Type
a. Boolean (bool)
Booleans are logical values that can be either True or False.
# Example of boolean
is_admin = True
print(type(is_admin)) # Output: <class 'bool'>
7. Binary Types
a. Bytes (bytes)
Bytes are sequences of integers in the range 0 <= x < 256.
# Example of bytes
data = b"Hello"
print(type(data)) # Output: <class 'bytes'>
# Example of bytearray
mutable_data = bytearray(b"Hello")
print(type(mutable_data)) # Output: <class 'bytearray'>
# Example of memoryview
view = memoryview(b"Hello")
print(type(view)) # Output: <class 'memoryview'>
8. None Type
a. None (NoneType)
None is a special type with a single value, None.
# Example of None
no_value = None
print(type(no_value)) # Output: <class 'NoneType'>
These examples cover all the built-in data types in Python, demonstrating how to declare and
use them in code.
⁂