You are on page 1of 7

07-operators

February 27, 2023

1 Operators
• Operator are special symbols in python that carry out arithematic or logical computation.

1.1 Operator Types


1. Arithematic Operators
2. Assignment Operators
3. Comparision (Relational) Operators
4. Logical (Boolean) Operators
5. Membership Operators
6. Identity Operators

1.2 Arithematic Operators


• Arithematic operators are used to perform the mathematical operations like addition, sub-
traction, multiplication etc.
• +,-,/,%,//,*, ** are the arithematic operators

[1]: # Addition
3+5

[1]: 8

[2]: # Subtraction
54 - 46

[2]: 8

[3]: # Multiplication
2*3

[3]: 6

[4]: # Division
8/2

[4]: 4.0

1
• Division -> always gives output as float
[6]: # Floor division --> Quotient
16//5

[6]: 3

[7]: # Modulo --> Remainder


16%5

[7]: 1

[8]: # power/exponent
10**2

[8]: 100

[9]: # Paranthesis
(2+3) * (5+5)

[9]: 50

Arithematic Operator Precedence


• Paranthesis
• Exponents
• Floor Division
• Division or Multiplication
• Modulus
• Addition or Subtraction
[10]: 5*5+5/5-5

[10]: 21.0

• When we use arithematic operators, the boolean values will be automatically


converted to int
[11]: True + True

[11]: 2

[12]: b = 3.9
c = False
b+c

[12]: 3.9

2
[13]: b = 3.9
c = False
b/c

---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Input In [13], in <cell line: 3>()
1 b = 3.9
2 c = False
----> 3 b/c

ZeroDivisionError: float division by zero

1.3 Assignment Operators


• Assignment operators are used in python to assign values to variables.
• (=, +=,-=,*=,/=,%=,//=) are assignment operators
• First right side part of the code will be executed and then assign to the left side variabe.
[14]: # = is a simple assignment operator that assigns the value on the right to the␣
↪variable on the left

a = 10
a

[14]: 10

[15]: id(a)

[15]: 2694075411024

[16]: a = a+1
print(a)

11

[17]: id(a)

[17]: 2694075411056

[19]: a -= 4 # a = a-4
a

[19]: 3

• Multiply and (*=)


• Divide and (/=)
• Modulus and (%=)

3
• floor division and (//=)
• Exponent and (** =)

1.4 Comparision/Relational Operator


• Comprarision operators are used to compare values, It either returns True or False according
to the condition.
• , <, ==, !=, >=, <= are the comparision operators
[20]: # is grater than
45 > 34

[20]: True

[21]: # is less than


56 < 23

[21]: False

[23]: 3*3 < 4*2

[23]: False

[24]: # is equal to
45 == 45

[24]: True

[25]: # is not equal to


3 != 5

[25]: True

[26]: # greater than or equal to


1 >= 1

[26]: True

[27]: # lessthan or equal to


5 <= 4

[27]: False

[28]: 45 == 45.0

[28]: True

4
[29]: 'hi' == 'Hi'

[29]: False

1.5 Logical Operators


• It returns bool type only
• Logical operators are and, or, not operators.
• when we have, ‘or’ operators… atleast 1 condition should be True then only overall output
will be True
• When we have, ‘and’ operator… all conditions should be True then only overall output will
be True
[30]: (1 > 2) or (2 < 3)

[30]: True

[31]: (1 > 2) and (2 < 3)

[31]: False

[32]: not True

[32]: False

[33]: not False

[33]: True

[34]: my_str = 'siva'


my_str.isalpha() or my_str.alnum() #is alphabets or is alphanumeric

[34]: True

Logical Operators Precedence


• Logical NOT
• Logical AND
• Logical OR
[35]: (2==2) or (3==3) and (3==4) #--> 'Logical and' followed by 'logical or'

[35]: True

1.6 Identity Operators


• si and is not are the identity operators in python.
• they are used to check if two values(or variables) are indication to the same object or not

5
• – is operator (# is - true if the operands are identical)
• – is not operators(# is not - True if the operands are not identical)

[36]: a = 5
b = 5
print(a is b)

True

[39]: s1 = 'krishna'
s2 = 'Krishna'
print(s1 is s2)

False

[42]: a = 6
b = 8
a is not b

[42]: True

1.7 Membership Operators


• in and not in are the membership operators
• They are used ot test whether a value or variable is found in a sequence(string, list, tuple,
set and dictionary).

[43]: a = 'venkatesh'
'eh' in a

[43]: False

[44]: lst = [1,2,3,4]


1 in lst

[44]: True

[45]: a = [1,2,3,4,5,6]
9 not in a

[45]: True

• only arithematic operators return with value


• remaining all the operators, return boolean value

1.8 Operator Precedence:


• Arithematic Operators

6
• Comparision Operators
• Membership Operators
• Identity Operators
• Logical Operators
• Assignment Operators
[46]: b = (1>2) or (3>=3)
b

[46]: True

You might also like