top of page
Writer's pictureHui Wang

89. Machine learning: Python Operators

Python divides the operators into the following groups:


1. Arithmetic operators


Arithmetic operators perform basic math operations like addition, subtraction, multiplication, and division.


It includes + (addition), - (subtraction), * (multiplication), / (division), % (modulo), ** (exponentiation), and // (floor division).


Example:

# Arithmetic operators
x = 10
y = 5

# Addition
print(x + y)  # Output: 15

# Subtraction
print(x - y)  # Output: 5

# Multiplication
print(x * y)  # Output: 50

# Division
print(x / y)  # Output: 2.0

# Modulo
print(x % y)  # Output: 0

# Exponentiation
print(x ** y)  # Output: 100000

# Floor division
print(x // y)  # Output: 2

2. Comparison operators


Comparison operators are used to comparing two values and return a Boolean result indicating whether the comparison is true or false.


It includes == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).


Example:

# Comparison operators
d = 10
e = 5

# Equal to
print(d == e)  # Output: False

# Not equal to
print(d != e)  # Output: True

# Greater than
print(d > e)  # Output: True

# Less than
print(d < e)  # Output: False

# Greater than or equal to
print(d >= e)  # Output: True

# Less than or equal to
print(d <= e)  # Output: False

3. Assignment operators


Assignment operators are used to assigning values to variables.


It includes = (assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulo and assign), **= (exponentiate and assign), and //= (floor divide and assign).


Example:

# Assignment operators
a = 10
b = 5

# Simple assignment
c = a
print(c)  # Output: 10

# Add and assign
c += a
print(c)  # Output: 20

# Subtract and assign
c -= a
print(c)  # Output: 10

# Multiply and assign
c *= a
print(c)  # Output: 100

# Divide and assign
c /= a
print(c)  # Output: 10.0

# Modulo and assign
c %= a
print(c)  # Output: 0.0

# Exponentiate and assign
c **= a
print(c)  # Output: 1e+50

# Floor divide and assign
c //= a
print(c)  # Output: 1

4. Logical operators


Logical operators are used to combine the results of multiple comparisons or to negate a comparison.


It includes and, or, and not.


Example:

# Logical operators
f = True
g = False

# And
print(f and g)  # Output: False

# Or
print(f or g)  # Output: True

# Not
print(not f)  # Output: False

5. Identity operators


Identity operators are used to comparing the identity of two objects in memory.


It includes is and is not.


Example:

# Identity operators
h = [1, 2, 3]
i = [1, 2, 3]
j = h

# Is
print(h is i)  # Output: False
print(h is j)  # Output: True

# Is not
print(h is not i)  # Output: True
print(h is not j)  # Output: False

6. Membership operators


Membership operators test whether a value is contained in a sequence (such as a list or string).


It includes in and not in.


Example:

# Membership operators
k = [1, 2, 3]
l = 2
m = 4

# In
print(l in k)  # Output: True
print(m in k)  # Output: False

# Not in
print(l not in k)  # Output: False
print(m not in k)  # Output: True

7. Bitwise operators


Bitwise operators are used to perform operations on the binary representations of integers.


It includes & (and), | (or), ^ (xor), ~ (not), << (left shift), and >> (right shift).


Example:

# Bitwise operators
n = 10  # 1010 in binary
o = 4   # 0100 in binary

# And
print(n & o)  # Output: 0

# Or
print(n | o)  # Output: 14

# Xor
print(n ^ o)  # Output: 14

# Not
print(~n)  # Output: -11

# Left shift
print(n << 1)  # Output: 20

# Right shift
print(n >> 1)  # Output: 5

Recent Posts

See All

Comments


bottom of page