What is d[a]?
This function outputs things such as messages to the screen
What is print()?
To use something in a module, you must do this
What is import it?
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
This statement removes an entry from the dictionary
What is del?
Example: del d[key]
The purpose of input()
What is gathering the user's response to a question or prompt?
This function gives you the list of contents in a module
What is dir()
The code for accessing the working directory
What is os.getcwd()
This function returns all the keys in a dictionary as a list
The following code outputs:
def printPattern(n):
for i in range(n, 0, -1):
print(((n-i) * ' ') + (i * '*'))
What is an inverted star pattern
****
***
**
*
I want to write a piece of code that will simulate a random number generator. I need to import this module.
What is random?
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)
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)
What is:
def function_name(parameters):
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
Count the number of lines in a file using a generator
def reader(file_name):
for row in open(file_name, "r"):
yield row
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)
This function has the main logic and calls other functions
What is main()?
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?
This program prints line 4 in a file.
with open("test.txt", "r") as fp:
lines = fp.readlines()
print(lines[3])