0% found this document useful (0 votes)
21 views

Pre-Lab For Lab 4 SE100L Fall 2023

This document provides an introduction to conditional statements and operator precedence in Python programming. It discusses various mathematical operators like addition, subtraction, multiplication, division and their precedence order PEMDAS. It also introduces logical operators like IF statement to write conditional code in Python. Various escape sequences and formatting of numbers are explained along with examples to demonstrate their usage.

Uploaded by

M.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Pre-Lab For Lab 4 SE100L Fall 2023

This document provides an introduction to conditional statements and operator precedence in Python programming. It discusses various mathematical operators like addition, subtraction, multiplication, division and their precedence order PEMDAS. It also introduces logical operators like IF statement to write conditional code in Python. Various escape sequences and formatting of numbers are explained along with examples to demonstrate their usage.

Uploaded by

M.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Al Faisal University

College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab

Pre-lab for Lab 4– More Programming Problems and


Introduction to IF Statement
Introduction
In the first part of this lab, students will be reminded of the fundamentals of operator precedence
(PEMDAS) in order for them to examine how mathematical expressions are evaluated by the
computer program. Next, they will be introduced to the basic IF statement in order to understand
how conditional statements work in a program.
Operator Precedence in Python
Before we discuss the concept of precedence, let us remind ourselves of the fundamental
mathematical operators in Python:

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:

 P – Parentheses (Most Priority)


 E – Exponents
 M – Multiplication
 D – Division
 A – Addition
 S –Subtraction (Least Priority)
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
Division (/) and Integer Division (//)
Python 3 has two different division operators. The / operator performs floating-point division, and
the // operator performs integer division. Both operators divide one number by another. The
difference between them is that the / operator gives the result as a floating-point value, and the //
operator gives the result as a whole number. Let’s use the interactive mode interpreter to demonstrate:
>>> 5 / 2 Enter
2.5
>>>
In this session, we used the / operator to divide 5 by 2. As expected, the result is 2.5. Now let’s use
the // operator to perform integer division:
>>> 5 // 2 Enter
2
>>>
As you can see, the result is 2. The // operator works like this:
•When the result is positive, it is truncated, which means that its fractional part is
thrown away.
•When the result is negative, it is rounded away from zero to the nearest integer.
The following interactive session demonstrates how the // operator works when the result
is negative:
>>> −5 // 2 Enter
−3
>>>

The Remainder (Modulus) Operator (%)


In Python, the % symbol is the remainder operator. (Also known as the modulus operator.) The
remainder operator performs division, but instead of returning the quotient, it returns the remainder.
The following statement assigns 2 to leftover:
leftover = 17 % 3
This statement assigns 2 to leftover because 17 divided by 3 is 5 with a remainder of 2.The remainder
operator is useful in certain situations. It is commonly used in calculations that convert times or
distances, detect odd or even numbers, and perform other specialized operations.

The Exponent Operator (**)

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.

Grouping with Parentheses

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

Converting Expressions to Programming Statements

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.

Table of Escape Characters:

Formatting Numbers:

- Can format display of numbers on screen using built-in format function


- Two arguments:
· Numeric value to be formatted
· Format specifier
- Returns string containing formatted number
- Format specifier typically includes precision and data type
- Can be used to indicate scientific notation, comma separators, and the minimum field width used
to display the value
Al Faisal University
College of Engineering
Software Engineering Department
SE 100L – Programming for Engineers Lab
- The % symbol can be used in the format string of format function to format number as percentage
- To format an integer using format function:
· Use d as the type designator
· Do not specify precision
· Can still use format function to set field width or comma separator
Example

print(format(1234, "f"))
print(format(1234, ".2f"))
print(format(1234, "d"))
print(format(1234, ","))
print(format(1234, ",.4f"))

The Simple IF Statement


In the flowchart below, the diamond symbol indicates some condition that must be tested. In this
case, we are determining whether the condition Cold outside is true or false. If this condition is true,
the action Wear a coat is performed. If the condition is false, the action is skipped. The action is
conditionally executed because it is performed only when a certain condition is true.

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.

You might also like