0% found this document useful (0 votes)
12 views

Operators - Ipynb - Colaboratory

best book for operators for students

Uploaded by

microsoftone26
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Operators - Ipynb - Colaboratory

best book for operators for students

Uploaded by

microsoftone26
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

18/11/2023, 19:19 Operators.

ipynb - Colaboratory

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 + 5 = 9. Here, 4 and 5 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
Arithmetic operators are used to perform mathematical operations on numerical values. These
operations are Addition, Subtraction, Multiplication, Division, Modulus, Exponents and Floor
Division.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 1/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

a = 21
b = 10

# Addition
print ("a + b : ", a + b)

# Subtraction
print ("a - b : ", a - b)

# Multiplication
print ("a * b : ", a * b)

# Division
print ("a / b : ", a / b)

# Modulus
print ("a % b : ", a % b)

# Exponent
print ("a ** b : ", a ** b)

# Floor Division
print ("a // b : ", a // b)

a + b : 31
a - b : 11
a * b : 210
a / b : 2.1
a % b : 1
a ** b : 16679880978201
a // b : 2

Comparison Operators
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.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 2/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

a = 40
b = 50

# Equal
print ("a == b : ", a == b)

# Not Equal
print ("a != b : ", a != b)

# Greater Than
print ("a > b : ", a > b)

# Less Than
print ("a < b : ", a < b)

# Greater Than or Equal to


print ("a >= b : ", a >= b)

# Less Than or Equal to


print ("a <= b : ", a <= b)

a == b : False
a != b : True
a > b : False
a < b : True
a >= b : False
a <= b : True

Assignment Operators
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.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 3/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

# Assignment Operator
a = 10

# Addition Assignment
a += 5
print ("a += 5 : ", a)

# Subtraction Assignment
a -= 5
print ("a -= 5 : ", a)

# Multiplication Assignment
a *= 5
print ("a *= 5 : ", a)

# Division Assignment
a /= 5
print ("a /= 5 : ",a)

# Remainder Assignment
a %= 3
print ("a %= 3 : ", a)

# Exponent Assignment
a **= 2
print ("a **= 2 : ", a)

# Floor Division Assignment


a //= 3
print ("a //= 3 : ", a)

a += 5 : 15
a -= 5 : 10
a *= 5 : 50
a /= 5 : 10.0
a %= 3 : 1.0
a **= 2 : 1.0
a //= 3 : 0.0

Bitwise Operators

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 4/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 5/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101

# Binary AND
c = a & b # 12 = 0000 1100
print ("a & b : ", c)

# Binary OR
c = a | b # 61 = 0011 1101
print ("a | b : ", c)

# Binary XOR
c = a ^ b # 49 = 0011 0001
print ("a ^ b : ", c)

# Binary Ones Complement


c = ~a; # -61 = 1100 0011
print ("~a : ", c)

# Binary Left Shift


c = a << 2; # 240 = 1111 0000
print ("a << 2 : ", c)

# Binary Right Shift


c = a >> 2; # 15 = 0000 1111
print ("a >> 2 : ", c)

a & b : 12
a | b : 61
a ^ b : 49
~a : -61
a << 2 : 240
a >> 2 : 15

Logical Operators

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 6/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

x = 5

print(x > 3 and x < 10)


print(x > 3 or x < 10)
print(not(x > 3 and x < 10))

True
True
False

Membership Operators
Membership operators test for membership in a sequence, such as strings, lists, or tuples.

a = 1
b = 20
list1 = [1, 2, 3, 4, 5 ]

print(a in list1)
print(b not in list1)

print(a not in list1)


print(b in list1)

True
True
False
False

Identity Operators
Identity operators compare the memory locations of two objects.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 7/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

a = 10
b = 20

print(a is b)
print(a is not b)

False
True

Operators Precedence
Operator precedence affects how an expression is evaluated.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 8/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d #( 30 * 15 ) / 5
print(e)

e = ((a + b) * c) / d # (30 * 15 ) / 5
print(e)

e = (a + b) * (c / d); # (30) * (15/5)


print(e)

e = a + (b * c) / d; # 20 + (150/5)
print(e)

90.0
90.0
90.0
50.0

Expressions
An expression is a combination of operators and operands that is interpreted to produce some
other value. In any programming language, an expression is evaluated as per the precedence of its
operators. So that if there is more than one operator in an expression, their precedence decides
which operation will be performed first.

Constant Expression
These are the expressions that have constant values only.

# Constant Expressions
x = 15 + 1.3
print(x)

16.3

Arithmetic Expressions

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 9/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

An arithmetic expression is a combination of numeric values, operators, and sometimes


parenthesis. The result of this type of expression is also a numeric value. The operators used in
these expressions are arithmetic operators like addition, subtraction, etc.

# Arithmetic Expressions
x = 40
y = 12

add = x + y
sub = x - y
pro = x * y
div = x / y

print(add)
print(sub)
print(pro)
print(div)

52
28
480
3.3333333333333335

Integral Expressions
These are the kind of expressions that produce only integer results after all computations and type
conversions.

# Integral Expressions
a = 13
b = 12.0

c = a + int(b)
print(c)

25

Floating Expressions
These are the kind of expressions which produce floating point numbers as result after all
computations and type conversions.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 10/11
18/11/2023, 19:19 Operators.ipynb - Colaboratory

# Floating Expressions
a = 13
b = 5

c = a / b
print(c)

2.6
Relational Expressions
In these types of expressions, arithmetic expressions are written on both sides of relational
operator (> , < , >= , <=). Those arithmetic expressions are evaluated first, and then compared as per
relational operator and produce a boolean output in the end.

https://colab.research.google.com/drive/1-3rLkwAqbOTOn32HjrjTQWvd_2lHsjfx#printMode=true 11/11

You might also like