0% found this document useful (0 votes)
35 views10 pages

Python Chapter 2.1

Uploaded by

nishaya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views10 pages

Python Chapter 2.1

Uploaded by

nishaya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Unit 2 chapter 2.

operator precedence

Operators are special symbols in Python that carry out arithmetic


or logical computation. The value that the operator operates on is called
the operand.
For example:
Python
>>>12+93
115

Here, + is the operator that performs addition. 12 and 93 are the operands
and 115 is the output of the operation.

Arithmetic operators:

Arithmetic operators are used to perform mathematical operations


like addition, subtraction, multiplication, etc.

Operator Meaning Example


+ Add two operands or x + y+ 2
unary plus
- Subtract right operand x - y- 2
from the left or unary
minus
* Multiply two operands x*y
/ Divide left operand by x/y
the right one (always
results into float)
% Modulus - remainder of x %y (remainder of
the division of left x/y)
operand by the right
// Floor division - division x // y
that results into whole
number adjusted to the
left in the number line
** Exponent - left operand x**y (x to the power y)
raised to the power of
right

Example 1: Arithmetic operators in Python

x = 15
y=4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

9
# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Output
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison operators:

Comparison operators are used to compare values. The comparison


operators are also known as relational operator. It returns either True or
False according to the condition.

Operator Meaning:

> Greater than if left operand is greater than the right


True a>b
< Less than True if left operand is less than the right a<b
== Equal to True if both operands are equal a == b
!= Not equal to if operands are not equal a != b
True
>= Greater than or if left operand is greater than or equal to the
equal to True right a >= b
<= Less than or if left operand is less than or equal to the right
equal to True a <= b

Example : Comparison operators

a = 100
b = 112

print('a>ais',a>b)
Output: a > b is False

print('a<bis',a<b)
Output: a < b is True

print('a == b is',a==b)
Output: a == b is False
10
print('a != b is',a!=b)
Output: a != b is True

print('a >= b is',a>=b)


Output: a>= b is False

print('a <= b is',a<=b)


Output: a<= b is True

Logical operators

Logical operators are theand, or, not operators.

Operator Meaning
and if both the operands are true then condition become true.
or if either of the operands is true then condition become true.
not Used to reverse the logical state of its operand.

Example : Logical Operators


a = True
b = False
print('a and bis',a and b)
print('a or bis',x or y)
print('not ais',nota)
Output
a and b is False
a or b is True
nota is False
Bitwise operators
Bitwise operator worked on bits and performed bit by bit operation.
& Binary AND Operator This Operator is used to copy a bit to the
result if it exists in both operands.
| Binary OR Operator It copies a bit if it exists in any operand.
^ Binary XOR Operator It copies a bit if it is set in one operand
but not both.
~ Binary ones It is unary and will flip the bits of an
Complement Operator integer, so 1 will become 0 , and 0 will
become 1.
<< Binary Left Shift It shifts the left operands value to left by
Operator the number of bits given by the right
operand.
>> Binary Right Shift It shifts the left operands value to right
Operator by the number of bits given by the right
operand.
11
Assignment operators

Assignment operators are used to assign values to variables.

= These operators are used to assign the values to right side


operands to left side operand.
+= These operators are used to add right operand to the left
operand and result is assigned to the left operand.
-= The right operand gets subtracted from left operand and result
is assigned to the left operand.
*= The right operand gets multiplied with the left operand and
result is assigned to the left operand.
/= The left operand is divided by the right operand and result is
assigned to the left operand.
%= It performs the modulus operation and result is assigned to
the left operand.

a = 25 is a simple assignment operator that assigns the value 25 on the


right to the variable a on the left.

Example 1:

a=25
a += 10
print(“ a is : ”,a)

output:
a is : 35

Example 2:

a=25
a -= 10
print(“ a is : ”,a)

output:
a is : 15

Example 3:

a=25
a *= 2
print(“ a is : ”,a)

output:
a is : 50
Example 4:

a=25
a /= 5
print(“ a is : ”,a)

output:
a is : 5

Example 5:

a=26
a %= 5
print(“ a is : ”,a)

output:
a is : 1

Special operators

Python language offers some special types of operators like the identity
operator or the membership operator.

Identity operators

Identity operators are used to compare the memory location of two


