0% found this document useful (0 votes)
3 views3 pages

Lesson03 Operators

The document provides an overview of operators in Python, categorizing them into arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It explains the functionality of each type, including examples of arithmetic operations and operator precedence. Additionally, it details how assignment operators work and the significance of operator precedence in expression evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Lesson03 Operators

The document provides an overview of operators in Python, categorizing them into arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It explains the functionality of each type, including examples of arithmetic operations and operator precedence. Additionally, it details how assignment operators work and the significance of operator precedence in expression evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 3: Operators in Python

Operators are the constructs, which can manipulate the value of operands

Types of Operators

Python supports the following types of operators-

a) Arithmetic Operators
b) Comparison (Relational) Operators
c) Assignment Operators
d) Logical Operators
e) Bitwise Operators
f) Membership Operators
g) Identity Operators

Arithmetic Operators

An arithmetic operation is performed between two operands. The operators are


Addition (+), Subtraction (-), Multiplication (*), division (/), modulus (%),
exponent (**), and floor (//).

For example, the last 3 operators;

9%2 # The result is 1


9**2 # The result is 81
9//2 # The result is 4

Comparison / Relational Operators

Compare values on either side of the operator and decide if the relation among
them is either true or false. These are equal to (==), not equal to (!=),
greater than (>), less than (<), greater than or equal to (>=), less than or
equal to ( <=)

Assignment Operators

Operator Description Example


= Assigns values from right c = a + b assigns value of a + b into
side operands to left side c
operand
+= Add AND It adds right operand to c += a is equivalent to c = c + a
the left operand and
assign the result to left
operand
-= Subtract AND It subtracts right c -= a is equivalent to c = c - a
operand from the left
operand and assign the
result to left operand

A. Irungu
*= Multiply AND It multiplies right c *= a is equivalent to c = c * a
operand with the left
operand and assign the
result to left operand
/= Divide AND It divides left operand c /= a is equivalent to c = c / a
with the right operand
and assign the result to
left operand
%= Modulus AND It takes modulus using c %= a is equivalent to c = c % a
two operands and assign
the result to left operand
**= Exponent AND Performs exponential c**=a is equivalent to c = c** a
(power) calculation on
operators and assign
value to the left operand
//= Floor Division It performs floor division c //= a is equivalent to c = c // a
on operators and assign
value to the left operand

Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. Assume if a


= 60; and b = 13; Now in binary format they will be as follows-

a = 0011 1100
b = 0000 1101
a&b = 0000 1100 (AND)
a|b = 0011 1101 (OR)
a^b = 0011 0001 (XOR)
~a = 1100 0011 (unary operation Binary One’s complement)

Logical Operators

Logical Operations evaluate to either true or false. The operators used are
and, or, and not

Membership Operators

Membership operators test for membership in a sequence; such as strings,


lists, or tuples. These operators are in and not in

Identity Operators

Identity operators compare the memory locations of two objects. These


operators are is and is not.

Operator precedence

Operator precedence affects the evaluation of an expression. In the table


below, the operators with the highest precedence appear at the top, while
those with the lowest appear at the bottom.

A. Irungu
Exponentiation (raise to the power)
**
complement, unary plus and minus
~, +@, -@
Multiply, divide, modulo and floor division
*, /, %, //
Addition and subtraction
+, -
Right and left bitwise shift
>>, <<
Bitwise 'AND'
&
Bitwise exclusive `OR' and regular `OR'
^, |
Comparison operators
<=, <, >, >=
Equality operators
<>, ==, !=
Assignment operators
= ,%=, /=, //=, -
=, +=, *=,**=
Identity operators
is, is not
Membership operators
in, not in
Logical operators
not, or, and

A. Irungu

You might also like