Operators are symbols that perform operations on operands. An expression is a combination of operators, and values that produces a result. Let's explore
3.3.1 Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, modulus, exponentiation, and floor division as shown in the following code.
a = 10 b = 3 # Perform all arithmetic operations on these numeric variables and print results print(a,"+",b,"=",a+b) # Output: 10 + 3 = 13 print(a,"*",b,"=",a*b) # Output: 10*3 = 30 print(a,"/",b,"=",a/b) # Output: 10 / 3 = 3.333333333333335 print(a,"//",b,"=",a//b) # floor division # Output: 10 / 3 = 3 print(a,"%",b,"=",a%b) # Output: 10% 3 = 1 print(a,"**",b,"=",a**b) # ** represent power operator: # Output: 10**3 = 1000
3.3.2 Comparison Operators
Comparison operators are used to compare two values or expressions. They determine the relational logic between them, such as equality, inequality, greater than, less than, and so on. These operators return a Boolean value (True or False) based on the comparison result. Here's a Python program that demonstrates the usage of all comparison operators.
x = 10 y = 5 # Greater than print (x, " > " , y, " = " , x > y) # Output: 10 > 5 = True # Less than print (x, " > " , y, " = " , x < y) # Output: 10 < 5 = False