Pre-Lab For Lab 4 SE100L Fall 2023
Pre-Lab For Lab 4 SE100L Fall 2023
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
Additionally, let us all recall the very famous PEMDAS acronym that we all learned in school to
remember which operator is evaluated first. Note that operators are listed in descending order of
priority:
In addition to the basic math operators for addition, subtraction, multiplication, and division, Python
also provides an exponent operator. Two asterisks written together (**) is the exponent operator, and
its purpose is to raise a number to a power. For example, the following statement raises the length
variable to the power of 2 and assigns the result to the area variable:
area = length**2
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
…Back to Operator Precedence
You can write statements that use complex mathematical expressions involving several operators.
The following statement assigns the sum of 17, the variable x, 21, and the variable y to the variable
answer:
answer = 17 + x + 21 + y
Some expressions are not that straightforward, however. Consider the following statement:
outcome = 12.0 + 6.0 / 3.0
What value will be assigned to outcome? The number 6.0 might be used as an operand for either the
addition or division operator. The outcome variable could be assigned either 6.0 or 14.0, depending
on when the division takes place. Fortunately, the answer can be predicted because Python follows
the same order of operations that you learned in math class (never forget PEMDAS).
First, operations that are enclosed in parentheses are performed first. Then, when two operators share
an operand, the operator with the higher precedence is applied first. The precedence of the math
operators, from highest to lowest, are:
Exponentiation: **
Multiplication, division, integer division, and remainder: * / // %
Addition and subtraction: + −
Notice the multiplication (*), floating-point division (/), integer division (//), and remainder (%)
operators have the same precedence. The addition (+) and subtraction (−) operators also have the
same precedence. When two operators with the same precedence share an operand, the operators
execute from left to right. Now, let’s go back to the previous math expression:
outcome = 12.0 + 6.0 / 3.0
The value that will be assigned to outcome is 14.0 because the division operator has a higher
precedence than the addition operator. As a result, the division takes place before the addition.
Parts of a mathematical expression may be grouped with parentheses to force some operations to be
performed before others. In the following statement, the variables a and b are added together, and
their sum is divided by 4:
result = (a + b) / 4
Without the parentheses, however, b would be divided by 4 and the result added to a.
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
You probably remember from algebra class that the expression 2xy is understood to mean2 times x
times y. In math, you do not always use an operator for multiplication. Python, as well as other
programming languages, requires an operator for any mathematical operation.
When converting some algebraic expressions to programming expressions, you may have to insert
parentheses that do not appear in the algebraic expression. For example, look at the following
formula:
𝒂+𝒃
𝒙=
𝒄
To convert this to a programming statement, a + b will have to be enclosed in parentheses:
x = (a + b)/c
Escape Sequences
An escape character is a special character that is preceded with a backslash (\), appearing inside a
string literal. When a string literal that contains escape characters is printed, the escape characters
are treated as special commands that are embedded in the string.
For example, \n is the newline escape character. When the \n escape character is printed, it isn’t
displayed on the screen. Instead, it causes output to advance to the next line. For example, look at
the following statement:
print('One\nTwo\nThree')
When this statement executes, it displays
One
Two
Three
The \t escape character advances the output to the next horizontal tab position. (A tab position
normally appears after every eighth character.) The following statements are illustrative:
print('Mon\tTues\tWed')
print('Thur\tFri\tSat')
This statement prints Monday, then advances the output to the next tab position, then prints Tuesday,
then advances the output to the next tab position, then prints Wednesday. The output will look like
this:
Mon Tues Wed
Thur Fri Sat
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
You can use the \' and \" escape characters to display quotation marks. The following statements
are illustrative:
print("Your assignment is to read \"Hamlet\" by tomorrow.")
print('I\'m ready to begin.')
These statements display the following:
Your assignment is to read "Hamlet" by tomorrow.
I'm ready to begin.
You can use the \\ escape character to display a backslash, as shown in the following:
print('The path is C:\\temp\\data.')
This statement will display:
The path is C:\temp\data.
Formatting Numbers:
print(format(1234, "f"))
print(format(1234, ".2f"))
print(format(1234, "d"))
print(format(1234, ","))
print(format(1234, ",.4f"))
In Python, we use the if statement to write a single alternative decision structure. Here is the general
format of the if statement:
if condition:
statement
statement
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
For simplicity, we will refer to the first line as the if clause. The if clause begins with the word if,
followed by a condition, which is an expression that will be evaluated as either true or false. A colon
appears after the condition. Beginning at the next line is a block of statements. A block is simply a
set of statements that belong together as a group. Notice in the general format that all of the
statements in the block are indented. This indentation is required because the Python interpreter uses
it to tell where the block begins and ends. When the if statement executes, the condition is tested. If
the condition is true, the statements that appear in the block following the if clause are executed. If
the condition is false, the statements in the block are skipped.