Skip to main content

Introduction to Python programming for Beginners 3: Chapter 2 Exercise Solutions


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

#Prompt the user for their name

name = input("Enter your name: ")

#Print a welcome message with the user's name

print("Hello", name, "!", "Welcome to our program.")

When you run this program, it will display the message "Enter your name:" in the console. The user can type in their name, and after pressing Enter, the program will print a welcome message using their name.

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

# Prompt the user for the number of hours worked

hours = int(input("Enter the number of hours worked: "))

#Prompt the user for the hourly pay rate

rate = int(input("Enter the hourly pay rate: "))

# Calculate the gross pay

pay = hours * rate

#Display the gross pay

print("Pay: " pay)

In this program, we first use the input function to get the number of hours worked and the hourly pay rate from the user. We convert the input values to integer numbers using int() since pay rates and hours must be numbers.

Then, we calculate the gross pay by multiplying the hours worked by the hourly rate. Finally, we display the gross pay.

3. 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

1. width // 2: This expression performs integer division (floor division) of width by 2. Since width is an integer (17), the result will also be an integer. So, the value is 8 (floor division of 17 by 2), and the type is int.

2. width / 2.0: Here, width is divided by 2.0, which is a float. When you perform any operation involving a float, the result is also a float. So, the value is 8.5 (division of 17 by 2.0), and the type is float.

3. height / 3: height is a float (12.0), and we're dividing it by 3, which is an integer. The result will be a float. So, the value is 4.0 (division of 12.0 by 3), and the type is float.

4. 1 + 2 * 5: This expression involves both addition and multiplication. According to the order of operations (PEMDAS/BODMAS), multiplication is performed before addition. So, 2 * 5 is evaluated first, resulting in 10, and then 1 + 10 is evaluated, resulting in 11. The value is 11, and the type is int.


4. Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature

# Prompt the user for a Celsius temperature

celsius = float(input("Enter a temperature in Celsius: "))

# Convert Celsius to Fahrenheit

fahrenheit = (celsius * 9/5) + 32

# Print the converted temperature

print("The temperature in Fahrenheit is: ", fahrenheit,"°F")

In this program:

1. We use the input function to get user input, which is initially a string.

2. We convert the input to a floating-point number using float().

3. We then perform the conversion from Celsius to Fahrenheit using the formula (Celsius * 9/5) + 32.

4. Finally, we print out the converted temperature in Fahrenheit.

When you run this program, it will prompt you to enter a temperature in Celsius, and it will display the equivalent temperature in Fahrenheit.


Comments

Popular posts from this blog

Chapter 9 - Dictionaries in Python

Chapter 9 of the Python textbook,  Python for Everybody: Exploring Data Using Python 3 by Dr Charles R. Severance,  covers dictionaries, which are fundamental data structures in Python. Here are the significant points of each section along with explanations and examples: Section 9.1: Dictionaries Dictionaries are similar to lists but more general; they map keys to values. Keys can be almost any data type. You can create an empty dictionary using {} . To add items to a dictionary, use square brackets. Example: eng2sp = dict() eng2sp['one'] = 'uno' Section 9.2: Dictionary as a Set of Counters Dictionaries can be used to count the occurrence of items. You can create a dictionary to count characters in a string or words in a text. Example: word = 'brontosaurus' counts = dict() for c in word:     if c not in counts:         counts[c] = 1     ...

A Complete Guide to Developing Web Applications for Personal/Business Purposes (2023)

  In the early days of the internet, webpages were static, often featuring basic images and, occasionally, videos. However, the concept of web application development remained distant until around 2005 when the introduction of Ajax marked a turning point. Ajax brought forth the possibility of creating more sophisticated, faster, and interactive web applications. Fast forward to 2023, web applications have seamlessly integrated into our daily lives, often without us realizing it. From widely used applications like MS Word and PowerPoint to popular platforms like twitter, and even the ubiquitous Facebook, these applications offer personalized, immersive experiences comparable to native apps, accessible directly from web browsers. Modern web applications combine the user-centric nature of native mobile apps with the convenience of browser accessibility across various devices. This convergence makes web application development a coveted skill among developers, and more importantly,...

Free Self-taught Education in Programming/Computer Science!

    Contents Summary Community Curriculum Code of conduct Team Summary The programming skills for everyone is a  complete  computer science/programming  education path based solely on freely available online materials. It's for you if you want a proper,  well-rounded  basis in concepts fundamental to all programming and computing disciplines. You need to possess the discipline, will, and (most importantly!) good habits to obtain this skills largely on your own, but with support from a community of fellow learners. It is designed according to the degree requirements of undergraduate computer science majors, minus general education (non-CS) requirements, as it is assumed most of you are already educated/have knowledge outside the field of programming/CS. The courses themselves are among the very best in the world, often coming from Harvard, Princeton, MIT, etc. The coursework is also supplemented with relevant books when necessary. When there are courses...