Object Types Flashcards

(44 cards)

1
Q

What are the built-in object types?

A

Numbers, strings, lists, dictionaries, tuples, files, sets

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

For a sequence type, what does the [index] operator do?

A

Returns the index-th element. Zero-based.

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

For a sequence type, what does the [-index] operator do?

A

Returns items from the end of the sequence. -1 is the last item, and so on.

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

How do you create slices with sequences?

A

S[1:3]

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

How do you get the length of a string?

A

len(S)

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

How do you get everything from the 2nd character to the end?

A

S[1:]

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

How do you concatenate strings?

A

s1 + s2

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

How do you create repeated strings

A

‘Spam’ * 8

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

Are strings mutable?

A

Nope. Modify, then copy.

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

How do you find the position of substrings?

A

S.find(‘pa’)

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

How do you find and replace in strings?

A

S.replace(‘pa’, ‘xyz’)

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

How do you remove whitespace from the right of a string?

A

S.rstrip()

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

How do you get a list of attributes for an object?

A

dir(object)

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

What do you import to do pattern matching?

A

import re

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

What is a list?

A

A mutable sequence.

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

How do you initialize a list?

A

L = [a, b, c]

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

How do you add an element to the end of a list?

A

L.append(ele)

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

How do you remove an element from the list?

A

L.pop(position)

19
Q

How do you sort a list?

20
Q

How do you reverse the order of a list?

21
Q

What is nesting?

A

containing one object inside another. A list that contains lists, for example.

22
Q

What are list comprehensions?

A

Complicated but probably useful for matrix operations. Out of scope.

23
Q

What is a dictionary?

A

A set of key->value mappings.

24
Q

How do you initialize a dictionary?

A

D = {‘key1’, ‘value1’, ‘key2’, ‘value2’ }

25
How do you retrieve a key's value from a dictionary?
D['key']
26
How do you create a new key/value pair in a dictionary?
Just assign it as D['key'] = 'value'
27
How do you test for an object not being in a dictionary?
if not 'f' in D: | code
28
What is a tuple?
An immutable list, used to represent fixed collections of items.
29
How do you code tuples?
(a, b, c). Contrast with a list: [a,b,c]
30
How to you open a file?
f = open('filename', 'w')
31
How do you write to a file?
f.write('text')
32
How do you close a file?
f.close()
33
How do you read from a file?
text = f.read()
34
What is a set?
An unordered collection of immutable objects
35
How do you initailize a set?
Y = {'h', 'a', 'm'}
36
How can you compute with decimal numbers?
import decimal | d = decimal.Decimal('3.1.415')
37
How can you compute with fractions?
from fractions import Fraction | f = Fraction(2,3)
38
When are variables created?
When they are assigned.
39
How do you create a variable without assigning it?
You can't.
40
How do you create comments in python?
comment...
41
How do you create random numbers?
import random | random.random()
42
How do you create random integers in a range?
import random | random.randint(1, 10)
43
How do you randomly select an item?
import random | random.choice(['a', 'b', 'c'])
44
Where can you get advanced numeric methods?
NumPy