Lecture03 Operators
Lecture03 Operators
(Booleans, Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Booleans
• Booleans represent one of two values: True or False.
2
Booleans
• When you run a condition in an if statement, Python returns True or
False:
• Example:
– Print a message based on whether the condition is True or False:
3
Evaluate Values and Variables
• The bool() function allows you to evaluate any value, and give you
True or False in return,
• Example:
– Evaluate a string and a number:
• Example:
– Evaluate two variables:
4
Most Values are True
• Almost any value is evaluated to True if it has some sort of content.
• Any list, tuple, set, and dictionary are True, except empty ones.
• Example:
– Following will return True.
5
Some Values are False
• In fact, there are not many values that evaluate to False, except
empty values, such as (), [], {}, "", the number 0, and the value None.
• And of course the value False evaluates to False.
• Example:
– Following will return False:
6
Some Values are False
• One more value, or object in this case, evaluates to False, and that is
if you have an object that is made from a class with a __len__
function that returns 0 or False:
7
Functions can Return a Boolean
• You can create functions that returns a Boolean Value:
• Example:
– Print the answer of a function:
8
Functions can Return a Boolean
• You can execute code based on the Boolean answer of a function:
• Example:
– Print "YES!" if the function returns True, otherwise print "NO!":
9
Functions can Return a Boolean
• Python also has many built-in functions that return a boolean value,
like the isinstance() function, which can be used to determine if an
object is of a certain data type:
• Example:
– Check if an object is an integer or not:
10
Python Programming
(Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Operators
• Operators are used to perform operations on variables and values.
12
Arithmetic Operators
• Arithmetic operators are used with numeric values to perform
common mathematical operations:
13
Assignment Operators
• Assignment operators are used to assign values to variables:
14
Comparison Operators
• Comparison operators are used to compare two values:
15
Logical Operators
• Logical operators are used to combine conditional statements:
16
Identity Operators
• Identity operators are used to compare the objects, not if they are
equal, but if they are actually the same object, with the same
memory location:
17
Membership Operators
• Membership operators are used to test if a sequence is presented in
an object:
18
Bitwise Operators
• Bitwise operators are used to compare (binary) numbers:
19
Operator Precedence
• Operator precedence describes the order in which operations are
performed.
• Example:
– Parentheses has the highest precedence, meaning that expressions inside
parentheses must be evaluated first:
• Example:
– Multiplication * has higher precedence than addition +, and therefor
multiplications are evaluated before additions:
20
Operator Precedence
• The precedence order is described in the table below, starting with
the highest precedence at the top:
21
Python Programming
(Arithmetic Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Arithmetic Operators
• Arithmetic operators are used with numeric values to perform
common mathematical operations:
23
Python Programming
(Assignment Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Assignment Operators
• Assignment operators are used to assign values to variables:
25
Assignment Operators
• assignment
• Addition + assignment
• Subtraction + assignment
26
Assignment Operators
• Multiplication + assignment
→ 15
• Division + assignment
→ 1.6666667
→2
27
Assignment Operators
• Floor division + assignment
→1
• Exponentiation + assignment
→ 125
28
Python Programming
(Comparison Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Comparison Operators
• Comparison operators are used to compare two values:
30
Equal
• 1. Equal
– Return Ture if both are equal
– Otherwise return False
• 2. not equal
– Return Ture if both are not equal
– Otherwise return False
31
Greater than / less than
• 1. Greater than
– Return Ture if the first one is greater than the second
– Otherwise return False
• 1. Less than
– Return Ture if the first one is less than the second
– Otherwise return False
32
Greater than or equal to
• 1. Greater than or equal to
– Return Ture if the first one is greater than or equal to the second one.
– Otherwise return False
33
Less than or equal to
• 1. Less than or equal to
– Return Ture if the first one is less than or equal to the second one.
– Otherwise return False
34
Python Programming
(Logical Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Logical Operators
• Logical operators are used to combine conditional statements:
36
and
• Returns True if both statements are true
• Example:
– x < 5 and x < 10 → True
37
or
• Returns True if both statements are true
• Example:
– x < 5 or x < 4 → True
38
not
• Reverse the result, returns False if the result is true
• Example:
– not(x < 5 and x < 10) → False
39
Python Programming
(Identity Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Identity Operators
• Identity operators are used to compare the objects, not if they are
equal, but if they are actually the same object, with the same
memory location:
41
is
• Returns True if both variables are the same object
• Example:
– x is y
42
Is not
• Returns True if both variables are not the same object
• Example:
– x is not y
43
Python Programming
(Membership Operators)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Membership Operators
• Membership operators are used to test if a sequence is presented in
an object:
45
in
• Returns True if a sequence with the specified value is present in the
object
• Example:
– x in y
46
Not in
• Returns True if a sequence with the specified value is not present in
the object
• Example:
– x not in y
47
Conclusion
48