0% found this document useful (0 votes)
10 views20 pages

Python Basic Operators Overview

The document provides an overview of basic operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each operator type is explained with definitions, examples, and code snippets demonstrating their usage. Additionally, it covers operator precedence in Python.

Uploaded by

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

Python Basic Operators Overview

The document provides an overview of basic operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each operator type is explained with definitions, examples, and code snippets demonstrating their usage. Additionally, it covers operator precedence in Python.

Uploaded by

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

Python

Basic Operators

An operator, in computer programing, is a symbol that usually represents an


action or process.

#Types of Operator
Python language supports the following types of operators.

 Arithmetic Operators
 Comparison (Relational) Operators
 Assignment Operators
 Logical Operators
 Bitwise Operators
 Membership Operators
 Identity Operators
#Python Arithmetic Operators
Arithmetic operators take numerical values (either literals or variables) as their
operands and return a single numerical value.

Assume variable a holds 30(a=30) and variable b holds 18(b=18)


then

Operator Description Example

+ Addition Adds values on either side of the operator. a + b = 48

- Subtraction Subtracts right hand operand from left hand operand. a – b = 12

* Multiplies values on either side of the operator a*b=


Multiplication 540

/ Division Divides left hand operand by right hand operand a/b =


1.666

% Modulus Divides left hand operand by right hand operand and a% b = 12


returns remainder

** Exponent Performs exponential (power) calculation on operators a**b =30


to the
power 18

// Floor Division - The division of operands where the result a//b = 1


is the quotient in which the digits after the decimal point
are removed. But if one of the operands is negative, the
result is floored, i.e., rounded away from zero (towards
negative infinity):

EXAMPLE CODE
a,b=30,18

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

