Free Coding Tutorials: Mastering Python Fundamentals – Part 3

independent python projects

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 of a and b.
  • The result, 15, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as addition_example.py.
  4. Run the program by typing:
   python addition_example.py
  1. The output will display the sum of a and b.

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 subtracts b from a.
  • The result, 5, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as subtraction_example.py.
  4. Run the program by typing:
   python subtraction_example.py
  1. The output will display the difference between a and b.

3.1.3 Multiplication (*)

The multiplication operator multiplies two numbers.

Example:

a = 10
b = 5
result = a * b
print("Product:", result)
  • Explanation:
  • a * b multiplies a and b.
  • The result, 50, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as multiplication_example.py.
  4. Run the program by typing:
   python multiplication_example.py
  1. The output will display the product of a and b.

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 divides a by b.
  • The result, 2.0, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as division_example.py.
  4. Run the program by typing:
   python division_example.py
  1. The output will display the quotient of a and b.

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 divides a by b and returns the remainder.
  • The result, 1, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as modulus_example.py.
  4. Run the program by typing:
   python modulus_example.py
  1. 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 raises a to the power of b.
  • The result, 8, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as exponentiation_example.py.
  4. Run the program by typing:
   python exponentiation_example.py
  1. The output will display the result of raising a to the power of b.

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 if a is equal to b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as equal_to_example.py.
  4. Run the program by typing:
   python equal_to_example.py
  1. The output will display whether a and b 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 if a is not equal to b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as not_equal_to_example.py.
  4. Run the program by typing:
   python not_equal_to_example.py
  1. The output will display whether a and b 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 if a is greater than b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as greater_than_example.py.
  4. Run the program by typing:
   python greater_than_example.py
  1. The output will display whether a is greater than b.

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 if a is less than b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as less_than_example.py.
  4. Run the program by typing:
   python less_than_example.py
  1. The output will display whether a is less than b.

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 if a is greater than or equal to b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as greater_than_equal_example.py.
  4. Run the program by typing:
   python greater_than_equal_example.py
  1. The output will display whether a is greater than or equal to b.

**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 if a is less than or equal to b.
  • The result, True, is stored in the variable result.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as less_than_equal_example.py.
  4. Run the program by typing:
   python less_than_equal_example.py
  1. The output will display whether a is less than or equal to b.

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 returns True only if both a and b are True.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as and_operator_example.py.
  4. Run the program by typing:
   python and_operator_example.py
  1. The output will display whether both a and b 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 returns True if either a or b is True.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as or_operator_example.py.
  4. Run the program by typing:
   python or_operator_example.py
  1. The output will display whether at least one of a or b 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 returns False because a is True.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as not_operator_example.py.
  4. Run the program by typing:
   python not_operator_example.py
  1. 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 value 10 to the variable a.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as simple_assignment_example.py.
  4. Run the program by typing:
   python simple_assignment_example.py
  1. 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 adds 5 to a and then assigns the result back to a.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as add_assign_example.py.
  4. Run the program by typing:
   python add_assign_example.py
  1. The output will display the value of a after adding 5.

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 subtracts 5 from a and then assigns the result back to a.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as subtract_assign_example.py.
  4. Run the program by typing:
   python subtract_assign_example.py
  1. The output will display the value of a after subtracting 5.

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 multiplies a by 5 and then assigns the result back to a.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as multiply_assign_example.py.
  4. Run the program by typing:
   python multiply_assign_example.py
  1. The output will display the value of a after multiplying by 5.

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 divides a by 5 and then assigns the result back to a.

Step-by-Step Guide:

  1. Open your text editor.
  2. Type the code above into your editor.
  3. Save the file as divide_assign_example.py.
  4. Run the program by typing:
   python divide_assign_example.py
  1. The output will display the value of a after dividing by 5.

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 … )

Latest Posts