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
      Java Developer

      This blog will break down the skills from the Software Developer learning track you must focus on. Java Programming Language In this track, you should start off learning the Java Programming language. Here, you will need to learn about the basic building blocks of a programming language such as loops, control flow, and data types, […]

      Imtiaz Ahmad
      Self Improvement | Software Development
      Aging Software Developer

      This blog is dedicated to the Aging Software Developer out there. And this topic was inspired by an older gentleman who reached out to me through the mail. His concern was based on the restructuring that took place at his company where the executives were planning to replace their workforce. And this aging developer was […]

      Imtiaz Ahmad
      Software Development
      Entry Level Developer Job Requires 2 Years Experience?

      There are some myths surrounding job postings today. Maybe you came across Entry Level Developer job postings that required two years of experience and wondered, “What’s all this about?” Good job postings versus bad job postings To perform well in a job, candidates must possess the right skill set, which is listed in the job […]

      Imtiaz Ahmad