data = [10, 2.5, "3"]Classwork 2
Python Fundamentals
Question 1
(a)
Using Python operations only, calculate below: \[\frac{2^5}{7 \cdot (4 - 2^3)}\]
(b)
Given:
Using Python only, calculate 10 + 2.5 + 3
(c)
Given:
exam_score = {"Midterm": 78, "Final": 92}Compute the Total Exam Score:
\[ \begin{align} \text{(Total Exam Score)} = \max\{ &0.50\times \text{(Midterm)} + 0.50\times \text{(Final)},\\ &0.33\times \text{(Midterm)} + 0.67\times \text{(Final)} \} \end{align} \]
Question 2
For each expression below, what is the value of the expression? Explain thoroughly.
(a)
20 == '20'(b)
x = 4.0
y = .5
x < y or 3*y < xQuestion 3
(a)
What will the following code print? (Answer without running it.)
status = "Gold"
cart_total = 92.5
free_shipping_levels = ["Gold", "Platinum"]
if status in free_shipping_levels:
shipping = 0
msg = f"Status: {status} → Free shipping! Total = ${cart_total}"
elif cart_total >= 100:
shipping = 0
msg = f"Spend threshold met → Free shipping! Total = ${cart_total}"
else:
shipping = 7.99
msg = f"Status: {status}, Total = ${cart_total} → Shipping = ${shipping}"
print(msg)(b)
- Fill in the blanks:
- If
nameis"Geneseo"andscore >= 90→ print"Geneseo high score" - Else if
score >= 90→ print"High score" - Else → print
"Try again"
- If
name = "Geneseo"
score = 88
# Fill in the blanks
if ______________________________:
print("Geneseo high score")
elif ____________________________:
print("High score")
else:
print("Try again")Question 4
(a)
What will the following code print? (Do not run it—reason it out.)
nums = [10, 20, 30, 40, 50, 60, 70]
print(nums[2:6])
print(nums[:4])
print(nums[::2])
print(nums[-4:-1])(b)
fare = "$10.00"
tip = "2.00$"
tax = "$ 0.80"Write a Python code that uses slicing and the print() function to print out the following message:
Question 5
list_variable = [100, 144, 169, 1000, 8]Write a Python code that uses print() and max() functions to print out the largest value in the list, list_variable, as follows:
Question 6
vals = [3, 2, 1, 0]- Use a
whileloop to print each value of the list [3, 2, 1, 0], one at a time. - Use a
forloop to print each value of the list [3, 2, 1, 0], one at a time.
Question 7
Assign the value 7 to the variable
guess_me, and the value 1 to the variablenumber.Write a
whileloop that comparesnumberwithguess_me.- Print ‘too low’ if
numberis less than guess me. - If
numberequalsguess_me, print ‘found it!’ and then exit the loop. - If
numberis greater thanguess_me, print ‘oops’ and then exit the loop. - Increment
numberat the end of the loop.
- Print ‘too low’ if
Write a
forloop that comparesnumberwithguess_me.- Print ‘too low’ if
numberis less than guess me. - If
numberequalsguess_me, print ‘found it!’ and then exit the loop. - If
numberis greater thanguess_me, print ‘oops’ and then exit the loop. - Increment
numberat the end of the loop.
- Print ‘too low’ if
Question 8
- You are given a variable value that contains a string.
value = "$320"- Write a Python program to:
- Attempt to convert value to a floating-point number using
float(). - If the conversion is successful, print: “You entered:
”. - If the conversion fails (e.g., the value is not a valid number), print: “That’s not a valid number!”.
- Attempt to convert value to a floating-point number using
- Use a
try-exceptblock to handle the potential error.
Question 9
- Import the
pandaslibrary aspd. - Install the
seleniumlibrary. - From
selenium, import the functionswebdriver.
Discussion
Welcome to our Classwork 2 Discussion Board! 👋
This space is designed for you to engage with your classmates about the material covered in Classwork 2.
Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.
If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 2 materials or need clarification on any points, don’t hesitate to ask here.
All comments will be stored here.
Let’s collaborate and learn from each other!