Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.18 Review Questions

1. What does the acronym IDE stand for? What does it do?

2. What does the acronym CPU stand for? What does it do?

3. How many bytes are in a GB? What does GB stand for?

4. What is the decimal equivalent of the binary number 01101100?

5. What is the hexadecimal equivalent of the binary number 01101100?

6. What is the binary equivalent of the number −62?

7. What is the ASCII equivalent of the decimal number 62?

8. What is a type in Python? Give an example. Why are there types in Python programs?

9. How can you tell what type of value is stored in 4 contiguous bytes of memory?

10. How can you interactively work with the Python interpreter?

11. What is prototyping as it applies to computer programming?

12. Name two different types of errors that you can get when writing a computer program? What is unique about each type of error?

13. What is a reference in a Python program?

14. Why is it that the result of 4.01−3.59 is 0.41999999999999993 when using at least some implementations of Python 3?

15. What would you have to write to ask the user to enter an integer and then read it into a variable in your program? Write some sample code to do this.

16. Assume that you have a constant defined for pi = 3.14159. You wish to print just 3.14 to the screen using the pi variable. How would you print the pi variable

so it only display 3.14?

1.19 Exercises

1. Write a program that asks the user to enter their name. Then it should print out the ASCII equivalent of each of the first four characters of your name. For instance, here is a sample run of the program below.

Please enter your name : Kent K ASCII value i s 75

e ASCII value i s 101 n ASCII value i s 110 t ASCII value i s 116

2. Write a program that capitalizes the first four characters of a string by converting the characters to their ASCII equivalent, then adding the necessary amount to capitalize them, and converting the integers back to characters. Print the capitalized string. Here is a sample of running this program.

Please enter a four character string : kent The string capitalized i s KENT

3. You can keep track of your car's miles per gallon if you keep track of how many miles you drive your car on a tank of gas and you always fill up your tank when getting gas. Write a program that asks the user to enter the number of miles you drove your car and the number of gallons of gas you put in your car and then prints the miles per gallon you got on that tank of gas. Here is a sample run of the program.

Please enter the miles you drove : 256

Please enter the gallons of gas you put in the tank : 10.1 You got 25.346534653465348 mpg on that tank of gas .

4. Write a program that converts US Dollars to a Foreign Currency. You can do this by finding the exchange rate on the internet and then prompting for the exchange rate in your program. When you run the program it should look exactly like this:

What i s the amount of US Dollars you wish to convert ? 31.67 What i s the current exchange rate

(1 US Dollar equals what in the Foreign Currency )? 0.9825 The amount in the Foreign Currency i s $31 .12

5. Write a program that converts centimeters to yards, feet, and inches. There are

2.54 cm in an inch. You can solve this problem by doing division, multiplication, addition, and subtraction. Converting a float to an int at the appropriate time will help in solving this problem. When you run the program it should look exactly like this (except possibly for decimal places in the inches):

How many centimeters do you want to convert ? 127.25 This i s 1 yards , 1 feet , 2.098425 inches .

6. Write a program that computes the minimum number of bills and coins needed to make change for a person. For instance, if you need to give $34.36 in change you would need one twenty, one ten, four ones, a quarter, a dime, and a penny. You don't have to compute change for bills greater than $20 dollar bills or for fifty cent pieces. You can solve this problem by doing division, multiplication, subtraction, and converting floats to ints when appropriate. So, when you run the program it should look exactly like this:

How much did the item cost : 65.64

How much did the person give you : 100.00 The person 's change is $ 34 . 36

The bills or the change should be :

1 twenties

1 tens

0 fives

4 ones

1 quarters

1 dimes

0 nickels

1 pennies

Fig. 1.23 A right triangle

7. Write a program that converts a binary number to its decimal equivalent. The binary number will be entered as a string. Use the powers of 2 to convert each of the digits in the binary number to its appropriate power of 2 and then add up the powers of two to get the decimal equivalent. When the program is run, it should have output identical to this:

Please enter an eight digit binary number : 01010011 The decimal equivalent of 01010011 i s 83.

8. Write a program that converts a decimal number to its binary equivalent. The decimal number should be read from the user and converted to an int. Then you should follow the algorithm presented in Example 1.1 to convert the decimal number to its binary equivalent. The binary equivalent must be a string to get the correct output. The output from the program must be identical to this:

Please enter a number : 83

The binary equivalent of 83 i s 01010011.

You may assume that the number that is entered is in the range 0–255. If you want to check your work, you can use the bin function. The bin function will take a decimal number and return a string representation of that binary number. However, you should not use the bin function in your solution (Fig. 1.23).

9. Complete the program started in Practice Problem 1.10. Write a program that asks the user to enter the two legs of a right triangle. The program should print the length of the hypotenuse. If sideA and sideB are the lengths of the two legs and sideC is the length of the third leg of a right triangle, then the Pythagorean

theorem says that side A2 + side B2 = sideC2. Ask the user to enter side A and

side B. Your program should print the value of sideC.

Please enter the length of the first leg : 3

Please enter the length of the second leg : 4 The length of the hypotenuse i s 5.0

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel