Operators in Python by AryaDrj
Operators in Python by AryaDrj
Topic covered:
- Arithmetic Operators
- (+, - , * , / , % ,**, //)
- Logical Operators
- (and, or , not)
- Unary minus operators
- (- variable)
- Membership Operators
- ( in , not in)
- Bitwise Operators
- (~,&,|,^,<<,>>)
- Identity Operators
- (is, is not)
- Relational Operators
- (>,>=,<,<=,==,!=)
1. The and operator in Python
- If x in false it will return x otherwise y
x=1
y=12
print( x and y )
2. Program 2:
Code:
a = 14
b = 4
print(b and a)
print(b & a)
- This is because ‘and’ tests whether both expressions are logically True while ‘&’performs bitwise AND operation on the result of both statements.
3. Unary minus operator (- variable)
n = 10
print(-n)
num = - 10
num = -num
print(num)
Bitwise Operator
- Bitwise AND operator (&)
- Bitwise OR operator (|)
- Bitwise XOR operator (^)
- Bitwise Left shift operator (<<)
- Bitwise Right shift operator (>>)
- Bitwise Complement operator (~)
Example 1:
#Bitwise Complement Operator (~)
x = 10
print(~x)
- It will change the 0's to 1's and vice versa and taking complement.
- Or formula is : -(x)-1
Example 2:
#Bitwise And Operator (~)
x = 10
y = 11
print(x&y)
Tracing:
10 = 0000 1010
11 = 0000 1011
10 & 11 = 0000 1010
= 10
Example 3:
#Bitwise OR Operator (~)
x = 10
y = 11
print(x&y)
Tracing:
10 = 0000 1010
11 = 0000 1011
10 & 11 = 0000 1011
= 11
Example 4:
#Bitwise XOR Operator (^)
x = 10
y = 11
print(x^y)
Tracing:
10 = 0000 1010
11 = 0000 1011
10 & 11 = 0000 0001
= 1
Example 5:
#Bitwise Left Shift Operator (<<)
x = 10
print(x<<2)
Tracing:
10 = 0000 1010
BCD Value :
128 64 32 16 8 4 2 1
Result = 0010 1000
Example 6:
#Bitwise Right Shift Operator (>>)
x = 10
print(x>>2)
Precedence and Associativity of Operators
Operators
- ()
- Parentheses
- **
- Exponent
- +x, -x, ~x
- Unary plus, Unary minus, Bitwise NOT
- *, /, //, %
- Multiplication, Division, Floor division, Modulus
- (Left to right)
- +, -
- Addition, Subtraction
- <<, >>
- Bitwise shift operators
- &
- Bitwise AND
- ^
- Bitwise XOR
- |
- Bitwise OR
- ==, !=, >, >=, <, <=, is, is not, in, not in
- Comparisons, Identity, Membership operators
- not
- Logical NOT
- and
- Logical AND
- or
- Logical OR
- = (Assignment Operator)
- (Right to left)
Solve this:
- -6 + 9 * 7
- (54+9) % 8
- 21 + -4*6 / 9
- 6 + 15 / 3 * 3 - 8 % 3
Solve this:
que= (x+y)*z**a//b+c
Where
x = 1
y = 2
z = 3
a = 2
b = 2
c = 3
Explanation:
1. First Parentheses are evaluated.
2. Exponentiation is done.
3. Multiplication ,Division ,Modulus and floor division.
4. Addition and subtraction are done afterwards.
5. Finally assignment operation is performed.
Example:
(x+y)*z**a//b+c
3*z**a//b+c
3*9//2+3
27//2+3
13+3
16 (Solved)
Questions :
- Which operator have lowest precedence value?
- Which operator have highest precedence value?
- What is operator?
- Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.
- a+b
- Here + is operator.
- What is operand?
- a+b
- Here a and b are operands.
5. What is expression?
- An expression is a combination of operators, constants and variables
- Example: 3*z**a//b+c
Comments
Post a Comment