Characters, Strings, and Lists Flashcards

1
Q

Calculate the number of digits in a number. Find the bug:
»> n = 1234
»> len(n)

A
>>> n = 1234
>>> len(n)
TypeError: object of type 'int' has no len()
>>> len(str(n)) # correction
4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Calculate the number of digits in a negative number. Find the bug:
»> n = -42
»> len(str(n))

A
>>> n = -42
>>> len(str(n))
3 (wrong, should be 2)
>>> len(str(abs(n))) """abs() returns absolute value of operand"""
2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you do multiline comments? What is wrong with it?

A

Triple quotation mark (“”” or ‘’’) but it is unconventional and should be avoided.

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

What is the FIRST comment that should be present in a Python program?

A

What does this program do

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

What is the SECOND comment that should be present in a Python program?

A

Author(s): who wrote me

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

What is the THIRD comment that should be present in a Python program?

A

Date created

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

What is the FOURTH comment that should be present in a Python program?

A

Date modified and reason

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

What comments should key variables and user-defined functions have?

A

All key variables and user-defined functions should have comments explaining what they are used for.

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

What is ‘indexing’ a string?

A

Return a single character at a particular location (indexes start at 0, not 1)

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

What is string ‘slicing’?

A

Extracting a substring of arbitrary length

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

What is string ‘splitting’?

A

Breaking up a string into components based on particular substrings.

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

Complete the code:
»> string = “It was a dark”
»> string[4]
???

A

> > > string = “It was a dark”
string[4]
a

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

Complete the code:
»> string = “It was a dark”
»> string[-8]
???

A

Complete the code:
»> string = “It was a dark”
»> string[-8]
s

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

What is the general index for the LAST character in a string using positive numbers?

A
n-1, because indexes start at 0 from the left
Eg:
>>> name = "Levi"
>>> name[4]
IndexError: string index out of range
>>> name[3]
'i'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the general index for the LAST character in a string using negative numbers?

A
-1, because indexes start at -1 (not 0) from the right
Eg:
>>> city = "Melbourne"
>>> city[-0]
'L'
>>> city[-1]
'e'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the general index for the FIRST character in a string using negative numbers?

A
-n, as indexing from the right starts at -1
>>> food = "icecream"
>>> food[1]
'c'
>>> food[-1]
'm'
17
Q

What is the general index for the FIRST character in a string using positive numbers?

A
0, as indexing starts at 0
>>> furniture = "chair"
>>> furniture[1]
'h'
>>> furniture[0]
'c'
18
Q

Slice the following string to print on the word “police”:

|&raquo_space;> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[8:])
‘police’

19
Q

Slice the following string to print on the word “da”:

|&raquo_space;> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[5:7])
‘da’

20
Q

Slice the following string to print on the word “fuck”:

|&raquo_space;> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[:4])
‘fuck’

21
Q

Slice the following string to print on the word “police”, USING NEGATIVE INDEXING:
»> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[-6:])
‘police’

22
Q

Slice the following string to print on the word “da”, USING NEGATIVE INDEXING:
»> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[-9:-7])
‘da’

23
Q

Slice the following string to print on the word “fuck”, USING NEGATIVE INDEXING:
»> nwa = “fuck da police”

A

> > > nwa = “fuck da police”
print(nwa[-14:-10])
‘fuck’

24
Q

> > > nwa = “fuck da police”
???
‘kcuf’

A

> > > nwa = “fuck da police”
nwa[-11:-15:-1]
‘kcuf’

25
Q

What is the general length of a sliced substring, string[R:L]?

A

Substring length = R - L

Indexing starting at 0, for left to right slicing

26
Q

> > > nwa = “fuck da police”
??? # use slicing
‘fuck da police’

A

> > > nwa = “fuck da police”
nwa[:] # from start to finish
‘fuck da police’

27
Q

What is the general formal for for reverse slicing a string of len(string) = n?

A

> > > string = “abcdef”
len(string) = 6
string[-4:-7:-1]
‘cba’

General formula:
string[-a:-b:-1]
abs(a) ≥ 1
abs(b) ≤ n + 1
-1 flips the string (third value specifies direction)
28
Q

For string of an arbitrary length, how can you slice to get only the FIRST half?

A

string[:int(len(string)*0.5)]

29
Q

For string of an arbitrary length, how can you slice to get only the SECOND half?

A

string[int(len(string)*0.5):]

30
Q

For string of an arbitrary length, how can you slice to get only the ‘MIDDLE’ half?

A

string[int(len(string)0.25)+1:int(len(string)0.75)]

31
Q

Define ‘method’.

A

Method: a function that is a member of an object

32
Q

Define ‘object’.

A

Object: a collection of data and functions, eg str

33
Q

Explain .format()

A

> > > “{0} string {1}”.format(“Hey!”, “noodles”)
‘Hey! string noodles’

Replaces the indexes in braces {} with the corresponding argument.

34
Q

> > > ”{???} & {???}”.format(‘abcde’)
‘b & d’

What is the point?

A

> > > “{0[1]} & {0[3]}”.format(‘abcde’)
‘b & d’

Point: can refer to indexes inside the arguments of format()

35
Q

How does Python3 encode characters?

A

UTF-8: unicode with 8 bits for ASCII

36
Q

How do you convert an ASCII character to its code (internal representation)?

A

ord( ‘str’ )
»> ord(‘*’)
42

37
Q

How do you convert an int code (internal representation) (0-255) into its corresponding ASCII character?

A

chr( int )
»> chr(42)
‘*’