Python Variables and Operators: A Friendly Guide for Beginners
Malinda Gamage
Senior Software Engineer @ Persistent Systems | Java Instructor | C++ | Java | Python | MSc in Information Technology | BSc in Manufacturing & Industrial Engineering
So, you’ve decided to embark on the journey of learning Python—great choice! Python is known for being beginner-friendly, and today, we’re going to dive into two fundamental concepts: variables and operators. If you’re wondering what those are or why they’re important, don’t worry. By the end of this guide, you’ll have a clear understanding and be ready to use them in your own coding projects.
What Are Variables?
Let’s kick things off with variables. At their core, variables are like little storage boxes where you can keep data. Imagine you have a box labeled "my_age," and inside that box, you place the number 25. Whenever you want to know what’s inside, you just look inside the box, and there it is—25!
In Python, variables work the same way. They hold data that you might want to use or manipulate later on. And the best part? You get to name them (within reason, of course—there are a few rules, but we’ll get to that in a bit).
Here’s a quick example:
my_age = 25
In this snippet, my_age is the variable, and it’s storing the value 25. Simple, right?
Naming Variables
Before we move on, let’s talk about naming these little boxes, because it’s not a free-for-all. Python has some rules about what you can name your variables:
So, stick to these guidelines, and you’ll be golden!
What Are Operators?
Now that you’ve got your variables set up, you’re probably wondering, “What can I do with them?” Enter operators. Operators are like tools that allow you to perform operations on variables and values.
Let’s explore the most common types of operators in Python:
1. Arithmetic Operators
These are the operators you’ll use for basic math—addition, subtraction, multiplication, and division. Here’s a quick rundown:
result = 5 + 3
print(result) # Output will be 8
result = 10 - 2
print(result) # Output will be 8
result = 4 * 2
print(result) # Output will be 8
result = 16 / 2
print(result) # Output will be 8.0
result = 10 % 3
print(result) # Output will be 1
result = 2 ** 3
print(result) # Output will be 8
result = 16 // 3
print(result) # Output will be 5
2. Assignment Operators
Assignment operators are used to assign values to variables. You’ve already seen one—the equal sign =. But there are others, which are just variations on this theme:
x = 10
领英推荐
x += 5 # Equivalent to x = x + 5
print(x) # Output will be 15
x -= 3 # Equivalent to x = x - 3
print(x) # Output will be 12
x *= 2 # Equivalent to x = x * 2
print(x) # Output will be 24
x /= 4 # Equivalent to x = x / 4
print(x) # Output will be 6.0
3. Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is always either True or False, which is what we call a Boolean value.
result = (5 == 5)
print(result) # Output will be True
result = (5 != 3)
print(result) # Output will be True
result = (10 > 8)
print(result) # Output will be True
result = (5 < 10)
print(result) # Output will be True
result = (10 >= 10)
print(result) # Output will be True
result = (5 <= 5)
print(result) # Output will be True
4. Logical Operators
Logical operators are used to combine multiple conditions. The result is also a Boolean value (True or False).
result = (5 > 3 and 8 > 5)
print(result) # Output will be True
result = (5 > 3 or 3 > 8)
print(result) # Output will be True
result = not(5 > 3)
print(result) # Output will be False
Practical Examples of Variables and Operators
Alright, enough theory! Let’s see how variables and operators work together in real life. Suppose you’re writing a program to calculate the final price of an item after a discount. Here’s how you might do it:
original_price = 100.0 # Original price in dollars
discount_percentage = 20 # Discount percentage
# Calculate the discount amount
discount_amount = original_price * (discount_percentage / 100)
# Calculate the final price after discount
final_price = original_price - discount_amount
print("The final price after discount is: $" + str(final_price))
In this example, we’re using variables to store the original price and discount percentage. Then, we use arithmetic operators to calculate the discount amount and the final price. Finally, we print out the result.
Wrapping It Up
And there you have it! We’ve covered the basics of variables and operators in Python. By now, you should have a good grasp of how to store data in variables and manipulate that data using various operators. These are foundational skills that you’ll use over and over again as you continue your Python journey.
Remember, practice makes perfect. Don’t be afraid to play around with these concepts in your own code. Try writing small programs that use variables and operators—calculate your weekly grocery costs, figure out the area of a rectangle, or even create a basic calculator.