SlideShare a Scribd company logo
Presented By
Saurabh Upadhyay
1773513908
Python operators
Operators
 An operator, in computer programming, is a symbol
that usually represents an action or process.
 It is a symbol that perform operation on one or more
operands.
 Operators required operands to perform their job.
3 4+
Operands
Operators
List Of Operators
i. Arithmetic Operator
ii. Relational Operator
iii. Logical Operator
iv. Bitwise Operator
v. Assignment Operator
vi. Identity Operator
vii. Membership Operator
1. Arithmetic Operator
 Arithmetic operators take numerical values as their
operands and return a single numerical value.
Operator Example Result
+ Addition 4+3 7
- Subtraction 4-3 1
* Multiplication 6*6 36
/ Division 3/2 1.5
% Modulus 7%3 1
** Exponent 3**2 9
// 3//2 1
Arithmetic Operator Example
10**-2
0.01
>>> -2**3
-8
>>> -2**2
-4
>>> 10//3
3
>>> 10.0//3.0
3.0
>>> -4%3
2
>>> -4%-3
-1
>>> x='abc'
>>> y='def'
>>> x+y
'abcdef’
>>> True+1
2
>>> 'ab'*3
'ababab'
2. Relational Operator
Oper
ator
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.
Relational Operators Examples
 Relational operators always yield true or false as a result.
>>>5>4
True
>>>int(5>4)
1
 It is also used to compare string.
>>>’ac’==‘ac’
True
>>>ac==ac
error
 5>4<2 if any result value is false all result value is false.
>>>5>4>2
True
 If we use equality(==) operator with any type of operand
they never give error.
>>>5==‘5’
False
>>>’a’==97
False
>>>5=5.0
True
>>>’AB’==‘CD’
False
3. Logical Operator
Operator Expression Example
and a>b and a>c
When both operands are
True ,result will be True
or a>b or a>c
When both operands are
False result will be False
not not a>b
Invert the result
Expression 1 Expression 2 and or not(Exp 1)
T T T T F
T F F T F
F T F T T
F F F F T
 These operators return boolean value by validating
more than one expression.
 If the operands of logical operator are boolean value
then result is boolean otherwise if the operands of
logical operators are integer then result is integer.
>>>4>3 or 3<2
True
>>> 0 or False
False
 Logical operator must be written in lowercase only.
>>>5 and 4
4
 Empty string is always False non-empty string is True.
>>>3 and ‘ab’ #‘ab’ is string so ab=1(True)
‘ab’
Logical Operator Examples
>>>not ‘null’ #String is treated as True
False
>>>not ‘’
True
>>>’ram’ and ‘shyam’
‘ram’
>>>3 and x=4 #Assignment operator does not work their
error
>>>3 or 5/0
3
>>>3 and 5/0 #Evaluation is infinite
error
4. Bitwise Operator
 Bitwise operators work upon bits Ex:- 0 and 1
Operators Expression Example
& 5 & 6 4
| 5 | 6 7
~ ~5 -6
^ 5^6 3
>> 99>>2 24
<< 5<<3 40
I. &(AND):- 5 & 6
 Convert 5 and 6 into binary
