[Solved] Python for Everybody - Chapter 2: Variables, Expressions, and Statements

Minh Vu

By Minh Vu

Updated Jan 05, 2024

Figure: [Solved] Python for Everybody - Chapter 2: Variables, Expressions, and Statements

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

This tutorial gives you the solutions to the exercises in Chapter 2 of the book Python for Everybody: Exploring Data Using Python 3 by Charles R. Severance.

Link to the book:

Solutions for other chapters:

Contents

1. Chapter 2: Variables, Expressions, and Statements - Exercise 2

Exercise 2: Write a program that uses input to prompt a user for their name and then welcomes them.

Answer:

exercise-2.py
name = input('Enter your name: ') print('Hello', name)

Output:

console
python exercise-2.py Enter your name: Minh Vu Hello Minh Vu

Explanation:

  1. We use the input function to prompt the user for their name by displaying the message Enter your name:.
  2. The input function returns the user's input as a string then we assign it to the variable name.
  3. We use the print function to display the message Hello followed by the user's name.

2. Chapter 2: Variables, Expressions, and Statements - Exercise 3

Exercise 3: Write a program to prompt the user for hours and rate per hour to compute gross pay.

Answer:

exercise-3.py
hours = float(input('Enter Hours: ')) rate = float(input('Enter Rate: ')) pay = hours * rate print('Pay:', pay)

Output:

console
python exercise-3.py Enter Hours: 35 Enter Rate: 2.75 Pay: 96.25

Explanation:

  1. Similar to exercise 2, we use the input function to prompt the user for the number of hours and the rate per hour.
  2. However, as the input function returns the user's input as a string, we need to convert it to a float using the float function.
  3. We then calculate the gross pay by multiplying the number of hours and the rate per hour.
  4. Finally, we use the print function to display the gross pay.

Advanced:

We can use the round function to round the gross pay to 2 decimal places.

exercise-3-advanced.py
hours = float(input('Enter Hours: ')) rate = float(input('Enter Rate: ')) pay = hours * rate print('Pay:', pay) print('Pay:', round(pay, 2))

3. Chapter 2: Variables, Expressions, and Statements - Exercise 4

Exercise 4: Assume that we execute the following assignment statements:

width = 17 height = 12.0

For each of the following expressions, write the value of the expression and the type (of the value of the expression).

  1. width//2
  2. width/2.0
  3. height/3
  4. 1 + 2 * 5

Use the Python interpreter to check your answers.

Answer:

exercise-4.py
width = 17 height = 12.0 print(width//2, type(width//2)) print(width/2.0, type(width/2.0)) print(height/3, type(height/3)) print(1 + 2 * 5, type(1 + 2 * 5))

Output:

console
python exercise-4.py 8 <class 'int'> 8.5 <class 'float'> 4.0 <class 'float'> 11 <class 'int'>

Explanation:

  1. width//2 is an integer division, so the result is an integer.
  2. width/2.0 is a float division, so the result is a float.
  3. height/3 is a float division, so the result is a float.
  4. 1 + 2 * 5 is a mathematical expression, so the result is an integer.

4. Chapter 2: Variables, Expressions, and Statements - Exercise 5

Exercise 5: Write a program that prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature.

Answer:

exercise-5.py
celsius = float(input('Enter Celsius temperature: ')) fahrenheit = celsius * 9 / 5 + 32 print('Fahrenheit temperature:', fahrenheit)

Output:

console
python exercise-5.py Enter Celsius temperature: 99 Fahrenheit temperature: 210.2

Explanation:

  1. The formula to convert Celsius to Fahrenheit is F = C * 9 / 5 + 32.
  2. We use the input function to prompt the user for the Celsius temperature.
  3. We then convert the Celsius temperature to Fahrenheit using the formula above.
  4. Finally, we use the print function to display the Fahrenheit temperature.
Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.