Dictionaries
Functions
Modules
File I/O
100
Syntax for accessing the value mapped to key "a" in dictionary d

What is d[a]?

100

This function outputs things such as messages to the screen

What is print()?

100

To use something in a module, you must do this

What is import it? 

100

This keyword automatically closes the file for you (you don't need to call fh.close())

What is with?


Example: 

with open("test.txt") as fh

200

This statement removes an entry from the dictionary

What is del?

Example: del d[key]

200

The purpose of input()

What is gathering the user's response to a question or prompt?

200

This function gives you the list of contents in a module

What is dir()

200

The code for accessing the working directory

What is os.getcwd()

300

This function returns all the keys in a dictionary as a list

What is d.keys()?
300

The following code outputs:

def printPattern(n):

    for i in range(n, 0, -1):

         print(((n-i) * ' ') + (i * '*')) 

What is an inverted star pattern

****

  ***

   **

     *


300

I want to write a piece of code that will simulate a random number generator. I need to import this module. 

What is random?

300

This is the code to count the number of lines in a file

with open("text.txt", 'r') as fp:    

    x = len(fp.readlines())    

    print('Total lines:', x)

400

The algorithm to convert the following lists to a dictionary


keys = ['Ten', 'Twenty', 'Thirty'] 

values = [10, 20, 30]

keys = ['Ten', 'Twenty', 'Thirty'] 

values = [10, 20, 30] 

res_dict = dict(zip(keys, values)) 

print(res_dict)

400
The syntax to define a function (the first line in a function declaration)

What is:

def function_name(parameters):

400

The errors in this piece of code are:

import math()

sqrt(math, 2)

What is removing the () after the import statement and changing the sqrt to math.sqrt(2)?

**dot notation

400

Count the number of lines in a file using a generator

def reader(file_name):
    for row in open(file_name, "r"):
        yield row

500

Merge two dictionaries into one:

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30} 

dict2 = {'Thirty': 30, 'Forty': 40, 'Fifty': 50}

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}

 dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50} 

dict3 = dict1.copy()

 dict3.update(dict2) 

print(dict3)  

500

This function has the main logic and calls other functions

What is main()?

500

The difference between these 3 lines: 

1) from math import sqrt

2) from math import *

3) from math import sqrt as square_root

What is importing a specific function, importing all the functions, and importing a specific function and naming it something else? 

500

This program prints line 4 in a file. 


with open("test.txt", "r") as fp:    

lines = fp.readlines()   

print(lines[3])  

M
e
n
u