Introduction to Python
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://petrock.mit.edu) to authenticate, and then you will be redirected back to this page.
Practice and review what you have learned about Python using the following problems! This is not a comprehensive list of practice problems, but we hope it is still helpful!
1) Variables and Assignment
# Given the following code snippet, what is the value of y at the end:
x = 1.0
y = 3.0
swap = x
x = y
y = swap
# Given the following code snippet, what is the value of position at the end:
initial = 'left'
position = initial
initial = 'right'
a = 123, what happens if you try to get the second digit of a via a[1]?
# What does the following program print?:
atom_name = 'carbon'
print(atom_name[1:3])
species_name = "Acacia buxifolia" what does species_name[11:-3] return?
2) Data Types and Type Conversion
3.25 + 4?
If num_subjects is the number of subjects taking part in a study, and
num_per_survey is the number that can take part in a single survey,
write an expression that calculates the number of surveys needed
to reach everyone once.
first = 1.01, second = "1", and third = "1.1". Which of the following will return the floating point number 2.0?
2.1) Note on complex numbers
Python provides complex numbers, which are written asx = 1.0 + 2.0j.
A complex number's real and imaginary parts can be accessed using x.real and x.imag.
3) Built-in Functions and Help
# What is the final value of radiance?
radiance = 1.0
radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5))
# What is the value of final_val?
easy_string = "abc"
final_val = max(easy_string)
# What is the value of final_val?
final_val = max('gold','tin')
3.1) Note on getting help with functions
You can use thehelp function to get more information about a function, such as calling help(round).
4) Lists
# What is the final value of my_list?
my_list = [1,2,3]
my_list.append(8)
my_list.append(7)
# What is the value of my_str?
my_list = [a,b,c]
my_str = '_'.join(my_list)
# What is the value of my_str?
my_str = 'hi wrld'
my_list = list(my_str)
# What is the value of my_str?
starter_word = "apricot"
my_str = starter_word[::2]
# what does the following program print
letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)
# What is the value of old?
old = list('old')
new = old
new[1] = 'L'
# What is the value of old?
old = list('old')
new = old[:]
new[1] = 'L'
5) For Loops
6) Conditionals
# What does this program print?
pressure = 71.9
if pressure > 50.0:
pressure = 25.0
elif pressure <= 50.0:
pressure = 0.0
print(pressure)
# What is the value of result?
original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4]
result = []
for value in original:
if value < 0.0:
result.append(0)
else:
result.append(1)
7) Writing Functions
You have already been writing and using functions in previous practice problems, but here are a few more practice problems with them.
# What is the value of result?
def print_time(hour, minute, second):
time_string = str(hour) + ':' + str(minute) + ':' + str(second)
print(time_string)
result = print_time(11, 37, 59)
Assume list will only contain ints.