0% found this document useful (0 votes)
24 views13 pages

Operators Python

The document provides an overview of various operators in Python, including Arithmetic, Assignment, Identity, and Logical Operators. It details the functions and examples of each operator type, explaining how they are used in programming. The document also emphasizes the differences in memory addresses for objects when using identity operators.
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)
24 views13 pages

Operators Python

The document provides an overview of various operators in Python, including Arithmetic, Assignment, Identity, and Logical Operators. It details the functions and examples of each operator type, explaining how they are used in programming. The document also emphasizes the differences in memory addresses for objects when using identity operators.
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
You are on page 1/ 13

=======================================================

1. Arithmetic Operators
=======================================================
=>The purpose of Arithmetic Operators is that "To perform Arithmetic Operations such as
Addition,Substraction, Multiplicaion..etc".
=>If two or more Variables (Objects) connected with Arithmetic Operators then we call it as Arithmetic
Expression.
=>In Python Programming, we have 7 Arithmetic Operators. They are given in the following table.
================================================================================
=
SLNO SYMBOL MEANING EXAMPLE a=10 b=3
================================================================================
=
1. + Addition print(a+b)---------->13

2. - Substraction print(a-b)----------->7

3. * Multiplication print(a*b)----------->30

4. / Division print(a/b)---------->3.333333333333335
(Float Quotient)

5. // Floor Division print(a//b)---------->3


(Integer Quotient)

6. % Modulo Division print(a%b)--------->1


(remainder)
7. ** Exponentiation print(a**b)--------->1000
(power of )
================================================================================
=

==================================================
2. Assigment Operator
==================================================
=>The purpose of assignment operator is that " To assign or transfer Right Hand Side (RHS) Value /
Expression Value to the Left Hand Side (LHS) Variable "
=>The Symbol for Assigment Operator is single equal to ( = ).
=>In Python Programming,we can use Assigment Operator in two ways.
1. Single Line Assigment
2. Multi Line Assigment

1. Single Line Assigment:


----------------------------------------
=>Syntax: LHS Varname= RHS Value
LHS Varname= RHS Expression

=>With Single Line Assigment at a time we can assign one RHS Value / Expression to the single LHS
Variable Name.
------------------
Examples:
-----------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print(a,b,c)------------10 20 30
-----------------------------------------------------------------------------------
2. Multi Line Assigment:
----------------------------------------

=>Syntax: Var1,Var2.....Var-n = Val1,Val2....Val-n

Var1,Var2.....Var-n= Expr1,Expr2...Expr-n

Here The values of Val1, Val2...Val-n are assigned to Var1,Var2...Var-n Respectively.


Here The values of Expr1, Expr2...Expr-n are assigned to Var1,Var2...Var-n Respectively.

Examples:
-------------------
>>> a,b=10,20
>>> print(a,b)------------10 20
>>> c,d,e=a+b,a-b,a*b
>>> print(c,d,e)-------------30 -10 200
>>> sno,sname,marks=10,"Rossum",34.56
>>> print(sno,sname,marks)---------10 Rossum 34.56
===================================X======================================

