0% found this document useful (0 votes)
170 views8 pages

Python Operators Overview

The document discusses the different types of operators in Python including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It provides examples of common operators like +, -, *, / as well as precedence rules for how operations are performed.

Uploaded by

mdmazharmm0786
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)
170 views8 pages

Python Operators Overview

The document discusses the different types of operators in Python including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It provides examples of common operators like +, -, *, / as well as precedence rules for how operations are performed.

Uploaded by

mdmazharmm0786
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

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

print(5+4)

Types of operator

There are seven types of operatorython divides the operators in the following groups:

Arithmetic operators

Assignment operators

Comparison operators

Logical operators

Identity operators

Membership operators

Bitwise operators

Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:

Operator Name Example

+ Addition x+y

- Subtraction x-y
* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3
*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

//= x //= 3 x = x // 3

**= x **= 3 x = x ** 3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


Python Comparison Operators
Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Python Logical Operators
Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))

Example

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated before additions:

print(100 + 5 * 3)

The precedence order is described in the table below, starting with the highest precedence at the top:

Operator Description Try it

() Parentheses

** Exponentiation

+x -x ~x Unary plus, unary minus, and bitwise NOT

* / // % Multiplication, division, floor division, and modulus

+ - Addition and subtraction

<< >> Bitwise left and right shifts

& Bitwise AND

^ Bitwise XOR

| Bitwise OR

== != > >= < <= is is not in not in Comparisons, identity, and membership operators

not Logical NOT

and AND

or OR

If two operators have the same precedence, the expression is evaluated from left to right. Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))

Example

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated before additions:

print(100 + 5 * 3)

The precedence order is described in the table below, starting with the highest precedence at the top:

Operator Description Try it

() Parentheses

** Exponentiation

+x -x ~x Unary plus, unary minus, and bitwise NOT

* / // % Multiplication, division, floor division, and modulus

+ - Addition and subtraction

<< >> Bitwise left and right shifts

& Bitwise AND

^ Bitwise XOR

| Bitwise OR

== != > >= < <= is is not in not in Comparisons, identity, and membership operators

not Logical NOT

and AND

or OR

If two operators have the same precedence, the expression is evaluated from left to right.

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))

Example

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated before additions:

print(100 + 5 * 3)
The precedence order is described in the table below, starting with the highest precedence at the top:

Operator Description Try it

() Parentheses

** Exponentiation

+x -x ~x Unary plus, unary minus, and bitwise NOT

* / // % Multiplication, division, floor division, and modulus

+ - Addition and subtraction

<< >> Bitwise left and right shifts

& Bitwise AND

^ Bitwise XOR

| Bitwise OR

== != > >= < <= is is not in not in Comparisons, identity, and membership operators

not Logical NOT

and AND

or OR

If two operators have the same precedence, the expression is evaluated from left to right.

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be
evaluated first:

print((6 + 3) - (6 + 3))

Example

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated
before additions:

print(100 + 5 * 3)

The precedence order is described in the table below, starting with the highest precedence at the
top:

Operator Description Try it

() Parentheses

** Exponentiation

+x -x ~x Unary plus, unary minus, and bitwise NOT


* / // % Multiplication, division, floor division, and modulus

+ - Addition and subtraction

<< >> Bitwise left and right shifts

& Bitwise AND

^ Bitwise XOR

| Bitwise OR

== != > >= < <= is is not in not in Comparisons, identity, and membership operators

not Logical NOT

and AND

or OR

If two operators have the same precedence, the expression is evaluated from left to right.

Common questions

Powered by AI

Operator precedence in Python dictates the order in which operations are performed within an expression. This impacts the way expressions are evaluated, ensuring that some operations are executed before others. For instance, multiplication and division operations have higher precedence than addition and subtraction, so in the expression 100 + 5 * 3, the multiplication is performed first, resulting in 115 rather than 315 . Parentheses override standard precedence rules, allowing explicit control over evaluation order to achieve the desired calculation sequence .