objects. Is and is not are two Identity operators are used in python.

is True - If the variable on either side of the operator point


to the same object
False - If the variable on either side of the operator not
point to the same object
Is not False - if the variable on either side of the operator point
to the same object.
True - if the variable on either side of the operator point
to the different object.

Example:

p=50
q=50

print(‘p =’,p,’:’,id(p))
p=50 : 80983211
print(‘q =’,p,’:’,id(q))
q=50 : 80983211

if(p is q)
13
print(‘p and q have same identity’)
else
print(‘p and q do not have same identity’)

output:
p and q have same identity

if(id(p) is id(q))
print(‘p and q have same identity’)
else
print(‘p and q do not have same identity’)

output:
p and q have same identity

if(p is not q)
print(‘p and q do not have same identity’)
else
print(‘p and q have same identity’)

output:
p and q have same identity

Membership operators

There are two membership operators in Python (in and not in).
They are used to test for membership in a sequence(string, list, tuple and
dictionary).

In Evaluates to true if it finds a variable in the sequence and


return false if it not finds a variable in the sequence
not in Evaluates to false if it finds a variable in the sequence
and return true if it not finds a variable in the sequence

Example:
p=60
q=30
list1=[10,30,50,66]
example 1:
if(p in list):
print(‘p is in the list’)
else
print(‘p is not in the list’)
output:
p is not in the list
example 2:
if(p not in list):
print(‘p is not in the list’)
else
14
print(‘p is in the list’)
output:
p is not in the list

example 3:
if(q in list):
print(‘q is in the list’)
else
print(‘q is not in the list’)
output:
q is in the list

4 Operator precedence.
The order of the operation means the operator precedence to evaluate
an expression. The operator will help to evaluate the expression.
Following table shows which operator has highest presedence to lowest.

Operator Description
() Parentheses
** Exponent
+a, -a, ~a Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operator
& Bitwise and
^ Bitwise XOR
| Bitwise or
==, !=, >, >=, <, <= Comparisons operator
Not Logical NOT
And Logical AND
Or Logical OR

operators with the highest precedence appear at the top of the table

Example:
>>> 5*2+10
20

In this example the first precedence is given to the multiplication


then addition operator.
>>>5*(2+10)
60
In this example the first precedence is given to the parenthesis then
multiplication operator.
>>>10/5+10
In this example the first precedence is given to the division then addition
operator.

The table below is showing associativity of various Python operators.

Description Associa
Oper tivity
ator

( ) Parentheses left to
right
** Exponent right to
left
* / % Multiplication / division / modulus left to
right
+ - Addition / subtraction left to
right
<< Bitwise left shift / Bitwise right shift left to
>> right
< <= Relational operators: less than / less than or left to
> >= equal to / greater than / greater than or equal to right
== != Relational operators: is equal to / is not equal to left to
right
is, is Identity operators left to
not Membership operators right
in,
not in
& Bitwise AND operator left to
right
^ Bitwise exclusive OR operator left to
right
| Bitwise inclusive OR operator left to
right
not Logical NOT right to
left
and Logical AND left to
right
or Logical OR left to
right

Associativity=+=of-=Python Operators
Assignment operators: right to left

Addition / subtraction
*= /= Multiplication / division
%= Modulus / bitwise AND
Associativity is defined as the order according to which an expression with
&= Bitwise exclusive / inclusive OR
^= |= Bitwise shift left / right shift
<<=
multiple operators of the same precedence is evaluated. Generally all the
>>=

operators have left to right associativity.


For example, floor division and multiplication have the same precedence.
Hence, if both floor division and multiplication operators are present in an
expression, the left one is evaluated first.

CODE:

# Left-right associativity
print(6 * 5 // 2)

# Shows left-right associativity


print(6 * (5 // 2))
Output
15
12

Non Associative Operators

Several operators, such as comparison or assignment operators, don't have


such associativity rules in Python. Patterns of this type of operator have
their own rules that can not be represented as associativity. For instance, a
< b < c is not the same as (a < b) < c or a < (b < c). a < b < c is assessed
from left to right and is similar to a < b and b < c.

Additionally, while linking assignment operators such as a = b = c = 3 is


entirely acceptable, a = b = c += 2 is not acceptable.
Code

a=b=c=3
a=b=c+=2

You might also like