Python Beginner Tutorial # 2 : Basic Operations

Operators are symbols used in Python to preform Operations. Operators are used to manipulate the value of Operands.

Ex.

6 / 2 = 3 Operator: ( / ) and Operand: ( 6 , 2)

Types of Operators :
• Arithmetic Operators = Used to perform mathematical operations like addition, subtraction, multiplication and division.
• Relational operators  = Used to compare values. Greater than, Less than, Equal to. These operations return a Boolean.
• Logical operators = Used for And, OR,and Not operations.
• Bitwise operators = Used to preform bit by bit operations.
• Assignment operators = Used to assign values to variables.
Arithmetic Operators
x = 45
y = 25

print(x + y)

# Subtraction
print (x - y)

# Multiplication
print(x * y)

# Division(float)
print (x / y)

# Division(floor)
print (x // y)

# Modulo
print (x % y)
70
20
1125
1.8
1
20

Relational operators

x = 57 + 60
y = 25 + 120

print(x)
print(y)

# x > y is False
print(x > y)

# x < y is True
print(x < y)

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

# x != y is True
print(x != y)

# x >= y is False
print(x >= y)

# x <= y is True
print(x <= y)
117
145
False
True
False
True
False
True

Bitwise Operator

x = 15
y = 8

#  bitwise AND operation
print(x & y)

# bitwise OR operation
print(x | y)

# Bitwise NOT operation
print(~ x)

# Bitwise XOR operation
print(x ^ y)

# Bitwise right shift operation
print(x >> 4)

# Bitwise left shift operation
print(x << 6)
8
15
-16
7
0
960

Logical Operators

x = 25
y = 30

# Print x and y if true
print(x < 100 and y < 100)

# Print a or y if true
print(x < 100 or y < 100)

# Print False if True statement
print (not(x < 100 and y < 100) )
True
True
False

Assignment operators

a = 25
b = 6
b += a #  += Operator
print(" += Operator Example: ", b)

b -= a # -= Operator
print("-= Operator Example:", b)

b *= a #  *= Operator
print("*= Operator Example: ", b)

b //= a #  //= Operator
print("//= Operator Example: ", b)

b **= a #  **= Operator
print("**= Operator Example: ", b)

b /= a #  /= Operator
print("/= Operator Example:", b)

b %= a #  %= Operator
print("%= Operator Example:  ", b)

x = 55
y = 92
x &= y # &= Operator
print("&= Operator Example: ", x)

x |= 9 #  |= Operator
print("|= Operator Example: ", x)

x ^= y # Using ^= Operator
print("^= Operator Example: ", x)
Output :
+= Operator Example:  31
-= Operator Example:  6
*= Operator Example:  150
//= Operator Example:  6
**= Operator Example:  28430288029929701376
/= Operator Example:  1.1372115211971881e+18
%= Operator Example:  21.0
&= Operator Example:  20
|= Operator Example:  29
^= Operator Example:  65