W16 Flashcards
How to add an element to a list?
list.append() –> adds it to the end
How to delete all elements of a list?
list.clear()
How to copy a list?
import copy
x=copy.copy(list)
it copies the reference
what does count() do in a list?
it returns the repetition number of a specified values
How to find a value index in a list?
list.index(value)
how to add an element at the end of a list?
how to add an element in a specified index?
list. append(“h”)
list. insert(6, “h”)
pop() vs remove() vs del
del and pop() delete a specific index.
remove() deleted a specific value.
sort() vs. sorted()
1- list.sort(), does not return anything. it just changes the original list. It one of the python list method
2- sorted(list), returns a new sorted list. It is one of the built-in functions.
both sort a list in an ascending order. to sort a list in a descending order:
list.sort(reverse = True)
sorted(list, reverse = True)
a method to reverse the order of a list:
reverse()
What are the membership operators?
(in) and (not in)
monthInQuarter = [ 0 ] ∗ 10
One common use of replication is to initialize a list with a fixed value
shallow copy vs. deep copy
for (2D lists, string lists and string)
int and float lists are always considered as deepcopy (check)
A shallow copy also makes a separate new object object or list, but it simply copies the references to their memory addresses.
(Hence, if you make a change in the original object, it would reflect in the copied object, and vice versa)
A deep copy makes a new and separate copy of an entire object or list with its own unique memory address. (Using the copy module)
Examples of shallow and deep copy:
Shallow:
import copy
lst2=copy.copy(lst1)
Deep:
import copy
lst2=copy.deepcopy(lst1)
list[i][j], what are i and j?
i –> column
j –> row
to print or access a 2D list you will use nested loop, so what will the inner and outer loops print?
outer –> rows
inner –> columns
how to find a list columns and rows number
rows == len(list)
columns == len(list[0])
A container that stores a collection of unique values the elements or members of it are not stored in any particular order
set and dictionary
True or False?
Sets can be accessed by position?
False, since it is not ordered
How to create an empty set?
with values?
empty:
Don’t –> x= {}
Do –> x=set()
not empty:
x={1,2,3}
x=set(list)
the number of elements in a set:
len(set)
True or False:
Two sets are equal if and only if they have exactly the same elements
True, since the order does not matter.
Are sets mutable or immutable?
sets are mutable
how to add elements in a set?
add()
add(“hello”), if hello is already exited in the set what will happen?
no error will occur, and the new “hello” will not be added