Python Basics: Mastering Operators in Python
Shirish Shankhdhar
Senior Analyst | People Analytics Leader | Data Science MS | Machine Learning | Driving Data-Driven Insights at Mastercard
Welcome back to our Python learning journey! In the previous article (Python Basics: Variables, Data Types, and Interactive Programs), we covered variables, data types, and how to write interactive programs using user input. Now that you’re comfortable with those concepts, we’ll build on them by learning about operators in Python.
Python Operators are the symbols and keywords that let you perform operations on variables and values. Whether you’re adding numbers, comparing values, or checking conditions, operators are a crucial part of every Python program.
In this article, we’ll explore the most commonly used types of operators in Python:
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
Example: Using Arithmetic Operators
a = 10
b = 3
# Addition
sum = a + b
print("Sum:", sum)
# Subtraction
difference = a - b
print("Difference:", difference)
# Multiplication
product = a * b
print("Product:", product)
# Division
quotient = a / b
print("Quotient:", quotient)
# Modulus (remainder)
remainder = a % b
print("Remainder:", remainder)
# Exponentiation
power = a ** b
print("Power:", power)
# Floor division
floor_div = a // b
print("Floor Division:", floor_div)
Output:
Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333335
Remainder: 1
Power: 1000
Floor Division: 3
Explanation: We performed a series of operations using a = 10 and b = 3, which demonstrate the various arithmetic operators in action.
2. Assignment Operators
Assignment operators are used to assign values to variables. The most common operator is the equals sign =, but Python also has other shorthand assignment operators.
Example: Using Assignment Operators
x = 5
print("Initial value of x:", x)
x += 3 # Same as: x = x + 3
print("After x += 3:", x)
x *= 2 # Same as: x = x * 2
print("After x *= 2:", x)
x %= 5 # Same as: x = x % 5
print("After x %= 5:", x)
Output:
Initial value of x: 5
After x += 3: 8
After x *= 2: 16
After x %= 5: 1
Explanation: Here, we used assignment operators to update the value of x with shorthand notations. This makes code more concise.
3. Comparison Operators
Comparison operators are used to compare two values. They return either True or False depending on the condition.
领英推荐
Example: Using Comparison Operators
a = 10
b = 3
print("a == b:", a == b)
print("a != b:", a != b)
print("a > b:", a > b)
print("a < b:", a < b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)
Output:
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False
Explanation: The comparison operators allow us to check how the values of a and b relate to each other, returning True or False.
4. Logical Operators
Logical operators combine conditional statements. They are used to evaluate whether conditions are True or False.
Example: Using Logical Operators
x = 5
y = 10
# Logical AND
print("x > 3 and y < 15:", x > 3 and y < 15)
# Logical OR
print("x < 3 or y > 5:", x < 3 or y > 5)
# Logical NOT
print("not (x > 3):", not (x > 3))
Output:
x > 3 and y < 15: True
x < 3 or y > 5: True
not (x > 3): False
Explanation: Logical operators let us combine multiple conditions to check more complex logic in our programs.
5. Combining Operators for More Complex Logic
Let’s look at an example where we combine different types of operators to create more advanced logic.
Example: A Simple Grading System
score = int(input("Enter your test score: "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
Please run above code and put your input and output in the comments.
Explanation: Here we use comparison and logical operators in a real-world scenario where a student’s score is evaluated against multiple conditions to assign a grade.
Conclusion
In today’s article, we explored the various types of operators in Python, from arithmetic and assignment operators to comparison and logical operators. Understanding how to use operators effectively will allow you to perform complex calculations, make decisions in your code, and manipulate data.
Stay tuned for the next article, where we will dive deeper into Conditional Statements and Loops to add more logic and structure to your Python programs.
Note:
This article was generated with the assistance of ChatGPT, an AI language model developed by OpenAI.
Very informative. Looking forward to more informative posts in the future. ??Shirish Shankhdharr Keep it up!