Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python Operators

Uploaded by

tejaswinignteju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Operators

Uploaded by

tejaswinignteju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lesson Plan:

Python Operators
Topics to be covered:
Python Operator
Operators Type
Operators Precedenc
Operators associativity

Python Operators:
Python operators are symbols or special keywords that are used to perform operations on values or
variables

In Python programming, Operators allow you to manage data, do computations, and make decisions.

Operators Types:
There are several types of python operators as follows:

2.1. Arithmetic Operators: Arithmetic operators are used for mathematical operations.

Addition (+): Combining two or more values.

Subtraction (-): Finding the difference between two values.

Data Science With Generative AI Course


Multiplication (*): Multiplying values.

Division (/): Dividing values.

Modulus (%): Finding the remainder of a division.

Exponentiation (**): Raising a value to a power.

2.2. Comparison Operators: Comparison operators are used to compare two values and return a
Boolean result (True or False).

Equal to (‘==’): Checks if two values are equal.

Not equal to (‘!=’): Checks if two values are equal.

Greater than (>): Checks if one value is greater than another.

Data Science With Generative AI Course


Less than (<): Checks if one value is less than another.

Greater than or equal to (>=): Checks if one value is greater than or equal to another.

Less than or equal to (<=): Checks if one value is less than or equal to another.

2.3. Logical Operators: Logical operators are used to combine and manipulate Boolean values.

Logical AND (and): Returns True if both conditions are True.

Logical OR (or): Returns True if at least one condition is True

Logical NOT (not): Negates a condition (True becomes False, and False becomes True).

2.4. Bitwise Operators: In Python, bitwise operators are used to perform operations at the bit-level,
manipulating individual bits within integers.

Bitwise AND (&): Returns 1 for each bit position where both operands have a 1.

Data Science With Generative AI Course


AND Operation Table

Example:

Bitwise OR (|): Returns 1 for each bit position where at least one of the operands has a 1.

OR Operation Table

Data Science With Generative AI Course


Example:

Bitwise NOT (~): Inverts the bits; 1s become 0s and vice versa.

NOT Operation Table

Example:

Bitwise XOR (^): Returns 1 for each bit position where exactly one of the operands has a 1.

XOR Operation Table

Data Science With Generative AI Course


Example:

Left Shift (<<): Shifts the bits to the left by a specified number of positions, filling with 0s on the right.

Right Shift (>>): Shifts the bits to the right by a specified number of positions, filling with 0s on the left (for non-
negative numbers) or sign bits (for negative numbers).

2.5. Assignment Operators: Assignment operators are used to assign values to variables.

Assignment (=): Assigns a value to a variable.

Addition Assignment (+=): Adds a value to the variable and updates it.

Data Science With Generative AI Course


Subtraction Assignment (-=): Subtracts a value from the variable and updates it.

Multiplication Assignment (*=): Multiplies the variable by a value and updates it.

Division Assignment (/=): Divides the variable by a value and updates it.

2.6. Membership Operators: Membership operators are used to test if a value is present in a sequence.

in: Checks if a value is present in a sequence (e.g., a string, list, or tuple).

not in: Checks if a value is not present in a sequence.

2.7. Identity Operators: Identity operators are used to compare the memory location of two objects.

is: Returns True if both variables point to the same object.

Data Science With Generative AI Course


is not: Returns True if both variables point to different objects..

Operator’s Precedence:
Python operators have different levels of precedence, which determine the order in which they are
evaluated in an expression. Higher precedence operators are evaluated first

Here's a simplified Python operator precedence table, arranged from highest precedence to lowest
precedence:

Data Science With Generative AI Course


Parentheses():

Example:

2. Exponentiation’ ** ’: This operator raises a number to a power.

Example:

3. Unary Plus +X, Unary Minus -X, Bitwise NOT ~X (X is variable):

Example:

Data Science With Generative AI Course


4. Multiplication ‘*’, Division ‘/’, Floor Division ‘//’, Modulus ‘%’:
Example:

5. Addition +, Subtraction :
Example:

6. Bitwise Shift Operators <<, >>:


Example:

Data Science With Generative AI Course


7. Bitwise AND ‘&’, Bitwise XOR ‘^’, Bitwise OR ‘|’:

Example:

8. Comparisons, Identity, Membership Operators:


Example:

Data Science With Generative AI Course


9. Logical NOT ‘not’, Logical AND ‘and’, Logical OR ‘or’:

Example:

Operators Associativity:
Operator associativity in Python is an important concept that determines how operators are grouped and
evaluated in expressions. Here's a concise explanation in four bullet points:

1. Order of Evaluation: Operator associativity defines the order in which operators of the same precedence are
evaluated within an expression. It specifies whether operators are evaluated from left to right or right to left.

2. Left-to-Right Default: By default, most operators in Python have left-to-right associativity, which means they
are evaluated from left to right when they appear in sequence in an expression.

3. Right-to-Left Associativity: Some operators, like the exponentiation operator `**`, have right-to-left
associativity, meaning they are evaluated from right to left. This can affect the order of operations in
expressions with multiple operators of the same precedence.

4. Control with Parentheses: If you need to override the default associativity and explicitly control the order of
evaluation, you can use parentheses `()` to group operations. This allows you to ensure that specific operations
are performed before others.

Data Science With Generative AI Course


Python Operator’s Associativity:

Data Science With Generative AI Course

You might also like