Python Midterm Prep

0.0(0)
studied byStudied by 6 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/112

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

113 Terms

1
New cards

Text file

contains data that has been encoded as text

2
New cards

Binary file

contains data that has not been converted to text

3
New cards

Sequential access

file read sequentially from beginning to end, can’t skip ahead

4
New cards

Direct access

can jump directly to any piece of data in the file

5
New cards

what are the two general types of files

Text File

Binary file

6
New cards

what are the two ways to access data stored in file ?

sequential access

direct access

7
New cards

open function

used to open a file ,

creates a file object and associates it with a file on the disk

8
New cards

General format of the open function

file_object = open(filename, mode)

9
New cards

Mode

string specifying how the file will be opened

reading (‘r’), writing (‘w’), appending (‘a’)

10
New cards

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’

11
New cards

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’

12
New cards

If a file with the same name exists in the current directory, it will be overwritten

true

13
New cards

If open function receives a filename that does not contain a path, it assumes that file is in same directory as program

True

14
New cards

Method

a function that belongs to an object

performs operations using that object

15
New cards

What is an example of a method?

name = input(“what is your name? “)

print(name.capitalize())

16
New cards

how would you write data to a file ?

file_variable.write(string)

17
New cards

How would you close a file ?

file_variable.close()

18
New cards

what does the rstrip method do ?

Strips specific characters from end of the string

19
New cards

Is it necessary to concatenate a “\n’ to data before writing it ?

Yes and you do so by adding a + to it.

20
New cards

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

21
New cards

What is the format of the read method ?

file_variable.read()

22
New cards

readline method

file object method that reads a line from the

line returned as a strong, including ‘\n’

23
New cards

Read Position

marks the location of the next item to be read from a file

24
New cards

when open file with ‘w’ mode, if the file exists it is overwritten

true

25
New cards

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

26
New cards

Numbers must be converted to strings before they are written to a file

True

27
New cards

str function

converts value to string

28
New cards

How do you convert a string to a numeric value ?

use int and float functions

29
New cards

the readline method uses an empty string as a sentinel when the end of file is reached

True

30
New cards

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

31
New cards

To retrieve a specific value, use the key associated with it

true

32
New cards

Example of a dictionary

phonebook = {‘Chris’: ‘555-’1111’, ‘Katie’ : ‘555-2222’}

33
New cards

How to retrieve a value from a dictionary

dictionary[key]

If key in the dictionary, associated value is returned, otherwise,

KeyError exception is raised

34
New cards

How do you test whether a key is in a dictionary?

Use the in and not in operators

35
New cards

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

36
New cards

how do you delete a key-value pair":

del dictionary[key]

  • del phonebook[‘Chris’]

37
New cards

what is purpose of the len function ?

used to obtain number of elements in a dictionary

38
New cards

Can values be stored in a single dictionary as different types ?

Yes

39
New cards

to create an empty dictionary:

Use { }

Use built-in function dict ()

40
New cards

what do items methods do ?

returns all the dictionaries keys and associated values

41
New cards

What does the clear method do?

deletes all the elements in a dictionary, leaving it empty,

Format: dictionary.clear()

42
New cards

What does the get method do?

gets a value associated with specified key from the dictionary

dictionary.get(key, default)

43
New cards

What does the key method do ?

Returns all the dictionaries keys as a sequence

dictonary.keys()

44
New cards

What does the pop method ?

returns value associated with specified key and removes that key-value pair from the dictionary

dictionary.pop(key, default)

45
New cards

what does the value method do ?

returns all the dictionary values as a sequence

dictionary.values()

46
New cards

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

47
New cards

what does the set function do ?

used to create a set

48
New cards

what does the add method do ?

adds an element to a set

49
New cards

what does an update method do ?

adds a group of elements to a set

50
New cards

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

51
New cards

What does the clear method do ?

clears all the elements of the set

52
New cards

intersection of two sets

a set that contains only the elements found in both sets

53
New cards

difference of two sets

a set that contains the elements that appear in the first set do no not appear in the second set

54
New cards

what does it mean to serialize an Object ?

convert the object to a stream of bytes that can easily be stored in a file

55
New cards

what does pickling mean ?

serializing an object

56
New cards

Chapter 10 Classes and OOP

Chapter 10

57
New cards

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

58
New cards

What is the Focus of Procedural Programming?

to create procedures that operate on the program’s data

59
New cards

Object-oriented programming

focused on creating objects

Data is known as data attributes

Procedures are known as methods

60
New cards

what is an object?

an entity that contains data and procedures

61
New cards

What is encapsulation?

A self contained unit combining data and code into a single object

62
New cards

An object contains data attributes and methods

True

63
New cards

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

64
New cards

What is object Reusability ?

The same object can be used in different programs

65
New cards

What does outside code interact with in OOP ?

the object methods

66
New cards

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

67
New cards

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.

68
New cards

What is the definition of a class ?

set of statements that define a class’s methods and data attributes

69
New cards

What does the self parameter method do?

references the specific object that the method is working on

- required in every method in the class

70
New cards

What is the Object’s state ?

the values of the object’s attribute at a given moment

71
New cards

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

72
New cards

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

73
New cards

What does the mutator method do ?

Stores or changes the value of a data attribute

74
New cards

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

75
New cards

What happens if many instances of a class are created?

Each would have its own set of attributes

76
New cards

What do methods and functions need ?

to accept objects and arguments

77
New cards

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.

78
New cards

what is the UML diagram ?

the standard diagrams for graphically depicting object-oriented systems

79
New cards

Chapter 11

Chapter 11 Inheritance

80
New cards

What do we need inheritance in coding?

In the real world, many objects are a specialized version of more general objects

81
New cards

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

82
New cards

What is inheritance ?

used to create an “is a” relationship between classes

83
New cards

What is a superclass (base class) ?

A general class

84
New cards

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

85
New cards

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

86
New cards

What is polymorphism ?

It is an objects ability to take different forms

87
New cards

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

88
New cards

AttributeError exception:

raised when a method receives an object which is not an instance of the right class

89
New cards

isinstance function:

determines whether object is an instance of a class

  • isinstance(object, class)

90
New cards

Chapter 13

Chapter 13 GUI programming

91
New cards

What is the User interface ?

the part of the computer with which the user interacts

92
New cards

What is the Command line interface ?

displays a prompt and the user types a command that is then executed

93
New cards

Graphical User interface (GUI) ?

allows users to interact with a program through graphical elements on the screen

94
New cards

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.

95
New cards

tkinter module purpose ?

allows you to create simple GUI programs

96
New cards

Widget

graphical element that the user can interact with or view

presented by a GUI program

97
New cards

What builds the GUI ?

the __init__

98
New cards

Label Widget

displays a single line of text in a window

made by creating an instance of tkinter module’s Label class

99
New cards

pack method

determines where a widget should be positioned and makes it visible when the main window is displayed

100
New cards

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