Lokang 

Python and MySQL

Operator

I'll provide the operator symbols along with a code example for each operator in Python.

Arithmetic Operators:

  • + Addition:
result = 3 + 4
print(result)  # Output: 7

- Subtraction:

result = 10 - 5
print(result)  # Output: 5

* Multiplication:

result = 3 * 7
print(result)  # Output: 21

/ Division:

result = 10 / 2
print(result)  # Output: 5.0

% Modulus:

result = 10 % 3
print(result)  # Output: 1

** Exponentiation:

result = 4 ** 2
print(result)  # Output: 16

// Floor Division:

result = 8 // 3
print(result)  # Output: 2

 

Comparison Operators:

== Equal to:

result = (5 == 3)
print(result)  # Output: False

!= Not equal to:

result = (5 != 3)
print(result)  # Output: True

> Greater than:

result = (5 > 3)
print(result)  # Output: True

< Less than:

result = (5 < 3)
print(result)  # Output: False

>= Greater than or equal to:

result = (5 >= 5)
print(result)  # Output: True

<= Less than or equal to:

result = (3 <= 5)
print(result)  # Output: True

 

Logical Operators:

and Logical AND:

result = (5 > 3) and (4 > 2)
print(result)  # Output: True

or Logical OR:

result = (5 > 3) or (4 < 2)
print(result)  # Output: True

not Logical NOT:

result = not(5 > 3)
print(result)  # Output: False

 

Assignment Operators:

= Assignment:

number = 5
print(number)  # Output: 5

+= Add AND assignment:

number = 5
number += 3  # Equivalent to number = number + 3
print(number)  # Output: 8

-= Subtract AND assignment:

number = 5
number -= 3  # Equivalent to number = number - 3
print(number)  # Output: 2

*= Multiply AND assignment:

number = 5
number *= 3  # Equivalent to number = number * 3
print(number)  # Output: 15

/= Divide AND assignment:

number = 10
number /= 2  # Equivalent to number = number / 2
print(number)  # Output: 5.0

More assignment operators exist such as %=, //=, **=, &=, |=, ^=, <<=, >>=; they work in a similar fashion by performing the operation and then assigning the result.

Bitwise Operators:

  • & Bitwise AND:
result = 5 & 3  # 5 = 0101, 3 = 0011, 0101 & 0011 = 0001
print(result)  # Output: 1

| Bitwise OR:

result = 5 | 3  # 5 = 0101, 3 = 0011, 0101 | 0011 = 0111
print(result)  # Output: 7

^ Bitwise XOR:

result = 5 ^ 3  # 5 = 0101, 3 = 0011, 0101 ^ 0011 = 0110
print(result)  # Output: 6

~ Bitwise NOT:

result = ~5  # ~0101 = 1010
print(result)  # Output: -6 (due to two's complement)

<< Bitwise left shift:

result = 5 << 1  # 0101 << 1 = 1010
print(result)  # Output: 10

>> Bitwise right shift:

result = 5 >> 1  # 0101 >> 1 = 0010
print(result)  # Output: 2

 

Membership Operators:

in:

x = 'Hello world'
result = 'Hello' in x
print(result)  # Output: True

not in:

x = 'Hello world'
result = 'Hi' not in x
print(result)  # Output: True

 

Identity Operators:

is:

x = 5
y = 5
result = x is y
print(result)  # Output: True

is not:

x = 5
y = 3
result = x is not y
print(result)  # Output: True

These examples demonstrate the usage of different operators in Python, and they form the basis of many common operations in various Python scripts and applications.