Combining Conditions
In Boolean logic, two fundamental operators are OR and AND. What do they represent?
The OR operator checks if either of the conditions is true and returns True
if so; otherwise, it returns False
.
The AND operator ensures both conditions are true before returning True
. If not, it returns False
.
In Python, to combine conditions, use the and
& or
operators (always in lowercase).
For example:
condition1 and condition2
yieldsTrue
only when both conditions areTrue
;condition1 or condition2
givesTrue
if at least one condition isTrue
.
Note
You can also chain more than two conditions using these operators. Employ parentheses to clarify the order of operations.
As an illustration, consider these conditions:
- Whether
2
exceeds1
and if"bbb"
isn't the same as"aaa"
; - If the character with index
2
in the string"my string"
is either"y"
or"s"
.
Code Description
2 > 1 and "bbb" != "aaa"
contains two conditions, connected by the and
operator. The first condition 2 > 1
is true, as 2 is greater than 1. The second condition "bbb" != "aaa"
is also true, because the strings "bbb" and "aaa" are not equal. Since both conditions are true and connected through and
, the entire expression is evaluated as true.In the second line
"my string"[2] == "y" or "my string"[2] == "s"
checks two conditions, connected by the or
operator. The expression "my string"[2]
refers to the third character of the string "my string", which is " ". The first condition "my string"[2] == "y"
is false. The second condition "my string"[2] == "s"
is also false. Therefore, since both conditions are false and they are connected through or
, the overall expression is evaluated as false.
How should we interpret the outcomes? The initial print()
issues a True
response since both 2 > 1
and "bbb" != "aaa"
hold true. The following print()
yields False
because the character at index 2
is neither 'y'
nor 's'
(it's actually a space).
Note
If you wish to reverse a boolean value, employ the
not
operator. For instance,not 1 == 1
results inFalse
because1 == 1
isTrue
, and we've negated that toFalse
.
Code Description
print(0 > 10 and 5 > 2)
: Outputs False
, because although 5 > 2
is true, 0 > 10
is false. The and
operator requires both conditions to be true, but since one of them is false, the whole expression is false.print(2*2 == 5 or 1+1 != 3)
: Outputs True
, because 2*2 == 5
is false, but 1+1 != 3
is true. The or
operator requires at least one of the conditions to be true for the whole expression to be true.
Everything was clear?
Course Content
Introduction to Python
3. Conditional Statements
Introduction to Python
Combining Conditions
In Boolean logic, two fundamental operators are OR and AND. What do they represent?
The OR operator checks if either of the conditions is true and returns True
if so; otherwise, it returns False
.
The AND operator ensures both conditions are true before returning True
. If not, it returns False
.
In Python, to combine conditions, use the and
& or
operators (always in lowercase).
For example:
condition1 and condition2
yieldsTrue
only when both conditions areTrue
;condition1 or condition2
givesTrue
if at least one condition isTrue
.
Note
You can also chain more than two conditions using these operators. Employ parentheses to clarify the order of operations.
As an illustration, consider these conditions:
- Whether
2
exceeds1
and if"bbb"
isn't the same as"aaa"
; - If the character with index
2
in the string"my string"
is either"y"
or"s"
.
Code Description
2 > 1 and "bbb" != "aaa"
contains two conditions, connected by the and
operator. The first condition 2 > 1
is true, as 2 is greater than 1. The second condition "bbb" != "aaa"
is also true, because the strings "bbb" and "aaa" are not equal. Since both conditions are true and connected through and
, the entire expression is evaluated as true.In the second line
"my string"[2] == "y" or "my string"[2] == "s"
checks two conditions, connected by the or
operator. The expression "my string"[2]
refers to the third character of the string "my string", which is " ". The first condition "my string"[2] == "y"
is false. The second condition "my string"[2] == "s"
is also false. Therefore, since both conditions are false and they are connected through or
, the overall expression is evaluated as false.
How should we interpret the outcomes? The initial print()
issues a True
response since both 2 > 1
and "bbb" != "aaa"
hold true. The following print()
yields False
because the character at index 2
is neither 'y'
nor 's'
(it's actually a space).
Note
If you wish to reverse a boolean value, employ the
not
operator. For instance,not 1 == 1
results inFalse
because1 == 1
isTrue
, and we've negated that toFalse
.
Code Description
print(0 > 10 and 5 > 2)
: Outputs False
, because although 5 > 2
is true, 0 > 10
is false. The and
operator requires both conditions to be true, but since one of them is false, the whole expression is false.print(2*2 == 5 or 1+1 != 3)
: Outputs True
, because 2*2 == 5
is false, but 1+1 != 3
is true. The or
operator requires at least one of the conditions to be true for the whole expression to be true.
Everything was clear?