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!
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:
Python1# Addition 2addition = 5 + 3 3print(addition) # prints 8 4 5# Subtraction 6subtraction = 5 - 3 7print(subtraction) # prints 2 8 9# Multiplication 10multiplication = 5 * 3 11print(multiplication) # prints 15 12 13# Division 14division = 5 / 3 15print(division) # prints 1.6666666666666667 16 17# Integer Division 18# The integer division returns only the integer part of the division, always rounding the result down 19integer_division = 5 // 3 20print(integer_division) # prints 1
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.
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:
Python1# Modulus 2remainder = 5 % 3 3print(remainder) # prints 2 4 5# Exponents 6cubed = 2 ** 3 7print(cubed) # prints 8 (2 * 2 * 2) 8 9# Float exponents 10square_root = 9 ** (1 / 2) # equals to (9 ** 0.5), or to square root of 9 11print(square_root) # prints 3.0
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.
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:
Python1# And operator 2print(False and False) # prints False 3print(False and True) # prints False 4print(True and False) # prints False 5print(True and True) # prints True 6 7# Or operator 8print(False or False) # prints False 9print(False or True) # prints True 10print(True or False) # prints True 11print(True or True) # prints True 12 13# Not operator 14print(not False) # prints True 15print(not True) # prints False
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:
Python1apples = 2 2apples_price = 3.5 3 4total_price = apples * apples_price 5print("Total price:", total_price) 6 7oranges = 3 8oranges_price = 4 9 10# The next line is a short form of total_price = total_price + oranges * oranges_price 11total_price += oranges * oranges_price 12print("Total price:", total_price)
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.
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!