Computer Programming: Python
Computer Programming: Python
Python Programming
Fundamentals
• Module 2
Boolean Data Types
• Python Boolean type is one of built-in data
types which represents one of the two values
either True or False.
https://realpython.com/python-operators/
Operators
• Python operators are the constructs which can
manipulate the value of operands.
• These are symbols used for the purpose of logical,
arithmetic and various other operations.
• Consider the expression 4 + 3 = 7.
Here, 4 and 3 are called operands and + is called operator.
Types of Python Operators
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
• Python arithmetic operators are used to
perform mathematical operations on
numerical values.
• These operations are Addition, Subtraction,
Multiplication, Division, Modulus, Exponents
and Floor Division.
Arithmetic Operators
7%2=1
Arithmetic - Example
Comparison Operators
• Python comparison operators compare the
values on either sides of them and decide the
relation among them. They are also called
relational operators.
• These operators are equal, not equal, greater
than, less than, greater than or equal to and less
than or equal to.
Comparison Operators
Example
Assignment Operators
• Python assignment operators are used to
assign values to variables.
• These operators include simple assignment
operator, addition assign, subtraction assign,
multiplication assign, division and assign
operators etc.
Assignment Operators
Example
Bitwise Operator
Operator Name Example
& Binary AND Sets each bit to 1 if both bits are 1
| Binary OR Sets each bit to 1 if one of two bits is 1
^ Binary XOR Sets each bit to 1 if only one of two
bits is 1
~ Binary Ones Inverts all the bits
Complement
<< Binary Left Shift left by pushing zeros in from the
Shift right and let the leftmost bits fall off
>> Binary Right Shift right by pushing copies of the
Shift leftmost bit in from the left, and let
the rightmost bits fall off
Example
Binary to decimal conversion ---
Decimal to binary conversion ---
multiply the binary number by 2^a
Divide the decimal number by 2
and sum the result
Example
Left shift
Shift left by pushing zeros in from the
right and let the leftmost bit shift off.
We are gaining bits.
a<<2 = a*2^n, here n represents no.of
bits to be shifted
Right shift
Shift right by pushing zeros from the left
and let the rightmost bits fall off.
We are losing bits.
a>>2 = a/2^n, here n represents no.of
bits to be shifted
Membership Operator
• Python’s membership operators test for
membership in a sequence, such as strings, lists,
or tuples.
Membership Operator - Example
Identity Operator
• Identity operators compare the memory
locations of two objects. There are two Identity
operators explained below −
Example
Precedence of Operators
• An expression in python consists
of variables, operators, values, etc.
• When the Python interpreter encounters any
expression containing several operations, all
operators get evaluated according to an ordered
hierarchy, called operator precedence.
Operator precedence and associativity
Operator precedence and associativity