print('\na+b=',a+b)
print('a-b=',a-b)
print('a*b=',a*b)
print('a/b=',a/b)
print('a%b=',a%b)
print('a**b=',a**b)
print('a//b=',a//b)

RESULT
a= 30
b= 18
a+b= 48
a-b= 12
a*b= 540
a/b= 1.6666666666666667
a%b= 12
a**b= 387420489000000000000000000
a//b= 1

#Python Comparison Operators


In computer science, a relational operator is a programming language construct or
operator that tests or defines some kind of relation between two entities. relational
operators return the integers 0 or 1, where 0 stands for false and 1 stands for true

Assume variable a holds 30 and variable b holds 18,

Operator Description Example

== If the values of two operands are equal, then the (a ==


condition becomes true. b) is not
true.

!= If values of two operands are not equal, then


a!=b is true
condition becomes true.

> If the value of left operand is greater than the value (a > b)
of right operand, then condition becomes true. is true.

< If the value of left operand is less than the value of (a < b)
right operand, then condition becomes true. is not
true.
>= If the value of left operand is greater than or equal (a >=
to the value of right operand, then condition b) is
becomes true. true.

<= If the value of left operand is less than or equal to (a <=


the value of right operand, then condition becomes b) is
true. not
true.

EXAMPLE CODE
a,b=30,18
print('a=',a)
print('b=',b)

print('\na>b is ',a>b)
print('a<b is',a<b)
print('a==b is ',a==b)
print('a!=b is',a!=b)
print('a<=b is',a<=b)
print('a>=b is',a>=b)

RESULT
a= 30
b= 18

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

#Python Assignment Operators


Assume variable a holds 30 and variable b holds 18, and c is a variable

Operator Description Example

= Assigns values from right side operands to left side operand c=a+b
assigns value of
a + b into c

+= Add AND It adds right operand to the left operand and assign the result c += a is
to left operand equivalent to c
=c+a

-= Subtract AND It subtracts right operand from the left operand and assign the c -= a is
result to left operand equivalent to c
=c-a

*= Multiply AND It multiplies right operand with the left operand and assign the c *= a is
result to left operand equivalent to c
=c*a

/= Divide AND It divides left operand with the right operand and assign the c /= a is
result to left operand equivalent to c
= c / ac /= a is
equivalent to c
=c/a

%= Modulus AND It takes modulus using two operands and assign the result to c %= a is
left operand equivalent to c
=c%a
**= Exponent AND Performs exponential (power) calculation on operators and c **= a is
assign value to the left operand equivalent to c
= c ** a

//= Floor Division It performs floor division on operators and assign value to the c //= a is
left operand equivalent to c
= c // a

EXAMPLE CODE
a,b=30,18
print('a=',a)
print('b=',b)
c=a+b
print('c=a+b=',c)
c+=a #c=c+a c=48+30=78 now c is 78
print('c=c+a=',c)
c-=a #c=c-a c=78-30=48 now c is 48
print('c=c-a=',c)
c*=a #c=c*a c=48*30=1440 now c is
1440
print('c=c*a=',c)
c/=a #c=c/a c=1440/30=48 now c is
48
print('c=c/a=',c)
c%=a #c=c%a c=48%30=18 now c is 18
print('c=c%a=',c)
c**=a #c=c**a
c=18^30=4.551715960790334e+37 now c is
4.551715960790334e+37
print('c=c^a=',c)
c//=a #c=c//a
c=4.551715960790334e+37//30=1.517238653596778e+3
6 now c is 1.517238653596778e+36
print('c=c//a',c)
RESULT
a= 30
b= 18
c=a+b= 48
c=c+a= 78
c=c-a= 48
c=c*a= 1440
c=c/a= 48.0
c=c%a= 18.0
c=c^a= 4.551715960790334e+37
c=c//a 1.517238653596778e+36

#Python Bitwise Operators


Bitwise operator works on bits and performs bit by bit operation.
At first discuss about AND , OR , XOR AND NOR gate little bit
27 26 22 24 23 22 21 20
128 64 32 16 8 4 2 1
…………………………………………………………………………………………………
0 0 1 1 1 1 0 0 60
0 0 0 0 1 1 0 1 13

Assume if a = 60; and b = 13; Now in binary format they will be as follows

a=0011 1100
b=0000 1101

|
Binary OR
a | b Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of a AND of b is 0,
otherwise it's 1.

a 0011 1100
or |
b 0000 1101 Decimal
………………………………………………………………………………………………………….
0011 1101 61
&
Binary AND
a & b Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of a AND of b is
1, otherwise it's 0.

a 0011 1100
and &
b 0000 1101
…………………………………………………………………………………………………………
0000 1100 12

^
Binary XOR
x ^ y Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in
x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.

a 0011 1100
xor ^
b 0000 1101
…………………………………………………………………………………………………………
0011 0001 49
~
Binary Ones Complement
~ a Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a
1. This is the same as -a - 1.

a 0011 1100
Ones Complement ~
………………………………………………………………………………………………………..
1100 0011 -61
1 1 0 0 0 0 1 1
-(64-0-0- 0-0-2)-1=64-2-1=-61
Sign bit (1=- or 0=+)
<<
Binary Left Shift
a << 2 Returns a with the bits shifted to the left by 2 places (and new bits on the right-hand-
side are zeros).

a<<2 0 0 1 1 1 1 0 0<<2 11110000 240

>>
Binary Right Shift

a >> 2 Returns a with the bits shifted to the right by 2 places (and new bits on the left-hand-
side are zeros).
a>>2 0 0 1 1 1 1 0 0>> 00001111 15

Operator Description Example

& Binary AND Operator copies a bit to the result if it exists (a & b) (means
in both operands 0000 1100)

| Binary OR It copies a bit if it exists in either operand. (a | b) = 61


(means 0011
1101)

^ Binary XOR It copies the bit if it is set in one operand but (a ^ b) = 49


not both. (means 0011
0001)

~ Binary Ones It is unary and has the effect of 'flipping' bits. (~a ) = -61
Complement (means 1100
0011 in 2's
complement form
due to a signed
binary number.

<< Binary Left The left operands value is moved left by the a << 2 = 240
Shift number of bits specified by the right operand. (means 1111
0000)

>> Binary Right The left operands value is moved right by the a >> 2 = 15
Shift number of bits specified by the right operand. (means 0000
1111)
EXAMPLE CODE
a=0b00111100
b=0b00001101
print('a=',bin(a),'b=',bin(b))

print('a or b is=',bin(a|b))

print('a and b is=',bin(a&b))


print('a xor b is=',bin(a^b))
print('Ones Complement of a=',bin(~a))
print('a Left Shift by 2 is',bin(a<<2))
print('a Right Shift by 2 is',bin(a>>2))

RESULT
a= 0b111100 b= 0b1101
a or b is= 0b111101
a and b is= 0b1100
a xor b is= 0b110001
Ones Complement of a= -0b111101
a Left Shift by 2 is 0b11110000
a Right Shift by 2 is 0b1111
#Python Logical Operators
here are following logical operators supported by Python language. Assume
variable a holds 10 and variable b holds 20 then

Operator Description Example

and Logical If both the operands are true then condition (a and
AND becomes true. b) is
true.

or Logical OR If any of the two operands are non-zero then (a or b)


condition becomes true. is true.

not Logical Used to reverse the logical state of its operand. Not(a
NOT and b) is
false.

EXAMPLE CODE
a,b=10,20
c=c=(a>11)and (b>10) # 0 and 1 so result is false
print(c)
c=(a>11)or(b>10) # 0 or 1 so result is true
print(c)
a,b=10,20
c=not((a>11)and (b>10)) # not(0 and 1)=not(false) so result is true
print(c)
RESULT
False
True
True
#Python Membership Operators

Python’s membership operators test for membership in a sequence, such as strings, lists, or
tuples. There are two membership operators as explained below

Operator Description Example

in Evaluates to true if it finds a variable in the specified sequence and false x in y, here in results in a
otherwise. 1 if x is a member of
sequence y.

not in Evaluates to true if it does not find a variable in the specified sequence and x not in y, here not in
false otherwise. results in a 1 if x is not a
member of sequence y.

EXAMPLE CODE
a=[1,2,3,4,5,6,7,8,9,10] # a is a list

print(3 in a) # 3 in list a so result is true


print(20 in a) # 20 not in list a so result is false

print(3 not in a) # 3 not in list a ,(but 3 in list a) so result is false

print(20 not in a) # 20 not in list a ,(but 20 not in list a ) so result is


true

RESULT
True
False
False
True
#Python Identity Operators
dentity operators compare the memory locations of two objects. There are
two Identity operators as explained below

Operator Description Example

is Evaluates to true if the variables on either side of the operator point x is y, here is
to the same object and false otherwise. results in 1 if id(x)
equals id(y).

is not Evaluates to false if the variables on either side of the operator point x is not y, here is
to the same object and true otherwise. not results in 1 if
id(x) is not equal to
id(y).

EXAMPLE CODE
a,b=10,100
print(a is b)
print(a is not b)

RESULT

False
True
ALL Python Operators Precedence

Operator Description

** Exponentiation (raise to the power)

~+- Complement, unary plus and minus (method names for the
last two are +@ and -@)

* / % // Multiply, divide, modulo and floor division

+- Addition and subtraction

>> << Right and left bitwise shift

& Bitwise 'AND'

^| Bitwise exclusive `OR' and regular `OR'

<= < > >= Comparison operators

<> == != Equality operators


Operator Description

= %= /= //= -= += Assignment operators


*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators

[Link]
Department of Electrical and Electronic Engineering (EEE)
Bangladesh University of Business and Technology (BUBT)
Dhaka-1216, Bangladesh.
Email:rajib9924@[Link]

You might also like