Classwork 4

Python Basics

Author

Byeong-Hak Choe

Published

January 29, 2025

Modified

February 3, 2025

Question 1

Using Python operations only, calculate below: \[\frac{2^5}{7 \cdot (4 - 2^3)}\]

Answer




Question 2

For each expression below, what is the value of the expression? Explain thoroughly.

20 == '20'
x = 4.0
y = .5

x < y or 3*y < x

Answer




Question 3

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:

The total trip cost is: $12.80


Answer




Question 4

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:

The largest value in the list is: 1000


Answer




Question 5

vals = [3, 2, 1, 0]
  • Use a while loop to print each value of the list [3, 2, 1, 0], one at a time.
  • Use a for loop to print each value of the list [3, 2, 1, 0], one at a time.


Answer




Question 6

  • Assign the value 7 to the variable guess_me, and the value 1 to the variable number.

  • Write a while loop that compares number with guess_me.

    • Print ‘too low’ if number is less than guess me.
    • If number equals guess_me, print ‘found it!’ and then exit the loop.
    • If number is greater than guess_me, print ‘oops’ and then exit the loop.
    • Increment number at the end of the loop.
  • Write a for loop that compares number with guess_me.

    • Print ‘too low’ if number is less than guess me.
    • If number equals guess_me, print ‘found it!’ and then exit the loop.
    • If number is greater than guess_me, print ‘oops’ and then exit the loop.
    • Increment number at the end of the loop.


Answer




Question 7

  • You are given a variable value that contains a string.
value = "$320"
  • Write a Python program to:
    1. Attempt to convert value to a floating-point number using float().
    2. If the conversion is successful, print: “You entered: ”.
    3. If the conversion fails (e.g., the value is not a valid number), print: “That’s not a valid number!”.
  • Use a try-except block to handle the potential error.


Answer




Question 8

  • Import the pandas library as pd.
  • Install the itables library.
  • From itables, import the functions init_notebook_mode and show.


Answer




Discussion

Welcome to our Classwork 4 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 4.

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 4 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!

Back to top