Python Operators
Python Operators are symbols that perform operations on variables and values. They are the building blocks of expressions and statements. Here are some commonly used operators:
Arithmetic Operators Python
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 |
Real-life Examples of Python Operators in Arithmetic
- Arithmetic: Calculating total cost at a store.
- Adding the prices of items in a shopping cart to find the total bill.
- Calculating the total score of a student by adding individual scores in different subjects.
- Determining the total cost of buying multiple items with the same price by multiplying the price by the quantity.
Examples
- Addition (+): Adding two numbers together.
2 + 3 = 5
Python- Subtraction (-): Finding the difference between two numbers.
5 - 3 = 2
Python- Multiplication (*): Multiplying two numbers.
2 * 3 = 6
Python- Division (/): Dividing one number by another.
6 / 2 = 3
Python- Modulus (%): Finding the remainder after division.
7 % 3 = 1
PythonAssignment Operators in Python
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 |
Examples
Assign (=): Assigns a value to a variable.
x = 5
PythonAdd and assign (+=): Adds a value to a variable and assigns the result.
x += 2 # Equivalent to x = x + 2
PythonBitwise Operators in Ptython
Bitwise operators are used to deal with binary operations.
Name | Operator | Example |
---|---|---|
Bitwise AND | Bitwise AND | a & b |
Bitwise OR | Bitwise OR | a |
Bitwise NOT | Bitwise NOT | ~a |
Bitwise XOR | Bitwise XOR | a ^ b |
Bitwise right shift | Bitwise right shift | a>> |
Bitwise left shift | Bitwise left shift | b<< |
Comparison Operators in Python
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 |
Example of Python Comparison Operators
- Equal to (==): Checks if two values are equal.
2 == 2 # True
Python- Not equal to (!=): Checks if two values are not equal.
2 != 3 # True
Python- Greater than (>): Checks if one value is greater than another.
5 > 3 # True
Python- Less than (<): Checks if one value is less than another.
2 < 4 # True
PythonIdentity Operators in Python
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 in Python
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) |
Example of Python Logical Operators
Logical Operators: Combine conditional statements.
- AND (and): Returns True if both conditions are true.
(2 > 1) and (3 < 5) # True
Python- OR (or): Returns True if at least one condition is true.
(2 > 1) or (3 > 5) # True
Python- NOT (not): Returns the opposite of the condition.
not (2 > 1) # False
PythonMembership 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 |
Python Operators In Precedence
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 |
Conclusion
Python operators are fundamental tools for performing various operations in programming. Understanding how to use them effectively is crucial for writing efficient and expressive code. With real-life examples and practice, you can master the use of operators in Python.
Frequently Asked Questions
Ans: Operators are symbols used to perform operations on variables and values in Python.
Q2. Can I use operators with different data types?
Ans: Yes, Python is flexible with data types, but some operations may not be applicable to certain combinations of types.
Q3. Are there any other types of operators?
Ans: Yes, there are bitwise operators, membership operators, and identity operators, among others.