Podcast
Questions and Answers
The index of the first item in a list is 1.
The index of the first item in a list is 1.
False (B)
A module behaves like a function, but has a slightly different syntax.
A module behaves like a function, but has a slightly different syntax.
False (B)
In a dictionary, a -> separates a key and its value.
In a dictionary, a -> separates a key and its value.
False (B)
While the median of a list of values occurs most frequently, it is not always a unique value.
While the median of a list of values occurs most frequently, it is not always a unique value.
All data output to or input from a text file must be strings.
All data output to or input from a text file must be strings.
To prevent aliasing, a new object can be created and the contents of the original can be copied to it.
To prevent aliasing, a new object can be created and the contents of the original can be copied to it.
Many applications now use data mangling to protect information transmitted on networks.
Many applications now use data mangling to protect information transmitted on networks.
The string method isalphabetic returns True if the string contains only letters or False otherwise.
The string method isalphabetic returns True if the string contains only letters or False otherwise.
In Python, an index is used to specify the position of an element within a list.
In Python, an index is used to specify the position of an element within a list.
The remove file system function is included in the os.path module.
The remove file system function is included in the os.path module.
A Caesar cipher uses a plaintext character to compute one encrypted character.
A Caesar cipher uses a plaintext character to compute one encrypted character.
A Python dictionary's elements are referenced by their indexes.
A Python dictionary's elements are referenced by their indexes.
Strings in Python are mutable data structures.
Strings in Python are mutable data structures.
Using a tuple is more efficient when the data structure will not change.
Using a tuple is more efficient when the data structure will not change.
The list method ascending mutates a list by arranging its elements in ascending order.
The list method ascending mutates a list by arranging its elements in ascending order.
It is more convenient to define a function in a Python shell than in an IDLE window.
It is more convenient to define a function in a Python shell than in an IDLE window.
Flashcards are hidden until you start studying
Study Notes
Functions and Methods
- Main function in scripts often does not require arguments and typically returns no value.
- Functions and methods can both return values, they differ primarily in their association with objects.
- Methods are invoked on objects, while functions exist independently.
- Python uses the
if __name__ == "__main__":
block to determine script entry points, enabling scripts to run without a dedicated init function.
Data Structures
- Lists in Python are indexed starting from 0; the first item is at index 0, not 1.
- The median refers to the middle value in a sorted list, not the most frequently occurring value.
- A dictionary uses colons (
:
) to separate keys from values, not arrows (->
). - The index of the last item in a list corresponds to
length - 1
, not the total length.
File Handling
- When opening a file in write mode (
'w'
), if the file does not exist, Python does not raise an error; it creates the file. - Files can be read and written in string format only; other datatypes must be converted to strings.
List Operations
- Elements in lists can be replaced using their index, which refers to their position, not the list itself.
- Each item in a list has a unique index.
- Lists support mutation, whereas tuples do not; this affects their performance and safety.
Tuples vs. Lists
- Tuples are immutable and therefore can use less memory, offering faster performance for fixed-size data.
- Lists are mutable, allowing modification of elements but can lead to accidental changes.
- Use tuples for data that won't change, like constants or coordinates; use lists for mutable data, like to-do lists.
Error Handling
- Using
pop
with a non-existent key in a dictionary raises an error. - Python raises an error if a file opened for reading (
'r'
) does not exist.
Miscellaneous
- Data structures should be chosen for specific use cases, considering mutability and safety.
- The string method that checks if characters are alphabetic does not return True for non-alphabetic characters.
- The method to sort lists in ascending order is
sort()
, which directly modifies the original list.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.