Introduction

Welcome back! Today, our fun coding journey will guide us through the world of arithmetic and logical operations. These fundamental concepts are frequently used in Python programming and significantly assist in performing calculations as well as making evaluations that direct decision-making in code. By the end of this lesson, you'll master performing basic arithmetic operations, such as adding the numbers of a high score or dividing measurements for a recipe. You'll also understand how to use logical operations to check multiple conditions simultaneously, a deciding factor in a program that, for instance, only permits access if a user has not been banned and has entered the correct credentials. Let's embark on this exciting journey!

Basic Arithmetic Operators

Python enables us to seamlessly perform basic arithmetic operations — addition, subtraction, multiplication, and division — akin to how we would do them on paper. Let's open our CodeSignal IDE and work with these operations:

In this example, we conducted five basic arithmetic operations using Python operators and stored the results in correspondingly named variables such as addition, subtraction, multiplication, division, and integer_division. We then printed the results. These operations essentially serve as the foundation for more intricate calculations in your code.

Advanced Arithmetic Operations

Beyond the basic arithmetic operations, Python also offers operators for modulus (finding the remainder from division) and exponents (raising a number to the power of another). Let's delve into these with some examples:

In real-world programming, the modulus operator helps determine if a number is even (hint: number % 2) or odd, aiding in specific game algorithms. Similarly, the exponent operator is vital in computations like calculating the area of a shape or determining growth over time in compound interest math.

Logical Operations

Additionally, Python offers three simple yet potent logical operators to evaluate multiple conditions: and, or, and not. These operators establish robust logic algorithms, control the program flow, or aid in making decisions based on the program's scenario.

As demonstrated in the examples below, and returns True only when all conditions are true, or returns True if any condition is true, and not inverses the truth value, changing True to False and vice versa. Eventually, these become indispensable tools in creating intricate decision-making in conditional statements that we will cover later in this course.

Let's see how they function:

Applying operations to variables

Applying operations to raw numbers is fun, but calculators can do the same! However, the most important feature of Python is that you can operate with variables and apply all the above operations to variables as well. Here are some examples:

As you can see, we first calculated the total price to purchase 2 apples, and then decided to buy 3 more oranges - it was as simple as updating the existing total_price variable, adding the price of 3 oranges to it. Note how we used the += operator here - it's basically just a short form of total_price = total_price + oranges * oranges_price, which makes it very effective. You can use other operators like -=, *=, /=, and //= the same way.

Lesson Summary

You've done a stellar job! This lesson has equipped you with diverse arithmetic and logical operations in Python. These essential tools enable you to perform key calculations, extract meaningful information from numbers, and evaluate logical conditions, stepping up your Python coding game. But learning doesn't stop here!

Get set for our practice exercises! Using these, you'll apply your new knowledge of arithmetic and logical operators to real-life problems. Remember, practice makes perfect. So, dive into these exercises and continue your remarkable Python programming journey. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal