Object Types Flashcards

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?

A

L.sort()

20
Q

How do you reverse the order of a list?

A

L.reverse()

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
Q

How do you retrieve a key’s value from a dictionary?

A

D[‘key’]

26
Q

How do you create a new key/value pair in a dictionary?

A

Just assign it as D[‘key’] = ‘value’

27
Q

How do you test for an object not being in a dictionary?

A

if not ‘f’ in D:

code

28
Q

What is a tuple?

A

An immutable list, used to represent fixed collections of items.

29
Q

How do you code tuples?

A

(a, b, c). Contrast with a list: [a,b,c]

30
Q

How to you open a file?

A

f = open(‘filename’, ‘w’)

31
Q

How do you write to a file?

A

f.write(‘text’)

32
Q

How do you close a file?

A

f.close()

33
Q

How do you read from a file?

A

text = f.read()

34
Q

What is a set?

A

An unordered collection of immutable objects

35
Q

How do you initailize a set?

A

Y = {‘h’, ‘a’, ‘m’}

36
Q

How can you compute with decimal numbers?

A

import decimal

d = decimal.Decimal(‘3.1.415’)

37
Q

How can you compute with fractions?

A

from fractions import Fraction

f = Fraction(2,3)

38
Q

When are variables created?

A

When they are assigned.

39
Q

How do you create a variable without assigning it?

A

You can’t.

40
Q

How do you create comments in python?

A

comment…

41
Q

How do you create random numbers?

A

import random

random.random()

42
Q

How do you create random integers in a range?

A

import random

random.randint(1, 10)

43
Q

How do you randomly select an item?

A

import random

random.choice([‘a’, ‘b’, ‘c’])

44
Q

Where can you get advanced numeric methods?

A

NumPy