Chapter 3: Operators
In this chapter, “Mastering Python Fundamentals”, we’ll explore the various types of operators in Python, which are essential tools for performing calculations, comparisons, and logical operations in your programs. You’ll learn how to use arithmetic, comparison, logical, and assignment operators, among others. By the end of this chapter, you’ll be able to manipulate data and control the flow of your programs effectively.
3.1 Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.
3.1.1 Addition (+
)
The addition operator adds two numbers together.
Example:
a = 10
b = 5
result = a + b
print("Sum:", result)
- Explanation:
a + b
adds the values ofa
andb
.- The result,
15
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
addition_example.py
. - Run the program by typing:
python addition_example.py
- The output will display the sum of
a
andb
.
3.1.2 Subtraction (-
)
The subtraction operator subtracts one number from another.
Example:
a = 10
b = 5
result = a - b
print("Difference:", result)
- Explanation:
a - b
subtractsb
froma
.- The result,
5
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
subtraction_example.py
. - Run the program by typing:
python subtraction_example.py
- The output will display the difference between
a
andb
.
3.1.3 Multiplication (*
)
The multiplication operator multiplies two numbers.
Example:
a = 10
b = 5
result = a * b
print("Product:", result)
- Explanation:
a * b
multipliesa
andb
.- The result,
50
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
multiplication_example.py
. - Run the program by typing:
python multiplication_example.py
- The output will display the product of
a
andb
.
3.1.4 Division (/
)
The division operator divides one number by another.
Example:
a = 10
b = 5
result = a / b
print("Quotient:", result)
- Explanation:
a / b
dividesa
byb
.- The result,
2.0
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
division_example.py
. - Run the program by typing:
python division_example.py
- The output will display the quotient of
a
andb
.
3.1.5 Modulus (%
)
The modulus operator returns the remainder when one number is divided by another.
Example:
a = 10
b = 3
result = a % b
print("Remainder:", result)
- Explanation:
a % b
dividesa
byb
and returns the remainder.- The result,
1
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
modulus_example.py
. - Run the program by typing:
python modulus_example.py
- The output will display the remainder of the division.
3.1.6 Exponentiation (**
)
The exponentiation operator raises one number to the power of another.
Example:
a = 2
b = 3
result = a ** b
print("Power:", result)
- Explanation:
a ** b
raisesa
to the power ofb
.- The result,
8
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
exponentiation_example.py
. - Run the program by typing:
python exponentiation_example.py
- The output will display the result of raising
a
to the power ofb
.
3.2 Comparison Operators
Comparison operators compare two values and return a boolean (True
or False
). They are often used in decision-making statements.
3.2.1 Equal To (==
)
The equal to operator checks if two values are equal.
Example:
a = 5
b = 5
result = (a == b)
print("Are they equal?", result)
- Explanation:
a == b
checks ifa
is equal tob
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
equal_to_example.py
. - Run the program by typing:
python equal_to_example.py
- The output will display whether
a
andb
are equal.
3.2.2 Not Equal To (!=
)
The not equal to operator checks if two values are not equal.
Example:
a = 5
b = 3
result = (a != b)
print("Are they not equal?", result)
- Explanation:
a != b
checks ifa
is not equal tob
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
not_equal_to_example.py
. - Run the program by typing:
python not_equal_to_example.py
- The output will display whether
a
andb
are not equal.
3.2.3 Greater Than (>
)
The greater than operator checks if one value is greater than another.
Example:
a = 7
b = 5
result = (a > b)
print("Is a greater than b?", result)
- Explanation:
a > b
checks ifa
is greater thanb
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
greater_than_example.py
. - Run the program by typing:
python greater_than_example.py
- The output will display whether
a
is greater thanb
.
3.2.4 Less Than (<
)
The less than operator checks if one value is less than another.
Example:
a = 3
b = 5
result = (a < b)
print("Is a less than b?", result)
- Explanation:
a < b
checks ifa
is less thanb
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
less_than_example.py
. - Run the program by typing:
python less_than_example.py
- The output will display whether
a
is less thanb
.
3.2.5 Greater Than or Equal To (>=
)
The greater than or equal to operator checks if one value is greater than or equal to another.
Example:
a = 5
b = 5
result = (a >= b)
print("Is a greater than or equal to b?", result)
- Explanation:
a >= b
checks ifa
is greater than or equal tob
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
greater_than_equal_example.py
. - Run the program by typing:
python greater_than_equal_example.py
- The output will display whether
a
is greater than or equal tob
.
**3.2.6
Less Than or Equal To (<=
)**
The less than or equal to operator checks if one value is less than or equal to another.
Example:
a = 3
b = 5
result = (a <= b)
print("Is a less than or equal to b?", result)
- Explanation:
a <= b
checks ifa
is less than or equal tob
.- The result,
True
, is stored in the variableresult
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
less_than_equal_example.py
. - Run the program by typing:
python less_than_equal_example.py
- The output will display whether
a
is less than or equal tob
.
3.3 Logical Operators
Logical operators are used to combine conditional statements. The three main logical operators in Python are and
, or
, and not
.
3.3.1 and
Operator
The and
operator returns True
if both statements are true.
Example:
a = True
b = True
result = a and b
print("Both are True:", result)
- Explanation:
a and b
returnsTrue
only if botha
andb
areTrue
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
and_operator_example.py
. - Run the program by typing:
python and_operator_example.py
- The output will display whether both
a
andb
are true.
3.3.2 or
Operator
The or
operator returns True
if at least one of the statements is true.
Example:
a = True
b = False
result = a or b
print("At least one is True:", result)
- Explanation:
a or b
returnsTrue
if eithera
orb
isTrue
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
or_operator_example.py
. - Run the program by typing:
python or_operator_example.py
- The output will display whether at least one of
a
orb
is true.
3.3.3 not
Operator
The not
operator returns the opposite of the statement.
Example:
a = True
result = not a
print("Opposite of a:", result)
- Explanation:
not a
returnsFalse
becausea
isTrue
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
not_operator_example.py
. - Run the program by typing:
python not_operator_example.py
- The output will display the opposite of
a
.
3.4 Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is =
.
3.4.1 Simple Assignment (=
)
The simple assignment operator assigns the value on the right to the variable on the left.
Example:
a = 10
print("Value of a:", a)
- Explanation:
a = 10
assigns the value10
to the variablea
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
simple_assignment_example.py
. - Run the program by typing:
python simple_assignment_example.py
- The output will display the value of
a
.
3.4.2 Add and Assign (+=
)
The add and assign operator adds the value on the right to the variable on the left and then assigns the result to the variable.
Example:
a = 10
a += 5 # Equivalent to a = a + 5
print("Value of a after += 5:", a)
- Explanation:
a += 5
adds5
toa
and then assigns the result back toa
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
add_assign_example.py
. - Run the program by typing:
python add_assign_example.py
- The output will display the value of
a
after adding5
.
3.4.3 Subtract and Assign (-=
)
The subtract and assign operator subtracts the value on the right from the variable on the left and then assigns the result to the variable.
Example:
a = 10
a -= 5 # Equivalent to a = a - 5
print("Value of a after -= 5:", a)
- Explanation:
a -= 5
subtracts5
froma
and then assigns the result back toa
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
subtract_assign_example.py
. - Run the program by typing:
python subtract_assign_example.py
- The output will display the value of
a
after subtracting5
.
3.4.4 Multiply and Assign (*=
)
The multiply and assign operator multiplies the variable on the left by the value on the right and then assigns the result to the variable.
Example:
a = 10
a *= 5 # Equivalent to a = a * 5
print("Value of a after *= 5:", a)
- Explanation:
a *= 5
multipliesa
by5
and then assigns the result back toa
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
multiply_assign_example.py
. - Run the program by typing:
python multiply_assign_example.py
- The output will display the value of
a
after multiplying by5
.
3.4.5 Divide and Assign (/=
)
The divide and assign operator divides the variable on the left by the value on the right and then assigns the result to the variable.
Example:
a = 10
a /= 5 # Equivalent to a = a / 5
print("Value of a after /= 5:", a)
- Explanation:
a /= 5
dividesa
by5
and then assigns the result back toa
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
divide_assign_example.py
. - Run the program by typing:
python divide_assign_example.py
- The output will display the value of
a
after dividing by5
.
Understanding operators in Python is crucial for writing effective and efficient code. With these operators, you can perform a wide range of operations, from simple arithmetic to complex logical operations. By mastering these concepts, you’re well on your way to becoming proficient in Python programming. Happy coding!
Read Next :
Mastering Python Fundamentals – Part 4 : Flow control ( if, then,else … )