0% found this document useful (0 votes)
4 views

Lecture 3 b Handout Operators

Uploaded by

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

Lecture 3 b Handout Operators

Uploaded by

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

Operators in Python

Operators in Python are symbols or keywords used to perform operations on variables and values.
They allow you to manipulate data and build expressions in a program. Python provides a variety of
operators categorized based on their functionality.

1. Arithmetic Operators
Used for basic mathematical operations:

Operator Description Example Output


+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division 5/2 2.5
// Floor Division 5 // 2 2
% Modulus (remainder) 5%2 1
** Exponentiation 2 ** 3 8

2. Comparison Operators
Used to compare two values and return a Boolean result (True or False):

Operator Description Example Output


== Equal to 5 == 3 False
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 5<3 False
>= Greater than or equal to 5 >= 3 True
<= Less than or equal to 5 <= 3 False

3. Logical Operators
Used to combine conditional statements:

Operator Description Example Output


and Returns True if both are true True and False False
or Returns True if one is true True or False True
not Reverses the Boolean value not True False

1
4. Assignment Operators
Used to assign values to variables:

Operator Description Example Equivalent To


= Assignment x=5 x=5
+= Add and assign x += 3 x=x+3
-= Subtract and assign x -= 2 x=x-2
*= Multiply and assign x *= 3 x=x*3
/= Divide and assign x /= 2 x=x/2
//= Floor divide and assign x //= 2 x = x // 2
%= Modulus and assign x %= 3 x=x%3
**= Exponent and assign x **= 2 x = x ** 2

5. Bitwise Operators
Operate at the binary level on integers:

Operator Description Example Output


& AND 5&3 1
| OR 5|3 7
^ XOR 5^3 6
~ NOT ~5 -6
<< Left shift 5 << 1 10
>> Right shift 5 >> 1 2

6. Membership Operators
Used to check for membership in sequences like lists, strings, or tuples:

Operator Description Example Output


in Checks if value exists 'a' in 'apple' True
not in Checks if value does not exist 'b' not in 'apple' True

7. Identity Operators
Used to compare memory locations of objects:

Operator Description Example Output


is Same memory location x is y True
is not Different locations x is not y False

You might also like