0% found this document useful (0 votes)
5 views

Boolean Values and Operators

The document explains Boolean values, expressions, and operators, which are fundamental to computer logic. It covers comparison operators, logical operations (and, or, not), and the 'in' operator for membership testing in Python. Examples illustrate how these concepts are applied in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Boolean Values and Operators

The document explains Boolean values, expressions, and operators, which are fundamental to computer logic. It covers comparison operators, logical operations (and, or, not), and the 'in' operator for membership testing in Python. Examples illustrate how these concepts are applied in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Conditionals: Boolean values and

operators
Boolean values and expressions

• A Boolean value is either true or false. It is


named after the British mathematician,
• George Boole, who first formulated Boolean
algebra — some rules for reasoning about and
combining these values.
• This is the basis of all modern computer logic.
Example

• bool passwordIsValid = false;

• if (enteredPassword == "magic")
• {
• passwordIsValid = true;
• }
• else {
• passwordIsValid = false;
• }
Boolean expression
• A Boolean expression is an expression that
evaluates to produce a result which is a Boolean
value.
• For example, the operator == tests if two values
are equal.
• It produces (or yields) a Boolean value.
• So another much more compact way to write
lines previous slite code would be like this:
• passwordIsValid = enteredPassword == "magic";
Comparison operators

• The == operator is one of six


common comparison operators which all
produce a bool result
• x == y // x is equal to
• y x != y // x is not equal to
• yx>y // x is greater than
• y x < y // x is less than y
• x >= y // x is greater than or equal to
• y x <= y // x is less than or equal to y
Examples
Comparing Numbers Comparing Numbers
25==25 2 == 2.0
Output? Output?
6!=7 1.99999 != 2
Output?
Output?
5.5<=6.5
Output?
-5>-6
Output?
Comparing Strings and Lists

• When comparing strings, Python will check for alphabetical ordering. "a" comes
before "b", "can" comes before "candle", and remember upper case letters come
before lower case.

• >>> "abc" == "abc"


True/false?
>>> "abc" == "abcd"
True/false ?
>>> "cba" != "abc"
True/false ?
>>> "alpha" < "beta"
True/false ?
>>> "alpha" > "BETA"
True/false ?
>>> "Mouse" > "Moose"
True/false?
Logic Operations
• The not operator takes a value, looks at its Boolean value and converts this value
to its Boolean counterpart. e.g. not True gives False, and not False gives True.
• >>> not 1
True/false?
>>> not 0
True/false?
>>> not 1 == 2
True/false?
>>> not 5 > 3
True/false?
>>> not "string"
True/false?
>>> not ""
True/false?
>>> not [0,1,-1]
True/false?
>>> not []
True/false?
and

The and operator takes two values, v1 and v2, and implements the
following table to find the result.

Boolean value of v1 Boolean value of v2 result (Boolean value)

True True v2 (True)

True False v2 (False)

False True v1 (False)

False False v1 (False)


and
• >>> True and True
True
>>> False and True
False
>>> 1 and 1
1
>>> 1 and 0
0
>>> 0 and "hello"
0
>>> "hello" and "goodbye"
'goodbye'
>>> [] and [7,8,9]
[]
>>> "" and []
''
or
The or operator is similar to the and operator, but instead it implements the following
table to find the result when given two values v1 and v2.

Boolean value of v1 Boolean value of v2 result (Boolean value)


True True v1 (True)
True False v1 (True)
False True v2 (True)
False False v2 (False)
or
• >>> False or False
False
>>> True or False
True
>>> 0 or 1
1
>>> 0 or 0
0
>>> "" or "string"
'string'
>>> ["b", "a", "c"] or []
['b', 'a', 'c']
>>> 0 or []
[]
in

• The in operation is used to check if a given


element x is a member of a given value s.
e.g. "n" in "Python" is True, 1 in
[2,4,6,8] is False, and 10 in "not
• Number" produces an error because the
integer 10 cannot be checked for membership
in the string "not a number".
In
• >>> "a" in "abc"
True
>>> "yth" in "Python"
True
>>> "" in "string"
True
>>> 3 in [0,1,2,3,4]
True
>>> [1,2] in [1,2,3,4]
False
>>> [1,2] in [[3,4],[2,3],[1,2]]
True
Using Multiple Logic Operations

• The operators described above can be used


together to create expressions as complex as is
required. For example, we can combine
several and and or operators like
• So: True and True or False and True. Python
evaluates this expression from left to right in the
following manner, where the current evaluation is
in bold:
• True and True or False and True
• True or False and True
• True and True
• True
• >>> my_string = "I am using Python"
>>> "c" in my_string
False
>>> "a" in my_string
True
>>> "a" in my_string and "c" not in my_string
True
>>> my_list = [0,4,8,12,16,20]
>>> 4 * 3 in my_list
True
>>> my_list[0] and my_list[1]
0
>>> (my_list[0] and my_list[1]) or my_list[5]
20

You might also like