Exam 3
code for printing each element in a matrix
for row in range(len(matrix)): for col in range(len(matrix[row])): print(matrix[row][col], end=" ") print() or for row in matrix: for value in row: print(value, end=" ") print()
if you have a matrix, distances, then print(len(distances[0]))
gives the number of columns in the matrix (it's giving you the length of the first row technically, but the length of the first row in a matrix IS the number of columns)
if you have a matrix, distances, then print(len(distances))
gives the number of rows in the matrix
To place a button in a specified row and column in its parent container, use ________.
grid manager
abstraction
hiding details you don't need to know at the moment ex: circle1.draw( ) will draw a circle... doesn't matter how it gets done, it just does. Details are abstracted away.
padx and pady
how much padding you want on either side in pixels
Q14.3
idk
decorator
idk
row.sort()
idk
Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 > d2 is _______. A. True B. False C. illegal
illegal
Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, s1 + s2 is ____________?
illegal
import *
imports everything
To read the entire remaining contents of the file as a string from a file object infile, use _________.
infile.read()
To read two characters from a file object infile, use _________.
infile.read(2)
To read the next line of the file from a file object infile, use _________.
infile.readline()
To read the remaining lines of the file from a file object infile, use _________.
infile.readlines()
how to use grid command
label.grid(row= 0, column= 0)
how do you put things one under another in the window?
label.pack( )
how to create a label
label= Label(window,....)
Suppose s = {1, 2, 4, 3}, _______ returns 4. A. sum(s) B. len(s) C. min(s) D. max(s) E. None
len(s) or max(s)
object identity
like a person's social security number; Python automatically assigns each object a unique ID
how to add a list to a matrix
matrix.append([ ])
tool kit interface (tkinter)
module used to build GUI
window.mainloop()
need it to leave window open, similar to turtle.done( )
range(0,3)
not inclusive means 0-2
objects and classes
objects of the same kind are defined by using a common class ex: you can make as many apple pies (objects) as you want from a single recipe (class)
check to see if a set contains something
print("red" in set1) will give you T or F
object (aka instance)
represents an entity in the real world that can be distinctly identified (ex: a student, a desk, a circle, a button, etc)
Using a grid manager, you can use the option _________ to place a component in multiple rows and columns.
rowspan and columnspan
difference
s1.difference(s2) or s1 - s2 a set that contains the elements in set1 but not in set2
intersection
s1.intersection(s2) or s1 & s2 a set that contains the only the elements that appear in both sets
Suppose s1 = {1, 2, 4, 3} and s2 = {0, 1, 5, 3, 4, 2, 13}, ________ is true. A. s1.issubset(s2) B. s1.issuperset(s2) C. s2.issubset(s1) D. s2.issuperset(s1)
s1.issubset(s2) s2.issuperset(s1)
subset
s1.issubset(s2) or s1 < s2 s1 is a subset of s2 if every element in s1 is also in s2
superset
s1.issuperset(s2) or s1 > s2 s1 is a superset of set s2 if every element in s2 is also in s1
symmetric_difference aka exclusive or
s1.symmetric_difference(s2) or s1 ^ s2 a set that contains the elements in either set, but not in both sets
union
s1.union(s2) or s1 | s2 the set that contains all of the elements from both sets
You can use ___________ to create an empty set.
set( ) note: since dictionaries also use { }, use set( ) to differentiate an empty one
add something to a set
set1.add("purple")
remove something from a set
set1.remove("yellow")
why make a variable private?
so you can control how the variable is manipulated
data structure
store data and info ex: lists, tuples, set, dictionaries
inheritance
subclass is able to inherit features from its superclass with additional new features -have to initialize with super( ).__init__(stuff in parent class)
if you have the same attribute in the parent class and the specific class, what happens?
the attribute in the specific class will override the parent class
Suppose t = (1, 2), 2 * t is _________.
(1,2,1,2)
Suppose t = (1, 2, 4, 3), t[1 : -1] is _________.
(2,4)
constructor
-creates an object in the memory for the class -invokes the class's __init__ method to initialize the object
dictionary
-data structure -stores a key value pair, a key goes to a value and you use the key to look up the value -can't have duplicates in the keys but can in the values
set
-data structure -not ordered (means you can't index them) -can include mixed data types -mutable -don't allow duplicates, can't add something that is already there -much faster than lists... Usually use sets to check to see if something is in the set. If you don't need things sorted and you don't have duplicates, use a set.
recursive functions
-must have a base case or stopping condition -every call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case -infinite recursion can occur if recursion doesn't reduce the problem in a manner that allows it to eventually converge into the base case -usually take more space than non-recursive functions -sometimes enables you to give a simple solution to a program that would otherwise be difficult to solve
The 4 main principles of OOP
1) abstraction 2) encapsulation 3) inheritance 4) polymorphism
Assume m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] what are len(m)?
3
Assume m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what are len(m[0])?
3
print(7%4)
3 modulo gives you remainder of long division
Assume x = ((1, 2), (3, 4, 5), (5, 6, 5, 9)), what are len(x) are len(x[0])?
3 and 2
What will be displayed by the following program? values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for row in range(0, len(values)): for column in range(0, len(values[row])): if v < values[row][column]: v = values[row][column]
33
Which of the following statements are true? A. A Python list is immutable if every element in the list is immutable. B. A Python set is immutable if every element in the set is immutable. C. A Python tuple is immutable if every element in the tuple is immutable. D. A Python tuple is immutable.
A python tuple is immutable if every element in the tuple is immutable
To create a label under parent window, use _______. A. label = Label(text = "Welcome to Python") B. label = Label(window, text = "Welcome to Python") C. label = Label(text = "Welcome to Python", fg = " red") D. label = Label(text = "Welcome to Python", fg = " red", bg = "white")
B. label = Label(window, text = "Welcome to Python")
To create a button under parent window with command processButton, use _______. A. Button(text = "OK", fg = "red", command = processButton) B. Button(window, text = "OK", fg = "red") C. Button(window, text = "OK", fg = "red") D. Button(window, text = "OK", command = processButton)
D. Button(window, text = "OK", command = processButton)
Which of the following is a Python dictionary? A. [1, 2, 3] B. (1, 2, 3) C. {1, 2, 3} D. {}
D. {}
tuple
Data type in python similar to a list, but immutable (once they are made, you can't change them). Can mix data types.
widgets
Everything in tkinter is under a class called "widget". When you use widgets, you have to use a parent component.
Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 == d2 is _______. A. True B. False C. illegal
False
Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d.get("susan")?
It is executed fine and no exception is raised, and it returns None
Suppose t = (1, 2, 4, 3, 8, 9), [t[i] for i in range(0, len(t), 2)] is _________.
[1,4,8]
The readlines() method returns a ____________.
a list of lines
initializer
a method that is invoked to initialize a new object's state when it is created
overriding methods
a method that says exactly what you want to print
When you open a file for writing, if the file does not exist, what happens?
a new file is created
object-oriented programming (OOP)
a style of programming that involves representing items, things, and people as objects rather than basing the logic around actions
object behavior
Python uses methods to define an object's behavior. Ex for circle: getArea( )
object state (aka properties or attributes)
Represented by variables called data fields. A circle object, for example, has a data field radius, which is an attribute that characterizes a circle.
Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d["susan"]?
Since "susan" is not a key in the set, Python raises a KeyError exception
Suppose s = {1, 2, 4, 3}, what happens when invoking s.remove(12)?
Since 12 is not in the set, Python raises a KeyError exception
To open a file c:\scores.txt for appending data, use ________
To open a file c:\scores.txt for appending data, use ________ outfile = open("c:\\scores.txt", "a")
To open a file c:\scores.txt for reading, use __________. A. infile = open("c:\scores.txt", "r") B. infile = open("c:\\scores.txt", "r") C. infile = open(file = "c:\scores.txt", "r") D. infile = open(file = "c:\\scores.txt", "r")
To open a file c:\scores.txt for reading, use __________. infile = open("c:\\scores.txt", "r")
To open a file c:\scores.txt for writing, use __________. A. outfile = open("c:\scores.txt", "w") B. outfile = open("c:\\scores.txt", "w") C. outfile = open(file = "c:\scores.txt", "w") D. outfile = open(file = "c:\\scores.txt", "w")
To open a file c:\scores.txt for writing, use __________. outfile = open("c:\\scores.txt", "w")
Suppose d = {"john":40, "peter":45}, "john" in d is __________ A. True B. False
True
When you open a file for reading, if the file does not exist, what happens?
an error occurs
polymorphism
an object of a subclass can be used wherever its superclass object is used ex: can pass in different shapes at same time as long as within super class of shapes
describe the fibonacci sequence
begins with 0 and 1 and each subsequent number is the sum of the preceding 2 numbers in the series
label.bind
binds a method/function to a button
Suppose s = {1, 2, 4, 3}, which of the following is incorrect? A. print(s[3]) B. s[3] = 45 C. print(max(s)) D. print(len(s)) E. print(sum(s))
can't index a set print(s[3]) s[3] = 45
To bind a canvas with a left mouse click event p, use __________
canvas.bind("<Button-1>", p)
To bind a canvas with a right mouse click event p, use __________
canvas.bind("<Button-3>", p)
encapsulation
controlling access to the attributes of an object, helps avoid errors in your code -can make an object private by adding a dunder -making a class in encapsulation in itself
When you open a file for writing, if the file exists, how is the file written?
the existing file is overwritten with the new file
how do you make a blank tuple?
tuple = ( )
Which of the following statement is false? A. Lists are mutable B. Tuples are mutable C. Sets are mutable D. Strings are mutable
tuples and strings are INmutable
class
uses variables to store data fields and defines methods to perform actions
button= Button(window, command= self.process_ok)
when button is clicked, goes in to self.process_ok method
How do you create a window?
window = Tk()
how do you set the size of the window?
window.geometry("300x300")
How do you create an event loop?
window.mainloop()
how to create a title in a window
window.title("insert title")
Graphical User Interface (GUI)
windows that you use to do everything on your computer, always done in object-oriented way
Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 - s2?
{2,3} subtract from set1 what is also in set2
