3 Basic Python Operators
3 Basic Python Operators
Introduction.
Operators are the constructs which can manipulate the value of operands. Consider the expression
4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Operators are used to perform
operations on variables and values. Operators are used to perform operations on values and
variables. Operators can manipulate individual items and returns a result. The data items are
referred as operands or arguments. Operators are either represented by keywords or special
characters.
Objectives
Objectives by the end of this topic you should be able to describe:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Learning activities
Learning Activity 5.1: Reading
Read further on Bitwise operators.
Learning Activity 5.2: Journal
Summarize the usage of operators from python documentation.
Learning Activity 5.3: Discussion
In groups of three write programs to demonstrate usage od python operators.
Assessment
Topic resources
1. The Python Tutorial¶. (n.d.). Retrieved from https://docs.python.org/3/tutorial/index.html
2. Mueller, J. P. (n.d.). Beginning Programming with Python For Dummies. S.l.: For
Dummies.
3. (n.d.). Python 3.7.4 documentation. Retrieved from https://docs.python.org/3
4. (n.d.). Git Handbook. Retrieved from https://guides.github.com/introduction/git-
handbook/
5. Shaw, Z. (2017). Learn Python 3 the hard way: a very simple introduction to the
terrifyingly beautiful world of computers and code. Boston: Addison-Wesley.
6. Bader, D. (2018). Python tricks: the book. Vancouver, BC: Dan Bader.
7. Downey, A. B. (2015). Think Python. Sebastopol: OReilly.
8. Ramalho, L. (2016). Fluent Python:Beijing: OReilly.
URL Links
https://www.w3schools.com/python/python_operators.asp
https://docs.python.org/3.4/library/operator.html
https://www.tutorialspoint.com/python/python_basic_operators.htm
TOPIC 5 NOTES
1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations
like addition, subtraction, multiplication and division.
Output:
13
5
36
2.25
2
1
Output:
False
True
False
Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
Output:
0
14
-11
14
2
40
4. Assignment operators: Assignment operators are used to assign values to the variables.
5. Special operators: There are some special type of operators like-
• Identity operators-
is and is not are the identity operators both are used to check if two values are located on
the same part of the memory. Two variables that are equal does not imply that they are
identical.
Output:
True
True
False
True
False
Revision questions
1. What is the output of the following code : print(9//2)
2. What is the answer of this expression, 22 % 3 is?
3. (T/F) x = 8, print(x > 3 or x < 20)
4. Which function overloads the >> operator?
(A) more()
(B) gt()
(C) ge()
(D) None of the above