Sunday, November 5, 2023

Python Operators

In Python, operators are special symbols or keywords used to perform various operations on variables, values, or expressions. Python provides a wide range of operators, including arithmetic operators, comparison operators, logical operators, assignment operators, and more. Here are some of the most commonly used Python operators:

1. Arithmetic Operators:
   - Addition: `+`
   - Subtraction: `-`
   - Multiplication: `*`
   - Division: `/`
   - Floor Division (integer division): `//`
   - Modulus (remainder): `%`
   - Exponentiation: `**`

Example:...


2. Comparison Operators:   
   - Equal to: `==`
   - Not equal to: `!=`
   - Greater than: `>`
   - Less than: `<`
   - Greater than or equal to: `>=`
   - Less than or equal to: `<=`

Example:


3. Logical Operators:
   - Logical AND: `and`
   - Logical OR: `or`
   - Logical NOT: `not`

Example:


4. Assignment Operators:
   - Assignment: `=`
   - Add and assign: `+=`
   - Subtract and assign: `-=`
   - Multiply and assign: `*=`
   - Divide and assign: `/=`
   - Modulus and assign: `%=`
   - Floor divide and assign: `//=`
   - Exponentiate and assign: `**=`

Example:



5. Identity Operators:
   - `is`: Returns `True` if both operands are the same object.
   - `is not`: Returns `True` if both operands are not the same object.

Example:


6. Membership Operators:
   - `in`: Returns `True` if a value is found in a sequence (e.g., list, tuple, string).
   - `not in`: Returns `True` if a value is not found in a sequence.

Example:


7. Bitwise Operators (for working with binary numbers):
   - Bitwise AND: `&`
   - Bitwise OR: `|`
   - Bitwise XOR: `^`
   - Bitwise NOT: `~`
   - Left shift: `<<`
   - Right shift: `>>`

These operators are used for low-level operations and are less commonly used in everyday Python programming.

These are the basic operators in Python, but there are more operators and nuances to explore as you delve deeper into Python programming. Operators are fundamental for performing calculations, making decisions, and manipulating data in your Python programs.

No comments:

Post a Comment