Logical Operators
Sometimes we need to combine multiple conditions. For example, if it's Saturday or Sunday, then it's the weekend. Using the word or (or
), we combined two conditions. Here's how we would implement it in code:
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend")
Besides or
, we have another logical operator for combining conditions: and
. For example, if it's Friday and the temperature is greater than 20°C, then I'm going out:
day = "Friday"
temperature = 25
if day == "Friday" and temperature > 20:
print("I'm going out")
We can combine these operators as many times as we need. For example:
day = "Friday"
temperature = 25
if day == "Friday" or day == "Saturday" or day == "Sunday" and temperature > 20:
print("I'm going out")
Instructions
Use an if
statement to print Welcome
if the username is admin
and (and
) the password is 123456
.
Start programming for free
7/10