Lecture 2 — Exercises¶
Two sets of exercises are given below. Problems in the first set are for you to work on your own but not submit for a grade. Problems in the second set must be submitted for a grade. Normally, these will be due within 24 hours of the end of the lecture they are associated with. For these Lecture 2 exercises only, they will be due at the end of Lab 1 so that any problems with submission can be ironed out.
Students are welcome to work on these problems in small groups, but each student should write the final version of their solutions independently. Each student should submit their own solutions.
- Write a single line of Python code that calculates the radius of a circle with area 15 units and prints the value. The output should just be the number that your code produces. Your code should include the use of an expression involving division and exponentiation (to compute the square root). Use the value 3.14159 for pi.
- Which of the following are legal Python variable names?
import 56abc abc56 car-talk car_talk car talk
- Which of these lines of code contain syntax errors? Once you fix the syntax errors, the program will still not correctly print the area of a circle with radius 6.5. What two more changes are needed to fixed these errors?
pi = 22 / 7 area = pi * r * r r = 6.5 r + 5 = r_new print(area)
Problems for Grade Submission¶
- Write a single line of Python code that converts the temperature 64 from Celsius to Fahrenheit and prints the value. Submit a Python file containing just this single line of code. The output should just be the number that your code produces. Your code must include the use of an expression involving multiplication and a print function call.
- Write Python code that creates three variables called length, width and height to store the dimensions of a 16.5 x 12.5 x 5 box. Write additional code that calculate the volume of the box and calculations its surface area, storing each in a variable. Print the values of these variables. Your code must use five assignment statements and two print function calls. Submit a file containing these seven lines of Python code. Your output should be
volume = 1031.25 area = 702.5
- Your problem is to determine the output of the Python program shown below. You must submit a text file showing the output. (Hint: there should be two lines with one integer on each line.) While it is possible to just run the program and copy the output, we strongly encourage you to not do this. You will need to develop the ability to read code and understand what it will do. You will be tested on it.
z = 2 z = z**2**3 print(z) x = 6 x = x**2 + 6 - z // 10 * 2 print(x)