Operations

Section Vocab:

  • Arithmetic Operators: Standard addition, subtraction, and multiplication operators.
  • Comparison Operations: Compares values variables and returns true or false.
  • Logical Operators: Used to combine multiple true and false values to return a single true or false value.

Associated Learning Outcome:

At the end of this module students will be able to:

  • Identify arithmetic/comparison/logical operators in relation to variables.

Arithmetic Operations

(ankthon, 2020)

Arithmetic Operators are quite useful in python programming. They are used to carry out mathematical operations such as addition, subtraction, multiplication, and division.

Order of Arithmetic Operations:

The order of operations follows much like BEDMAS in mathematics. The order of operations is listed below with 1 being the fist operations completed and 4 being the last operations completed.

  1. Brackets ():
  2. Exponents **:
  3. Multiplication *, Division (Float) /, Division (Floor) //, Modulus %
  4. Addition +, Subtraction –

Another note about arithmetic operations is that output type can depend on the two input data types. Some operations work how we expect, like adding two integers outputs an integer; however, other operations may work slightly different than expected, such as dividing integers, “4 / 2” would output the float 2.0.

Comparison Operations

Comparison Operations are used to compare values and variables. When two values or variables are compared the result will be a Boolean (True or False).

Let’s take a look at some comparison operators.

(ankthon, 2020)

Comparison operations work much like asking a true or false question.

For example:

The statement β€œ4 < 5” is like asking β€œIs 4 less than 5?”. The answer or output would be True. On the other hand, the statement ”5 < 4” is like asking ”Is 5 less than 4?”.  The answer or output would be False.

The β€œequal to” or == is another useful comparison operation. It is used to determine if two values are the same. The statement β€œ4 == 5” is like asking β€œIs 4 equal to 5” and the output would be False. The statement β€œ4 == 4” is like asking β€œIs 4 equal to 4”, the answer would be yes, and the output would be True.

Logical Operations

(ankthon, 2020)

Logical AND and Logical OR are two logical operators that allow us to combine multiple True/False statements to produce a single Boolean output. A logical operator is used between two comparison statements to provide a single Boolean output (True / False).

Building on our example above we can use a logical operator between the two statements,

 β€œ4 < 5 and 4 == 5”. This statement would be equivalent to asking, β€œIs 4 less than 5 and is 4 equal to 5?”. It is true that 4 is less than 5 but it is false that 4 is equal to 5. Since we used the β€œand” operator, both elements of the statement must be True for the statement to be True. Therefore, the output for the statement β€œ4 < 5 and 4 == 5” would be False. In contrast, if we used the Logical Or, β€œ4 < 5 or 4 == 5”, the statement would be like asking β€œIs 4 less than 5 or is 4 equal to 5?” In this case, we used the β€œor” operator and if one of the statements is True than the entire statement becomes True. Therefore, the statement β€œ4 < 5 or 4 == 5” is True.

Combining Operations

Combining multiple operators can be an effective method to create complex logic statements. Before we go into an example it is important to note that there is an order in which the operators are evaluated.

Order of operator evaluation:

  1. First, the arithmetic operators.
  2. Next, the comparison operators.
  3. Finally, the logical operators.

Now let’s take a look at the statement β€œ5 + 7 <= 3 * 5 and 3**2 == 18 / 2”.

In order to correctly evaluate the output of this statement we must use our rules for the order of operator evaluation. The first operations evaluated are the arithmetic operations, in this case that would be the β€œ+, *, **, /”. So, β€œ5 + 7” equals 12,  β€œ3 * 5” equals 15, β€œ3 ** 2” equals 9.0, and β€œ18 / 2” equals 9.0.  After the arithmetic operations are completed, the statement would look like β€œ12 <= 15 and 9.0 == 9.0”.

Next, the comparison operations are assessed, that would be the β€œ<=, ==”. β€œ12 <= 5” is True and β€œ9.0 == 9.0” is True. After the comparison operations are completed, the statement would look like β€œTrue and True”.

Finally, the logical operator β€œand” is considered, β€œTrue and True” is True.

Therefore, when the statement β€œ5 + 7 <= 3 * 5 and 3**2 == 18 / 2” is evaluated, the output is β€œTrue”.

Interactive Activities:

Reading:

https://pythonnumericalmethods.berkeley.edu/notebooks/chapter01.05-Logial-Expressions-and-Operators.html

Viewing:

(Analyst, 2022)

References

Analyst, A. T. (2022, Dec). Comparison, Logical, and Membership Operators in Python | Python for Beginners . Retrieved from YouTube: https://www.youtube.com/watch?v=lPVke-p4S7s

ankthon. (2020, Aug 29). Relational Operators in Python. Retrieved from Geeks for Geeks: https://www.geeksforgeeks.org/relational-operators-in-python/

Qingkai Kong, T. S. (2020). Logical Expressions and Operators. Retrieved from Python Mumerical Methods: https://pythonnumericalmethods.berkeley.edu/notebooks/chapter01.05-Logial-Expressions-and-Operators.html