Class
Welcome 🤗! In the next ~4h we will cover:
Valiant Technology Ltd., CC BY-SA 3.0, via Wikimedia Commons Source
Before we begin with the class:
Test ¶ (25 min)
Accept the assignment
👉 Use Submission from the editor
Preparation review ¶ E: Reading check ¶ E: Arithmetics, expressions, strings, values and types ¶ Let us check your understanding to find out other holes.
E: Variables and statements ¶ Which line/s contain good variable names?
I don’t know
Which line/s show correct way of using variables?
9/5 * (fahrenheit - 32) = celsiusaltitude = speed * durationI don’t know
Which keyword do we use to include the not builtin features of Python like sqrt and pi?
I don’t know
Which are true?
x**2 is a statement , because it has an effect, but not value.
Computing the value of an expression is called evaluation .
The computer executes statements.
x = 2 is an assignment statement .
I don’t know
What is the output?
# base x = 2
y = 3 # exponent
# addend
z = 1
print(x ** y + z)9
16
SyntaxError
NameError
I don’t know
E: Functions ¶ Which code/s use correct function definition syntax?
int area(int w, l):
return w * lint area(int w, int l):
return w * ldef area(w, l):
return w * ldef area(w, l):
print(w * l)I don’t know
What is the output?
a = 1
def f():
a = 2
b = 3
f()
a, bNameError
(2, 3)
(1, 3)
(3, 1)
I don’t know
Whenever a function is executed, a new local namespace is generated for this function call. This namespace may introduce its of names and can be obtained by locals()
a = 1
def f():
a = 2
print(locals())
f()outputs:
Note that locals() returns a dict
When we are not in a function, we are at the module level. This namespace can be obtained by globals():
a = 1
def f():
a = 2
print(globals()['a'])
print(locals()['a'])
f()outputs:
When we are not in a function, the global and local namespaces are the same:
a = 1
def f():
a = 2
print(globals() == locals())
f()
globals() == locals()outputs:
What is the output?
a = 1
def f():
b = a
print(b)
f()I don’t know
Which code/s contain valid repetition syntax?
for i in range(10):
print(i) foreach book in books:
book.read()for (i in range(i)):
print(i)for _ in range(10):
print("❤️") I don’t know
Which is/are valid reasons to use functions?
To name a group of statements which together solve a problem. This makes my program easier to read.
Functions make a program smaller and therefore easier to read by eliminating repetitive code.
Testing my program becomes easier, because I can test every function alone.
I can reuse functionality, e.g., making the robot arm move in a particular sequence, somewhere else in my program.
I don’t know
You got the following traceback from your program. Where would you typically first look for errors?
NameError Traceback (most recent call last)
Cell In[44], line 1
----> 1 cat_twice(line1, line2)
Cell In[36], line 3, in cat_twice(part1='Always look on the ', part2='bright side of life.')
1 def cat_twice(part1, part2):
2 cat = part1 + part2
----> 3 print_twice(cat)
Cell In[41], line 2, in print_twice(string='Always look on the bright side of life.')
1 def print_twice(string):
----> 2 print(cat) # NameError
3 print(cat)
NameError: name 'cat' is not definedAt the top of the traceback (line 1)
In the middle: (line 3)
At the bottom: (line 2)
Right after the first error at the top (line 2)
I don’t know.
Coding ¶ Heads up - Update your template and uninstall pyright extension ¶ I modified the template. To update it:
Go to Extensions
Uninstall Pyright
Go to Source Control and pull the changes
You will seetwo additional extensions recommended. If not, go to Extensions and look to recommended extensions and install them.
Live coding ¶ (5-10 min)
Most drink containers have a deposit. Write a program deposit.py that reads the number of containers from the user for each type of bottle and displays the refund amount.
Assume the following types[1] :
Type A: DKK 1.00
Type B: DKK 1.50
Type C: DKK 3.00
The refund output must be formatted.
Enter type A count: 1
Enter type B count: 2
Enter type C count: 3
Refund amount: DKK 13.00
E: Restaurant bill ¶ (5-10 min)
Do you need support? ¶ Answer the following in 📊 Live poll :
Do you need more support for the exercises in the class than what the teacher (and TA) supports you?
Yes, most of the time I need more support than I currently get.
Sometimes — depends on the exercise.
No, the current support is enough and/or I rarely need help.
I don’t know.
Do you have time and energy to support your fellows, including those sitting further from you?
Yes, most of the time.
Sometimes – depends on the exercise.
No.
I don’t know.
E: Restaurant bill ¶ (5-10 min)
E: Center align ¶ Write a function named center_align that takes a string named text as the first parameter and width as the second parameter and prints the string with enough leading spaces that the strings are centered int the middle of width number of columns.
def print_center_aligned(text, width):
""" Aligns the string in the center of the `width` number of columns."""
## YOUR CODE HERE
print_center_aligned("geko", 20)
print_center_aligned("is", 20)
print_center_aligned("very happy today!", 20)
print_center_aligned("geko", 50)
print_center_aligned("is", 50)
print_center_aligned("very happy today!", 50)
The code should output:
geko
is
very happy today!
geko
is
very happy today!
E: 99 Bottles of beer ¶ (10-20 min)
https:// allendowney .github .io /ThinkPython /chap03 .html #id3
E: Converting seconds to hours, minutes, and seconds ¶ E: Budget variance ¶ Turtle graphics ¶ We will use Turtle 🐢 to learn algorithmic thinking.
E: Hello Turtle ¶ E: Reflection ¶