If-statements check whether a statement meets a certain condition, and are used to write code that "makes decisions". They start with if
followed by a condition which is evaluated to be either true or false. If it's true, the block after the colon (:
) is run. If it's false, the block under the else:
is run.
weather = "sunny"
if weather == "sunny":
print("Bring your shades")
elif weather == "rainy":
print("Bring your umbrella")
else:
print("I don't know what you should bring! I'm just a little program...")
- More detailed explanation of if-statements and conditionals on Real Python.
- Quick explanation of if-statements from w3schools.