Object Types Flashcards
What are the built-in object types?
Numbers, strings, lists, dictionaries, tuples, files, sets
For a sequence type, what does the [index] operator do?
Returns the index-th element. Zero-based.
For a sequence type, what does the [-index] operator do?
Returns items from the end of the sequence. -1 is the last item, and so on.
How do you create slices with sequences?
S[1:3]
How do you get the length of a string?
len(S)
How do you get everything from the 2nd character to the end?
S[1:]
How do you concatenate strings?
s1 + s2
How do you create repeated strings
‘Spam’ * 8
Are strings mutable?
Nope. Modify, then copy.
How do you find the position of substrings?
S.find(‘pa’)
How do you find and replace in strings?
S.replace(‘pa’, ‘xyz’)
How do you remove whitespace from the right of a string?
S.rstrip()
How do you get a list of attributes for an object?
dir(object)
What do you import to do pattern matching?
import re
What is a list?
A mutable sequence.
How do you initialize a list?
L = [a, b, c]
How do you add an element to the end of a list?
L.append(ele)