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.
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