Title | Python Intro Worksheet Solutions |
---|---|
Course | Models Of Computation |
Institution | The University of British Columbia |
Pages | 10 |
File Size | 246.7 KB |
File Type | |
Total Downloads | 69 |
Total Views | 178 |
first worksheet solutions...
LastName: ______________________ FirstName: ______________________ Signature: ______________________UBCStudent#:
IntrotoPythonWorksheet:Wesuggestthatyouworkingroupsofthreeorfour studentstocompletetheseexercises.Collaboratewithyourpeers,butbesure thatyouunderstandeachsolution.Eachofyouwillneedtohandinacopyofthe worksheetforparticipationmarks.
Mark:
8 236376 355150
Introduction to Python Worksheet Please use Jupyter to write Python code so that you can confirm your answers to these questions. We suggest that you make a prediction for what will happen before testing it in Jupyter, and then test in Jupyter to see if your understanding was correct. 1. What type of data is each of the following values?
Value
Type (float, int, str or bool)
10.0
float
'pi'
str
17
int
'3.14'
str
True
bool
'False'
str
-0.001
float
2. What are the first three things you like to look for to figure out what type of data a value is? (There's no one right answer, but it's good to think about your strategies.) Your response may vary, but some of the things that I look for are
Does the value have quotes around it? If so, it’s a string If the value is a number, is it a whole number (e.g. 3)? If so, then it’s an int. If not, it’s a float. Is it True or False? If so, it’s a bool.
Please use Jupyter to write Python code so that you can confirm your answers to the rest of the programming questions. We suggest that you make a prediction for what will happen before testing it in Jupyter, and then test in Jupyter to see if your understanding was correct. 3. For each of the following expressions, indicate what value it will evaluate to. Expression 3
*
Value
4 + 7
19
3
*
1.5
4.5
4
*
1.0
4.0
4.0 + '1'
TypeError
4.0/2.0 + 1.0
3.0
10/2 + 3
8.0
4. Circle all of the expressions below that will evaluate to the value True. 2 > 3
10 10.0 .0 > 9.999
3 = 3.0
5.0 == 5 5.0 .0
1.0 + 1.0 == 2.0
not False
not (10 < 20)
4.0 == 3 + 1
3.0 != 4 4.0 .0
False or True
False and True
True and True
(4 < 8 an and d 3 + 1 == 4) or 1 ! != = 2
2 > 1 or 2 > 4
5. The following code is evaluated in order. In the boxes on the right, provide the value that the expression on the left will evaluate to. a a a b a a a b a
= 3 + 2 = a + 1 = 2 == 4 + b = a + b
5 True 6 2 6
6. The following code is evaluated in order. In the boxes on the right, provide the value that the expression on the left will evaluate to. x y x y y x x x y
= 1 = 4 + 4 = 2 == 4 + y = y + 3
None False 3 5 2
7. For each of the following expressions, indicate what value the expression will be evaluated to, or if it will produce an error.
Expression
Value
'hello' + 'world' 'hello'
*
3
'helloworld' 'hellohellohello'
'hello' + 3.0 'hello'
*
'3'
TypeError TypeError
'world'[2]
'r'
'world'[:2]
'wo'
'world'[2:]
'rld'
'helloworld'[-1]
'd'
8. Using str1, str2 and str3 and basic string operations, write an expression that will evaluate to each of the following strings. str1 = 'University ' str2 = 'of ' str3 = 'British Columbia'
a. 'University of British Columbia' str1 + str2 + str3
b. 'Universe' str1[:7] + str1[4]
9. What is the value stored in result when each of the following code fragments have run? a.
value = 13 result = 'default' if value == 7: result = 'lucky' elif value == 13: result = 'unlucky' else: result = 'boring' 'unlucky'
b.
temp = 35.0 result = 'default' if temp < 10.0: result = 'too cold' elif temp > 25.0 : result = 'too hot' elif temp > 30.0 : result = 'way too hot' else: result = 'just right' 'too hot'
10. Write a code fragment (i.e. one or more lines of code) that creates a variable and assigns a value to it. It should assign 'short' if a string, s, is 3 characters or less, 'medium' if it is 7 characters or less, and 'long' otherwise. # here we assume s is defined above result = '' if len(s) 5: return 4.5 * 2 elif x < 2: return 4.5 + 7 else: return 4.5 if 4.5 > 5: return 4.5 * 2 elif x < 2: return 4.5 + 7 elif True: return 4.5 if False: return 4.5 * 2 elif x < 2: return 4.5 + 7 elif True: return 4.5 if False: return 4.5 * 2 elif False: return 4.5 + 7 elif True: return 4.5 if False: return elif 4.5 < return elif True: return 4.5
4.5 * 2 2: 4.5 + 7 4.5
14. Write a function that adds 2 to a number. def add_two(n): return n + 2
15. Write a function that produces a solid square of a given side-length and colour. def solid_square(side_length, colour): return square(side_length, "solid", colour)
16. a. Write a line of code that calls the function you wrote in problem 15 and assigns the square to a new variable that you created. red_square = solid_square(50, "red")
b. Write a line of code that calls the function you wrote in problem 15 with different inputs. Assign the value that is returned to a new variable. blue_square = solid_square(100, "blue")
c. Write a line of code that creates a new shape by placing the two squares beside each other. shape = beside(red_square, blue_square)
17. Is there anything you’d like to share with us so that we can help you achieve your goals in this class? For example, family or other commitments, personal needs such as needing to sit close to the front of class, etc.
---- If you are finished you can begin working on the pre-class work for next class ----...