Get 60% off lifetime access &
job ready curriculum guide

YOU WILL RECEIVE DISCOUNT CODE AND THE GUIDE INTO YOUR EMAIL AFTER SIGNING UP

    We don’t send spam. Unsubscribe anytime.

    Author

    “Your course helped me get my job as a software engineer at Charles Schwab. The course was easy to follow and covered all the material I'd need to nail a leetcode style interview. Thank you!”

    - Mohammed Khan
    Arithmetic Operators in Python

    Arithmetic Operators in Python

    October 23, 2024 by Imtiaz Ahmad

    Table of Contents

    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!

    Subscribe

      We do not send spam. Unsubscribe anytime.
      Back to blog
      Software Development
      Can you become a Software Developer?

      Can you become a Software Developer? To answer this question, let me bust some myths you might have about a career in Software Development. Need for a college degree To begin with, do you need a college degree? Absolutely. It doesn’t have to be in computer science or information technology. It could also be anything […]

      Imtiaz Ahmad
      Self Improvement
      The No PLAN-B Mindset & How Champions Win

      In this blog, I want to share with you today the secret ingredient every confident and successful person has planted deep within them. I want to tell you that, you will not overcome your fear, anxiety, or self-doubt. You will not overcome the thought that everyone else is better than you. Until you face the […]

      Imtiaz Ahmad
      Software Development
      How to Succeed as a Software Developer

      Now, you are on a journey to pursue Software Development. How can you succeed? It’s through Determination. Determination is that sense of joy that boosts us to move forward. It’s that sense of urgency that helps us achieve our goals.  So, now comes the question, how does determination work? Triangle of belief To understand this, […]

      Imtiaz Ahmad