## CS105 Study Guide

### 2a. Explain Python arithmetic operators, relational operators, logical operations, and the bool data type

• How do arithmetic operators differ from relational operators?
• How do logical operators differ from relational operators?
• What are the truth tables for the logical AND and logical OR?

Computer languages need a host of arithmetic operators that can emulate any mathematical expression so they can perform computations. Every arithmetic operator provided in Python is of equal importance, and you need to understand each one. Do not fall into the trap of knowing some but not fully understanding others. The same holds true for relational and logical operators.

The bool data type can only take a value of either True or False. Make sure to practice combinations of logical and relational operators in order to predict their outcome. To do this, it is important to know the truth tables for logical operations such as OR and AND:

 A B A OR B False False False False True True True False True True True True

 A B A AND B False False False False True False True False False True True True

Take care to summarize the meaning of the logical OR and logical AND. It is important to understand the truth tables for these operators, but you should also try to develop an intuitive understanding of their meaning. The logical AND between two values can only evaluate to True if both values are True. The logical OR between two values can only evaluate to False if both values are False. These summary statements can be used as "shortcuts" when evaluating combinations of logical and relational operators.

### 2b. Explain operator precedence

• What is PEMDAS?
• In the absence of parentheses, in what direction should an expression with multiple operators be evaluated?
• What is the purpose of operator precedence?

The general idea behind operator precedence is that sequences of operations should be consistent with basic mathematical rules of algebraic expressions. This is the point of PEMDAS: parentheses first, then multiplication or division before addition or subtraction. The programming of an arithmetic expression should reflect our expectation of the outcome of the expression. If you are comfortable with evaluating algebraic expressions, the rules for programming should be a natural extension of your basic mathematical training. If this is the case, then programming arithmetic expressions follows in a very logical manner.

The leap to integrating relational and logical operators into operator precedence follows from the rules of Boolean algebra. Since most introductory programming courses do not cover Boolean algebra to any great depth, it is best to memorize the operator precedence for logical and relational operators.

To review, see Practice With Operator Precedence.

### 2c. Construct programs that apply variable types, operators, and operator precedence

• How can using combinations of operators be useful for programming?
• Why is it important to be aware of data types when combining various operators?
• Why is operator precedence a crucial topic when it comes to programming?

It is all well and good to understand operators when applied at the level of evaluating an expression. However, this is a programming course and our goal is to construct programs that apply data types and combinations of operators. Strict attention must be paid to data types and operator precedence in order to guarantee a desired result. For example, the difference between using floating point division (/) and integer division (//) can be substantial. Understanding practical applications of the modulo (%) operator such as finding the divisor of an integer should not be minimized. Finally, you should be able to accurately predict the outcome of a series of instructions involving variables, operators, and operator precedence. For example, given the following combination of arithmetic, logical, and relational operators:

a=True
b=10
c=3.0
y=b%c<=5 and not a or b//5>1
print(y)

you should be equipped to conclude that a True would be printed out to the screen because b//5>1 is True. Even though the AND truth table tells us that b%c<=5 and not a is False, the OR truth table tells us that False or True evaluates to a value of True.

To review, see Operators and Expressions.

### Unit 2 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

• arithmetic operators
• relational operators
• logical operators
• bool data type
• truth tables
• operator precedence
• PEMDAS
• floating point division
• integer division
• modulo operator