Python Exam 3 Review: Data Structures, Files, Exceptions, and Classes

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/65

flashcard set

Earn XP

Description and Tags

Vocabulary flashcards covering core topics for Python Exam 3: collections (lists, strings, tuples, sets, dictionaries), file input/output, exceptions and common error types, and object-oriented programming concepts (classes, instances, methods, and operator overloading).

66 Terms

1
New cards

Python Indexing

Sequence indexing in Python always starts at position 00.

2
New cards

List

A built-in data type that is ordered, mutable, allows duplicate values, and is accessed by index (e.g., [1, 2, 3], indexed via a[0]).

3
New cards

String

A built-in data type that is ordered, not mutable (immutable), allows duplicates, and is accessed by index (e.g., "abc", indexed via s[0]).

4
New cards

Tuple

A built-in sequence that is ordered, not mutable (cannot be changed after creation), allows duplicates, and is indexed like a list (e.g., (1, 2, 3)).

5
New cards

Set

A collection that is unordered, mutable, contains no duplicates, and does not support indexing (elements are accessed using in or a loop; e.g., {1, 2, 3}).

6
New cards

Dictionary (dict)

A collection that keeps inserted order, is mutable, requires unique keys while values can repeat, and accesses values by key (e.g., d["a"]).

7
New cards

Empty Dictionary Syntax

{} creates an empty dictionary.

8
New cards

Empty Set Syntax

set() creates an empty set (since {} is reserved for an empty dictionary).

9
New cards

One-Item Tuple Syntax

A single-item tuple must include a trailing comma (e.g., (1,)); without the comma, Python treats it as just an integer.

10
New cards

List .append()

A list method that adds an item to the end of the list.

11
New cards

List .insert()

A list method that adds an element at the specified index position.

12
New cards

List .remove()

A list method that removes the first instance of the given value.

13
New cards

List .pop()

A list method that removes an item from a given index, defaulting to removing the last element if no index is passed.

14
New cards

List .index()

A list method that returns the first index position of the given value.

15
New cards

List .count()

A list method that returns how many times a given value appears in the list.

16
New cards

Slicing

Sequence slicing syntax [start:stop:stride], which is exclusive of the stop index.

17
New cards

String Immutability Rule

Strings cannot be modified in place; every string method returns a brand new string.

18
New cards

String .strip()

A string method that removes whitespace or newline characters from the beginning and end of a string.

19
New cards

String .split()

A string method that separates one string into multiple substrings at the given delimiter instance.

20
New cards

String .find()

A string method that returns the index position of the given substring, or returns −1-1 if it is not found.

21
New cards

Tuple Unpacking

Assigning elements of a tuple directly into variables (e.g., a, b = (1, 2) results in a = 1 and b = 2).

22
New cards

Tuple Methods

Tuples only possess two methods: count() and index().

23
New cards

Set .add()

Adds an item to a set; causes no change and produces no error if the value is already present in the set.

24
New cards

Set .remove()

Removes an item from a set, but raises a KeyError if the value is missing from the set.

25
New cards

Set .discard()

Removes an item from a set, with no change or error if the value is missing.

26
New cards

Set .pop()

Removes and returns an arbitrary item from the set, taking no arguments due to sets being unordered.

27
New cards

Set .clear()

Removes all elements, clearing the entire set.

28
New cards

Dictionary Key Access (d["key"])

Returns the value associated with the specified key, but raises a KeyError if the key is missing.

29
New cards

Dictionary .get()

Returns the value of the given key without raising an error if the key is missing.

30
New cards

del d["key"]

Deletes the specified key and its associated value pair from the dictionary.

31
New cards

Dictionary .keys()

A dictionary method that returns a view showing all the keys in the dictionary.

32
New cards

Dictionary .values()

A dictionary method that returns a view showing all the values in the dictionary.

33
New cards

Dictionary .items()

A dictionary method that returns a view showing all keys along with their corresponding value pairs.

34
New cards

File Mode "r"

Read mode (the default mode); opens a file and starts at the beginning, throwing a FileNotFoundError if the file does not exist.

35
New cards

File Mode "w"

Write mode; opens and erases all contents of the file, or creates a new empty file if it does not already exist.

36
New cards

File Processing Pattern

The standard sequence of file handling operations: open, use, close.

37
New cards

F.read()

A file method that reads the entire file content and returns it as a single string.

38
New cards

F.readline()

A file method that reads and returns the first or next single line of the file as a string.

39
New cards

F.readlines()

A file method that reads and returns all lines individually as a list, each ending in a newline.

40
New cards

F.close()

Saves file data to disk and releases the system file resource.

41
New cards

try Block

The exception handling block that always runs first, executing statements sequentially up to the first error.

42
New cards

except Block

The block that runs only if a matching error occurs in the try block; Python checks except blocks top to bottom and only runs the first match.

43
New cards

else Block (Exceptions)

The block in an exception construct that runs only if the try block finishes completely with no errors.

44
New cards

ValueError

An error raised when a function receives an argument that has the right data type but an inappropriate or invalid value (e.g., int("abc")).

45
New cards

TypeError

An error raised when an operation is applied to an object of the wrong type (e.g., "5" + 3, or attempting to index an unordered set or mutate an immutable string/tuple).

46
New cards

ZeroDivisionError

An error raised when attempting to divide or compute modulo (%) by 00.

47
New cards

IndexError

An error raised when attempting to access an index past the end of a list, string, or tuple (e.g., [1, 2, 3][5]).

48
New cards

KeyError

An error raised when attempting to access a non-existent dictionary key or removing a non-existent element with set.remove().

49
New cards

FileNotFoundError

An error raised when attempting to open a missing or non-existent file in "r" mode.

50
New cards

SyntaxError

An error raised when the code violates Python's formal grammar rules (e.g., a missing colon).

51
New cards

EOFError

An error raised when there is no more data to read from an input stream (end of input).

52
New cards

NameError

An error raised when a variable or function name is referenced without being previously defined.

53
New cards

Class

A blueprint for creating objects (e.g., class Dog:).

54
New cards

Instance (Object)

A concrete entity built from a class blueprint that receives its own copy of the class attributes (e.g., d = Dog("Rex")).

55
New cards

Attribute

A variable that belongs specifically to an instance of a class (e.g., self.name).

56
New cards

Method

A function defined inside a class whose first parameter must be self (e.g., def bark(self):).

57
New cards

Constructor (__init__)

A special method that automatically runs the moment an instance is created, used to set the initial values of attributes.

58
New cards

self

A parameter referring to the specific instance of the class that is calling the method, set fresh each time the method is invoked.

59
New cards

Operator Overloading

Defining custom behavior for standard Python operators (such as +, -, or ==) within a user-defined class.

60
New cards

__add__

The special method invoked when evaluating the a + b operator on class instances.

61
New cards

__sub__

The special method invoked when evaluating the a - b operator on class instances.

62
New cards

__mul__

The special method invoked when evaluating the a * b operator on class instances.

63
New cards

__eq__

The special method invoked when evaluating the equality operator a == b.

64
New cards

__lt__ and __gt__

Special methods invoked when evaluating comparison operators a < b and a > b, respectively.

65
New cards

__str__

The special method invoked by calling print(a) or str(a) to produce a readable string representation of an object.

66
New cards

__len__

The special method invoked by calling len(a) on an instance of a class.