-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Operators 4
More file actions
82 lines (76 loc) · 2.06 KB
/
Python Operators 4
File metadata and controls
82 lines (76 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Python Operators
Python has different types of operators for different operations. They are as follows:
Arithmetic operators:
Arithmetic operators are used to perform arithmetic/mathematical operations.
Name Operator Example
Addition + a+b
Subtraction - a-b
Multiplication * a*b
Division / a/b
Exponential ** a**b
Modulus % a%b
Floor Division // a//b
Assignment operators:
These operators are used to assign values to variables.
Name Evaluated As
= a=b
+= a+=b or a=a+b
-= a-=b or a=a-b
*= a*=b or a=a*b
**= a**=b or a=a**b
/= a/=b or a=a/b
//= a//=b or a=a//b
%= a%=b or a=a%b
&= a&=b or a=a&b
** =**
^= a^=b or a=a^b
>>= a>>=b or a=a>>b
<<= a<<=b or a=a<<b
Bitwise operators:
Bitwise operators are used to deal with binary operations.
Name Operator Example
Bitwise AND & a & b
Bitwise OR | a | b
Bitwise NOT ~ ~a
Bitwise XOR ^ a ^ b
Bitwise right shift >> a>>
Bitwise left shift << b<<
Comparison operators:
These operators are used to compare values.
Name Operator Example
Equal == a==b
Not Equal != a!=b
Less Than < a<b
Greater Than > a>b
Less Than or Equal to <= a<=b
Greater Than or Equal to >= a>=b
Identity operators:
Name Example Evaluated As
is a is b Returns True if a and b are same
is not a is not b Returns True if a and b are not same
Logical operators:
These operators are used to deal with logical operations.
Name Operator Example
AND and a=2 and b=3
OR or a=2 or b=3
NOT not Not(a=2 or b=3)
Membership operators:
Name Example Evaluated As
in a in b Returns True if a is present in given sequence or collection
not in a not in b Returns True if a is not present in given sequence or collection
Operator Precedence in Python:
Name Operator
Parenthesis ()
Exponential **
Complement, unary plus, unary minus ~ , +, -
Multiply, divide, modulus, floor division *, /, %, //
Addition, subtraction +, -
Left shift and right shift operators <<, >>
Bitwise and &
Bitwise or and xor ^, |
Comparison operators <, >, >=, <=
Equality operators ==, !=
Assignment operators =, %=, /=, //=, -=, +=, *= , **=
Identity operators is, is not
Membership operators in, not in
Logical operators and, or, not