» Quick Introduction to Python » 1. Basics » 1.7 Operators

Operators

Arithmetic Operators

x, y = 5, 8

print('x + y:', x + y)  # Addition
print('x - y:', x - y)  # Subtraction
print('x * y:', x * y)  # Multiplication
print('x / y:', x / y)  # Division
print('x % y:', x % y)  # Modular Division
print('x // y:', x // y) # Floor Division
print('x ** y:', x ** y) # Exponentiation

Comparison Operators

x, y = 5, 8

print('x == y:', x == y)  # Equal to
print('x != y:', x != y)  # Not Equal to
print('x > y:', x > y)   # Greater than
print('x < y:', x < y)   # Less than
print('x >= y:', x >= y)  # Greater than or Equal to
print('x <= y:', x <= y)  # Less than or Equal to

Logical Operators

x, y = True, False

print('x and y:', x and y) # And
print('x or y:', x or y) # Or
print('not x:', not x) # Negate

Bitwise Operators

x, y = 5, 8

print('x & y:', x & y) # Bitwise AND
print('x | y:', x | y) # Bitwise OR
print('~y:', ~y) # Bitwise NOT
print('x ^ y:', x ^ y) # Bitwise XOR
print('x << y:', x << y) # Left Shift
print('x >> y:', x >> y) # Right Shift

Identity Operators

x, y = [5, 8], [5, 8]
z = x

print('x is y:', x is y)
print('x is z:', x is z)

print('x is not y:', x is not y)
print('x is not z:', x is not z)

Membership Operators

x = 'literank'
y = ['literank', 'tutorial', 'article']

print('x in y:', x in y)
print('x not in y:', x not in y)

Code Challenge

Try to modify the code provided in the editor to make it print [12, 20, 28].

Loading...
> code result goes here
Prev
Next