October 13, 2024

Python Data Types

Python has a variety of built-in data types that allow you to store and manipulate different kinds of data. Understanding these data types is fundamental to programming in Python. Here’s an overview of the primary data types in Python:

1. Numeric Types

Python supports several types of numbers:

  • Integer (int):
    • Whole numbers without a fractional component.
    • Example: x = 10, y = -3
    python

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

Floating-Point (float):

  • Numbers with a decimal point (fractional component).
  • Example: pi = 3.14, g = -9.81
python

pi = 3.14
g = -9.81
print(type(pi)) # Output: <class 'float'&gt
;

Complex (complex):

  • Numbers with a real and an imaginary component, denoted by j.
  • Example: z = 2 + 3j
python

z = 2 + 3j
print(type(z)) # Output: <class 'complex'>

2. Sequence Types

These data types store collections of items.

  • String (str):
    • A sequence of characters enclosed in single, double, or triple quotes.
    • Example: name = "Alice", greeting = 'Hello, World!'
    python


name = "Alice"
greeting = 'Hello, World!'
print(type(name)) # Output: <class 'str'>

List (list):

  • An ordered, mutable collection of items, which can be of different types.
  • Lists are defined by square brackets [].
  • Example: numbers = [1, 2, 3, 4, 5], mixed = [1, "two", 3.0]

“`python
numbers = [1, 2, 3, 4, 5]
mixed = [1, "two", 3.0]
print(type(numbers)) # Output: <class 'list'>

Tuple (tuple):

  • An ordered, immutable collection of items, which can be of different types.
  • Tuples are defined by parentheses ().
  • Example: coordinates = (10, 20), person = ("Alice", 30, "Engineer")

“`python
coordinates = (10, 20)
person = ("Alice", 30, "Engineer")
print(type(coordinates)) # Output: <class 'tuple'>

Range (range):

  • Represents an immutable sequence of numbers, commonly used for looping a specific number of times in for loops.
  • Example: range(5) produces numbers from 0 to 4.

“`python
r = range(5)
print(type(r)) # Output: <class 'range'>

“`

3. Mapping Type

  • Dictionary (dict):
    • A collection of key-value pairs, where each key is unique.
    • Dictionaries are mutable and unordered. They are defined by curly braces {}.
    • Example: person = {"name": "Alice", "age": 25, "job": "Engineer"}

 

“`python
person = {"name": "Alice", "age": 25, "job": "Engineer"}
print(type(person)) # Output: <class 'dict'>

“`

4. Set Types

Sets are unordered collections of unique items.

  • Set (set):
    • A collection of unique, unordered items.
    • Sets are mutable and defined by curly braces {} or by using the set() function.
    • Example: unique_numbers = {1, 2, 3, 3, 4}

 

“`python
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers) # Output: {1, 2, 3, 4}
print(type(unique_numbers)) # Output: <class 'set'>

“`

Frozenset (frozenset):

  • An immutable version of a set. Once created, its elements cannot be changed.
  • Example: frozen = frozenset([1, 2, 3, 4])

“`python
frozen = frozenset([1, 2, 3, 4])
print(type(frozen)) # Output: <class 'frozenset'>

“`

5. Boolean Type

  • Boolean (bool):
    • Represents one of two values: True or False.
    • Booleans are often used in conditional statements.
    • Example: is_active = True, is_done = False

 

“`python
is_active = True
is_done = False
print(type(is_active)) # Output: <class 'bool'>

“`

6. Binary Types

Python also provides data types to work with binary data.

  • Bytes (bytes):
    • Represents an immutable sequence of bytes.
    • Example: byte_data = b"Hello"

 

“`python
byte_data = b"Hello"
print(type(byte_data)) # Output: <class 'bytes'>

“`

Bytearray (bytearray):

  • A mutable sequence of bytes.
  • Example: mutable_bytes = bytearray(b"Hello")

“`python
mutable_bytes = bytearray(b"Hello")
print(type(mutable_bytes)) # Output: <class 'bytearray'>

“`

Memoryview (memoryview):

  • Allows for memory-efficient access to slices of data from binary sequences without copying.
  • Example: m = memoryview(b"Hello")

“`python
m = memoryview(b"Hello")
print(type(m)) # Output: <class 'memoryview'>

“`

7. None Type

  • NoneType (None):
    • Represents the absence of a value or a null value.
    • None is often used to signify that a variable has no value or that a function does not return a value.
    • Example: x = None

 

“`python
x = None
print(type(x)) # Output: <class 'NoneType'>

“`

Type Conversion (Casting)

Python allows you to convert values between different data types using built-in functions:

  • int(): Converts a value to an integer.

“`python
x = int(3.5) # Converts float 3.5 to integer 3
“`

float(): Converts a value to a float.
“`python
y = float(3) # Converts integer 3 to float 3.0
“`

str(): Converts a value to a string.
“`python
z = str(100) # Converts integer 100 to string "100"
“`

list(): Converts a sequence (like a string or tuple) to a list.
“`python
l = list("abc") # Converts string "abc" to list ['a', 'b', 'c']
“`

tuple(): Converts a sequence (like a list) to a tuple.
“`python
t = tuple([1, 2, 3]) # Converts list [1, 2, 3] to tuple (1, 2, 3)
“`

set(): Converts a sequence to a set.
“`python
s = set([1, 2, 3, 3]) # Converts list [1, 2, 3, 3] to set {1, 2, 3}
“`

dict(): Converts a sequence of key-value pairs to a dictionary.
“`python
d = dict([('name', 'Alice'), ('age', 25)]) # Converts list of tuples to dict
“`

Understanding these data types is essential for effective programming in Python, as they form the basis for data manipulation and storage in your programs.