Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python Data Types: A Comprehensive Technical Guide

Tech 2

Numeric Types

Python supports several numeric types:

Integer (int)

  • On a 32-bit system, integers use 32 bits with a range of -2³¹ to 2³¹-1 (-2147483648 to 2147483647)
  • On a 64-bit system, entegers use 64 bits with a range of -2⁶³ to 2⁶³-1 (-9223372036854775808 to 9223372036854775807)

Long Integer

  • Unlike C, Python's long integers have no bit-width limitation
  • Since Python 2.2, integers automatically convert to long integers when overflow occurs, so the L suffix is no longer required

Float

  • Floating-point numbers handle real numbers with decimal portions
  • Similar to C's double type,占用8 bytes (64 bits): 52 bits for mantissa, 11 bits for exponent, 1 bit for sign
  • Example: 3.14, 52.3E-4 represents 52.3 × 10⁻⁴

Complex Numbers

  • Consist of real and imaginary parts in the form x + yj
  • x and y are both real numbers

Note: Python maintains a small integer pool ranging from -5 to 257.

Boolean Type

Represents truth values: True or False (also equivalent to 1 or 0).

Strings

Strings are sequences of characters enclosed in quotes:

message = "hello world"

String Concatenation Warning: Python strings are implemented as character arrays in memory. Each string creation requires contiguous memory allocation. Using the + operator for concatenation creates new memory allocations each time, which can be inefficient for large-scale operations.

String Formatting:

username = "alice"
print("i am %s" % username)

Format specifiers: %s for strings, %d for integers, %f for floats.

Common String Operations:

  • Strip whitespace
  • Split
  • Length
  • Indexing
  • Slicing

Lists

Ordered, mutable sequences:

items = ['alpha', 'beta', 'gamma']
# or
items = list(['alpha', 'beta', 'gamma'])

Basic Oeprations:

  • Indexing
  • Slicing
  • Appending
  • Deletion
  • Length
  • Iteration
  • Membership testing

Tuples

Immutable sequences:

coordinates = (10, 20, 30, 40, 50)
# or
coordinates = tuple((10, 20, 30, 40, 50))

Dictionaries

Unordered key-value pairs:

employee = {"name": "john", 'age': 25}
# or
employee = dict({"name": "john", 'age': 25})

Common Operations:

  • Key access
  • Insertion
  • Deletion
  • Keys/values/items iteration
  • Length

Practical Examples

List Indexing and Slicing

data = "001 002 003 004"
items = ["001", "002", "003", "004"]

print(items)
print(items[0], items[1])

print(items[1:3])
print(items[-1])
print(items[-3:-1])
print(items[-3:])
print(items[:4])

# Output:
# 001 002 003 004
# 001 002
# ['002', '003']
# 004
# ['002', '003']
# ['002', '003', '004']
# ['001', '002', '003', '004']

List Modification Operations

items = ["001", "002", "003", "004"]

items.append("005")
print(items)

items.insert(0, "0001")
print(items)

items[0] = "00001"
print(items)

# Deletion methods
items.remove("00001")
print(items)

del items[0]
print(items)

items.pop()
print(items)

items.pop(1)
print(items)

print(items.index("002"))
print(items.count("002"))

items.reverse()
print(items)

items.sort()
print(items)

new_items = ["007", "008", "009"]
items.extend(new_items)
del new_items
print(items)

items.clear()
print(items)

# Output:
# ['001', '002', '003', '004', '005']
# ['0001', '001', '002', '003', '004', '005']
# ['00001', '001', '002', '003', '004', '005']
# ['001', '002', '003', '004', '005']
# ['002', '003', '004', '005']
# ['002', '003', '004']
# ['002', '004']
# 0
# 1
# ['004', '002']
# ['002', '004']
# ['002', '004', '007', '008', '009']
# []

Shallow Copy vs Deep Copy

Shallow Copy:

original = ["001", "002", "003", ["007", "008"], "005"]
copied = original.copy()

original[0] = "modified"
print(original)
print(copied)

original[3][0] = "006"
print(original)
print(copied)

# Output:
# ['001', '002', '003', '004', '005']
# ['001', '002', '003', '004', '005']
# ['modified', '002', '003', '004', '005']
# ['001', '002', '003', '004', '005']
# ['001', '002', '003', ['006', '008'], '005']
# ['001', '002', '003', ['006', '008'], '005']

Shallow copy replicates only the top-level structure. Nested objects share the same memory references.

Deep Copy:

import copy

original = ["001", "002", "003", ["007", "008"], "005"]
copied = copy.deepcopy(original)

original[3][0] = "006"
print(original)
print(copied)

# Output:
# ['001', '002', '003', ['006', '008'], '005']
# ['001', '002', '003', ['007', '008'], '005']

Deep copy creates entirely independent copies of all nested objects.

List Ietration

data = ["001", "002", "003", ["007", "008"], "005"]

for item in data:
    print(item)

print(data[0:-1:2])
print(data[::2])

# Output:
# 001
# 002
# 003
# ['007', '008']
# 005
# ['001', '003']
# ['001', '003', '005']

Alternative Copy Methods

data = ["001", "002", "003", ["007", "008"], "005"]

copy1 = data[:]
print(copy1)

copy2 = list(copy1)
print(copy2)

# Output:
# ['001', '002', '003', ['007', '008'], '005']
# ['001', '002', '003', ['007', '008'], '005']

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.