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:
- [Solved] Python for Everybody - Chapter 2: Variables, Expressions, and Statements (this tutorial)
- [Solved] Python for Everybody - Chapter 3: Conditional Execution
- [Solved] Python for Everybody - Chapter 5: Iteration
- [Solved] Python for Everybody - Chapter 6: Strings
- [Solved] Python for Everybody - Chapter 8: Lists
Contents
- 1. Chapter 2: Variables, Expressions, and Statements - Exercise 2
- 2. Chapter 2: Variables, Expressions, and Statements - Exercise 3
- 3. Chapter 2: Variables, Expressions, and Statements - Exercise 4
- 4. Chapter 2: Variables, Expressions, and Statements - Exercise 5
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:
name = input('Enter your name: ') print('Hello', name)
Output:
python exercise-2.py Enter your name: Minh Vu Hello Minh Vu
Explanation:
- We use the
input
function to prompt the user for their name by displaying the messageEnter your name:
. - The
input
function returns the user's input as a string then we assign it to the variablename
. - We use the
print
function to display the messageHello
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:
hours = float(input('Enter Hours: ')) rate = float(input('Enter Rate: ')) pay = hours * rate print('Pay:', pay)
Output:
python exercise-3.py Enter Hours: 35 Enter Rate: 2.75 Pay: 96.25
Explanation:
- Similar to exercise 2, we use the
input
function to prompt the user for the number of hours and the rate per hour. - However, as the
input
function returns the user's input as a string, we need to convert it to a float using thefloat
function. - We then calculate the gross pay by multiplying the number of hours and the rate per hour.
- 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.
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).
width//2
width/2.0
height/3
1 + 2 * 5
Use the Python interpreter to check your answers.
Answer:
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:
python exercise-4.py 8 <class 'int'> 8.5 <class 'float'> 4.0 <class 'float'> 11 <class 'int'>
Explanation:
width//2
is an integer division, so the result is an integer.width/2.0
is a float division, so the result is a float.height/3
is a float division, so the result is a float.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:
celsius = float(input('Enter Celsius temperature: ')) fahrenheit = celsius * 9 / 5 + 32 print('Fahrenheit temperature:', fahrenheit)
Output:
python exercise-5.py Enter Celsius temperature: 99 Fahrenheit temperature: 210.2
Explanation:
- The formula to convert Celsius to Fahrenheit is
F = C * 9 / 5 + 32
. - We use the
input
function to prompt the user for the Celsius temperature. - We then convert the Celsius temperature to Fahrenheit using the formula above.
- Finally, we use the
print
function to display the Fahrenheit temperature.
Comments
Be the first to comment!