Logical operators ('and', 'or', 'not') in Python facilitate complex conditional logic by enabling multiple conditions to be combined within a single statement. 'And' requires both conditions to be true, 'or' requires at least one condition to be true, and 'not' inverses the truth value of a condition. Evaluating a statement like if (x > 10 and y < 5) or z == 1 involves evaluating each sub-condition sequentially based on precedence and combining their results. This combination allows developers to succinctly capture intricate decision rules crucial for operations such as user permission checks, simultaneous condition assessments, or control flow management in programs .

The 'not' operator in Python is a logical operator that is used to reverse the outcome of a condition. If the original condition is true, applying 'not' will make it false, and vice versa. It is significant in scenarios where you need to test for the absence or opposite of a particular condition. For example, the expression not(x < 5 and x < 10) evaluates to true if the condition x < 5 and x < 10 is false . The 'not' operator is essential for controlling the flow of logic in programs, especially in conditional statements .

Arithmetic operators perform fundamental mathematical operations on numeric data types, such as addition, subtraction, multiplication, and division (e.g., 5 + 4 evaluates to 9). Bitwise operators, on the other hand, deal with the binary level of numbers, performing operations like AND, OR, XOR, and bit shifts. For example, the bitwise AND of binary numbers 0110 (6 in decimal) and 0101 (5 in decimal) results in 0100 (4 in decimal). These operators enable manipulations of bits within integer data types directly .

In Python, the identity operators (is, is not) compare whether two references point to the same object in memory, whereas equality operators (==, !=) test if two objects have the same value, regardless of whether they are the same instance. For example, consider two lists: a = [1, 2, 3] and b = a, where a is b returns True because b is simply a reference to a. However, if b = [1, 2, 3], then a == b returns True as the lists have the same values, but a is b returns False because they are distinct objects in memory . This distinction is critical when comparing complex objects in Python, where object identity can affect logic flow and performance .

The modulo operator (%) in Python calculates the remainder of a division between two numbers. For instance, 10 % 3 evaluates to 1, as 3 fits into 10 three times with a remainder of 1 . A practical application is determining if a number is even or odd: an integer n is even if n % 2 equals 0; otherwise, it is odd. This operation is frequently used in algorithms that require partitioning or cycling operations based on whether numbers are divisible by certain divisors .

Assignment operators in Python are used to assign values to variables and modify them. They include simple assignment (=) as well as compound assignments that perform an operation and assignment in one step, such as +=, -=, *=, etc. For example, x += 3 is equivalent to x = x + 3 . Arithmetic operators, on the other hand, perform mathematical computations on values (e.g., + for addition) and don't inherently alter variable assignments unless combined with assignment operators . Assignment operators simplify coding by reducing redundancy and enhancing readability during variable updates .

Membership operators in Python, 'in' and 'not in', test whether a value exists within a sequence, such as a list, tuple, or string. The 'in' operator returns True if the specified value is found, and 'not in' returns True if it is not. For example, in the list lst = [1, 2, 3, 4], 3 in lst returns True. A common application is checking user-supplied data against predefined valid inputs or verifying the presence of a character in a string for text processing tasks . Membership operators are efficient for implementing validation checks and data filtering .

Floor division in Python uses the // operator to divide two numbers and then round down to the nearest whole number, essentially removing any decimal fraction. In contrast, standard division (/) results in a floating-point number. For instance, 7 // 3 yields 2, while 7 / 3 yields approximately 2.33 . Floor division is particularly useful in scenarios requiring integer-based partitions or index calculations, such as scheduling tasks to run every n hours, where fractional indices would be illogical or unusable .

Comparison operators are critical in decision-making processes within a Python application. They are used to compare values, enabling conditional execution of code segments based on the outcomes. Scenarios include verifying user input (e.g., ensuring password length is greater than 8 characters), managing loops (e.g., iterating while a counter is less than a limit), and implementing sorting algorithms (e.g., comparing elements to sort a list). Operators like ==, !=, <, >, <=, and >= are indispensable for any logic requiring evaluation of conditions or branching based on variable comparisons .

You might also like