Operations on sequence types Flashcards

1
Q

What is sequence?

A

It is an object which consists of sequential elements or other objects through which you can iterate.

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

Name all sequence data types in python.

A
  • strings;
  • Unicode strings;
  • lists;
  • tuples;
  • bytearrays;
  • buffers;
  • xrange objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are bytearrays?

A

It is a mutable sequence of bytes.

A bytearray is very similar to a regular python string (str in python2.x, bytes in python3) but with an important difference, whereas strings are immutable, bytearrays are mutable, a bit like a list of single character strings.

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

What are buffers?

A

buffer(object[, offset[, size]])

A new buffer object will be created which references the object argument.

An example usage:

> > > s = ‘Hello world’
t = buffer(s, 6, 5)
t

> > > print t
world

The buffer in this case is a sub-string, starting at position 6 with length 5, and it doesn’t take extra storage space - it references a slice of the string.

This isn’t very useful for short strings like this, but it can be necessary when using large amounts of data. This example uses a mutable bytearray:

> > > s = bytearray(1000000) # a million zeroed bytes
t = buffer(s, 1) # slice cuts off the first byte
s[1] = 5 # set the second element in s
t[0] # which is now also the first element in t!
‘\x05’

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

How to check if an item is equal or not to x?

A

x in s # true if x the items of x

x not in s

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

How to perform concatenation (addition) of two sequences of the same type?

A

s + t

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

What are next operations?
»> s * n
»> n * s

A

They are equivalent to adding s to itself n times.

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

How to get ith item of s by it’s index?

A

s[i] # indexation starts from zero, 0 is the first element

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

How to slice s from i to j? (excluding jth element)

A

s[i:j]

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

How to slice s from i to j with step k (selecting every kth item)? (excluding jth element)

A

s[i:j:k]

> > > s = ‘0123456789’
s[0:7:3]
‘036’

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

Get every second element in
s = ‘0123456789’
starting from zero.

A

s[::2]

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

What can happen in case you do next:
»> l = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
»> del l[4:9]

A

Those elements gonna be removed from the object:
»> l
[‘0’, ‘1’, ‘2’, ‘3’, ‘9’]

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

Get length of the sequence s.

A

len(s)

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

Get smallest item of s.

A

min(s)

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

Get largest item of s.

A

max(s)

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

Get index of the first x item occurrence in s

A

s.index(x)

17
Q

Get total number of occurences of x in s.

A

s.count(x)

18
Q

Make full copy of the sequence.

A

s[:]

19
Q

What could happen if you get negative n value in s*n expression?
like
»> ‘text’ * -1

A

It yields empty sequence of the same type as s:
»> ‘text’ * -1
‘’

20
Q

Are items in the sequence s in s*n copied?

A
No, they're referenced multiple times:
>>> lists = [[]] * 3
>>> lists 
[[], [], []]
>>> lists[0].append('hi')
>>> lists
[['hi'], ['hi'], ['hi']]

You can create list of different lists this way:
lists = [[] for i in range(3)]