0% found this document useful (0 votes)
0 views9 pages

2.operators in Python

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

2.operators in Python

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

OPERATORS

Operators are used to perform operations on variables and


values.

 Operators: Special symbols like -, + , * , /, etc.


 Operands: Value on which the operator is applied.

Ex
print(10 + 5)

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

ARITHMETIC OPERATORS:

Addition Operator
In Python, + is the addition operator. It is used to add 2 values.
val1 = 2
val2 = 3

# using the addition operator


res = val1 + val2
print(res)

--
x = 15
y = 4

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)

Subtraction Operator
In Python, - is the subtraction operator. It is used to subtract the second value from the first value.

val1 = 2
val2 = 3

# using the subtraction operator


res = val1 - val2
print(res)

Multiplication Operator
Python * operator is the multiplication operator. It is used to find the product of 2 values.

val1 = 2
val2 = 3

# using the multiplication operator


res = val1 * val2
print(res)

Division Operator
There are two types of division operators:
1. Float division
2. Floor division

/ - Division (returns a float)

// - Floor division (returns an integer)

Float division
The quotient returned by this operator is always a float number, no matter if two numbers are
integers. For example:
Example:

print(5/5)
print(10/2)

Integer division( Floor division)


The quotient returned by this operator is dependent on the argument being passed. If any of the
numbers is float, it returns output in float. It is also known as Floor division because, if any number is
negative, then the output will be floored. For example:
print(10//3)

Modulus Operator
The % in Python is the modulus operator. It is used to find the remainder when the first operand is
divided by the second.
val1 = 3
val2 = 2

# using the modulus operator


res = val1 % val2
print(res)

Output:
1
Exponentiation Operator
In Python, ** is the exponentiation operator. It is used to raise the first operand to the power of the
second.

val1 = 2
val2 = 3

# using the exponentiation operator


res = val1 ** val2
print(res)
Output:
8

Comparison Operators
Comparison (or Relational) operators compares values. It either returns True or False according to
the condition.
a = 13
b = 33

print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)

Logical Operators
Operator Description Syntax Example

Returns True if
both the
and x and y x>7 and x>10
operands are
true

Returns True if
or either of the x or y x<7 or x>15
operands is true

Returns True if
not(x>7 and x>
not the operand is not x
10)
false

a = True
b = False
print(a and b)
print(a or b)
print(not a)
Bitwise Operators
Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary
numbers.
Bitwise Operators in Python are as follows:
1. Bitwise NOT
2. Bitwise Shift
3. Bitwise AND
4. Bitwise XOR
5. Bitwise OR
a = 10
b=4

print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
Output
0
14
-11
14
2
40

a = 10 and b = 4.

To understand these, we first need their Binary forms:

 10 in binary: 1010
 4 in binary: 0100

Bitwise AND (&)

It compares each bit. It gives 1 only if both bits are 1.

Plaintext
1010 (10)
& 0100 (4)
-------
0000 -> Result: 0

print(a & b) outputs: 0


Bitwise OR (|)

It gives 1 if at least one of the bits is 1.

Plaintext
1010 (10)
| 0100 (4)
-------
1110 -> (8 + 4 + 2) = 14

print(a | b) outputs: 14

Bitwise NOT (~)

This flips all the bits (0 becomes 1, 1 becomes 0). In Python, this follows the formula: ~x = -(x
+ 1).

 Input: 10
 Calculation: -(10 + 1)

print(~a) outputs: -11

Bitwise XOR (^)

It gives 1 if the bits are different. If they are the same, it gives 0.

Plaintext
1010 (10)
^ 0100 (4)
-------
1110 -> Result: 14

print(a ^ b) outputs: 14

a = 10

b=4

a=a^b

b=a^b

a=a^b

print(a) # Output: 4

print(b) # Output: 10
Right Shift (>>)

Pushes the bits to the right. As we saw before, this is like integer division by 2^n

 1010 shifted right by 2 becomes 0010.


 Math: 10 / 2^2 = 10 / 4 = 2.5 (Floor value is 2).

print(a >> 2) outputs: 2

10>>2 -> 10/21 = 10/2 = 5

40>>3 40/23 40/8 5

Left Shift (<<)

Pushes the bits to the left and adds 0s at the end. This is like multiplying by 2^n.

 1010 shifted left by 2 becomes 101000.


 Binary 101000 = 32 + 8 = 40.
 Math: 10 \times 2^2 = 10 \times 4 = 40.

print(a << 2) outputs: 40

10<<2 -- 10x22 = 10x4 =40

Summary Table

Operator Name Rule Result

& AND Both must be 1 0

| OR Either can be 1 14

~ NOT -(x + 1) -11

^ XOR Must be different 14

>> Right Shift Divide by $2^n$ 2


Operator Name Rule Result

<< Left Shift Multiply by $2^n$ 40

Assignment Operators
Assignment operators are used to assign values to the variables. This operator is used to assign the
value of the right side of the expression to the left side operand.
a = 10
b=a
print(b)
b += a
print(b)
b -= a
print(b)
b *= a
print(b)
b <<= a
print(b)

Identity Operators
"is" and "is not" are the identity operators and both are used to check if two values are located on
the same part of the memory. Two variables that are equal do not imply that they are identical.

is True if the operands are identical


is not True if the operands are not identical

Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:

is Returns True if both variables are the same object x is y

is not Returns True if both variables are not the same object x is not y

The is operator returns True if both variables point to the same object:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z) //true
print(x is y) //False
print(x == y) //True

x = ["apple", "banana"]
y = ["apple", "banana"]

print(x is not y)
//true
--

list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print(list1 == list2)
# Output: True
print(list1 is list2)
# Output: False
print(list1 is list3)
# Output: True

Membership Operators
"in" and "not in" are the membership operators that are used to test whether a value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence

Operator Description Example

in Returns True if a sequence with the specified value is x in y


present in the object

not in Returns True if a sequence with the specified value is not x not in y
present in the object

fruits = ["apple", "banana", "cherry"]

print("banana" in fruits) //t

fruits = ["apple", "banana", "cherry"]

print("pineapple" not in fruits) //t

Membership in Strings
The membership operators also work with strings:
text = "Hello World"

print("H" in text)
print("hello" in text)
print("z" not in text)

Python - Comments
# This is a comment
print("Hello, World!")

 Single-line comments
 Multi-line comments

starting with a hash symbol (#)

Inline Single-Line Comment


print("Hello, World!") # Inline single line comment is placed here

Multi Line Comments in Python


Consecutive Single-Line Comments:

# This function calculates the factorial of a number

# using an iterative approach. The factorial of a number

# n is the product of all positive integers less than or

# equal to n. For example, factorial(5) is 5*4*3*2*1 = 120.

Multi Line Comment Using Triple Quoted Strings

""" This function calculates the greatest common divisor (GCD) of

two numbers using the Euclidean algorithm.

The GCD of two numbers is the largest number

that divides both of them without leaving a remainder. """

You might also like