Python List / Conversion Flashcards

 

card 1 of 50

 

["abc",3,True,"def",9.0]

 

square brackets denote a list

['abc',3,True,'def',9.0]

card 2 of 50

 

type([1,2,3])

 

lists have their own type

<type 'list'>

card 3 of 50

 

[True,(1,2,3),False]

 

lists can contain tuples

[True,(1,2,3),False]

card 4 of 50

 

(True,[1,2,3],False)

 

tuples can contain lists

(True,[1,2,3],False)

card 5 of 50

 

[True,[1,2,3],False]

 

lists can contain lists

[True,[1,2,3],False]

card 6 of 50

 

(True,[1,2,3),False]

 

look carefully

SyntaxError: parentheses and brackets must nest properly

card 7 of 50

 

len([True,[1,2,3],False])

 

length of a list is number of items in the list

3

card 8 of 50

 

[True,[1,2,3],False][0]

 

the square brackets "[" and "]" also mean indexing

True

card 9 of 50

 

type([True,[1,2,3],False][0])

 

evaluate argument of type() first

<type 'bool'>

card 10 of 50

 

type([True,[1,2,3],False][1])

 

evaluate argument of type() first

<type 'list'>

card 11 of 50

 

[2,3,4]+[True,'a']

 

operators + and * work on lists, like strings or tuples

[2,3,4,True,'a']

card 12 of 50

 

[4]

 

lists can have one item (and do not need extra comma that tuples need)

[4]

card 13 of 50

 

[]

 

empty list is easy

[]

card 14 of 50

 

[1,2,3]==[1,2,3]

 

common sense

True

card 15 of 50

 

[1,2,3]>[1,2,3]

 

common sense

False

card 16 of 50

 

[4,5]>[1,2,3,4,5]

 

somewhat like alphabetic comparison, left-to-right, item per item

True

card 17 of 50

 

4 in [1,2,3,4,5]

 

just like tuple interpretation of the "in" operator

True

card 18 of 50

 

[3,4] in [1,2,3,4,5]

 

just like tuple interpretation of the "in" operator

False

card 19 of 50

 

[3,4] in [1,2,[3,4],5]

 

just like tuple interpretation of the "in" operator

True

card 20 of 50

 

[123] == 123

 

comparison between different types does not work

False

card 21 of 50

 

[123] == '123'

 

comparison between different types does not work

False

card 22 of 50

 

['123'] == '123'

 

comparison between different types does not work

False

card 23 of 50

 

['a','b'] < 'ab'

 

this is strange (every language has some oddities)

True

card 24 of 50

 

tuple([4,True])

 

tuple function converts argument to a tuple

(4,True)

card 25 of 50

 

list((1,2,3))

 

list function converts argument to a list

[1,2,3]

card 26 of 50

 

list("abcd")

 

list function converts argument to a list

['a','b','c','d']

card 27 of 50

 

list('')

 

list function converts argument to a list

[]

card 28 of 50

 

list(True)

 

list function converts argument to a list

TypeError: boolean cannot be converted to list

card 29 of 50

 

list(7)

 

list function converts argument to a list

TypeError: integer cannot be converted to list

card 30 of 50

 

list([1,2,3])

 

why would anyone ever do this?

[1,2,3]

card 31 of 50

 

str([1,2])

 

str function converts argument to a string

'[1,2]'

card 32 of 50

 

list('[1,2]')

 

list function converts argument to a list

['[','1',',','2',']']

card 33 of 50

 

str(False)

 

str function converts argument to a string

'False'

card 34 of 50

 

bool('False')

 

bool function converts argument to Boolean

True

card 35 of 50

 

bool('a')

 

bool function converts argument to Boolean

True

card 36 of 50

 

bool('')

 

bool function converts argument to Boolean

False

card 37 of 50

 

bool(0)

 

bool function converts argument to Boolean

False

card 38 of 50

 

bool(107.3)

 

bool function converts argument to Boolean

True

card 39 of 50

 

bool([])

 

bool function converts argument to Boolean

False

card 40 of 50

 

bool([1,2])

 

bool function converts argument to Boolean

True

card 41 of 50

 

bool([False])

 

bool function converts argument to Boolean

True

card 42 of 50

 

int('70')

 

int function tries to convert string to integer

70

card 43 of 50

 

int('-2000')

 

int function tries to convert string to integer

-2000

card 44 of 50

 

int('2e5')

 

int function tries to convert string to integer

ValueError: invalid input for int()

card 45 of 50

 

float('2e5')

 

float function tries to convert string to float

200000.0

card 46 of 50

 

int(float('2e5'))

 

evaluate inside arguments first

200000

card 47 of 50

 

[10,2,9,5,4,6][2:5]

 

slicing also works on lists

[9,5,4]

card 48 of 50

 

list('winning here')[3:7] == 'raining heavy'[3:7]

 

in good style, comparison needs types to be the same

False

card 49 of 50

 

list('winning here')[3:7] == list('raining heavy'[3:7])

 

in good style, comparison needs types to be the same

True

card 50 of 50

 

[1,True,["three"],1e8][2][0][2:]

 

evaluate index/slice left-to-right

'ree'

 

End of Problem Set

(use browser reload to restart this problem set)