5 - 101 1 & 1 = 1
& 1 & 0 = 0
6 - 110 0 & 1 = 0
4 - 100 0 & 0 = 0
5 - 101 1 | 1 = 1
| 1 | 0 = 0
6 - 110 0 | 1 = 0
7 - 111 0 | 0 = 0
II. |(OR):- 5 | 6
III. ~(Complement):-
>>>~12
-13
~x = -x -1
~12 = -12-1 = -13
Note: Our system does not store the negative
numbers , first we convert the number into 2’s
complement and then store.
12 = 00001100
1’s complement = 11110011
13 = 00001101
1’s complement = 11110010
2’s complement = 11110011
IV. ^(XOR):- 5^6
5 - 0101 1 ^ 1 = 0
^ 1 ^ 0 = 1
6 - 0110 0 ^ 1 = 1
3 - 0011 0 ^ 0 = 0
V. >>(right shift) :- 99>>2 VI. <<(left shift) :- 5<<3
99=1100011 5 = 101
1st time = 0110001 1st time = 1010
2nd time = 0011000 =24 2nd time = 10100
3rd time = 101000=40
5. Assignment Operator
Operator expression
= X=5
+=,-=,*=,/=,%= X+=5
//=,**= X**=2
&=,/=,^= X&=3
>>=,<<= X>>=2
(i) x=5
x*=6
x=x*6
=5*6
=30
(ii) x=5
x+=6
x=x+6
=5+6
=11
(iii) x=5
x/=6
x=x/6
=5/6
=0.833
(iv) x=5
x%=6
x=x%6
=5%6
=5
(v) x=5
x//=2
x=5//2
x=2
(vi) x=5
x**=2
x=5**2
x=25
6. Identity Operator
Operator Explanation
is Return True if both variables
are same object.
is not Return False if both variables
are same object.
-> Every variable in python is an object.
->Objects are dynamically created.
-> Objects do not have name but references.
x=5 x
y=5
y
5
1000
>>>X=5
>>>Y=5
>>>X==Y
True
>>>X is Y
True
>>>X=5
>>>Y=5.0
>>>X==Y
True
>>>X is Y
False
>>>X is not Y
True
6. Membership Operator
Operator Explanation
in Return True if a sequence with the
specified value is present in the object.
not in Return False if a sequence with the
specified value is present in the object.
>>>x=“abc”
>>>‘a’ in x
True
>>>x=“abc”
>>>’A’ not in x
True
>>>x=256
>>>’5’ in x
error
>>>’5 ‘ in x
False
>>> x=1,2,3,4
>>> 4 in x
True
Practice Session
>>>x=25*3/2-1
36.5
>>>x=5>4>3>2
True
>>>x=3
>>>x * = 3 + 4
21
>>>x = 3 and 4
4
>>>x = 2 or ‘Hello’
2
>>>x = ‘Hello’ + 3
error
>>>x = ‘ab’ * 4
abababab
>>>x = 3 == ‘Three’
False
Python operators

More Related Content

What's hot (20)

PDF
Python strings
Mohammed Sikander
 
PDF
Puppeteer - Headless Chrome Node API
Wilson Su
 
PPTX
Python PPT
Edureka!
 
PDF
Control statements
Kanwalpreet Kaur
 
PDF
Get started python programming part 1
Nicholas I
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
PPT
4 greedy methodnew
abhinav108
 
PPTX
Conditional and control statement
narmadhakin
 
PPTX
Print input-presentation
Martin McBride
 
PPT
Divide and Conquer
Dr Shashikant Athawale
 
PPTX
11 Unit1 Chapter 1 Getting Started With Python
Praveen M Jigajinni
 
PPTX
Strings in Python
Amisha Narsingani
 
PPT
Introduction to Compiler
Radhakrishnan Chinnusamy
 
PDF
Introduction to python
Ahmed Salama
 
PPTX
Types of grammer - TOC
AbhayDhupar
 
PDF
Python Basics | Python Tutorial | Edureka
Edureka!
 
PPTX
PowerShell: Automation for Everyone
Intergen
 
PPTX
Namespace in C++ Programming Language
Himanshu Choudhary
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
Python strings
Mohammed Sikander
 
Puppeteer - Headless Chrome Node API
Wilson Su
 
Python PPT
Edureka!
 
Control statements
Kanwalpreet Kaur
 
Get started python programming part 1
Nicholas I
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
4 greedy methodnew
abhinav108
 
Conditional and control statement
narmadhakin
 
Print input-presentation
Martin McBride
 
Divide and Conquer
Dr Shashikant Athawale
 
11 Unit1 Chapter 1 Getting Started With Python
Praveen M Jigajinni
 
Strings in Python
Amisha Narsingani
 
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Introduction to python
Ahmed Salama
 
Types of grammer - TOC
AbhayDhupar
 
Python Basics | Python Tutorial | Edureka
Edureka!
 
PowerShell: Automation for Everyone
Intergen
 
Namespace in C++ Programming Language
Himanshu Choudhary
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 

Similar to Python operators (20)

PPTX
Operators and expressions in C++
Neeru Mittal
 
PDF
C++ Expressions Notes
Prof Ansari
 
