Lesson 6 / 8
Module 3Lesson 2⏱ 50 min study

Lesson 6: Data Structures, Strings, Formatting & User Input in Python

Master Python core primitive data structures (lists, tuples, dictionaries), format dynamic text using modern f-strings, and handle interactive user input with robust type conversion.

✦ Key Architectural Takeaways
  • •Python primitives include integers (arbitrary precision), floats (IEEE 754 doubles), booleans, and immutable strings.
  • •Lists ([]) are ordered, mutable dynamic arrays; Tuples (()) are immutable ordered sequences; Dictionaries ({}) are high-performance key-value hash maps.
  • •Formatted string literals (f-strings) provide readable, high-speed inline expression interpolation using {variable_name} syntax.
  • •The input() function always captures keyboard input as a string (str); arithmetic calculations require explicit type casting using int() or float().
  • •Python strings are immutable sequences supporting slicing syntax (string[start:stop:step]) and powerful built-in manipulation methods.

1. The Core Python Data Structure Suite

Python provides built-in high-performance data structures that would require hundreds of lines of code to implement manually in C:

PYTHONSYSTEM SCHEMATIC
# 1. Lists: Ordered, mutable, dynamic arrays
languages = ["C++", "Python", "Rust", "Go"]
languages.append("TypeScript")
print(languages[0])  # Access first element: "C++"

# 2. Tuples: Ordered, immutable (read-only) sequences
coordinates = (37.7749, -122.4194)
# coordinates[0] = 40.0  # ERROR: Tuples cannot be modified!

# 3. Dictionaries: Key-value hash maps with O(1) average lookup time
server_config = {
    "hostname": "production-node-1",
    "ip_address": "192.168.1.100",
    "port": 8080,
    "active": True
}
print(server_config["ip_address"])  # "192.168.1.100"
§

2. User Input & Explicit Type Casting

In Python, the input() built-in function halts script execution and waits for keyboard input. It always returns the data as a string (str):

PYTHONSYSTEM SCHEMATIC
# ==============================================================================
# Script Name : tax_calculator.py
# Purpose     : Demonstrate user input and type conversion
# ==============================================================================

product_name = input("Enter product description: ")

# input() returns strings; we must explicitly convert to float:
raw_price = input("Enter unit price in dollars: ")
price = float(raw_price)

quantity = int(input("Enter quantity purchased: "))

subtotal = price * quantity
tax_rate = 0.0825  # 8.25% state sales tax
total = subtotal * (1 + tax_rate)

# Modern Python f-string with precision formatting (.2f = two decimal places)
print(f"\n--- Order Summary ---")
print(f"Item     : {product_name}")
print(f"Subtotal : ${subtotal:.2f}")
print(f"Total Due: ${total:.2f}")
§

3. String Slicing & Built-In Methods

Python strings support intuitive slice notation to extract substrings without memory reallocation loops:

PYTHONSYSTEM SCHEMATIC
text = "ComputerScience"

# Slicing syntax: [start : stop : step]
print(text[0:8])    # "Computer" (index 0 up to, but not including, index 8)
print(text[8:])     # "Science"  (from index 8 to the end)
print(text[::-1])   # "ecneicSretupmoC" (reverses the string!)

# Built-in String Transformation Methods
message = "   deploy script ready   "
print(message.strip().title())  # "Deploy Script Ready"
print(message.replace("ready", "complete"))