CS worbook 2 Flashcards
.sort() vs sorted() difference
(ig) .sort() modifies the original list in place.
sorted() returns a new list and leaves the original unchanged.
What uses .pop, what’s the default parameter do(.pop())
(ig) Parameter Values
Parameter Description
pos Optional. A number specifying the position of the element you want to remove, default value is -1, which returns the last item
Can/explain if the index out bounds, and how do you know when the index is out of bounds, what happens with insert() and pop() method?
(ig) out of bounds: if index < 0 or index >= len(list)
we will get an IndexError for both methods.
What method removes the first occurrence of the element with the specified value. What uses this?
Parameter Description
elmnt Required. Any type (string, number, list etc.) The element you want to remove
explain the remove() method? list.remove(elmnt). Explain the parameter?
using what function to load the name.csv file as a list of lists and store the data in the variable by creating a variable like colors(x) =
process_csv(‘csv_name’)
colors = process_csv(‘colors.csv’)
print(colors)
[[‘\ufeffName’, ‘HEX’, ‘RGB’], [‘Alice Blue’, ‘#F0F8FF’, ‘rgb(239,247,255)’], [‘Antique White’, ‘#FAEBD7’, ‘rgb(249,234,214)’],
Elaborate the csv files types, optional to give more? What is it?
(ig) Don’t have types for their values—everything is a string. It’s just a text file of comma-separated values.
Don’t have settings for font size or color
Don’t have multiple worksheets
Can’t specify cell widths and heights
Can’t have merged cells
Can’t have images or charts embedded in them
you should always use what for reading and writing CSV files. Give reason?
(ig) you should always use the csv module for reading and writing CSV files. Since CSV files also have their own set of escape characters to allow commas and other characters to be included as part of the values. The split() method doesn’t handle these escape characters.
To read data from a CSV file with the csv module, you need to do what?
you need to create a reader object. A reader object lets you iterate over lines in the CSV file
» import csv
➋»_space;> exampleFile = open(‘example.csv’)
➌»_space;> exampleReader = csv.reader(exampleFile)
➍»_space;> exampleData = list(exampleReader)
➎»_space;> exampleData
Note that you don’t pass a filename string directly to the csv.reader() function.
The most direct way to access the values in the reader object is to convert it to a plain Python list by passing it to list()
For large CSV files, you’ll want to use what for reading data?
(ig) you’ll want to use the reader object in a for loop. This avoids loading the entire file into memory at once.
import csv
»> exampleFile = open(‘example.csv’)
»> exampleReader = csv.reader(exampleFile)
»> for row in exampleReader:
print(‘Row #’ + str(exampleReader.line_num) + ‘ ‘ + str(row))
Difference between using extend() and + operator on list?
(ig) Instead of using the extend() method there’s an even easier way to concatenate two lists in Python. We can use the + operator:
The only difference between this and the extend() method is that the + operator creates a new list, while the extend() method modifies the original list.
What do you use for shallow copy and deep copy and reason to use deep copy?
use copy.deepcopy(list_name) for deep copies:
use list_name.copy() for shallow copies
If you had a list of lists, you would need to use the copy.deepcopy() method to create a deep copy.
-
example of deepcopy
import copy
original_list = [[1, 2], [3, 4]]
cloned_list = copy.deepcopy(original_list)
(important) explain the relationship between the original list and the shallow copy?
(ig)The shallow copy itself does not directly modify the original.
Changes to nested (mutable) objects within a shallow copy do affect the original due to shared references.
However, because the shallow copy only copies references to nested objects (like sublists or dictionaries within the list), modifications to these nested objects in the shallow copy will reflect in the original list, and vice versa.
This won’t affect original because you are not directly affecting the elements
How does nametuple work? How to get fields, elements nametuple?
Create a namedtuple called ‘Employee’ with fields ‘name’, ‘age’, and ‘position’
Employee = namedtuple(“Employee”, [“name”, “age”, “position”])
elements can be accessed both by index (like a tuple) and by name (like a dictionary).
Access fields by name instead of index, making code more readable.
look below for elaboration
-
from collections import namedtuple
Define the namedtuple
Person = namedtuple(“Person”, [“name”, “age”, “city”])
Create instances of the namedtuple
person1 = Person(name=”Bob”, age=25, city=”New York”)
person2 = Person(name=”Jane”, age=32, city=”Los Angeles”)
Access the fields by name
print(person1.name) # Output: Bob
print(person2.city) # Output: Los Angeles
Access the fields by index
print(person1[1]) # Output: 25
What should you know about modification of nametuple?
Like regular tuples, namedtuple instances are immutable, so you can’t modify their fields directly after creation.
What do dictionary key have to be?
(ig) Dictionary keys have to be immutable types like strings, ints.