Let's look at some basic operations that you may require while programming.
Arithmetic operations:
Addition(+): adds two numbers
Subtraction (
-
): Subtracts the right operand from the left.Multiplication (
*
): Multiplies two numbers.Division (
/
): Divides the left operand by the right.Floor Division (
//
): Returns the floor of the division result (discards the fractional part).Modulus (
%
): Returns the remainder of the division.Exponentiation (
**
): Raises the left operand to the power of the right.Here is some code to further your understanding:
# Addition result_addition = 5 + 3 print("Addition:", result_addition) #outputs 8 # Subtraction result_subtraction = 10 - 4 print("Subtraction:", result_subtraction) #outputs 6 # Multiplication result_multiplication = 6 * 2 print("Multiplication:", result_multiplication) #outputs 12 # Division result_division = 15 / 3 print("Division:", result_division) #outputs 5 # Floor Division result_floor_division = 15 // 4 print("Floor Division:", result_floor_division) #outputs 3 # Modulus (Remainder) result_modulus = 17 % 5 print("Modulus:", result_modulus) #outputs 2 # Exponentiation result_exponentiation = 2 ** 3 print("Exponentiation:", result_exponentiation) #outputs 8
Next, let's look at some Logical Operators and comparison operators.
Logical operators and Comparison operators
Comparison Operators (
==
,!=
,>
,<
,>=
,<=
): Compare values and return boolean results i.e. true or false.x = 10 y = 5 is_equal = x == y #now is_equal is false is_not_equal = x != y #now is_equal is true
Logical Operators (
and
,or
,not
): Combine or negate boolean values.- and Operator:
The and
operator returns True
if both operands are True
, otherwise, it returns False
.
x = True
y = False
result = x and y
# result is False because one of the operands (y) is False
- or Operator:
The
or
operator returnsTrue
if at least one of the operands isTrue
. It returnsFalse
only if both operands areFalse
.x = True y = False result = x or y # result is True because one of the operands (x) is True
3. not Operator:
The
not
operator returns the opposite boolean value of the operand. If the operand isTrue
,not
returnsFalse
. If the operand isFalse
,not
returnsTrue
.
x = True
result = not x
# result is False because not True is False
You can also combine these operators to form complex expressions which can be used for evaluation and other purposes.
That's it for today!