Introduction to Python

You are not logged in.

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'
        

If you assign 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])
        

Given the following string: species_name = "Acacia buxifolia" what does species_name[11:-3] return?

2) Data Types and Type Conversion

What type of value is 3.25 + 4?

In Python 3, the // operator performs integer (whole-number) floor division, the / operator performs floating-point division, and the % (or modulo) operator calculates and returns the remainder from integer division

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.

def num_surveys_needed(num_subjects, num_per_survey): num_surveys_needed = 0 # Write code to calculate the number of surveys needed to reach # everyone once. Assign that calculation to num_surveys_needed return num_surveys_needed

Given the variables: 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 as x = 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 the help 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

Fill in the blanks in the program below so that it prints the reverse of the original character string.
def reverse_str(og_str): result = '' for char in og_str: # write code here to reverse the string result = '' return result

Fill in the blanks in the program below so that it adds up the number of characters in each word.
def add_word_len(words): total = 0 for word in words: # write code here to add up word lengths total = 0 return total

Create an acronym with the first letter of each word in the list. Capitalize each first letter. For example, ['red','green','blue'] should become 'RGB'
def make_acronym(words): acronym = '' for word in words: # write code here to create your acronym acronym = '' return acronym

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)
        

Fill in the code to make new_list contain only the items from original_list that are of type int.
def only_ints(original_list): new_list =[] for my_item in original_list: # modify the code to only append items that are of type int # the isinstance built-in function may be helpful new_list.append(my_item) return new_list

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)
        

Fill in the code to create a list of square roots given a list of numbers.

Assume list will only contain ints.

def square_root(num): return num**(0.5) def sqrt_list(original_list): pass # Fill out this function

Define a function named int_to_float that takes in a list of ints and returns a list of floats of the same number.