Lecture 1 Flashcards

Controlling exit loops, lists and their methods

1
Q

what does the break keyword do?

A

Exits the loop

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

how do you make an infinite loop?

A

while True:

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

what is a list? (3)

A

1 sequence data type
2 enclosed in [], data separated by commas
3 can contain data of many different types.

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

how do you access a list item within a list? (syntax)

A

listname[index of inner list[index of inner list item]]

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

what is the syntax for using the range object to create a list with numbers 1-10?

A

a=list(range(1, 11))

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

how do you create a list using list comprehensioN? (ex: squares of first 3 numbers)

A

squared_numbers = [x**2 for x in range(1, 4)]

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

what does append method do and what is syntax?

A

listname.append(x)
adds a single element x to the end of a list

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

what does the extend method do and what is the syntax?

A

listname.extend(x)

merges another list x to the end of the list

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

what is the insert method and what is the syntax?

A

listname.insert(index, x)

inserts element x at position index

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

what is the remove list method and what is the syntax?

A

listname.remove(value)

removes the first occurrence of element x from listname.

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

what is the pop method and what is the syntax?

A

listname.pop(index)
removes the LAST element of a list. if an argument is passed, that index item is removed.

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

what is the index method and what is the syntax?

A

listname.index(x)

searches for a given element from the start of the list and returns the position of the first occurrence. Throws error if not found.

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

what is the count method/syntax?

A

listname.count(x)

Counts the number of occurrences of an element x in a list listname.

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

sort method + syntax? (+3 things to remember)

A

listname.sort(reverse=True/False, key=function to specify sorting criteria by returning it)

**you have to use the keywords by name in the argument eg key=a

**you don’t need the () when naming the function in this syntax

*** does not work when sublists are involved

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

reverse method + syntax?

A

listname.reverse()

reverses the list

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

swapcase method + syntax

A

returns a string with all the cases changed.

stringname.swapcase()

17
Q

what is a way to reverse a list without the reverse method?

A

listname[::-1]

18
Q

len method + syntax

A

returns number of elements present in a list

syntax:

x=len(listname)

19
Q

max/min method + syntax

A

return max/min value in a series
min(listname)
max(listname)

20
Q

what is the definition of scope, and wht is the scope of a variable created iwthin function a?

A

scope=where our variables can be accessed!
the scope is only within function a.

21
Q

what is a global vs local variable?

A

local = defined within the function
global = defined outside all the fuctions in the general code

22
Q

how do you use a global variable wtihin a function?

A

global variablename allows you to pull a variable from outside the function and make it usable within the function

23
Q

how would you replace values in a list?

A

a=[list]
i=a.index[value to replace]
a[i]=new value

24
Q

how would you swap values in a list?

A

a = 2
b = 4
a,b = b,a
print(a,b)

25
Q

what is a nonlocal variable and what are its limitations?

A

The nonlocal keyword is used in nested functions to reference a variable in the parent function.

won’t work on local or global variables and therefore must be used to reference variables in another scope except the global and local one.