0
0
Pythonprogramming~15 mins

How Python executes code - Try It Yourself

Choose your learning style9 modes available
Understanding How Python Executes Code
📖 Scenario: Imagine you are writing a simple program to calculate the total cost of items you want to buy. You will see how Python reads and runs your code step by step.
🎯 Goal: You will create a small program that stores item prices, adds them up, and shows the total cost. This will help you understand how Python executes code line by line.
📋 What You'll Learn
Create a dictionary with item names and their prices
Create a variable to hold the total cost starting at 0
Use a for loop to add each item's price to the total cost
Print the total cost
💡 Why This Matters
🌍 Real World
Calculating totals is common in shopping apps, budgeting tools, and billing systems.
💼 Career
Understanding how Python executes code helps you write clear programs and debug errors in real projects.
Progress0 / 4 steps
1
Create a dictionary of items and prices
Create a dictionary called items with these exact entries: 'apple': 0.5, 'banana': 0.3, 'orange': 0.7
Python
Need a hint?

Use curly braces {} to create a dictionary. Separate each item with a comma.

2
Create a variable to hold the total cost
Create a variable called total_cost and set it to 0
Python
Need a hint?

Just write total_cost = 0 to start counting from zero.

3
Add each item's price to the total cost
Use a for loop with variables item and price to iterate over items.items(). Inside the loop, add price to total_cost
Python
Need a hint?

Use for item, price in items.items(): to get each price, then add it to total_cost using +=.

4
Print the total cost
Write print(total_cost) to display the total cost
Python
Need a hint?

Use print(total_cost) to show the final sum.