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)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Functions
FunctionsFunctions
Functions
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Recursion
RecursionRecursion
Recursion
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Strings in c
Strings in cStrings in c
Strings in c
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Python Loop
Python LoopPython Loop
Python Loop
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Python Basics.pdf
Python Basics.pdfPython Basics.pdf
Python Basics.pdf
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Python programming : Exceptions
Python programming : ExceptionsPython programming : Exceptions
Python programming : Exceptions
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
De Morgan Theorem B[1]
De Morgan Theorem B[1]De Morgan Theorem B[1]
De Morgan Theorem B[1]
 

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

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 

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