=====================================================
Identity Operators--Python Command / IDLE Shell Basis
=====================================================
=>The purpose of Identity Operators is that "To Compare Memory Address of Two Objects".
=>In Python Programming, we have Two Types of Identity Operators. They are
1. is
2. is not
----------------------------------------------------------------------------------------------------------------------------------------
1. is
----------------------------------------------------------------------------------------------------------------------------------------
=>Syntax: Object1 is Object2
=>"is" Operator returns True Provided Both the objects Contains SAME Address.
=>"is" Operator returns False Provided Both the objects Contains Different Address.
----------------------------------------------------------------------------------------------------------------------------------------
2. is not
----------------------------------------------------------------------------------------------------------------------------------------
Syntax: Object1 is not Object2
=>"is not " Operator returns True Provided Both the objects Contains Different Address.
=>"is not " Operator returns False Provided Both the objects Contains Same Address.
----------------------------------------------------------------------------------------------------------------------------------------
Examples:
----------------------------------------------------------------------------------------------------------------------------------------
>>> a=None
>>> b=None
>>> print(a,type(a),id(a))---------None <class ’NoneType’> 140704386324472
>>> print(b,type(b),id(b))--------None <class ’NoneType’> 140704386324472
>>> a is b---------True
>>> a is not b---------False
------------------------------------------------------------------------------------------------------------------------------------
>>> d1={10:"Apple",20:"Mango",30:"Kiwi"}
>>> d2={10:"Apple",20:"Mango",30:"Kiwi"}
>>> print(d1,type(d1),id(d1))-----{10: ’Apple’, 20: ’Mango’, 30: ’Kiwi’} <class ’dict’> 2864804287360
>>> print(d2,type(d2),id(d2))-----{10: ’Apple’, 20: ’Mango’, 30: ’Kiwi’} <class ’dict’> 2864804287488
>>> d1 is d2--------False
>>> d1 is not d2--True
------------------------------------------------------------------------------------------------------------------------------------
>>> s1={10,20,30,40}
>>> s2={10,20,30,40}
>>> print(s1,type(s1),id(s1))---------{40, 10, 20, 30} <class ’set’> 2864805025088
>>> print(s2,type(s2),id(s2))---------{40, 10, 20, 30} <class ’set’> 2864805024640
>>> s1 is s2-------------------False
>>> s1 is not s2-------------True
>>> fs1=frozenset(s1)
>>> fs2=frozenset(s1)
>>> print(fs1,type(fs1),id(fs1))--------frozenset({40, 10, 20, 30}) <class ’frozenset’> 2864805026208
>>> print(fs2,type(fs2),id(fs2))-------frozenset({40, 10, 20, 30}) <class ’frozenset’> 2864805026656
>>> fs1 is fs2-------------------------False
>>> fs1 is not fs2-------------------True
------------------------------------------------------------------------------------------------------------------------------------
>>> l1=[10,"Rossum",22.22]
>>> l2=[10,"Rossum",22.22]
>>> print(l1,type(l1),id(l1))----------[10, ’Rossum’, 22.22] <class ’list’> 2864804317696
>>> print(l2,type(l2),id(l2))---------[10, ’Rossum’, 22.22] <class ’list’> 2864804318400
>>> l1 is l2------------------False
>>> l1 is not l2-----------True
>>> t1=(10,20,30,40)
>>> t2=(10,20,30,40)
>>> print(t1,type(t1),id(t1))------------(10, 20, 30, 40) <class ’tuple’> 2864804748304
>>> print(t2,type(t2),id(t2))------------(10, 20, 30, 40) <class ’tuple’> 2864804752304
>>> t1 is t2--------------------------------False
>>> t1 is not t2--------------------------True
------------------------------------------------------------------------------------------------------------------------------------
>>> r1=range(10,20)
>>> r2=range(10,20)
>>> print(r1,type(r1),id(r1))--------range(10, 20) <class ’range’> 2864804444656
>>> print(r2,type(r2),id(r2))--------range(10, 20) <class ’range’> 2864804444224
>>> r1 is r2----------------------False
>>> r1 is not r2---------------True
>>> ba1=bytearray([10,20,30,40])
>>> ba2=bytearray([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))---bytearray(b’\n\x14\x1e(’) <class ’bytearray’> 2864808582512
>>> print(ba2,type(ba2),id(ba2))---bytearray(b’\n\x14\x1e(’) <class ’bytearray’> 2864808582896
>>> ba1 is ba2--------False
>>> ba1 is not ba2---True
>>> ba1=bytes([10,20,30,40])
>>> ba2=bytes([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))----b’\n\x14\x1e(’ <class ’bytes’> 2864804444320
>>> print(ba2,type(ba2),id(ba2))---b’\n\x14\x1e(’ <class ’bytes’> 2864804442592
>>> ba1 is ba2-----False
>>> ba1 is not ba2----True
---------------------
>>> s1="PYTHON"
>>> s2="PYTHON"
>>> print(s1,type(s1),id(s1))---PYTHON <class ’str’> 2864808583024
>>> print(s2,type(s2),id(s2))---PYTHON <class ’str’> 2864808583024
>>> s1 is s2-----True
>>> s1 is not s2---False
----------------------------
>>> s1="JAVA"
>>> s2="JAVa"
>>> print(s1,type(s1),id(s1))--------JAVA <class ’str’> 2864808582000
>>> print(s2,type(s2),id(s2))-------JAVa <class ’str’> 2864808582512
>>> s1 is s2------------False
>>> s1 is not s2------True
--------------------------------------------------------------------------------------------------------------------------------------
>>> a=2+3j
>>> b=2+3j
>>> print(a,type(a),id(a))--------(2+3j) <class ’complex’> 2864803995536
>>> print(b,type(b),id(b))-------(2+3j) <class ’complex’> 2864803999280
>>> a is b-----------False
>>> a is not b-----True
>>> a=True
>>> b=True
>>> print(a,type(a),id(a))---------True <class ’bool’> 140704386272104
>>> print(b,type(b),id(b))--------True <class ’bool’> 140704386272104
>>> a is b--------True
>>> a is not b---False
>>> c=False
>>> print(c,type(c),id(c))-----False <class ’bool’> 140704386272136
>>> a is c-------False
>>> a is not c-------True
>>> a=1.2
>>> b=1.2
>>> print(a,type(a),id(a))---------1.2 <class ’float’> 2864803995984
>>> print(b,type(b),id(b))--------1.2 <class ’float’> 2864803999280
>>> a is b--------------------False
>>> a is not b--------------True
----------------------------
>>> a=300
>>> b=300
>>> print(a,type(a),id(a))-----300 <class ’int’> 2864803995600
>>> print(b,type(b),id(b))----300 <class ’int’> 2864803999184
>>> a is b-----------False
>>> a is not b------True
>>> a=10
>>> b=10
>>> print(a,type(a),id(a))--------10 <class ’int’> 2864802955792
>>> print(b,type(b),id(b))-------10 <class ’int’> 2864802955792
>>> a is b---------True
>>> a is not b------False
>>> a=256
>>> b=256
>>> print(a,type(a),id(a))----256 <class ’int’> 2864802963664
>>> print(b,type(b),id(b))----256 <class ’int’> 2864802963664
>>> a is b--------True
>>> a is not b---False
>>> a=257
>>> b=257
>>> print(a,type(a),id(a))----257 <class ’int’> 2864803995600
>>> print(b,type(b),id(b))---257 <class ’int’> 2864803999248
>>> a is b-------False
>>> a is not b----True
>>> a=0
>>> b=0
>>> print(a,type(a),id(a))------0 <class ’int’> 2864802955472
>>> print(b,type(b),id(b))-----0 <class ’int’> 2864802955472
>>> a is b------------True
>>> a is not b-------False
---------------------------------------
>>> a=-1
>>> b=-1
>>> print(a,type(a),id(a))---------- -1 <class ’int’> 2864802955440
>>> print(b,type(b),id(b))--------- -1 <class ’int’> 2864802955440
>>> a is b--------True
>>> a is not b---False
>>> a=-5
>>> b=-5
>>> print(a,type(a),id(a))------------ -5 <class ’int’> 2864802955312
>>> print(b,type(b),id(b))------------ -5 <class ’int’> 2864802955312
>>> a is b--------------True
>>> a is not b---------False
>>> a=-6
>>> b=-6
>>> print(a,type(a),id(a))-------- -6 <class ’int’> 2864803999248
>>> print(b,type(b),id(b))------ -6 <class ’int’> 2864803995600
>>> a is b-----------------False
>>> a is not b----------True
---------------------------------------------------------------------------------------------
MOST IMP
---------------------------------------------------------------------------------------------
>>> a,b=400,400 # Multi Line Assigment
>>> print(a,type(a),id(a))-----400 <class ’int’> 2864803995728
>>> print(b,type(b),id(b))----400 <class ’int’> 2864803995728
>>> a is b-----True
>>> a is not b---False
>>> a,b=1.2,1.2 # # Multi Line Assigment
>>> print(a,type(a),id(a))---------1.2 <class ’float’> 2864803999120
>>> print(b,type(b),id(b))---------1.2 <class ’float’> 2864803999120
>>> a is b---------True
>>> a is not b----False
>>> a,b=2+3j,2+3j
>>> print(a,type(a),id(a))-------(2+3j) <class ’complex’> 2864803999344
>>> print(b,type(b),id(b))------(2+3j) <class ’complex’> 2864803999344
>>> a is b--------------------True
>>> a is not b--------------False
---------------------------------------------------------------------------------------------
MOST MOST IMP
---------------------------------------------------------------------------------------------
>>> l1,l2=[10,20,30],[10,20,30]
>>> print(l1,type(l1),id(l1))---------[10, 20, 30] <class ’list’> 2864804318400
>>> print(l2,type(l2),id(l2))--------[10, 20, 30] <class ’list’> 2864808577600
>>> l1 is l2---------------False
>>> l1 is not l2--------True
>>> d1,d2={10:"Python",20:"Java"},{10:"Python",20:"Java"}
>>> print(d1,type(d1),id(d1))---{10: ’Python’, 20: ’Java’} <class ’dict’> 2864804318976
>>> print(d2,type(d2),id(d2))---{10: ’Python’, 20: ’Java’} <class ’dict’> 2864804287872
>>> d1 is d2----------False
>>> d1 is not d2-----True
---------------------------------------------------------------------------------------------------------------------------------------
NOTE:
----------
=>What are all the objects Participates in DEEP Copy then Those Objects Contains Same Address and
"is" operator Returns True and "is not" operator Returns False.
=>What are all the objects Participates in SHALLOW DEEP Copy then Those Objects Contains Different
Address and "is" operator Returns False and "is not" operator Returns True.
---------------------------------------------------------------------------------------------------------------------------------------

===================================================
4. Logical Operators (Comparision Operators)
===================================================
=>The purpose of Logical Operators is that "To Combine Two or More Relational Expressions".
=>If Two or More Rerlational Expressions are connected with Logical Operators then we call it as Logical

Expressions OR Compound Condition.


=>The Result of Logical Expressions OR Compound Condition gives either True of False.
=>In Python Programming, we have 3 Types of Logical Operators and They are given in the following
Table.
================================================================================
=
SLNO SYMBOL MEANING
================================================================================
=
1. and Physical ANDing

2. or Physical ORing

3. not ------------------------
================================================================================
=
1) and Operator
=================
=>The Functionality of ’and’ Operator is given in the following Truth Table
-------------------------------------------------------------------------------------
RelExpr1 RelExpr2 RelExpr1 and RelExpr2
-------------------------------------------------------------------------------------
True False False

False True False

False False False

True True True


-------------------------------------------------------------------------------------
Examples:
------------------
>>> True and False----------False
>>> False and False---------False
>>> False and True----------False
>>> True and True-----------True
-------------------------------
>>> 10>5 and 20>40-------------False
>>> 10<30 and 30>3------------True
>>> 10<5 and 20>15------------False---Short Circuit Evaluation
>>> 10>20 and 45>30 and 45>20-------False--Short Circuit Evaluation
>>> 10>5 and 20>30 and 40>5 and -10>-20-----False---Short Circuit Evaluation
--------------------------------------------------------------------------------
Short Circuit Evaluation in the case of ’and’ Operator
--------------------------------------------------------------------------------
=>If Two or More relational expressions are connected with ’and’ Operator (Logical Expression) and if
First Relational Expr result is False then PVM will not evaluate rest of the Relational Expressions and
PVM Gives result of Entire Logical Expression as False. This Process of Evaulation is called "Short
Circuit Evaluation".
===============================================================================
2) or Operator
=================
=>The Functionality of ’or’ Operator is given in the following Truth Table
-------------------------------------------------------------------------------------
RelExpr1 RelExpr2 RelExpr1 or RelExpr2
-------------------------------------------------------------------------------------
True False True

False True True

False False False

True True True


-------------------------------------------------------------------------------------
Examples:
------------------
>>> True or False----------True
>>> False or True---------True
>>> False or False-------False
>>> True or True---------True
------------------------------------------------
>>> 10<5 or 30>20---------True
>>> 10>20 or 20>30 or 40>20-------True
>>> 10>20 or 20>30 or 40>40----False
>>> 10>5 or 20>40 or 40>30----True----Short Circuit Evaluation
>>> 100>200 or 500>2 or 500>1000 or -10>-5---True---Short Circuit Evaluation
--------------------------------------------------------------------------------
Short Circuit Evaluation in the case of ’or’ Operator
--------------------------------------------------------------------------------
=>If Two or More relational expressions are connected with ’or’ Operator (Logical Expression) and if
First Relational Expr result is True then PVM will not evaluate rest of the Relational Expressions and
PVM Gives result of Entire Logical Expression as True. This Process of Evaulation is called "Short Circuit
Evaluation".
===============================================================================
3) not Operator
=================
=>The Functionality of ’not’ Operator is given in the following Truth Table
-------------------------------------------------------------------------------------
RelExpr1 not RelExpr1
-------------------------------------------------------------------------------------
True False

False True
-------------------------------------------------------------------------------------
Examples:
------------------
>>> a=True
>>> print(a,type(a))--------True <class ’bool’>
>>> b=not a
>>> print(b,type(b))-------False <class ’bool’>
-----------------------------
>>> print(10>20)--------False
>>> print(not 10>20)----True
>>> print(not 100)-------False
>>> print(not -100)------False
>>> print(not 0)---------True
>>> 10>20 and 30>40------False
>>> not(10>20 and 30>40)-----True
>>> 10>2 or 20>40----------------True
>>> not(10>2 or 20>40)---------False
-------------------
>>> print(not "Python")--------False
>>> print(not "")------------------True
=============================================================================
MOST IMP Questions
--------------------------------------------------------
>>> 100 and 200---------------200
>>> -100 and -150------------- -150
>>> 100 and 200 and 300---- 300
>>> 100 and 0 and 400-------- 0
>>> 100 and True and False and 234---- False
>>> "PYTHON" and "False" and "JAVA"----’JAVA’
>>> #----------------------------------------------
>>> 10 or 20---------------10
>>> 10 or 20 or 0---------10
>>> 0 or 20 or 30--------20
>>> 0 or 20 or 0---------20
>>> "False" or "True" or "False"-------’False’
>>> False or "True" or "False"---------’True’
>>> False or True or "False"-----------True
>> 100 and 200 or 300---------200
>>> 100 or 200 and 300 and -100 or 201---- 100
>>> "Python" and "Java" and "Django" or "Python-Dsc"---’Django’
>>> True and False or True or False and True and 100----True
================================================================================
=============================================
6. Membership Operators
=============================================
=>The purpose of membership operators is that "To Check whether the Specific Value present in Iterable
object Or Not".
=>An Iterable object is one, which contains More Number of Values
(Examples: Sequence Type,List Type,Set type,Duct type)
=>In Python Programming, we have 2 types of Membership Operators. They are
1. in
2. not in
-----------------------------------------------------------------------------------------------------------------------------------------
1. in
-----------------------------------------------------------------------------------------------------------------------------------------
Syntax: Value in IterableObject
-----------
=>"in" operator returns True Provided Value present in Iterable object
=>"in" operator returns False Provided Value not present in Iterable object
-----------------------------------------------------------------------------------------------------------------------------------------
2. not in
-----------------------------------------------------------------------------------------------------------------------------------------
Syntax: Value not in IterableObject
-----------
=>"not in" operator returns True Provided Value not present in Iterable object
=>"not in" operator returns False Provided Value present in Iterable object
-----------------------------------------------------------------------------------------------------------------------------------------
Examples:
-----------------------------------------------------------------------------------------------------------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))------------PYTHON <class ’str’>
>>> "P" in s---------------------True
>>> "O" in s--------------------True
>>> "O" not in s--------------False
>>> "K" in s-------------------False
>>> "K" not in s--------------True
>>> "p" in s--------------------False
>>> "p" not in s--------------True
---------------------------------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))--------------PYTHON <class ’str’>
>>> "PYT" in s-------------------True
>>> "PYTH" not in s-----------False
>>> "PYTH" in s-----------------True
>>> "THON" not in s------------False
>>> "THON" in s----------------True
>>> s="PYTHON"
>>> print(s,type(s))-------------PYTHON <class ’str’>
>>> "PON" in s------------------False
>>> "HON" in s------------------True
>>> "PON" not in s-------------True
>>> "NOH" in s-----------------False
>>> "NOH" not in s------------True
>>> "PTO" in s-----------------False
>>> s="PYTHON"
>>> print(s,type(s))---------------PYTHON <class ’str’>
>>> "NOH" in s[::-1]---------------True
>>> "PTO" not in s[::2]----------False
>>> "PTO" in s[::2]---------------True
>>> "OTP" not in s[1::-2]----------True
>>> s[1::-2]-----------------------------’Y’
--------------------------------------------------------------
>>> lst=[10,"Rossum",True,2+3j]
>>> print(lst,type(lst))----------------[10, ’Rossum’, True, (2+3j)] <class ’list’>
>>> 10 in lst-----------------------------True
>>> True not in lst----------------------False
>>> 2+3j in lst---------------------------True
>>> "Ros" in lst------------------------False
>>> "Ros" in lst[1]-------------------True
>>> "mus" not in lst[1][::-1]---------False
>>> lst[::-1] not in lst[::]---------------True
>>> lst[1][::-1] in lst[::]----------------False
>>> lst[1][::-1] in lst[-3][::-1][::-1]-----False
-------------------------------------------------------------------
>>> lst=[10,"Rossum",True,2+3j]
>>> print(lst,type(lst))-------------------------[10, ’Rossum’, True, (2+3j)] <class ’list’>
>>> 2+3j in lst----------------------------------True
>>> 2+3j not in lst----------------------------False
-------------------------
>>> 2 in lst[-1]---------------------TypeError: argument of type ’complex’ is not iterable
>>> 3 in lst[-1].imag------------TypeError: argument of type ’float’ is not iterable
------------------------------------------------------------------------
>>> d1={10:"Python",20:"Java",30:"C++"}
>>> print(d1,type(d1))---------------{10: ’Python’, 20: ’Java’, 30: ’C++’} <class ’dict’>
>>> 10 in d1----------------True
>>> 30 not in d1-----------False
>>> 40 in d1----------------False
>>> "Python" in d1---------False
>>> "Python" in d1[10]----True
>>> "C++" in d1.get(30)---------True
>>> "C++"[::-1] not in d1.get(30)[::-1]----False
>>> "C++"[::-1] in d1.get(30)[::-1]---------True
==============================x===============================================

