Python Data Types Explained with Examples

Python Data Types with Examples

Python data types: It has several built-in data types that are used to store different kinds of data. Here’s an explanation of the main data types with examples:

Python Data Types: Numeric Types

Integer (int)

Whole numbers, positive or negative, without decimals.

x = 10
y = -5
print(type(x))  # Output: <class 'int'>

Float (float)

Numbers with decimal points or in exponential form.

a = 3.14
b = -0.5
c = 2.5e2  # 2.5 × 10² = 250.0
print(type(a))  # Output: <class 'float'>

Complex (complex)

Numbers with real and imaginary parts (j or J for imaginary part).

z = 3 + 4j
print(type(z))  # Output: <class 'complex'>
print(z.real)   # Output: 3.0
print(z.imag)   # Output: 4.0

Python Data Types: Sequence Types

String (str)

Ordered sequence of characters enclosed in single, double, or triple quotes.

s1 = 'Hello'
s2 = "Python"
s3 = '''Multi-line
string'''
print(type(s1))  # Output: <class 'str'>

List (list)

Mutable, ordered sequence of elements enclosed in square brackets.

my_list = [1, 2.5, 'apple', True]
print(type(my_list))  # Output: <class 'list'>
my_list[0] = 10  # Lists are mutable

Tuple (tuple)

Immutable, ordered sequence of elements enclosed in parentheses.

my_tuple = (1, 2.5, 'apple', True)
print(type(my_tuple))  # Output: <class 'tuple'>
# my_tuple[0] = 10  # This would raise an error (tuples are immutable)

Python Data Types: Mapping Type

Dictionary (dict)

Unordered collection of key-value pairs enclosed in curly braces.

my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(type(my_dict))  # Output: <class 'dict'>
print(my_dict['name'])  # Output: Alice

Python Data Types: Set Types

Set (set)

Unordered collection of unique elements enclosed in curly braces.

my_set = {1, 2, 3, 3, 2}  # Duplicates are removed
print(type(my_set))  # Output: <class 'set'>
print(my_set)  # Output: {1, 2, 3}

Frozen Set (frozenset)

Immutable version of a set.

f_set = frozenset([1, 2, 3])
print(type(f_set))  # Output: <class 'frozenset'>

Python Data Types: Boolean Type (bool)

Represents truth values: True or False.

is_valid = True
is_empty = False
print(type(is_valid))  # Output: <class 'bool'>

Python Data Types: Binary Types

Bytes (bytes)

Immutable sequence of bytes (0-255).

b = b'hello'
print(type(b))  # Output: <class 'bytes'>

Bytearray (bytearray)

Mutable sequence of bytes.

ba = bytearray(b'hello')
print(type(ba))  # Output: <class 'bytearray'>

Memoryview (memoryview)

Memory view object of a byte-based object.

mv = memoryview(b'hello')
print(type(mv))  # Output: <class 'memoryview'>

Checking Data Types

You can check the type of any object using the type() function:

x = 42
print(type(x))  # Output: <class 'int'>

Python Data Types: Type Conversion

Python allows you to convert between types using constructor functions:

# int to float
x = float(5)  # 5.0

# float to int (truncates decimal)
y = int(3.9)  # 3 (not rounded)

# string to int
z = int("10")  # 10

# int to string
s = str(25)  # "25"

Understanding these data types is fundamental to Python programming as they determine what operations can be performed on the data.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *