Skip to content

Latest commit

 

History

History

04-Python-Operators

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Python Operators

Operators in Python are symbols that instruct the interpreter to perform specific operations. They are fundamental tools in any programming language, allowing you to manipulate data, evaluate conditions, and perform various tasks within your code. Python categorizes operators into several groups, including Arithmetic, Relational, Logical, and Bitwise operators. Let's dive deeper into these operator groups and their functionality.

Assignment Operator

  • The = sign is the assignment operator, used to assign a value to a variable.
  • You can assign multiple values in one line, such as x, y = 5, 10.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations:

  • Addition (+): Adds two values.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies two values.
  • Division (/): Divides one value by another.
  • Power (**): Raises a number to the power of another, e.g., 2 ** 3 equals 8.
  • Floor Division (//): Divides two numbers and rounds down to the nearest whole number.
  • Modulo (%): Returns the remainder of a division operation.

Expressions

  • An expression is a combination of values, variables, operators, and calls to functions that the interpreter evaluates and computes to produce another value. For example, 3 + 5 or x * y.

  • Unary vs. Binary Operators:

    • Unary Operators: Operate on a single operand (e.g., -x).
    • Binary Operators: Operate on two operands (e.g., x + y).

Relational Operators

Relational operators 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 not equal.
  • Greater than (>): Checks if the left value is greater than the right.
  • Less than (<): Checks if the left value is less than the right.
  • Greater than or equal to (>=): Checks if the left value is greater than or equal to the right.
  • Less than or equal to (<=): Checks if the left value is less than or equal to the right.

Important Note: The assignment operator = is different from the equality operator ==. The former assigns a value to a variable, while the latter checks if two values are equal.

Logical Operators

Logical operators are used to combine conditional statements:

  • And (and): Returns True if both statements are true.
  • Or (or): Returns True if at least one statement is true.
  • Not (not): Reverses the Boolean value of the operand.

Combination Operators

Combination operators, also known as compound assignment operators, combine two operations into one. For example:

  • x += 1 is equivalent to x = x + 1.
  • Other examples include -=, *=, /=, %=, **=, and //=.

String Operators

String operators manipulate text:

  • Concatenation (+): Joins two strings together into one (e.g., 'Hello' + 'World' results in 'HelloWorld').
  • Repetition (*): Repeats a string a specified number of times (e.g., 'A' * 3 results in 'AAA').
  • Slicing ([]): Extracts parts of a string.
    • Basic Slice ([x]): Accesses the character at position x.
    • Range Slice ([x:y]): Extracts a substring from position x to y-1.

Bitwise Operators

Bitwise operators perform operations on binary numbers at the bit level:

  • Bitwise AND (&): Compares each bit of two numbers and returns 1 if both bits are 1.
  • Bitwise OR (|): Compares each bit of two numbers and returns 1 if at least one of the bits is 1.
  • Bitwise NOT (~): Inverts all the bits of the number.
  • Bitwise XOR (^): Compares each bit of two numbers and returns 1 if the bits are different.
  • Shift Right (>>): Shifts the bits of the number to the right.
  • Shift Left (<<): Shifts the bits of the number to the left.

Understanding these operators is crucial for performing various tasks in Python, from simple arithmetic to more complex data manipulations and logic operations.