Comparison: Python Data Types

The chart provides a comparison of different data types available in Python, including integers, floats, complex numbers, booleans, strings, bytes, bytearrays, ranges, lists, tuples, sets, frozensets, and dictionaries. Each data type is described in terms of its functionality, mutability (whether it can be changed after creation), and examples demonstrating its usage. This comparison helps understand the characteristics and usage of each data type in Python programming.

Data TypeDescriptionMutabilityExample(s)
Integers (int)Represents whole/integral numbersImmutableage = 25, quantity = -10, population = 789000
Float (float)Represents decimal/floating point numbersImmutabletemperature = 98.6, pi = 3.14159, price = 9.99
Complex (complex)Represents complex numbersImmutablez = 3 + 4j, w = -2j, c = complex(2, -5)
Boolean (bool)Represents logical values (True or False)Immutableis_sunny = True, has_passed_exam = False, is_valid = True
Strings (str)Represents sequences of charactersImmutablename = "John", address = '123 Main Street', message = """This is a multiline string"""
Bytes (bytes)Represents sequences of byte valuesImmutableb = bytes([65, 66, 67, 68]), binary_data = bytes.fromhex('2E4F')
Bytearray (bytearray)Represents sequences of byte valuesMutableba = bytearray([10, 20, 30]), ba.extend([40, 50, 60])
Range (range)Represents a range of valuesImmutabler = range(10), r1 = range(1, 10, 2), r2 = range(5, 25, 5)
Lists (list)Represents an ordered collection of objectsMutablenumbers = [1, 2, 3, 4, 5], names = ['Alice', 'Bob', 'Charlie'], mixed_list = [10, 'apple', True, 3.14]
Tuples (tuple)Represents an ordered collection of objects (immutable)Immutablecoordinates = (42.360, -71.058), dimensions = (10, 20, 30)
Sets (set)Represents an unordered collection of unique objectsMutables = {1, 2, 3, 4, 5, 6}, s2 = {'apple', 'banana', 'orange'}
Frozenset (frozenset)Represents an unordered collection of unique objects (immutable)Immutablefs = frozenset(s), fs2 = frozenset({'apple', 'banana', 'orange'})
Dictionaries (dict)Represents a group of key-value pairsMutabled = {101: 'durga', 102: 'ramu', 103: 'hari'}, employee = {'name': 'John', 'age': 30, 'salary': 50000}

This chart provides a detailed comparison of various data types in Python along with additional examples for each.

Leave a comment