=====================================================
3. Relational Operators (Comparision Operators)
=====================================================
=>The purpose of Relational Operators is that " To Compare Two Values OR Object Values".
=>If two or More Variables OR Objects Connected with Relational Operators then we it as Relational
Expression.
=>The Result of Relational Expression is Either True of False.
=>The Relational Expression is also Called Test Condition and it gives Either True or False.
=>In Python Programming, we have 6 Relational Operators. They are given in the following Table.
==============================================================================
SLNO SYMBOL MEANING EXAMPLES
==============================================================================
1. > Greater Than print(10>5)---------True
print(100>200)-----False

2. < LessThan print(10<20)-------True


print(100<50)------False
3. == Equality print(10==20)------False
(double equal to) print(100==100)---True

4. != Not Equal to print(10!=20)-------True


print(100!=100)----False

5. >= Greater Than print(10>=20)------False


or equal to print(10>=5)--------True

6. <= Less Than print(10<=20)-----True


or Equal to print(100<=50)----False
==============================================================================

==================================================
Operators and Expressions in Python
==================================================
=>An operator is a symbol, which is used to perform Certain Operation on given data and gives Result.
=>If Two or More Variables (Objects) Connected with an Operator then it is Called Expression,
=>In Otherwords, An Expression is a Collection of Variables (objects) connected with an an Operator.
=>In Python Programming, we have 7 Operators. They are

1. Arithmetic Operators
2. Assignment Operator
3. Relational Operators (Comparision Operators)
4. Logical Operators (Comparision Operators)
5. Bitwise Operators ( Most Imp )
6. Membership Operators
a) in
b) not in
7. Identity Operators
a) is
b) is not
====================================================================
NOTE1: Python Programming does not Support Unary Operators ( ++ , - - )
------------
NOTE2: Python Programming does not Support Ternary Operator of C,C++,C#.net, Java ( ? : )
------------------------------------------------------------------------------------------------------------------------------------------
NOTE3: Python Programming Contains Short Hand Operators
------------------
NOTE4: Python Programming Contains Its own Ternary Operator-- if...else operator
-----------------------------------------------------------------------------------------------------------------------------------------

==================================================
Ternary Operator in Python
==================================================
=>Python Programming contains a Ternary operator.
=>The Name of Ternary Operator in Pythin is "if ... else ".
----------------
=>Syntax varname = Expr1 if Test Cond else Expr2
----------------
Explanation
-------------------
=>Here if and else are the Keywords
=>Here Test Cond can be Either True Or False
=>Here Expr1 and Expr2 are Executable Statements
=>If the Result of Test Cond is True then PVM executes Expr1 and whose Placed in varname
=>If the Result of Test Cond is False then PVM executes Expr2 and whose Placed in varname
=>At any Point of Time PVM Executes Either Expr1 Or Expr2 and whose Result places in varname.
----------------------------------------------------------------------------------
======================================
4. Bitwise AND Operator (& )
======================================
=>The Functionality of Bitwise AND Operator ( | ) is shown in the following Truth table.

=> ------------------------------------------------------------
var1 var2 var1 & var2
------------------------------------------------------------
0 1 0
1 0 0
0 0 0
1 1 1
------------------------------------------------------------

Examples
----------------------------------
>>> 0&1---------------0
>>> 1&0---------------0
>>> 0&0---------------0
>>> 1&1---------------1
-----------------------------------
>>> a=8
>>> b=6
>>> c=a&b
>>> print(c)-------0
------------------
>>> a=10
>>> b=12
>>> c=a&b
>>> print(c)--------8
-----------------------------
Most Imp Points
-----------------------------
>>>s1={10,20,30}
>>>s2={10,15,25}
>>> s3=s1&s2 # Bitwise AND Operator for INTERSECTION Operation
>>> print(s3,type(s3))-------{10} <class ’set’>
--------------------------------------------
>>> s1={"Python","Data Sci"}
>>> s2={"C","C++","DSA","Python"}
>>> s3=s1&s2
>>> print(s3,type(s3))----------{’Python’} <class ’set’>
-----------------------
>>> s1={"Python","Data Sci"}
>>> s2={"C","C++","DSA"}
>>> s3=s1&s2
>>> print(s3,type(s3))-----------set() <class ’set’>
======================================x===============================

You might also like