163x Filetype PDF File size 0.74 MB Source: fivedots.coe.psu.ac.th
Python Exercises Aj. Andrew Davison CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th Starting with Python 1 Solve the following problems inside IDLE, or by writing small programs: a. Three people eat dinner at a restaurant and want to split the bill. The total is 352 Baht, and they want to leave a 15% tip. How much should each person pay? b. Calculate the area and perimeter of a rectangular room, 12.5 meters by 16.7 meters. c. If there are 3 buildings with 25 Ninjas hiding on each roof and 2 tunnels with 40 Samurai hiding inside each tunnel, how many Ninjas and Samurai are about to do battle? 2. Practice using Python as a calculator: 3 a. The volume of a sphere with radius r is 4/3 π r . What is the volume of a sphere with radius 5? Hints: π is about 3.141592, and 392.6 is the wrong answer! b. Suppose the cover price of a book is 249 Baht, but bookstores get a 40% discount. Shipping costs 30 Baht for the first copy and 15 Baht for each additional copy. What is the total cost for 60 copies? 3. Let x = 8 and y = 2. Write the values of the following expressions: a. x + y * 3 b. (x + y) * 3 c. x ** y d. x % y e. x / 12.0 f. x // 6 4. Let x = 4.66. Write the values of the following expressions: a. round(x) b. int(x) 5. Write a program that calculates and prints the number of minutes in a year. 6. Light travels at 3 * 108 meters per second. A light-year is the distance a light beam travels in one year. Write a program that calculates and displays the value of a light-year. 7. How does a Python programmer round a float value to the nearest int value? 8. How does a Python programmer add a number to the end of a string? Variables 1. Make a variable, and assign a number to it. Then display the variable using print(). Python Exercises 2. Modify the variable of question 1, either by replacing the old value with a new value, or by adding something to the old value. Display the new value using print(). 3. Get Python to calculate the number of minutes in a week using variables. Use variables called daysPerWeek, hoursPerDay, and minsPerHour, and then multiply them together. 4. How many minutes would there be in a week if there were 26 hours in a day? (Hint: Change the hoursPerDay variable in question 3.) 5. Assume that the foo variable has the value 33. What is the value of foo after the command foo = foo*2 executes? a. 35 b. 33 c. 66 6. The temperature converter program (tempConverter.py) outputs a number that shows too many decimal places. Modify the program to display at most two decimal places in the output number. Input 1. Write a program that asks for your first name, your last name, and then prints a message containing both your first and last names. 2. Write a program that asks for the width and length (in meters) of a rectangular floor, and then calculates and displays the total amount of carpet needed to cover the floor. 3. Write a program that does the same as in question 2, but also asks for the cost per square meter of carpet. Then have the program display three things: The total amount of carpet, in square meters The total amount of carpet, in square cms The total cost of the carpet 4. Write a program to calculate the volume and surface area of a sphere from its radius, 3 2 given as input. Some formulas that might be useful: V = 4/3 πr , A = 4 πr . 5. Write a program that calculates the cost per square inch of a circular pizza, given its diameter and price as input. A = πr2. 6. You can calculate the surface area of a cube if you know the length of an edge. Write a program that takes the length of an edge (an integer) as input and prints the cube’s surface area as output. 7. An object’s momentum is its mass multiplied by its velocity. Write a program that accepts an object’s mass (in kilograms) and velocity (in meters per second) as inputs, then outputs its momentum. Random and Maths 1. Write commands using the random module to print the following. a. A random integer in the range 0 to 10 b. A random float in the range -0.5 to 0.5 2 Python Exercises c. A random number representing the roll of a six-sided die d. A random number representing the sum resulting from rolling two six-sided dice e. A random float in the range -10.0 to 10.0 2. The math module includes a pow() function that raises a number to a given power. The first argument is the number, and the second argument is the exponent. Write code that imports this function and calls it to print the values 82 and 54. 3. The expression round(23.67) evaluates to which of the following values? a. 23 b. 23.7 c. 24.0 4. Write an import statement that imports just the functions sqrt() and log() from the math module. 5. Translate each of the following math expressions into a Python command. Assume that the math library has been imported (using import math). 6. Write a program that accepts four floats representing the points (x1,y1) and (x2,y2) and determines the distance d between them. Use √ 7. Write a program to calculate the area of a triangle given the length of its three sides a, b, and c. Use s = and A = √ 8. Write a program to calculate the length of a ladder required to reach a given height when leaned against a wall. The height h and angle A of the ladder are given as inputs. Use length = 9. The area of a hexagon can be calculated using the following formula (s is the length of a side of the hexagon): Write a program that prompts the user to enter the side of a hexagon, and then displays its area. Turtles 1. The Müller-Lyer illusion is shown below. Although the lines are the same length, they appear to be unequal. Draw this illusion with turtle graphics. 3 Python Exercises 2. Draw all following shapes using turtle graphics. a. A pentagon of length 50. b. A hexagon of length 60. c. An octagon of length 80. 3. Write a turtle program to draw a box without corners, like this one: 4. Write a turtle program to draw the shape: 5. Write a turtle program to draw the star shape: 6. Write a turtle program to draw the heart shape: Hints: the shape is made of two semi-circles and two straight lines. 7. Write a turtle program to draw the spiral shape: 4
no reviews yet
Please Login to review.