Py Lists & Dicts & Tuples (Ud.10.Apps) Flashcards

1
Q

how to create values 1-10 in a list without explicitly specifying each value?

A

}} list_1 = list( range( 1,10 ))
]] list_2
]] [1, 2, 3, 4, 5, 6, 7, 8, 9]

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

how to create every second value, starting from 2, between 1 and 10 into a list, without specifying each value ?

A

}} list_2 = list( range( 2,10,2 ))
]] list_2
]] [2, 4, 6, 8]

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

how to create every second value, starting from 1, between 1-10 into a list?

A

}} list_3 = list( range( 1,10,2 ))
]] list_3
]] [1, 3, 5, 7, 9]

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

how to see all functions attributable to one object type, e.g. lists?

A

]] dir( list )

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

how to find all inbuilt functions?

A

]] dir( __builtins__ )

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

how to count number of values in a list?

A

]] dir( list1 )
]] list1.count( 5 ) #have to provide an argument
]] 1

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

how to add key, value pairs to a dict?

A

]] dict_1 = { ‘key1’: 10.0, ‘key2’: 20.0, ‘key3’: 25.15 }

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

how to retrieve all values from a dict?

A

]] dict_1.values()

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

how to retrieve all keys from a dict?

A

]] dict_1.keys()

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

how to retrieve all pairs from a dict?

A

]] dict_1.items()

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

how to encapsulate values in list, dict, tuple?

A

]] list_1 = [ 1,2,3,4 ]
]] dict_A = { ‘A’:1, ‘B’:2, ‘C’:3, ‘D’:4 ]
]] tuple_! = ( ‘A’:1, ‘B’:2, ‘C’:3, ‘D’:4 )

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

what windows short command will clear the shell?

A

]] ctrl + < l >

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

create a list with 3 values and append a fourth

A

]] values = [ A,B,C ]

]] values.append( D )

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

remove the two lowest values from the list with 4 values

A

]] values.remove( A )

]] values.remove( B )

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

how do you use index for a list and what will it return?

A

]] values.index( D )
]] 1

– the value is expressed as an argument within fn-index and it will return the order of the value, only a single value

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

how to get the 2nd value from a list?

and what does Python do in the background

A

]] values[1]
]] ‘D’

– note: this is the same expression used as when the list was originally created

17
Q

how to get a list of Python’s all builtin-functions?

A

]] dir( __builtins__ )

18
Q

what is the output from: [‘abc’ , ‘def’, ‘ghi’, ‘jkl’, ‘mno’][-2][-2]

A

]] k
–The code first extracts the second-to-last string of the list which is ‘jkl’. Then extracts the second-to-last character of that string.

19
Q

What would the following code return?

‘abcdef’[:3]

A

]] ‘abc’

20
Q

the instruction to return the output: ‘cd’ , from: ‘abcdef’

A

]] ‘abcdef’[2:4]

– a’bcdef’ is a string and the extraction from a string uses the same syntax as for a list

21
Q

what is the output for ‘abcdef’[-2:]

A

]] ‘ef’

22
Q

create a list including ‘abcdef’ and print out [ ‘b’, ‘c’ ,’d’ }

A

]] letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]

]] print{ letters[ 1:4 ] )

23
Q

print ‘a’, ‘b’, ‘c’ from ‘letters’, a list including all letters of the alphabet

A

]] print( letters[ :3 ] )

24
Q

print ‘e’, ‘f’, ‘g’ from ‘letters’

– a list including letters in alphabetical order up until ‘g’

A

]] print( letters[ -3: ] )

25
Q

retrieve a value from a dictionary by using [ ]

A

]] dict_1 [ ]

26
Q

how to convert a dict key or value to a text string?

a value retrieved from a dict will appear in a list

A

]] ‘‘.join( )

27
Q

how to summarise values in a list?

A

]] sum( list_a )