Python Basics Unveiled: Mastering Operators

Andres Paniagua
4 min readNov 13, 2023

--

Introduction to Basic Operators in Python

Welcome to your second day of Python learning! After getting comfortable with variables and data types, it’s time to manipulate them using Python’s operators. These are the symbols that do the mathematical and logical operations within your code, making them incredibly powerful and versatile tools in programming.

What Exactly Are Operators?

Operators are symbols that instruct the compiler or interpreter to perform specific mathematical, relational, or logical operations and produce a final result. They are the elements that connect variables and values in programming languages.

In Python, we primarily have three categories of basic operators:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators

Let’s break down each type with examples:

Arithmetic Operators

These operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

Here’s the list of arithmetic operators:

  • + (Addition): Adds values on either side of the operator.
  • - (Subtraction): Subtracts the right-hand operand from the left-hand operand.
  • * (Multiplication): Multiplies values on either side of the operator.
  • / (Division): Divides the left-hand operand by the right-hand operand.
  • % (Modulus): Returns the remainder of dividing the left-hand operand by the right-hand operand.
  • // (Floor Division): Performs division and discards the decimal part.
  • ** (Exponentiation): Performs exponential (power) calculation on operators.
x = 15
y = 4
# Examples of Arithmetic Operators
print('x + y =', x + y) # Adds 15 and 4 = 19
print('x - y =', x - y) # Subtracts 4 from 15 = 11
print('x * y =', x * y) # Multiplies 15 by 4 = 60
print('x / y =', x / y) # Divides 15 by 4 = 3.75
print('x // y =', x // y) # Floor division of 15 by 4 = 3
print('x ** y =', x ** y) # 15 to the power of 4 = 50625

Comparison Operators

Comparison operators are used to compare two values. It’s important to note that these operators will return a Boolean value (True or False).

  • ==: If the values of two operands are equal, then the condition becomes true.
  • !=: If values of two operands are not equal, then condition becomes true.
  • >: If the value of the left operand is greater than the value of the right operand, then condition becomes true.
  • <: If the value of the left operand is less than the value of the right operand, then condition becomes true.
  • >=: If the value of the left operand is greater than or equal to the value of the right operand, then condition becomes true.
  • <=: If the value of the left operand is less than or equal to the value of the right operand, then condition becomes true.
a = 10
b = 20
# Examples of Comparison Operators
print('a == b is', a == b) # Checks if a is equal to b = False
print('a != b is', a != b) # Checks if a is not equal to b = True
print('a > b is', a > b) # Checks if a is greater than b = False
print('a < b is', a < b) # Checks if a is less than b = True

Logical Operators

Logical operators perform Logical AND, Logical OR, and Logical NOT operations.

  • and: It's a Logical AND operator. If both the operands are true, then the condition becomes true.
  • or: It's a Logical OR operator. If any of the two operands are non-zero, then the condition becomes true.
  • not: It's a Logical NOT operator. Used to reverse the logical state of its operand.
x = True
y = False
# Examples of Logical Operators
print('x and y is', x and y) # True and False = False
print('x or y is', x or y) # True or False = True
print('not x is', not x) # Not True = False

Simple Calculator Coding Example

Combining what we’ve learned, we can create a basic calculator that uses arithmetic operations based on user input.

# Take input from the user for two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Ask the user for the operation they would like to perform
operation = input("Enter operation (choose between +, -, *, /): ")
# Based on the operation, perform the calculation
if operation == '+':
print(num1 + num2)
elif operation == '-':
print(num1 - num2)
elif operation == '*':
print(num1 * num2)
elif operation == '/':
print(num1 / num2)
else:
print("Invalid Input")

This simple calculator takes two numbers and an operator as input from the user and performs the operation, then prints the result.

Congratulations on completing Day 2 of your Python learning journey! Today, you learned about basic operators in Python, including arithmetic, comparison, and logical operators. You’ve also seen how these operators can be used in practical code examples, like making a simple calculator. These concepts are fundamental as you continue to explore more advanced Python topics in the days to come. Keep practicing!

--

--

Andres Paniagua

After Hours Coding empowers beginners to dive into a new career in coding & transform their lives using spare time to learn.