Chapter 5 - data structures Flashcards
how to slice?
x[1:3] returns the substring from index 1 to index 2 (does not include 3rd)
what does this return?
myword = ‘help’
myword[-2:-1]
‘l’
what does this return?
w = ‘mannn’
print(myword[:2])
print(myword[2:])
ma
nnn
what happens?
w = ‘mann’
print([myword[:])
prints entire string
mann
x = “myy wordssss”
x[4:3]
what does this return?
’’
^empty string
what does this return?
x = “myy wordssss”
x[::2]
‘mywrss’
^skips every other index, due to jump of 2 in substring
what does this return?
x = “myy wordssss”
x[::-2]
‘ssdo y’
^indexes backwards in steps of 2
how to reverse real easy using step function?
x[::-1]
how would you concatenate a string to itself 3 times?
x*=3
create an empty list
ok = []
how to delete list elements w/o using del keyword?
set part of list x[0:2] = []
add 25 to the end of this list:
a = [1, 2, 4, 3, 2]
a += [25]
^can only add lists together, which is why 25 is in brackets
bob = “123456789”
bob[0:-1]
returns?
‘12345678’
what will this return:
numlist = [1, 2, 3, 4, 5, 6]
numlist[-1] = []
[1, 2, 3, 4, 5, []]
you need the colon operator to actually replace! (numlist-[-1:] = [])
what is the only way to index backwards using the [:]?
use negative steps, such as:
x[::-1]
what will the following return:
- iter(range(4))
- iter([1, 2, ‘4’, 8])
- iter(33)
<range_iterator at 0x7fb888bb480f0>
2.
<list_iterator at (stuff im too lazy to write)>
- TypeError: ‘int’ object is not iterably
what does range create?
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