Welcome back to our Python tutorial series at CipherTrick.com! After grasping Python data types and variables, let’s proceed to another fundamental topic – operators. Operators are special symbols in Python that perform arithmetic or logical computation. Understanding them is crucial for building meaningful and efficient Python programs.

Part 1: Python Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Part 2: Python Comparison Operators

Comparison operators are used to compare values. It either returns True or False according to the condition.

x = 10
y = 12

# Output: x > y is False
print('x > y is',x>y)

# Output: x < y is True
print('x < y is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

Part 3: Python Logical Operators

Logical operators are and, or, and not operators.

x = True
y = False

# Output: x and y is False
print('x and y is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

Part 4: Python Bitwise Operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

a = 10
b = 4

# Output: a & b = 0
print('a & b =',a&b)

# Output: a | b = 14
print('a | b =',a|b)

# Output: ~a = -11
print('~a =',~a)

# Output: a ^ b = 14
print('a ^ b =',a^b)

# Output: a >> 2 = 2
print('a >> 2 =',a>>2)

# Output: a << 2 = 40
print('a << 2 =',a<<2)

Conclusion

Understanding Python operators is vital for carrying out operations on values and variables in Python. From performing simple arithmetic to comparing values or even operating on individual bits of data, operators are essential tools in your Python toolkit. In our upcoming tutorials, we’ll apply these operators in real-world examples, ensuring these concepts stick. So stay tuned, and happy coding!