Python makes it easy to perform basic arithmetic operations, which are foundational for any kind of programming. By the end, you’ll be comfortable using Arithmetic Operators in Python, including addition, subtraction, multiplication, division, and more advanced operations like modulus. Whether you’re working with integers, floating-point numbers (decimals), or need to use more advanced operations, Python has you covered. In this blog, we’ll walk you through how to perform arithmetic calculations in Python, so you can build a solid foundation for your coding journey.
Arithmetic Operators in Python
Python supports several arithmetic operations, including:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
Let’s start by defining two variables and performing some basic calculations. For example, if we have:
num1 = 3 num2 = 10
You can easily perform arithmetic like addition, subtraction, and multiplication:
answer = num1 + num2 # Adds the numbers answer = num1 * num2 # Multiplies the numbers
If you print these results, you’ll see how Python handles these operations. For instance, multiplying 3
by 10
gives us 30
, which is an integer because it’s a whole number.
Division and Floats
When it comes to division, Python returns a float if the result isn’t a whole number. For example:
answer = num1 / num2
This will result in a float like 0.3
, because dividing 3
by 10
doesn’t result in a whole number. Python automatically converts the result to a float, making it easy to work with decimal values.
Modulus Operator (Remainders)
A slightly less common but useful operator is the modulus operator (%
). This operator gives you the remainder when dividing two numbers. For example:
answer = num2 % num1 # This gives the remainder of 10 divided by 3
In this case, 10 % 3
will return 1
, because when you divide 10
by 3
, the remainder is 1
. The modulus operator is incredibly useful in a variety of programming scenarios, especially when solving problems related to even/odd numbers or cycling through values.
Order of Operations (PEMDAS)
Just like in math, Python follows the order of operations. This means that multiplication and division are performed before addition and subtraction, unless you use parentheses to change the order. For example:
answer = 10 + 3 * 9 - 4
Here, Python will first multiply 3
by 9
, then subtract 4
, and finally add 10
, giving you the result 33
. If you want the addition to happen first, use parentheses:
answer = (10 + 3) * (9 - 4)
Now, Python will first calculate 10 + 3
, then 9 - 4
, and finally multiply the results to give 65
.
Working with Different Data Types
Python handles both integers and floats seamlessly. For example, if you work with:
answer = 999 / 756
The result will be a float, because dividing these numbers doesn’t yield a whole number. Understanding how Python automatically converts between types is essential for avoiding bugs in your code.
Resources
- Join Job Ready Programmer Courses and gain mastery in Data Analytics & Software Development.
- Access our free Programming Guide (PDF) to explore our comprehensive Job Ready Curriculum today!
- Check out the Free Python Basics Lecture Series on Job Ready Programmer’s YouTube Channel.
-
Special Discount: 20% Off PyCharm Professional IDE
- Use the promo code “JRP_ProPyPath_24” on this JetBrains Checkout Page: JetBrains Store
- Use this code and get a 20% discount on a PyCharm Professional IDE license, valid until February 5, 2025.
- We recommend this for our learners to explore more advanced features.
- Note: The entire crash course series can be followed using the free PyCharm Community version.
- Watch the following free lecture on the Arithmetic Operators in Python:
Conclusion
Arithmetic Operators in Python are straightforward and flexible, allowing you to work effortlessly with integers and floats. Understanding how these operations work will help you build more complex programs down the road. Stay tuned for more as you continue your Python learning journey!
About the Author
Imtiaz Ahmad is an award-winning Udemy Instructor who is highly experienced in big data technologies and enterprise software architectures. Imtiaz has spent a considerable amount of time building financial software on Wall St. and worked with companies like S&P, Goldman Sachs, AOL and JP Morgan along with helping various startups solve mission-critical software problems. In his 13+ years of experience, Imtiaz has also taught software development in programming languages like Java, C++, Python, PL/SQL, Ruby and JavaScript. He’s the founder of Job Ready Programmer — an online programming school that prepares students of all backgrounds to become professional job-ready software developers through real-world programming courses.
[fluentform id="8"]