foo = 1
bar = 0
for i in range(5):
print(foo)
bar = foo
foo = bar
1
1
1
1
1
What line of code opens a file with read only privileges?
s = 'i am code'
for i in s;
print(i)
print(s)
for i in s:
: instead of ;
key and value pair
What does a dictionary store?
A library with multi-dimensional arrays that supports linear algebra formalism
What is numpy?
d = {'a':1, 'b':2, 'c':3, 'd': 4}
d['e'] = 5
print(d.keys())
[a,b,c,d,e]
The ability to change an object from one type to another type
What is Polymorphism?
x = 0
while i < 10:
print(x)
i is not defined
d = {'a': ['b','c'], 'b':['a','c'], 'c':[3,4]}
output: a
print(d['b'][0])
import Matplotlib.pyplot as plt
What line of code allows us to use the Matplotlib library
d = {'a':1, 'b':2, 'c':3, 'd': 4}
d['a'] = 10
d['e'] = 11
for i in d.keys():
print(d[i])
10
2
3
4
11
What line of code opens a file with appending privilages?
define fun(var1):
print(F"The variable is {var1}")
def not define
d = {'a': ['b','c'], 'b':['a','c'], 'c':[3,4]}
d['d'] = [5, 6]
d['e'] = ['5','6']
output: [5,6]
print(d['d'])
file = 'input.txt'
try:input_file = open(file, "r")
print("first line of the file: " + input_file.readline())
except IOError:
print("File Open Error")
What is an example of Exception Handling?
class Bird(object):
def __init__(self, w):
self.__weight = w
def getWeight(self):
return str(self.__weight) + 'ounces'
def getColor(self):
raise NotImplementedError{'Method color not implemented'}
b1 = Bird(5.0)
b1.getColor()
b1.getWeight()
Method color not implemented
5.0
The blueprint vs the completed version based on the blueprint
What is the difference between Class and Instance
for i in range(num):
print(i)
print("done")
print(i) should be indented
d = {'a':1, 'b':2, 'c':3}
output: 2
print(d['b'])
import random as r
print(r.randint(0, 100))
How do you write a random number generator using the random library?
s = 'I am a CS major'
d = {}
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
print(d)
{'a': 3, 'I': 1, ' ': 4, 'm': 2, 'C': 1, 'S': 1, 'j': 1, 'o': 1, 'r': 1}
How do you make a member of a class private?
include random
l = ['blue', 'red', 'yellow']
print(random.choice(l))
include should be import
d = {'a':1, 'b':2, 'c':3}
Add 'd' : 4
d['d'] = 4
strings, lists, tuples, and numerical values
Which of the following types can be used as a key in Python dictionaries?