2Operators

Operators are fundamental in programming, they allow us to process and modify values.

As an example, let's say that we want to add two values together:

value1 = 10              #our variables
value2 = 5

sum = value1 + value2   #adding two values together with "+" operator
print(sum)              #dispalying the value

As we can see, operators are very important, they allow us to process stuff.

Operators:

Arithmetic Operators

  • + (Addition): Adds two values. Example: 5 + 3 β†’ 8

  • - (Subtraction): Subtracts the second value from the first. Example: 5 - 3 β†’ 2

  • * (Multiplication): Multiplies two values. Example: 5 * 3 β†’ 15

  • / (Division): Divides the first by the second (float result). Example: 5 / 2 β†’ 2.5

  • // (Floor Division): Divides and floors the result. Example: 5 // 2 β†’ 2

  • % (Modulus): Returns the remainder. Example: 5 % 2 β†’ 1

  • ** (Exponentiation): Raises the first to the power of the second. Example: 2 ** 3 β†’ 8


Comparison Operators

  • == (Equal): Checks if values are equal. Example: 5 == 5 β†’ True

  • != (Not Equal): Checks if values are not equal. Example: 5 != 3 β†’ True

  • > (Greater Than): Checks if first is greater. Example: 5 > 3 β†’ True

  • < (Less Than): Checks if first is less. Example: 5 < 3 β†’ False

  • >= (Greater Than or Equal): Example: 5 >= 5 β†’ True

  • <= (Less Than or Equal): Example: 5 <= 3 β†’ False


Logical Operators

  • and (Logical AND): True if both are True. Example: True and False β†’ False

  • or (Logical OR): True if at least one is True. Example: True or False β†’ True

  • not (Logical NOT): Inverts the value. Example: not True β†’ False


Assignment Operators

  • = (Assign): Assigns a value. Example: x = 5

  • += (Add and Assign): Example: x += 3 (x becomes 8 if x was 5)

  • -= (Subtract and Assign): Example: x -= 3 (x becomes 2 if x was 5)

  • *= (Multiply and Assign): Example: x *= 3 (x becomes 15 if x was 5)

  • /= (Divide and Assign): Example: x /= 2 (x becomes 2.5 if x was 5

Last updated