PPTX
Operators expressions-and-statements
CtOlaf
 
PPT
Java 2
Preethi Nambiar
 
PPT
Java - Operators
Preethi Nambiar
 
PPTX
Operators in C & C++ Language
PreSolutions Softwares
 
PPTX
What are operators?
AnuragSrivastava272
 
PPTX
OPERATORS UNDER C++ POWER POINT PRESENTATION
sj9399037128
 
PDF
power point presentation on topic in C++ called "OPERATORS"
sj9399037128
 
PPT
Operators
Kamran
 
PPT
Fundamentals of Programming Chapter 5
Mohd Harris Ahmad Jaal
 
PPTX
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
PDF
Operators_in_Python_Simplified_languages
AbhishekGupta692777
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
Operators inc c language
Tanmay Modi
 
PPTX
Few Operator used in c++
sunny khan
 
PPT
C operators ppt
RajniKashyap9
 
PPT
C Sharp Jn (2)
guest58c84c
 
PPT
C Sharp Jn (2)
jahanullah
 
PDF
4. operators in c programming by digital wave
RahulSharma4566
 
Operators and expressions in C++
Neeru Mittal
 
C++ Expressions Notes
Prof Ansari
 
Operators expressions-and-statements
CtOlaf
 
Java - Operators
Preethi Nambiar
 
Operators in C & C++ Language
PreSolutions Softwares
 
What are operators?
AnuragSrivastava272
 
OPERATORS UNDER C++ POWER POINT PRESENTATION
sj9399037128
 
power point presentation on topic in C++ called "OPERATORS"
sj9399037128
 
Operators
Kamran
 
Fundamentals of Programming Chapter 5
Mohd Harris Ahmad Jaal
 
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
Operators_in_Python_Simplified_languages
AbhishekGupta692777
 
Operators and expressions in c language
tanmaymodi4
 
Operators inc c language
Tanmay Modi
 
Few Operator used in c++
sunny khan
 
C operators ppt
RajniKashyap9
 
C Sharp Jn (2)
guest58c84c
 
C Sharp Jn (2)
jahanullah
 
4. operators in c programming by digital wave
RahulSharma4566
 
Ad

Recently uploaded (20)

PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Day2 B2 Best.pptx
helenjenefa1
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Ad

