Learn Pytho Flashcards

1
Q

List: How to define one?

A

users[1, 2, “buckle”, “my”, ‘shoe’]

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

List: Difference between forwards, backwards indexing?

A

Forwards: positive index, start at 0
Backwards, negative index, starts at -1
Backwards counts arrays from the end.

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

List syntax: access n’th element?

A

users[n-1]

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

List syntax: append element?

A

users.append(‘item’)

Takes only one argument

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

PCC: Difference between variables and strings?

A

Variables store numbers, strings store text
1, 2
“high” “low”

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

PCC: What’s a tuple, how is it defined?

A

(1920, 1080)
Lists where the items can’t be modified.
Uses parentheses instead of square brackets.
() instead of []

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

List: Syntax of list insert?

A

users.insert(n-1, item)

Inserts item in n’th place, pushing the rest

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

List: Delete by position n

A

del users(n-1)

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

List: Delete by item

A

users.remove(item)

Clever. Scans list for item and removes the first item that matches.

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

List: “Pop” or take item n from list

Pop last?

A

users. pop(n-1)

users. pop()

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

List: find length?

A

len(users)

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

List: Sort in reverse alphabetical permanently?

A

users.sort(reverse = True)

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

List: Reverse order?

A

users.reverse()

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

List: print all items

A

for user in users:
print(user)

Remember the four spaces.

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

List: slice a list

A

users(ni:nf)
ni = index of first item
nf = index of last item
blank = take the first (or last) item)

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

Dict: defining one

A

dict = {key:value, key:value]

17
Q

Dict: accessing a value

A

dict[‘key’]
OR
dict.get(‘key’)

18
Q

Dict: adding new keys or overwriting old ones

A

dict[‘key’] = value

19
Q

If: less than/equal to?

A

<=

less than comes first

20
Q

If: checking presence not in list

A

“item” not in list

Returns boolean

21
Q

If: Checking for empty lists?

A

if list

If list is empty, returns false

22
Q

If: exiting an infinite loop?

A

Ctrl+C

23
Q

Functions: defining one?

A
def function(input):
    codecodecode
24
Q

Functions: passing keywords

A

function(in1 = “item”, in2 = “poop”)

25
Q

Functions: specifying default value

A

def function(in1 = “defaultval”)

26
Q

Functions: specifying optional value

A

def function(inOpt = None)