ENGR102_Lecture2_F24(v2)
ENGR102_Lecture2_F24(v2)
Fall 2024
From last class
• Headers for .py files
• You can copy the header directly from Canvas or the provided .py file (the “don’t copy
code” rule does not apply to this)
• Print blank line
• print()
• Remember to read the lab instructions (pdf on Canvas)
• Provides additional information not given in zyBooks
• May contain additional coding tips not covered in lecture
• print("A day has", 24*60*60, "seconds")
• Rounding issues in Lab: Topic 1, Activity #2 (print_math.py)
• PT office hours
• May not have started yet
2
Computer Memory
• If we can’t remember things, we can’t really do very much.
• Computers have memory – the ability to remember information.
3
Computer Offline Memory Slower to access
More total data
(e.g. Cloud)
Memory More long-lasting
7
The (Python) rules for naming
• Names can start with a letter (lowercase or uppercase) or an
underscore (_).
• But you generally shouldn’t start them with an _ since this tends to imply
certain things about the variable (uses not covered in this class).
• The name can contain letters (lowercase or uppercase), numbers, and
underscore characters.
• The name cannot be a reserved keyword.
• These are special commands reserved for the language itself.
• If a variable name meets all these requirements, then it is considered
valid.
8
The 35 Python keywords – for your future
reference
False class from or
True continue global pass
None def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
9
In this course we will use these
False class from or
True continue global pass
None def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
10
Naming Variables and Constants
• So, you could name all your variables to minimize typing.
• e.g. a, b, c, …
• But this would result in confusion later – what do these mean?
• A better way of thinking about variable (and other) names you choose
in code is to think of communicating with other people.
• You are communicating with anyone you are sharing the code with, including
anyone who has to take over your code.
• But most importantly to you, you are communicating with the future you –
the you that will have forgotten many of the details of the code you are going
back to after doing other things.
11
The importance of names
• Names help us understand the world, identify particular things, and
so on.
• In computers, the name of a variable distinguishes one particular box
of memory.
• But a good name can give more information – about the purpose for
that piece of memory.
• Just because a name is valid, doesn’t mean it’s good.
12
Are these names valid and good?
• abcde Valid but not good (meaningless)
• Age Valid and good
• my_name Valid and good
• My_Name Valid and good (and different from previous)
• MyName Valid and good (and different from previous)
• 2nd_name Not valid (starts with a number)
• name_2nd Valid, and probably good
• Winner! Not valid (contains !)
• F0rTheW1n Valid, but not good (digits are confusing)
• _density Valid, probably not good (begins with _)
• Gig'Em Not valid (contains ')
• Gig Em Not valid (contains a space)
• Gig-Em Not valid (contains a -)
• Gig_Em Valid, but probably not good (not clear what it contains)
13
More about naming
• Generally, pick descriptive names
• Volume might be a better choice than V.
• Not too long, though
• Volume_of_the_sphere is probably too long
• V_sphere, or VolSphere, or Vsphere, etc. might be better names
• There are a few conventions people use:
• Constants (that never change) are often in ALL_CAPITALS.
• Variables i, j, and k often used for counting or indexing.
• Variables typically start with lower-case letters.
14
Determining the Variables You Need:
Mapping Problems to Software
• Mapping problems to software representations involves answering a
number of questions:
• What question are you trying to answer using the software?
• What are the characteristics of the problem that affect the outcome?
• How do they affect the outcome and how do they interact?
• Who is using the software you’re creating?
• Answers to the second question are likely to help determining what
variables your software will have.
• The other questions affect the design and the individual operations.
15
Example Problem Taken from Your Own Life
• One of the most important skills engineering students need to have is
financial literacy (e.g., how to take out a loan).
• What questions do we want to answer?
• How much will I pay in interest for a loan?
• How much do I need to save each month to avoid taking out a loan?
• What characteristics affect the outcome? Think of these as your variables.
• The interest rate
• The cost of college / a car / a house
• How long it takes to save for or pay off the loan
16
Names for our Loan Calculator Problem
• Important concepts/values:
• Time it takes to save for or pay off a loan
• t – advantage is it’s short, disadvantage is it’s ambiguous with lots of other concepts
• length – better, but could be confusing because most often associated with physical dimensions not
time
• time – also better, but this could be time until graduation, time to payoff loan, grace period, etc.
• payoff_time – even better, but what are the units (days, months, years)
• payoff_time_m – good try, but “m” is vague and the meaning is not clear
• payoff_time_months – more likely to be understood, but it’s now a long name
• Annual interest rate
• interest_rate
• Initial loan amount
• loan_amount
• Writing software requires trade-offs.
• One that will be apparent throughout this course is the trade-off between ease of 17
comprehension and compactness.
Variable Values
• When we see a variable name in a program, that is a placeholder for
the value contained in that variable’s “box” of memory.
• Example: say the variable age holds value 19
• What would be the value of age + 3?
• age + 3 = 19 + 3 = 22
19
age
18
Assignment
• The process of sticking a value into the box of memory is called
assignment.
• Can assign to any variable – new or old.
• When a new value is assigned to a variable, the old value disappears –
it is replaced by the new one.
• Can (and often does) happen many times for the same variable
Assign 30 to age
19 30
age age
19
Assignment statements
• Python assignment statements have the form:
<Variable name> = <Value to assign>
The = is the assignment The right hand side of the
The left hand side is the operator. assignment operator is the
variable name for the It is NOT the “equals” sign, value that will be assigned
“box” in memory that will though we still often read to the box.
hold the value. it aloud as “equals”. We
can read it aloud as “gets”
or “is assigned.”
19
age
20
Assignment statements
• Python assignment statements have the form:
<Variable name> = <Value to assign>
• When this statement is executed:
• FIRST we evaluate the right hand side
• THEN we assign that value to the left hand side
• Some examples follow
21
Example 3
x
• What is the result of the executing the
statement:
z = x + 1 5
y
*Original
value
before line
10 of code is
z run.
22
Example 3
x
• What is the result of the executing the
statement:
z = x + 1 5
1. We first find what is the value in variable
y
x, which is 3. *New value
after line of
2. Then, we add 3 + 1 to get 4. code is run.
23
Example 3
x
• What is the result of the executing the
statement:
z = x + 2 * y 5
y
*Original
value
before line
10 of code is
z run.
24
Example 3
x
• What is the result of the executing the
statement:
z = x + 2 * y 5
1. We first find what is the value in variables x y
and y, which are 3 and 5. *New value
2. Then, we multiply 2 * 5 (order of after line of
code is run.
operations!) to get 10. 13
3. Then, we add 3 + 10 to get 13. z
4. Then, we assign that value, 13, to variable z,
replacing the value previously stored there
25
Example 3
x
• What is the result of the executing the
statement:
z = z + 1 5
y
*Original
value
before line
10 of code is
z run.
26
Example 3
x
• What is the result of the executing the
statement:
z = z + 1 5
1. We first find what is the value in variable
y
z, which is 10. *New value
after line of
2. Then, we add 10 + 1 to get 11. code is run.
27
Example 3
x
• What is the result of the executing the
statement:
z = z + 1 5
• Notice: the use of the same variable on the y
right and left side is not a problem! *New value
after line of
• While this statement would make no sense code is run.
if the = really meant “equals”, it makes 11
perfect sense since = is actually an z
assignment operation.
28
Giving Instructions
• If you were asked to give a stranger directions from one place to
another, how would you do it?
• Drive straight to the next light
• Turn right
• Drive 1 mile
• Take the entrance ramp to the highway
• Drive until exit 391
• …
29
Sequential Steps
• We are used to giving and receiving instructions as a sequential series
of steps.
• This is a very important process – learning how to break up a complex task in
various ways is critical to programming, and to dealing with any large project.
• Including many parts of engineering
• You’ll get some practice with this in lab.
30
Sequential Steps
• When we give instructions to a computer, we’ll do the same thing.
• Our instructions will be a sequence of steps that we want the computer to do.
• We can think of the computer as mindlessly following those instructions, in
the order they’re specified.
• Later, we will see ways of giving instructions that aren’t sequential!
• See video
31
Writing sequential steps using pseudocode
• Pseudocode is a flexible, precise way to express steps to solve a
problem using natural language (English) instead of code (Python).
• Guidelines:
• Steps should be clear and concise Steps to solve the quadratic
equation
• Focus on the procedure rather than the details o set the coefficients of the
polynomial Ax^2 + Bx + C = 0
• Make a bulleted list, not sentences o calculate D = B^2 - 4AC
• Write one action per line o calculate E = √D / 2A
o calculate F = -B / 2A
• Don’t just write equations, include action words o calculate x1 = F - E
• Don’t use code, use natural language o calculate x2 = F + E
o print x1 and x2
• Start with pseudocode, then translate to Python.
32
Assignment sequences
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
z = 3 y
z = 5
z = 1
33
Assignment sequences
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
Nex
t
after the final statement?)
z = 3 It can be helpful to keep track of y
z = 5 the “instruction pointer” – that
is, what the next instruction to
z = 1 be executed is. We can think of
the program as being “at” these
points between statements.
34
Assignment sequences
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
Nex
t z = 3 After the first statement is y
z = 5 executed, the variable z has the
value 3 stored inside.
z = 1
3
z
35
Assignment sequences
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
z = 3 After the second statement, z y
Nex z = 5 has the value 5.
t
z = 1
5
z
36
Assignment sequences
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
z = 3 After the third statement, z is 1. y
z = 5
Nex
t
z = 1
1
z
37
Assignment sequences (2)
• Consider the following sequences of assignments. x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
x = 3 y
y = 5
z = x
x = 1
z
38
Assignment sequences (2)
• Consider the following sequences of assignments.
3
x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
Nex
t
x = 3 y
y = 5
z = x
x = 1
z
39
Assignment sequences (2)
• Consider the following sequences of assignments.
3
x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
x = 3 5
y
Nex y = 5
t
z = x
x = 1
z
40
Assignment sequences (2)
• Consider the following sequences of assignments.
3
x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
x = 3 5
y
y = 5
Nex
t
z = x
x = 1 3
z
41
Assignment sequences (2)
• Consider the following sequences of assignments.
1
x
What is the value of z at the end?
• (i.e. what is the value stored in the variable named z
after the final statement?)
x = 3 5
Notice that the value of x y
y = 5 changed, but NOT the value of z.
z = x
The third statement, assigning
Nex
t
x = 1 z=x was just assigning the 3
current value of x to z, not
making z forever equivalent to x.
z
42
Assignment sequences (3)
• Consider the following sequences of assignments. PI
What is the value of each variable at the end?
PI = 3.14159
r = 5.3 r
volume = PI * r * r * r * (4 / 3)
volume
43
Assignment sequences (3)
• Consider the following sequences of assignments.
3.14159
PI
What is the value of each variable at the end?
Nex
t PI = 3.14159
r = 5.3 r
volume = PI * r * r * r * (4 / 3)
volume
44
Assignment sequences (3)
• Consider the following sequences of assignments.
3.14159
PI
What is the value of each variable at the end?
PI = 3.14159
5.3
Nex
t
r = 5.3 r
volume = PI * r * r * r * (4 / 3)
volume
45
Assignment sequences (3)
• Consider the following sequences of assignments.
3.14159
PI
What is the value of each variable at the end?
PI = 3.14159
5.3
r = 5.3 r
Nex
t volume = PI * r * r * r * (4 / 3)
588.977
volume
46
Assignment sequences (4)
• Consider the following sequences of assignments. x
What is the value of each variable at the end?
x = 2
y = 3 y
x = x * x
y = y + 2
z = x * y z
z = z + 1
47
Assignment sequences (4)
• Consider the following sequences of assignments.
2
x
What is the value of each variable at the end?
Nex
t
x = 2
y = 3 y
x = x * x
y = y + 2
z = x * y z
z = z + 1
48
Assignment sequences (4)
• Consider the following sequences of assignments.
2
x
What is the value of each variable at the end?
x = 2
3
Nex
t
y = 3 y
x = x * x
y = y + 2
z = x * y z
z = z + 1
49
Assignment sequences (4)
• Consider the following sequences of assignments.
4
x
What is the value of each variable at the end?
x = 2
3
y = 3 y
Nex
t x = x * x
y = y + 2
z = x * y z
z = z + 1
50
Assignment sequences (4)
• Consider the following sequences of assignments.
4
x
What is the value of each variable at the end?
x = 2
5
y = 3 y
x = x * x
Nex
t
y = y + 2
z = x * y z
z = z + 1
51
Assignment sequences (4)
• Consider the following sequences of assignments.
4
x
What is the value of each variable at the end?
x = 2
5
y = 3 y
x = x * x
y = y + 2
Nex z = x * y 20
t
z
z = z + 1
52
Assignment sequences (4)
• Consider the following sequences of assignments.
4
x
What is the value of each variable at the end?
x = 2
5
y = 3 y
x = x * x
y = y + 2
z = x * y 21
z
Nex
t z = z + 1
53
Special Assignment Operators
• Certain types of operations are very common, so some special
assignment operators have been defined.
• Example:
x = x + c
• Can be written:
x += c
• Which means, for the variable on the left, increment by the amount on
right.
• Similarly, for: -=, *=, /=, etc.
• e.g. a -= 3 means a = a - 3
54
Assignment sequences (5)
• Consider the following sequences of assignments. x
What is the value of each variable at the end?
x = 2
y = 3 y
x *= 4
y += 7 * x
z = x + y z
z -= z + 1
55
Assignment sequences (5)
• Consider the following sequences of assignments.
2
x
What is the value of each variable at the end?
Nex
t x = 2
y = 3 y
x *= 4
y += 7 * x
z = x + y z
z -= z + 1
56
Assignment sequences (5)
• Consider the following sequences of assignments.
2
x
What is the value of each variable at the end?
x = 2
3
Nex
t
y = 3 y
x *= 4
y += 7 * x
z = x + y z
z -= z + 1
57
Assignment sequences (5)
• Consider the following sequences of assignments.
8
x
What is the value of each variable at the end?
x = 2
3
y = 3 The variable x gets its prior y
value, x, multiplied by 4.
Nex
t x *= 4
This was equivalent to:
y += 7 * x x=x*4
z = x + y z
z -= z + 1
58
Assignment sequences (5)
• Consider the following sequences of assignments.
8
x
What is the value of each variable at the end?
x = 2
59
y = 3 The variable y has its value y
incremented by 7 times the
x *= 4 value of x, or 56.
Nex
t
y += 7 * x This was equivalent to:
y=y+7*x
z = x + y z
z -= z + 1
59
Assignment sequences (5)
• Consider the following sequences of assignments.
8
x
What is the value of each variable at the end?
x = 2
59
y = 3 y
x *= 4
y += 7 * x
z = x + y 67
Nex
t z
z -= z + 1
60
Assignment sequences (5)
• Consider the following sequences of assignments.
8
x
What is the value of each variable at the end?
x = 2
59
y = 3 The variable z has its value y
decremented by the value of z
x *= 4 plus one.
z = x + y
z=z-(z+1) -1
z
Nex
t z -= z + 1
61
Assignment sequences (6)
• Consider the following sequences of assignments.
5
x
What is the value of each variable at the end?
Nex
t
z *= 2 + x
4
How does the order of y
operations work?
z = (z*2) + x = (3*2) + 5 = 11
OR
z = z*(2+x) = 3*(2+5) = 21
3
z
62
Assignment sequences (6)
• Consider the following sequences of assignments.
5
x
What is the value of each variable at the end?
Nex z *= 2 + x
t
4
How does the order of y
operations work?
z = (z*2) + x = (3*2) + 5 = 11
OR
z = z*(2+x) = 3*(2+5) = 21
21
z
63
A reminder of output
• Basic output is to the “console” – a window or the screen that shows the
output of the program.
• To show the value of a variable, we “print” it.
• Command is:
print(x)
where x is the thing you want to be printed
• The x can be:
• a constant fixed value (a “literal”)
• a variable
• or an expression
• a combination of literals, constants, variables, functions, etc. that compute to a single value
64
Output
• Example: The following program
a = 10
b = 2 ** 4
b -= a
c = b // 2
print(c)
• Output: 3
65
Lab Hints
66
Linear interpolation – 2D space
• See video and pdf in Canvas under Module 2.
Point 1: (1, 2)
Point 3: (3, 4)
Point 2: (2, ?)
( 𝑦 3− 𝑦1)
𝑦 2= ( 𝑥2 − 𝑥 1) + 𝑦 1
( 𝑥3− 𝑥1)
( 4 −2 )
𝑦 2= ( 2 − 1 ) + 2=3
( 3 − 1)
67
Explanations for Lab Topic 2
LAB: Topic 2 (team) part 2
Activity #3, Part 1
68
Explanations for Lab Topic 2
LAB: Topic 2 (team) part 2
Activity #3, Part 2
69
Linear interpolation – 3D space
• See video and slides in Canvas under Module 2
( 𝑥3 − 𝑥 1 )
𝑥 2= ( 𝑡 2 − 𝑡1 ) + 𝑥 1
( 𝑡3 − 𝑡 1 )
( 𝑦 3− 𝑦1)
𝑦 2= ( 𝑡 2 − 𝑡 1) + 𝑦 1
( 𝑡 3 − 𝑡1 )
( 𝑧3 − 𝑧 1 )
𝑧 2= ( 𝑡 2 − 𝑡1 ) + 𝑧 1
( 𝑡3 − 𝑡 1 )
70
Due Dates/Reminders
• Due times are 11:59 pm unless noted otherwise.
Assignment Section 506 Section 207 Section 576
Module 1 Quiz Thursday (8/22) Thursday (8/22) Wednesday (8/21)
(syllabus quiz)
LAB: Submission Sunday (8/25) Sunday (8/25) Monday (8/26)
practice
LAB: Topic 1 Sunday (8/25) Sunday (8/25) Monday (8/26)
LAB: Topic 2 Thursday (8/29) Thursday (8/29) Wednesday (8/28)
NEW (team) parts 1&2
LAB: Topic 2 Thursday (8/29) Thursday (8/29) Wednesday (8/28)
NEW (individual)
Module 2 Quiz Monday (in class) Monday (in class) Tuesday (in class)
Bring a
NEW calculator