Learn programming concepts interactively
# Python Loops - Interactive Examples# Learn for loops, range(), and iteration through hands-on practice# === PATTERN GENERATOR ===# Example of nested loops and range() usagerows = 5pattern_type = "stars"if pattern_type == "stars":for i in range(1, rows + 1):print("*" * i)elif pattern_type == "numbers":for i in range(1, rows + 1):for j in range(1, i + 1):print(j, end=" ")print() # New lineelif pattern_type == "pyramid":for i in range(1, rows + 1):spaces = " " * (rows - i)stars = "*" * (2 * i - 1)print(spaces + stars)# === DATA PROCESSOR ===# Example of list comprehensions and iterationinput_data = [1, 2, 3, 4, 5]operation = "double"if operation == "double":result = [x * 2 for x in input_data]elif operation == "square":result = [x ** 2 for x in input_data]elif operation == "filter_even":result = [x for x in input_data if x % 2 == 0]elif operation == "sum":total = 0for number in input_data:total += numberresult = totalprint(f"Operation: {operation}")print(f"Input: {input_data}")print(f"Result: {result}")# === SHOPPING CART CALCULATOR ===# Example of iterating through complex data structuresshopping_cart = [{"name": "Laptop", "price": 999.99, "quantity": 1},{"name": "Mouse", "price": 29.99, "quantity": 2},{"name": "Keyboard", "price": 79.99, "quantity": 1}]# Calculate subtotalsubtotal = 0for item in shopping_cart:item_total = item["price"] * item["quantity"]subtotal += item_totalprint(f"{item['name']}: ${item['price']:.2f} x {item['quantity']} = ${item_total:.2f}")# Apply discount if subtotal > $500discount = subtotal * 0.1 if subtotal > 500 else 0total = subtotal - discountprint(f"\nSubtotal: ${subtotal:.2f}")print(f"Discount: ${discount:.2f}")print(f"Total: ${total:.2f}")# Key Loop Concepts:# - for loops with range(start, stop, step)# - Iterating through lists, strings, and dictionaries# - List comprehensions for concise transformations# - Nested loops for complex patterns# - enumerate() for index and value pairs# - Breaking down complex problems into iterations
No operations yet
Start by adding or modifying elements