An algorithm is a finite, understandable set of steps for solving a problem. You can design algorithms before learning much Python syntax.
Understanding the Concept
Think about deciding whether to take an umbrella. You check whether rain is expected; if it is, you take one; otherwise you continue without it. This is an algorithm with a decision. Good algorithms identify the input, describe the steps in the correct order, and produce a meaningful result. Different algorithms can solve the same problem with different costs or levels of reliability.
When and Why You Would Use This
Start with a rule for determining whether a customer receives a discount. We will express it using an if statement. At this stage, focus on the idea that the same program can follow one of two paths depending on a value.
Practical Example
purchase_total = 120
if purchase_total >= 100:
print("Discount available")
else:
print("Regular price")
Understanding the Example
The input is the purchase total. The comparison asks whether the total is at least 100. Because 120 meets that condition, the discount message appears. The indented line belongs to its decision branch. Later guides explain if, comparisons, and indentation fully.
Example Output
Discount available
Try It Yourself
Write three plain-English steps for deciding whether a person may enter a movie based on a minimum age. Test your steps at the exact boundary.
Common Beginner Mistakes
- Writing code before defining the decision rule. State what each input means and what outcome you expect.
- Forgetting boundary values such as exactly 100. A correct comparison must match the intended rule.
- Treating algorithms as complicated mathematics. Everyday step-by-step processes are algorithms too.
Key Takeaways
- An algorithm is an ordered recipe for achieving a defined result.
- Algorithms can branch based on conditions.
- A clear rule is easier to translate into correct Python code.