1/112
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Text file
contains data that has been encoded as text
Binary file
contains data that has not been converted to text
Sequential access
file read sequentially from beginning to end, can’t skip ahead
Direct access
can jump directly to any piece of data in the file
what are the two general types of files
Text File
Binary file
what are the two ways to access data stored in file ?
sequential access
direct access
open function
used to open a file ,
creates a file object and associates it with a file on the disk
General format of the open function
file_object = open(filename, mode)
Mode
string specifying how the file will be opened
reading (‘r’), writing (‘w’), appending (‘a’)
what does this do ? customer_file = open('customers.csv', 'r’)
opens the file customers.csv and it is stored in the object variable ‘customer_file’
what does this do ? sales_file = open('sales.txt', 'w’)
creates a file named sale.txt and can write data to it through the object variable ‘sales_file’
If a file with the same name exists in the current directory, it will be overwritten
true
If open function receives a filename that does not contain a path, it assumes that file is in same directory as program
True
Method
a function that belongs to an object
performs operations using that object
What is an example of a method?
name = input(“what is your name? “)
print(name.capitalize())
how would you write data to a file ?
file_variable.write(string)
How would you close a file ?
file_variable.close()
what does the rstrip method do ?
Strips specific characters from end of the string
Is it necessary to concatenate a “\n’ to data before writing it ?
Yes and you do so by adding a + to it.
read method
file object method that reads entire file contents into memory
only works if file has been opened for reading
contents returned as a string
What is the format of the read method ?
file_variable.read()
readline method
file object method that reads a line from the
line returned as a strong, including ‘\n’
Read Position
marks the location of the next item to be read from a file
when open file with ‘w’ mode, if the file exists it is overwritten
true
Appending attributes
If file exists, it is not erased, and if it does not exists it is created
Data is written to the file at the end of the current contents
Numbers must be converted to strings before they are written to a file
True
str function
converts value to string
How do you convert a string to a numeric value ?
use int and float functions
the readline method uses an empty string as a sentinel when the end of file is reached
True
Dictionary
object that stores a collection of data
each element consists of a key and a value
-often referred to as mapping of key to value
- key must be an immutable object
To retrieve a specific value, use the key associated with it
true
Example of a dictionary
phonebook = {‘Chris’: ‘555-’1111’, ‘Katie’ : ‘555-2222’}
How to retrieve a value from a dictionary
dictionary[key]
If key in the dictionary, associated value is returned, otherwise,
KeyError exception is raised
How do you test whether a key is in a dictionary?
Use the in and not in operators
how do you add a key value to a dictionary?
dictionary[key] = value
if key exists in the dictionary, the value associated with it will be changed
how do you delete a key-value pair":
del dictionary[key]
del phonebook[‘Chris’]
what is purpose of the len function ?
used to obtain number of elements in a dictionary
Can values be stored in a single dictionary as different types ?
Yes
to create an empty dictionary:
Use { }
Use built-in function dict ()
what do items methods do ?
returns all the dictionaries keys and associated values
What does the clear method do?
deletes all the elements in a dictionary, leaving it empty,
Format: dictionary.clear()
What does the get method do?
gets a value associated with specified key from the dictionary
dictionary.get(key, default)
What does the key method do ?
Returns all the dictionaries keys as a sequence
dictonary.keys()
What does the pop method ?
returns value associated with specified key and removes that key-value pair from the dictionary
dictionary.pop(key, default)
what does the value method do ?
returns all the dictionary values as a sequence
dictionary.values()
what is a set in dictionary ?
an object that stores a collection of data in same way as mathematical set
all items must be unique
set is unordered
Elements can be of different data types
what does the set function do ?
used to create a set
what does the add method do ?
adds an element to a set
what does an update method do ?
adds a group of elements to a set
What do remove and discard methods do ?
remove the specified item from the set
the item that should be removed is passed to both methods as an argument
behave differently when the specified item is not found in the set
What does the clear method do ?
clears all the elements of the set
intersection of two sets
a set that contains only the elements found in both sets
difference of two sets
a set that contains the elements that appear in the first set do no not appear in the second set
what does it mean to serialize an Object ?
convert the object to a stream of bytes that can easily be stored in a file
what does pickling mean ?
serializing an object
Chapter 10 Classes and OOP
Chapter 10
Procedural Programming
Writing programs made of functions that perform specific tasks
Procedures typically operate on data items that are separate from the procedures,
Data items commonly passed from one procedure to another
What is the Focus of Procedural Programming?
to create procedures that operate on the program’s data
Object-oriented programming
focused on creating objects
Data is known as data attributes
Procedures are known as methods
what is an object?
an entity that contains data and procedures
What is encapsulation?
A self contained unit combining data and code into a single object
An object contains data attributes and methods
True
What is Data Hiding (making Private) ?
Objects data attributes are hidden from code outside of the object
Access restricted to the object’s methods
protects from accidental corruption
outside code does not need to know internal structure of the object
What is object Reusability ?
The same object can be used in different programs
What does outside code interact with in OOP ?
the object methods
What is a purpose of a class ?
Code that specifies the data attributes and methods of a particular type of object
similar to a blueprint of a house or a cookie cutter
What is an instance ?
an object created from a class
similar to a specific house built according to the blueprint
there can by many instances of one class.
What is the definition of a class ?
set of statements that define a class’s methods and data attributes
What does the self parameter method do?
references the specific object that the method is working on
- required in every method in the class
What is the Object’s state ?
the values of the object’s attribute at a given moment
what is the __str_ method?
displays the objects state
automatically called when the object is passed as an argument to the print function
Automatically called when the object is passed as an argument to the str function
What does the accessor method do ?
Returns a value from a class’s attribute without changing it
safe way for code outside the class to retrieve the value of attributes
What does the mutator method do ?
Stores or changes the value of a data attribute
What is a Instance attribute?
belongs to a specific instance of a class
created when a method uses the self parameter to create an attribute
What happens if many instances of a class are created?
Each would have its own set of attributes
What do methods and functions need ?
to accept objects and arguments
What are you passing when you pass an object as an argument ?
You are actually passing a reference to the object
therefore the receiving method or function has access to the actual object.
what is the UML diagram ?
the standard diagrams for graphically depicting object-oriented systems
Chapter 11
Chapter 11 Inheritance
What do we need inheritance in coding?
In the real world, many objects are a specialized version of more general objects
What is the “is a” relationship ?
exists when one object is a specialized version of another object
Daisy is a flower
Rectangle is a shape
What is inheritance ?
used to create an “is a” relationship between classes
What is a superclass (base class) ?
A general class
What is a subclass (derived class) ?
A specialized class
an extended version of the superclass
inherits attributes and methods of the superclass
New attributes and methods can be added
How do you indicate inheritance in code ?
class Car(Automobile):
the initializer method of a subclass calls the initializer method of the superclass and then initializes the unique data attributes
Add method definitions for unique methods
What is polymorphism ?
It is an objects ability to take different forms
What are essential ingredients in polymorphic behavior ?
Ability to define a method in a superclass and override it in a subclass
Ability to call the correct version of overridden method depending on the type of object that called for it
AttributeError exception:
raised when a method receives an object which is not an instance of the right class
isinstance function:
determines whether object is an instance of a class
isinstance(object, class)
Chapter 13
Chapter 13 GUI programming
What is the User interface ?
the part of the computer with which the user interacts
What is the Command line interface ?
displays a prompt and the user types a command that is then executed
Graphical User interface (GUI) ?
allows users to interact with a program through graphical elements on the screen
What is the difference between text-based and GUI environments ?
text-based has programs determining the order in which things happen, the GUI is event-driven.
tkinter module purpose ?
allows you to create simple GUI programs
Widget
graphical element that the user can interact with or view
presented by a GUI program
What builds the GUI ?
the __init__
Label Widget
displays a single line of text in a window
made by creating an instance of tkinter module’s Label class
pack method
determines where a widget should be positioned and makes it visible when the main window is displayed
Frame Widget
container that holds others widgets
useful for organizing and arranging groups of widgets in a window
The contained widgets are added to the frame widget which contains them