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
Arithmetic Operators Description
+ Addition: add two variables
Subtraction: subtract two variables
/ Division: divide two variables
* Multiplication: multiply two variables 
% Modules: dived two variables and get remainder
x = 45
y = 25
  
# Addition
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) 
Output :
70
20
1125
1.8
1
20

Relational operators

Relational Operators Description
== Equal to: True if both operands are equal
!= Not Equal to: True if operands are not equal
Greater than: True if left operand is greater than right
Less than: True if left operand is less than right
>= Greater than or Equal to: True if left operand is greater than or equal to right
<= Less than or Equal to: True if left operand is less than or equal to right
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) 
Output :
117
145
False
True
False
True
False
True

Bitwise Operator

Bitwise Operators Description
& Bitwise AND
~ Bitwise NOT
| Bitwise OR
^ Bitwise XOR
<<  Bitwise Left
>>  Bitwise Right
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) 
Output :
8
15
-16
7
0
960

Logical Operators

Logical Operators Description
AND True if both operands are true
OR True if either of the operands are true
NOT True of operand is False
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) )
Output :
True
True
False

Assignment operators

Assignment Operators Description
= Assign vale to right side
+= Add right and left operand
-= Subtract right and left operand
%= Takes modulus
/= Divide left and right operand
*= Multiply operands
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
Click here for Part Three : if, else statements