π Story: Zogg Needs Numbers! π½
Last time, we helped Zogg, the little alien, by sending a secret message using Python! πΈ But now, Zogg has a new problem…
“Oh no! My spaceshipβs fuel is running out! I need to calculate how much energy I have left. But I donβt know how to do math in Python! Can you help me?“
Zogg needs us to teach Python how to do math so he can safely return home! π Letβs get started!
π’ Python as a Super Calculator
Python is great at solving math problemsβjust like a real calculator! You can add, subtract, multiply, and divide using simple symbols:
Math Operation | Symbol | Example in Python | Answer |
---|---|---|---|
Addition | + | 5 + 3 | 8 |
Subtraction | - | 10 - 4 | 6 |
Multiplication | * | 7 * 2 | 14 |
Division | / | 16 / 4 | 4.0 |
Letβs try it out in Python!
π Step 1: Open Your Python Playground
Use one of these free online compilers (no sign-up needed!):
πΉ Trinket – Python Online (Recommended β
)
πΉ OnlineGDB – Python Compiler
πΉ Programiz – Python Online Compiler
π₯οΈ Step 2: Try Simple Math in Python!
Type this code into your Python playground and hit Run βΆοΈ:
print(5 + 3)
print(10 - 4)
print(7 * 2)
print(16 / 4)
What do you see? π Python calculates the answers for you!
π§ What If We Try Bigger Numbers?
Letβs help Zogg by calculating his spaceshipβs energy! Try this:
print(1000 - 250) # Fuel left after using 250
print(50 * 8) # Energy from solar panels
print(200 / 5) # Energy used per hour
Python can handle big numbers too! π
π€ What Happens with Division?
Try this code:
print(10 / 3)
Python gives 3.3333333333333335
. Thatβs because division in Python always gives a decimal number (float)!
If you only want the whole number, use //
(integer division):
print(10 // 3) # Only shows 3
This is useful if Zogg only has whole fuel tanks left! β½
π― Challenge: Solve Zoggβs Energy Problem!
Zoggβs spaceship needs 3 fuel tanks to reach home. Each fuel tank holds 500 units of energy.
π Mission: Write a Python program to find out how much total energy Zogg has!
Hint: Use multiplication (*
) to calculate:
print( ___ * ___ ) # Fill in the blanks!
Post your answer in the comments! π
β οΈ Common Mistakes and Fixes!
β Forgetting parentheses in print()
print 5 + 3 # β ERROR!
β Fix:
print(5 + 3) # β
Works!
β Using x
instead of *
for multiplication
print(5 x 3) # β ERROR!
β Fix:
print(5 * 3) # β
Works!
π Fun Fact: Python Can Handle HUGE Numbers!
Unlike regular calculators, Python can work with really big numbers! Try this:
print(999999999999999 * 2)
Python will still give you the correct answer! π€―
π Whatβs Next?
Zogg is one step closer to launching his spaceship! Next time, weβll learn how to store numbers in variables so Zogg can keep track of his fuel automatically!
π Next Mission: “Super Smart Variables!” π§ π‘
Comment below:
1οΈβ£ What was your answer to Zoggβs energy problem?
2οΈβ£ Did you enjoy this lesson?
See you in the next adventure, Junior Python Coder! π§βππ©βπ