πŸ“ Python Operators#

Operators are used to perform operations on variables and values.

For example we can use the + operator to add two number (or strings together)

5 + 10
15
'Drexel' + ' ' + 'Engineer'
'Drexel Engineer'

Arithmetic Operators#

These are operators that perform basic mathematical functions

Addition +#

x = 5
y = 3

print(x + y)
8

Subtraction -#

x = 5
y = 3

print(x - y)
2

Multiplication *#

x = 5
y = 3

print(x * y)
15

Division /#

x = 5
y = 3

print(x / y)
1.6666666666666667

Modulus %#

The remainder after dividing by a number

x = 5
y = 3

print(x % y)
2

Exponential **#

x = 5
y = 3

print(x ** y)
125

Floor Division //#

the floor division // rounds the result of division down to the nearest whole number

x = 5
y = 3

print(x // y)
1

Assignment Operators#

These are operators that allow you to assign a value to a variable.

Equals =#

x = 5

print(x)
5

Addition and Assignment +=#

x = 5
x += 3

print(x)
8

Subtraction and Assignment -=#

x = 5
x -= 3

print(x)
2

Multiplication and Assignment *=#

x = 5
x *= 3

print(x)
15

Division and Assignment /=#

x = 5
x /= 3

print(x)
1.6666666666666667

Modulus and Assignment %=#

x = 5
x %= 3

print(x)
2

Floor Division and Assignment //=#

x = 5
x //= 3

print(x)
1

Exponential and Assignment **=#

x = 5
x **= 3

print(x)
125

Comparison Operators#

There are operators that allow you to compare two values or variables to see if they are the same or different.

Equal To ==#

x = 3
y = 5

print(x == x)
print(x == y)
True
False

Not Equal To !=#

x = 3
y = 5

print(x != x)
print(x != y)
False
True

Greater Than >#

x = 3
y = 5

print(x > x)
print(x > y)
False
False

Less Than <#

x = 3
y = 5

print(x < x)
print(x < y)
False
True

Greater Than Equal To >=#

x = 3
y = 5

print(x >= x)
print(x >= y)
True
False

Less Than Equal To <=#

x = 3
y = 5

print(x <= x)
print(x <= y)
True
True

Logical Operators#

Logical operators allow you to create logical statements between values or variables.

and Operator#

Returns True if both statements are true

x = 5

print(x > 3 and x < 10)
print(x > 3 and x > 10)
print(x < 3 and x > 10)
True
False
False

or Operator#

Returns True if one statements is true

x = 5

print(x > 3 or x < 10)
print(x > 3 or x > 10)
print(x < 3 or x > 10)
True
True
False

not Operator#

Returns true is the result is false, returns false if the results is true

print(not (True))
False
print(not (False))
True
x = 5

print(not (x > 3 or x < 10))
print(not (x > 3 or x > 10))
print(not (x < 3 or x > 10))
False
False
True

Python Identity Operators#

Identity operators allow you to determine if two variables are physically the same. This means both value and location in memory.

is Operator#

Returns true if an object is the same in memory

x = 5
y = 5.

print(x is y)
False
x = 5
y = x


print(x is y)
True

is not Operator#

Returns true if the object are different in memory

x = 5
y = 5.

print(x is not y)
True
x = 5
y = x


print(x is not y)
False