0% found this document useful (0 votes)
3 views2 pages

Python Operators Explained with Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python Operators Explained with Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Operators (With Examples) - Class 11

1. Arithmetic Operators
Arithmetic operators perform mathematical operations.

Examples:
a = 10
b=3

a + b → 13
a-b→7
a * b → 30
a / b → 3.333...
a // b → 3
a%b→1
a ** b → 1000

2. Relational (Comparison) Operators


Relational operators compare two values and return True/False.

Examples:
5 == 5 → True
5 != 3 → True
10 > 7 → True
4 < 2 → False
6 >= 6 → True
3 <= 1 → False

3. Membership Operators
Used to check membership in sequences (string, list, tuple).

Examples:
'a' in 'apple' → True
5 in [1, 2, 3] → False
3 not in (1, 2, 3) → False

4. Identity Operators
Identity operators compare whether two variables refer to the same object.

Examples:
a = [1,2,3]
b=a
c = [1,2,3]

a is b → True
a is c → False
a is not c → True

5. Logical Operators
Used to combine conditions.

Examples:
(5 > 2) and (3 < 4) → True
(5 > 7) or (3 < 4) → True
not (5 > 2) → False

6. Assignment Operators
Assignment operators assign values to variables.

Examples:
x = 10
x += 5 → x becomes 15
x -= 3 → x becomes 12
x *= 2 → x becomes 24
x /= 4 → x becomes 6.0
x %= 4 → x becomes 2
x **= 3 → x becomes 8

End of PDF. Let me know if you want more examples or questions added.

Common questions

Powered by AI

The floor division operator ('//') is used instead of regular division ('/') when an integer result is needed. It divides and returns the largest integer less than or equal to the division result. This operator is particularly useful when performing integer division where the fractional part is irrelevant or should be discarded, thus avoiding type conversion or additional rounding operations that might be required with regular division. The use of floor division is common in scenarios like calculating indices in data structures or discretizing values .

To determine equivalence of two data sequences in Python, one can utilize both membership and identity operators to validate both content and object reference. Using membership operators ('in', 'not in'), one can iterate and ensure each element in one sequence appears in the other, validating their content equivalently. Identity operators ('is', 'is not'), when applied, ensure both sequences reference the same object in memory, which is a stricter form of equivalence. Therefore, combining '==' for content equivalence and 'is' for reference identity yields a comprehensive equivalence check .

Assignment operators in Python modify the value of a variable by performing an operation and assigning the result to the variable. Examples include '=', '+=', '-=', '*=', '/=', '%=', '**='. The simple assignment operator '=' assigns a value directly, while compound operators perform arithmetic operations with the current value before reassigning it. For instance, 'x += 5' increases the current value of 'x' by 5. These operators streamline code by combining arithmetic operations with assignments, enhancing readability and efficiency .

Logical operators in Python include 'and', 'or', and 'not', which are used to combine multiple conditions into a single logical expression. 'and' ensures all conditions must be true for the expression to be true, 'or' requires at least one condition to be true, and 'not' reverses the truth value of the condition it precedes. These operators allow the construction of complex conditional expressions to encapsulate multiple criteria within an if-statement, enabling more sophisticated decision-making processes within a script .

Arithmetic operators in Python include addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**). These operators perform mathematical operations: '+' adds two operands, '-' subtracts the second operand from the first, '*' multiplies operands, '/' divides the first operand by the second yielding a float, '//' divides and returns the largest integer smaller or equal to the result, '%' returns the remainder, and '**' raises the first operand to the power of the second .

The 'is' identity operator could lead to unexpected results when comparing complex objects or literals. 'is' checks for reference equality, meaning it only returns True if both operands point to the same object, which might not necessarily be the case for identical-looking but independently created objects like lists or dictionaries. For example, two lists with the same content are not 'is' equal unless they reference the same object. These issues can be mitigated by using '==' for value equality, which checks for equivalency in content rather than identity in memory. Understanding these nuances is crucial to prevent logical errors in condition checks .

The 'not' logical operator inverts the Boolean value of an expression, turning True into False and vice versa. This operation is crucial in condition checking when the focus is on excluding certain scenarios or when testing for exceptions to a condition. By altering the outcome of the expression, 'not' allows developers to simplify logical expressions and create more intuitive conditional flows, ensuring that code executes in scenarios explicitly marked as invalid or undesired .

The modulus operator ('%') calculates the remainder of a division operation and is highly useful in addressing tasks involving periodicity, such as determining whether a number is even or odd by checking if it is divisible by 2. It can also be used for cyclic data structures, like ring buffers, ensuring that indices wrap around when they exceed a predetermined limit. This technique aids in implementing features such as loops in animations, recurring events in simulations, or round-robin scheduling in computing tasks .

Relational operators in Python, such as '==', '!=', '>', '<', '>=', and '<=', compare values and return a Boolean value (True or False). They are crucial in decision making, allowing scripts to execute different branches of code based on the comparisons. For instance, using '>' to check if one number is greater than another can determine which code block executes. This ability to evaluate conditions and branch accordingly is fundamental to implementing control flow in programs .

Membership operators ('in', 'not in') and identity operators ('is', 'is not') serve different purposes. Membership operators check for the presence of an element within a sequence like a string, list, or tuple. For example, 'a' in 'apple' returns True if 'a' is found in 'apple'. Identity operators determine if two references point to the same object, such as 'a is b' being True if both variables point to the same memory location. Use cases for membership include validations or searching within data structures, while identity operators are useful for checking object references or ensuring unique identities .

You might also like