Python Operators


Operators are special symbols in Python used to perform operations on variables and values.
For example, + is an operator used to add two values.

Python provides several types of operators:


🧮 Types of Operators

Type Example Operators
Arithmetic +, -, *, /, //, %, **
Assignment =, +=, -=, *=, /=, etc.
Comparison ==, !=, >, <, >=, <=
Logical and, or, not
Identity is, is not
Membership in, not in
Bitwise &, `

➕ Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division
% Modulus
** Exponentiation

📝 Assignment Operators

Used to assign values to variables:

  • =, +=, -=, *=, /=, %=, //=, **=, etc.

Example:

x = 10
x += 5   # Now x becomes 15

⚖️ Comparison Operators

Used to compare two values and return True or False.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater or equal
<= Less or equal

🤔 Logical Operators

Used to combine conditional statements.

Operator Description
and True if both are true
or True if at least one is true
not Reverses the result

🔎 Identity Operators

Used to compare memory locations.

Operator Meaning
is Returns True if both variables point to the same object
is not Returns True if they do not

📦 Membership Operators

Used to check if a value is in a sequence (list, string, etc.)

Operator Meaning
in True if present
not in True if not present

⚙️ Bitwise Operators

Works on binary numbers.

Operator Name
& AND
` `
^ XOR
~ NOT
<< Left Shift
>> Right Shift

Practice Questions

Q1. Write a Python program to add two numbers using the + operator.

Q2. Write a Python program to subtract one number from another using the - operator.

Q3. Write a Python program to multiply two numbers using the * operator.

Q4. Write a Python program to divide two numbers using both / and // operators and observe the difference in the output.

Q5. Write a Python program to use the ** operator to calculate the square of a number.

Q6. Write a Python program to check if two values are equal using the == operator.

Q7. Write a Python program to use the and operator to check if two conditions are true.

Q8. Write a Python program to use the in operator to check if a word exists in a string.

Q9. Write a Python program to use the is operator to check if two variables refer to the same object.

Q10. Write a Python program to perform bitwise AND on two integers using the & operator.


Go Back Top