Home / Python Tutorial / Day 6
Day 6 — Python if, elif and else
Decisions. This is where a script stops being a calculator and starts being a program.
if
The colon and the indentation are both required. Python uses indentation where other languages use brackets.
if / else
elif for more than two outcomes
Order matters
Python checks each condition top to bottom and stops at the first true one. Put the strictest condition first — swap the 80 and 60 lines above and everyone becomes a B.
Comparison operators
==equal to (two equals signs — one is assignment)!=not equal to><>=<=
Combining conditions with and / or / not
Checking a value is present
= and == are not the same
= puts a value into a variable. == asks whether two things are the same. Using = in an if is a syntax error in Python — which is a mercy, because in some languages it silently does the wrong thing.Try these yourself
- Write an if that prints "Even" or "Odd" for a number.
- Grade a mark out of 100 into A, B, C or Fail.
- Check whether a city is in a list of NCR cities.
- Print "Discount" only when amount is over 10000 AND the customer is Retail.
Ready for pandas and real projects?
This tutorial covers core Python. Our Data Analytics course takes you into pandas, NumPy, real files and visualisation, with mentor support and portfolio projects.
See the course →