W16 Flashcards

1
Q

How to add an element to a list?

A

list.append() –> adds it to the end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to delete all elements of a list?

A

list.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to copy a list?

A

import copy

x=copy.copy(list)

it copies the reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does count() do in a list?

A

it returns the repetition number of a specified values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to find a value index in a list?

A

list.index(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to add an element at the end of a list?

how to add an element in a specified index?

A

list. append(“h”)

list. insert(6, “h”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pop() vs remove() vs del

A

del and pop() delete a specific index.

remove() deleted a specific value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

sort() vs. sorted()

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

a method to reverse the order of a list:

A

reverse()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the membership operators?

A

(in) and (not in)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

monthInQuarter = [ 0 ] ∗ 10

A

One common use of replication is to initialize a list with a fixed value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

shallow copy vs. deep copy

for (2D lists, string lists and string)
int and float lists are always considered as deepcopy (check)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Examples of shallow and deep copy:

A

Shallow:
import copy
lst2=copy.copy(lst1)

Deep:
import copy
lst2=copy.deepcopy(lst1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

list[i][j], what are i and j?

A

i –> column

j –> row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

to print or access a 2D list you will use nested loop, so what will the inner and outer loops print?

A

outer –> rows

inner –> columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to find a list columns and rows number

A

rows == len(list)

columns == len(list[0])

17
Q

A container that stores a collection of unique values the elements or members of it are not stored in any particular order

A

set and dictionary

18
Q

True or False?

Sets can be accessed by position?

A

False, since it is not ordered

19
Q

How to create an empty set?

with values?

A

empty:
Don’t –> x= {}
Do –> x=set()

not empty:
x={1,2,3}
x=set(list)

20
Q

the number of elements in a set:

A

len(set)

21
Q

True or False:

Two sets are equal if and only if they have exactly the same elements

A

True, since the order does not matter.

22
Q

Are sets mutable or immutable?

A

sets are mutable

23
Q

how to add elements in a set?

A

add()

24
Q

add(“hello”), if hello is already exited in the set what will happen?

A

no error will occur, and the new “hello” will not be added

25
Q

how to remove an element?

A

The discard() method removes an element if the element exists, and if not It has no effect

26
Q

how to check if set1 is a subset of set2?

A

set1.subset(set2)
the order is important:
A set is a subset of another set if and only if every element of the first set is also an element of the second set

27
Q

To add two sets:

A

set1.union(set2)

28
Q

True or False:

To concatenate two sets use (+)

A

False.

set concatenation creates an error

29
Q

how to find similar values in different sets?

A

set1.intersection(set2)

returns a set of intersected values