Python Operators.
Operators are constructs that can manipulate the value of an operand.
The Python programming language supports a variety of operators, including:
- Arithmetic Operators
Arithmetic operators are :
a. Addition (+) : Add up the value of each operand or number.
b. Subtraction (-) : Subtract the operand on the left using the operand on the right.
c. Multiplication (*) : Multiplies operands / numbers.
d. Division (/) : To divide the operand on the left use the operand on the right.
e. Modulus (%) : Get the remainder of the division of the operand on the left of the operator when it is divided by the operand on the right.
f. Floor division (//) : Just like division. It’s just that the decimal digits are omitted.
g. Exponentiation (**) : Move the operand to the left of the operator to the operand to the right of the operator.
example for Arithmetic Operators :
#Addition
print(13 + 2)
apple = 7
orange = 9
fruits = apple + orange
print(fruits)
#Subtraction
debt = 10000
pay = 5000
remainingDebt = debt - pay
print("You are remaining debt is ", remainingDebt)
#Multiplication
long = 15
wide = 8
large = long * wide
print(large)
#Division
cake = 16
child = 4
Div = cake / child
print("Each child will get as many pieces of the cake ", Div)
#Modulus
num1 = 14
num2 = 5
result = num1 % num2
print("Modulus of ", num1, " and ", num2, " is ", result)
#Floor division
print(10//3)
#10 divided by 3 is 3.3333. Because it is rounded, it will produce the value 3#Exponentiation
num3 = 8
num4 = 2
result = num3 ** num4
print(result)
- Comparison Operators
Comparison operators are used to compare a value of each operand.
a. Equal (==) : True value If each operand has the same value, then the condition has a true value or True.
b. Not equal (!=) : False values will produce values inverse of the actual condition.
c. Greater than (>) : True value If the value of the left operand is greater than the value of the right operand, then the condition becomes true.
d. Less than (<) : Value True If the value of left operand is less than the value of right operand, the condition becomes true.
e. Greater than or equal to (>=) : Value True If the value of left operand is greater than the value of right operand, or equal, the condition is true.
f. Less than or equal to (<=) : Value True If the value of left operand is less than the value of right operand, or equal, the condition is true.
- Logical Operators.
Logical operators are used to combine conditional statements:
a. and : Returns True if both statements are true.
b. or : Returns True if one of the statements is true.
c. not : Reverse the result, returns False if the result is true.
- Identity Operators.
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location :
a. is : Returns True if both variables are the same object.
b. is not : Returns True if both variables are not the same object.
- Assignment Operators.
The assignment operator is used to assign or modify a value into a variable.
a. = : Assigns the value on the right to the variable on the left.
b. += : Provides a variable value with the value of the variable itself plus the value to the right.
c. -= : Provides a variable value with the value of the variable itself minus the value to the right.
d. *= : Provides a variable value with the value of the variable itself multiplied by the value to the right.
e. /= : Provides a variable value with the value of the variable itself divided by the value to the right.
f. %= : Provides a variable value with the value of the variable itself divided by the value to the right. What will be taken later is the rest for him.
g. //= Divide round the left operand of the operator by the operand to the right of the operator, then the result is inputted to the left operand.
h. **= Provides a variable value with the value of the variable itself raised to the power of the value to the right.