Python Operators (With Examples and interview questions) | AsgarTech
Python Operators (With Examples) - AsgarTech
In Python, an operator is a symbol or a keyword used to perform a specific operation on one or more operands. Python supports various types of operators, such as arithmetic, relational, assignment, logical, bitwise, and membership operators.
Arithmetic operators: Arithmetic operators are used to perform mathematical operations, such as addition, subtraction, multiplication, division, and modulus.
Example:
x = 10
y = 5
# Addition
print(x + y) # Output: 15
# Subtraction
print(x - y) # Output: 5
# Multiplication
print(x * y) # Output: 50
# Division
print(x / y) # Output: 2.0
# Modulus
print(x % y) # Output: 0
Relational operators: Relational operators are used to compare values and return a boolean value.
Example:
x = 10
y = 5
# Greater than
print(x > y) # Output: True
# Less than
print(x < y) # Output: False
# Greater than or equal to
print(x >= y) # Output: True
# Less than or equal to
print(x <= y) # Output: False
# Equal to
print(x == y) # Output: False
# Not equal to
print(x != y) # Output: True
Assignment operators: Assignment operators are used to assign values to variables.
Example:
x = 10
y = 5
# Assignment
x = y
print(x) # Output: 5
# Addition assignment
x += y
print(x) # Output: 10
# Subtraction assignment
x -= y
print(x) # Output: 5
# Multiplication assignment
x *= y
print(x) # Output: 25
# Division assignment
x /= y
print(x) # Output: 5.0
# Modulus assignment
x %= y
print(x) # Output: 0
Logical operators: Logical operators are used to combine multiple conditions and return a boolean value.
Example:
x = 10
y = 5
# AND operator
print(x > y and x < 20) # Output: True
# OR operator
print(x > y or x < 20) # Output: True
# NOT operator
print(not(x > y and x < 20)) # Output: False
Bitwise operators: Bitwise operators are used to perform bitwise operations on binary numbers.
Example:
x = 10
y = 5
# Bitwise AND operator
print(x & y) # Output: 0
# Bitwise OR operator
print(x | y) # Output: 15
# Bitwise XOR operator
print(x ^ y) # Output: 15
# Bitwise left shift operator
print(x << 1) # Output: 20
# Bitwise right shift operator
print(x >> 1) # Output: 5
Membership operators: Membership operators are used to check if a value exists in a sequence.
Example:
my_list = [1, 2, 3, 4, 5]
# in operator
print(3 in my_list) # Output: True
# not in operator
print(6 not in my_list) # Output: True
These are the different types of operators available in Python.
here are some interview questions related to operators in Python:
What is the difference between "==" and "is" operators in Python?
The "==" operator is used to compare the values of two objects in Python, while the "is" operator is used to compare the identities of two objects. "==" checks for equality of values, whereas "is" checks for identity.
What is operator precedence in Python?
Operator precedence refers to the order in which Python evaluates the expressions that contain multiple operators. Python follows a set of rules to determine the order of evaluation, which is based on the precedence level of each operator. Operators with higher precedence are evaluated before operators with lower precedence.
How can you overload an operator in Python?
In Python, you can overload an operator by defining a special method for the class that corresponds to the operator. The method should have the same name as the operator and should take one or more arguments that correspond to the operands.
What are the bitwise operators in Python?
The bitwise operators in Python are "&" (bitwise AND), "|" (bitwise OR), "^" (bitwise XOR), "~" (bitwise NOT), "<<" (bitwise left shift), and ">>" (bitwise right shift).
What is the difference between "and" and "&" operators in Python?
The "and" operator is used for boolean expressions, while the "&" operator is used for bitwise operations. The "and" operator returns the first operand if it is false, and the second operand otherwise. The "&" operator performs a bitwise AND operation on the two operands.
What is the ternary operator in Python?
The ternary operator in Python is a shorthand way to write an if-else statement. It takes the form "x if condition else y", where "x" is the expression to evaluate if the condition is true, and "y" is the expression to evaluate if the condition is false.
What is the "not in" operator in Python?
The "not in" operator in Python is a membership operator that checks whether a value is not present in a sequence. It returns True if the value is not present in the sequence, and False otherwise.
What are the comparison operators in Python?
The comparison operators in Python are ">", "<", ">=", "<=", "==", and "!=". They are used to compare two values and return a Boolean value that indicates whether the comparison is true or false.
What is the difference between "or" and "|" operators in Python?
The "or" operator is used for boolean expressions, while the "|" operator is used for bitwise operations. The "or" operator returns the first operand if it is true, and the second operand otherwise. The "|" operator performs a bitwise OR operation on the two operands.
What is the difference between "is" and "==" operators in Python?
The "is" operator is used to check if two objects have the same identity, while the "==" operator is used to check if two objects have the same value. The "is" operator checks if two objects are the same object in memory, while the "==" operator checks if two objects have the same value, even if they are not the same object in memory.
No comments: