Chapter 5 - data structures Flashcards

1
Q

how to slice?

A

x[1:3] returns the substring from index 1 to index 2 (does not include 3rd)

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

what does this return?

myword = ‘help’
myword[-2:-1]

A

‘l’

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

what does this return?

w = ‘mannn’
print(myword[:2])
print(myword[2:])

A

ma
nnn

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

what happens?

w = ‘mann’
print([myword[:])

A

prints entire string

mann

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

x = “myy wordssss”
x[4:3]

what does this return?

A

’’
^empty string

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

what does this return?

x = “myy wordssss”
x[::2]

A

‘mywrss’

^skips every other index, due to jump of 2 in substring

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

what does this return?
x = “myy wordssss”
x[::-2]

A

‘ssdo y’

^indexes backwards in steps of 2

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

how to reverse real easy using step function?

A

x[::-1]

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

how would you concatenate a string to itself 3 times?

A

x*=3

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

create an empty list

A

ok = []

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

how to delete list elements w/o using del keyword?

A

set part of list x[0:2] = []

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

add 25 to the end of this list:
a = [1, 2, 4, 3, 2]

A

a += [25]

^can only add lists together, which is why 25 is in brackets

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

bob = “123456789”
bob[0:-1]

returns?

A

‘12345678’

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

what will this return:

numlist = [1, 2, 3, 4, 5, 6]
numlist[-1] = []

A

[1, 2, 3, 4, 5, []]

you need the colon operator to actually replace! (numlist-[-1:] = [])

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

what is the only way to index backwards using the [:]?

A

use negative steps, such as:

x[::-1]

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

what will the following return:

  1. iter(range(4))
  2. iter([1, 2, ‘4’, 8])
  3. iter(33)
A

<range_iterator at 0x7fb888bb480f0>
2.
<list_iterator at (stuff im too lazy to write)>

  1. TypeError: ‘int’ object is not iterably
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what does range create?

A

creates an iterator of type range (though can be casted to list) ONE AT A TIME until
n-1 value of range(n) is reached

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

what will this return?

l = [1, 2, 3, 4, 5, 6]
l[::-1]

A

[6, 5, 4, 3, 2, 1]

19
Q

true/false:
enumerate requires both an index and item iterator to be “used to its full potential”

A

false, if given only one iterator variable in a for loop will just set equal to a tuple (index, item)

20
Q

tuple vs. list

A

tuples are IMMUTABLE, and are put in parentheses instead of colons OR just plain text like:
tuple1 = 1, 3

slicing, indexing, concatenation, and len methods are the same

21
Q

how to create tuple with one entry

A

oneTup = (7,)
prints out (7,)

im told parentheses and comma are necessary, not sure abt the parentheses part bcz it worked :(

22
Q

what type is returned if you index a tuple?

A

returns a tuple mytup = (3, 7, “m”, 20)
mytup[2:3]
returns (“m”,)

23
Q

can a tuple be typecasted?

A

yes, to a list. also, can typecast lists and strings to tuples

24
Q

packing vs. unpacking

A

packing = puts multiple values separated by commas into tuple

unpacking = setting equal amount of variables to left side of tuple so each gets assigned tuple elements in that order

unpacking ispossible for lists and strings too!

25
Q

what will this return:
oneTup = “hello”
a, b, c = oneTup
print(a, b, c)

A

error, too many values to unpack!

26
Q

what does this return:
list(range(12,3,-2))

A

[12, 10, 8, 6, 4]

27
Q

Create the sequence [3, 2, 1].

A

list(range(3, 0, -1))

28
Q

what will this print:
list(range(2, 4, 5))

A

[2]

29
Q

what will this print:
empTup = (,)
len(empTup)

A

error, cannot have just a comma in a tuple

30
Q

myTuple = (1,2,3)
myTuple[0]

A

returns 1

31
Q

difference between +=, append, and extend for adding tuples?

A

+= and extend only add the values to the end, whereas append adds the actual tuple

32
Q

what does this return?
mytup = 7, 3, 5, 6
enumerate(mytup)

A

<enumerate at 0x78c7018b9d40>
basically nothing, but also not an error

33
Q

what is vectorizing code?

A

making code more succinct by using in-built functions BOY

34
Q

true/false:
The range function returns a list

A

False, returns an iterable of type range

35
Q

Tuples are a mutable data type

true/false

A

false

36
Q

what would the result be:
[c+’!’ for c in “Yay”]

A

[‘Y!’, ‘a!’, ‘y!’]
list comprehensions create, well, a list!
thus can be mutated where a string could not

37
Q

lab 13 Problem 6: Flatten a nested list using a list comprehension. For example, [[1,2], [3,4]] would become [1,2,3,4].
nestedlist = [[1, 2], [3, 4, 5], [9, 7]]

A

flatlist = [j for i in nestedlist for j in i]

38
Q

lab 13 Problem 7: Create a list of numbers. From the original list, create a list of the running sums of the numbers. Do this using a list comprehension

A

numlist = [1, 2, 3, 55, 6, 7, 8, 10]
listsum = [sum(numlist[:i]) for i in range(1, len(numlist)+1)]
print(listsum)

39
Q

list comprehension format

A

[ (output function set to each element of list) for (iterator) in (iterable) if _______]

MIGHT HAVE IF and/or ELSE MIGHT NOT

40
Q

what will the following return:

numlist = [2, 33, -5, 11, 4]
numlist.insert(3, 33)
numlist

A

[2, 33, -5, 33, 11, 4]

rmr, insert does not replace, it just adds!

41
Q

numlist = [2, 33, -5, 11, 4]
numlist.pop()
numlist

A

[2, 33, -5, 11, 4]
removes at index = -1, defaul pop() is pop(-1)

42
Q

imr = range(6)
next(imr)

vs.

imr = iter(range(6))
next(imr)

A

i have absolutely no idea

43
Q
A