SlideShare a Scribd company logo
1 of 20
Download to read offline
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
*
Multiplication
Multiplies values on either side of the operator a * b =
540
/ Division Divides left hand operand by right hand operand a/b =
1.666
% Modulus Divides left hand operand by right hand operand and
returns remainder
a% b = 12
** Exponent Performs exponential (power) calculation on operators a**b =30
to the
power 18
// Floor Division - The division of operands where the result
is the quotient in which the digits after the decimal point
are removed. But if one of the operands is negative, the
a//b = 1
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
condition becomes true.
(a ==
b) is not
true.
!= If values of two operands are not equal, then
condition becomes true.
a!=b is true
> If the value of left operand is greater than the value
of right operand, then condition becomes true.
(a > b)
is true.
< If the value of left operand is less than the value of
right operand, then condition becomes true.
(a < b)
is not
true.
>= If the value of left operand is greater than or equal
to the value of right operand, then condition
becomes true.
(a >=
b) is
true.
<= If the value of left operand is less than or equal to
the value of right operand, then condition becomes
true.
(a <=
b) is
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
to left operand
c += a is
equivalent to c
= c + a
-= Subtract AND It subtracts right operand from the left operand and assign the
result to left operand
c -= a is
equivalent to c
= c - a
*= Multiply AND It multiplies right operand with the left operand and assign the
result to left operand
c *= a is
equivalent to c
= c * a
/= Divide AND It divides left operand with the right operand and assign the
result to left operand
c /= a is
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
left operand
c %= a is
equivalent to c
= c % a
**= Exponent AND Performs exponential (power) calculation on operators and
assign value to the left operand
c **= a is
equivalent to c
= c ** a
//= Floor Division It performs floor division on operators and assign value to the
left operand
c //= a is
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 0 0 1 1 1 1 0 0
or |
b 0 0 0 0 1 1 0 1 Decimal
………………………………………………………………………………………………………….
0 0 1 1 1 1 0 1 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 0 0 1 1 1 1 0 0
and &
b 0 0 0 0 1 1 0 1
…………………………………………………………………………………………………………
0 0 0 0 1 1 0 0 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 0 0 1 1 1 1 0 0
xor ^
b 0 0 0 0 1 1 0 1
…………………………………………………………………………………………………………
0 0 1 1 0 0 0 1 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 0 0 1 1 1 1 0 0
Ones Complement ~
………………………………………………………………………………………………………..
1 1 0 0 0 0 1 1 -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 1 1 1 1 0 0 0 0 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>> 0 0 0 0 1 1 1 1 15
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists
in both operands
(a & b) (means
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
not both.
(a ^ b) = 49
(means 0011
0001)
~ Binary Ones
Complement
It is unary and has the effect of 'flipping' bits. (~a ) = -61
(means 1100
0011 in 2's
complement form
due to a signed
binary number.
<< Binary Left
Shift
The left operands value is moved left by the
number of bits specified by the right operand.
a << 2 = 240
(means 1111
0000)
>> Binary Right
Shift
The left operands value is moved right by the
number of bits specified by the right operand.
a >> 2 = 15
(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
AND
If both the operands are true then condition
becomes true.
(a and
b) is
true.
or Logical OR If any of the two operands are non-zero then
condition becomes true.
(a or b)
is true.
not Logical
NOT
Used to reverse the logical state of its operand. Not(a
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
otherwise.
x in y, here in results in a
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
false otherwise.
x not in y, here not in
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
to the same object and false otherwise.
x is y, here is
results in 1 if id(x)
equals id(y).
is not Evaluates to false if the variables on either side of the operator point
to the same object and true otherwise.
x is not y, here is
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
S.M.SALAQUZZAMAN
Department of Electrical and Electronic Engineering (EEE)
Bangladesh University of Business and Technology (BUBT)
Dhaka-1216, Bangladesh.
Email:rajib9924@gmail.com

More Related Content

What's hot (20)

Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Binary search
Binary searchBinary search
Binary search
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Python Modules
Python ModulesPython Modules
Python Modules
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Parsing
ParsingParsing
Parsing
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Python functions
Python functionsPython functions
Python functions
 
Report 02(Binary Search)
Report 02(Binary Search)Report 02(Binary Search)
Report 02(Binary Search)
 

Similar to Python : basic operators

Similar to Python : basic operators (20)

Python operators part2
Python operators part2Python operators part2
Python operators part2
 
C operators
C operatorsC operators
C operators
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
 
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operators
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Python tutorials for beginners | IQ Online Training
Python tutorials for beginners | IQ Online TrainingPython tutorials for beginners | IQ Online Training
Python tutorials for beginners | IQ Online Training
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
C operators
C operators C operators
C operators
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
Operators
OperatorsOperators
Operators
 

Recently uploaded

HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...vershagrag
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptxrouholahahmadi9876
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 

Recently uploaded (20)

HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 

Python : basic operators

  • 1.
  • 2. 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
  • 3. #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 * Multiplication Multiplies values on either side of the operator a * b = 540 / Division Divides left hand operand by right hand operand a/b = 1.666 % Modulus Divides left hand operand by right hand operand and returns remainder a% b = 12 ** Exponent Performs exponential (power) calculation on operators a**b =30 to the power 18 // Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the a//b = 1
  • 4. 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
  • 5. 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 condition becomes true. (a == b) is not true. != If values of two operands are not equal, then condition becomes true. a!=b is true > If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is true. < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is not true.
  • 6. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is true. <= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is 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
  • 7. 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 to left operand c += a is equivalent to c = c + a -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is 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 left operand c %= a is equivalent to c = c % a
  • 8. **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Floor Division It performs floor division on operators and assign value to the left operand c //= a is 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)
  • 9. 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.
  • 10. At first discuss about AND , OR , XOR AND NOR gate little bit
  • 11. 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 0 0 1 1 1 1 0 0 or | b 0 0 0 0 1 1 0 1 Decimal …………………………………………………………………………………………………………. 0 0 1 1 1 1 0 1 61
  • 12. & 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 0 0 1 1 1 1 0 0 and & b 0 0 0 0 1 1 0 1 ………………………………………………………………………………………………………… 0 0 0 0 1 1 0 0 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 0 0 1 1 1 1 0 0 xor ^ b 0 0 0 0 1 1 0 1 …………………………………………………………………………………………………………
  • 13. 0 0 1 1 0 0 0 1 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 0 0 1 1 1 1 0 0 Ones Complement ~ ……………………………………………………………………………………………………….. 1 1 0 0 0 0 1 1 -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 1 1 1 1 0 0 0 0 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).
  • 14. a>>2 0 0 1 1 1 1 0 0>> 0 0 0 0 1 1 1 1 15 Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands (a & b) (means 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 not both. (a ^ b) = 49 (means 0011 0001) ~ Binary Ones Complement It is unary and has the effect of 'flipping' bits. (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift The left operands value is moved left by the number of bits specified by the right operand. a << 2 = 240 (means 1111 0000) >> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. a >> 2 = 15 (means 0000 1111)
  • 15. 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
  • 16. #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 AND If both the operands are true then condition becomes true. (a and b) is true. or Logical OR If any of the two operands are non-zero then condition becomes true. (a or b) is true. not Logical NOT Used to reverse the logical state of its operand. Not(a 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
  • 17. #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 otherwise. x in y, here in results in a 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 false otherwise. x not in y, here not in 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
  • 18. #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 to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y). is not Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. x is not y, here is 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
  • 19. 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
  • 20. Operator Description = %= /= //= -= += *= **= Assignment operators is is not Identity operators in not in Membership operators not or and Logical operators S.M.SALAQUZZAMAN Department of Electrical and Electronic Engineering (EEE) Bangladesh University of Business and Technology (BUBT) Dhaka-1216, Bangladesh. Email:rajib9924@gmail.com