Python Operators

1. What is Python operators

Python operators are fundamental components of the Python programming language. They are symbols or special characters that enable developers to perform specific actions or operations on variables, values, or expressions. These operators act as building blocks for manipulating data and controlling program flow.
Python offers a comprehensive set of operators, each designed to serve a specific purpose. These operators can be classified into various categories, such as arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators. Each category caters to different aspects of computation and programming logic.

2. Arithmetic operators

Arithmetic operators are used to perform basic mathematical calculations. They include addition, subtraction, multiplication, division, floor division, etc. These operators allow for straightforward numerical computations and are essential for tasks involving calculations and formula-based operations:

  1. Addition (+): Adds two operands.
  2. Subtraction (-): Subtracts the right operand from the left operand.
  3. Multiplication (*): Multiplies two operands.
  4. Division (/): Divides the left operand by the right operand (resulting in a float).
  5. Floor Division (//): Divides the left operand by the right operand and returns the integer quotient.
  6. Modulus (%): Returns the remainder of the division.
  7. Exponentiation (**): Raises the left operand to the power of the right operand.

Example

3. Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is the equals sign (=), which assigns the value on the right side to the variable on the left side. Additionally, there are compound assignment operators such as +=, -=, *=, /=, %=, and **=, which combine arithmetic operations with assignment:

  1. Assignment (=): Assigns a value to a variable.
  2. Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  3. Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  4. Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
  5. Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  6. Modulus Assignment (%=): Performs modulus on the left operand with the right operand and assigns the result to the left operand.
  7. Exponentiation Assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.

Example




4. Comparison Operators

Comparison operators are used to compare values and determine their relationship. They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators are useful for making decisions based on conditions and for creating conditional statements like if-else and while loops.

  1. Equal to (==): Checks if the values of two operands are equal.
  2. Not equal to (!=): Checks if the values of two operands are not equal.
  3. Greater than (>): Checks if the left operand is greater than the right operand.
  4. Less than (<): Checks if the left operand is less than the right operand.
  5. Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  6. Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

Example

5. Logical Operators

Logical operators are used to combine and manipulate logical values (True and False) and Boolean expressions. The logical AND (and), logical OR (or), and logical NOT (not) operators enable developers to perform logical operations, such as checking multiple conditions or negating a condition, which are fundamental for control flow and decision-making in programming.

  1. Logical AND (and): Returns True if both operands are True.
  2. Logical OR (or): Returns True if at least one of the operands is True.
  3. Logical NOT (not): Returns the opposite of the operand's logical value.

Example

Bitwise Operators

Bitwise operators are used to manipulate individual bits within binary representations of data. They include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>). These operators are primarily used in scenarios that involve low-level programming, bit manipulation, or working with binary data.

  1. Bitwise AND (&): Performs a bitwise AND operation on the binary representations of two operands.
  2. Bitwise OR (|): Performs a bitwise OR operation on the binary representations of two operands.
  3. Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation on the binary representations of two operands.
  4. Bitwise NOT (~): Performs a bitwise NOT operation on the binary representation of the operand.
  5. Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
  6. Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.

Example

 

Younes Derfoufi
CRMEF OUJDA

Leave a Reply