Python operators

  • 3. Operators  An operator, in computer programming, is a symbol that usually represents an action or process.  It is a symbol that perform operation on one or more operands.  Operators required operands to perform their job. 3 4+ Operands Operators
  • 4. List Of Operators i. Arithmetic Operator ii. Relational Operator iii. Logical Operator iv. Bitwise Operator v. Assignment Operator vi. Identity Operator vii. Membership Operator
  • 5. 1. Arithmetic Operator  Arithmetic operators take numerical values as their operands and return a single numerical value. Operator Example Result + Addition 4+3 7 - Subtraction 4-3 1 * Multiplication 6*6 36 / Division 3/2 1.5 % Modulus 7%3 1 ** Exponent 3**2 9 // 3//2 1
  • 6. Arithmetic Operator Example 10**-2 0.01 >>> -2**3 -8 >>> -2**2 -4 >>> 10//3 3 >>> 10.0//3.0 3.0 >>> -4%3 2 >>> -4%-3 -1 >>> x='abc' >>> y='def' >>> x+y 'abcdef’ >>> True+1 2 >>> 'ab'*3 'ababab'
  • 7. 2. Relational Operator Oper ator 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.
  • 8. Relational Operators Examples  Relational operators always yield true or false as a result. >>>5>4 True >>>int(5>4) 1  It is also used to compare string. >>>’ac’==‘ac’ True >>>ac==ac error
  • 9.  5>4<2 if any result value is false all result value is false. >>>5>4>2 True  If we use equality(==) operator with any type of operand they never give error. >>>5==‘5’ False >>>’a’==97 False >>>5=5.0 True >>>’AB’==‘CD’ False
  • 10. 3. Logical Operator Operator Expression Example and a>b and a>c When both operands are True ,result will be True or a>b or a>c When both operands are False result will be False not not a>b Invert the result Expression 1 Expression 2 and or not(Exp 1) T T T T F T F F T F F T F T T F F F F T
  • 11.  These operators return boolean value by validating more than one expression.  If the operands of logical operator are boolean value then result is boolean otherwise if the operands of logical operators are integer then result is integer. >>>4>3 or 3<2 True >>> 0 or False False  Logical operator must be written in lowercase only. >>>5 and 4 4  Empty string is always False non-empty string is True. >>>3 and ‘ab’ #‘ab’ is string so ab=1(True) ‘ab’
  • 12. Logical Operator Examples >>>not ‘null’ #String is treated as True False >>>not ‘’ True >>>’ram’ and ‘shyam’ ‘ram’ >>>3 and x=4 #Assignment operator does not work their error >>>3 or 5/0 3 >>>3 and 5/0 #Evaluation is infinite error
  • 13. 4. Bitwise Operator  Bitwise operators work upon bits Ex:- 0 and 1 Operators Expression Example & 5 & 6 4 | 5 | 6 7 ~ ~5 -6 ^ 5^6 3 >> 99>>2 24 << 5<<3 40
  • 14. I. &(AND):- 5 & 6  Convert 5 and 6 into binary 5 - 101 1 & 1 = 1 & 1 & 0 = 0 6 - 110 0 & 1 = 0 4 - 100 0 & 0 = 0 5 - 101 1 | 1 = 1 | 1 | 0 = 0 6 - 110 0 | 1 = 0 7 - 111 0 | 0 = 0 II. |(OR):- 5 | 6
  • 15. III. ~(Complement):- >>>~12 -13 ~x = -x -1 ~12 = -12-1 = -13 Note: Our system does not store the negative numbers , first we convert the number into 2’s complement and then store. 12 = 00001100 1’s complement = 11110011 13 = 00001101 1’s complement = 11110010 2’s complement = 11110011
  • 16. IV. ^(XOR):- 5^6 5 - 0101 1 ^ 1 = 0 ^ 1 ^ 0 = 1 6 - 0110 0 ^ 1 = 1 3 - 0011 0 ^ 0 = 0 V. >>(right shift) :- 99>>2 VI. <<(left shift) :- 5<<3 99=1100011 5 = 101 1st time = 0110001 1st time = 1010 2nd time = 0011000 =24 2nd time = 10100 3rd time = 101000=40
  • 17. 5. Assignment Operator Operator expression = X=5 +=,-=,*=,/=,%= X+=5 //=,**= X**=2 &=,/=,^= X&=3 >>=,<<= X>>=2
  • 18. (i) x=5 x*=6 x=x*6 =5*6 =30 (ii) x=5 x+=6 x=x+6 =5+6 =11 (iii) x=5 x/=6 x=x/6 =5/6 =0.833 (iv) x=5 x%=6 x=x%6 =5%6 =5 (v) x=5 x//=2 x=5//2 x=2 (vi) x=5 x**=2 x=5**2 x=25
  • 19. 6. Identity Operator Operator Explanation is Return True if both variables are same object. is not Return False if both variables are same object. -> Every variable in python is an object. ->Objects are dynamically created. -> Objects do not have name but references.
  • 20. x=5 x y=5 y 5 1000 >>>X=5 >>>Y=5 >>>X==Y True >>>X is Y True >>>X=5 >>>Y=5.0 >>>X==Y True >>>X is Y False >>>X is not Y True
  • 21. 6. Membership Operator Operator Explanation in Return True if a sequence with the specified value is present in the object. not in Return False if a sequence with the specified value is present in the object. >>>x=“abc” >>>‘a’ in x True >>>x=“abc” >>>’A’ not in x True >>>x=256 >>>’5’ in x error >>>’5 ‘ in x False >>> x=1,2,3,4 >>> 4 in x True
  • 22. Practice Session >>>x=25*3/2-1 36.5 >>>x=5>4>3>2 True >>>x=3 >>>x * = 3 + 4 21 >>>x = 3 and 4 4 >>>x = 2 or ‘Hello’ 2 >>>x = ‘Hello’ + 3 error >>>x = ‘ab’ * 4 abababab >>>x = 3 == ‘Three’ False