Py Lists (Datacamp + UCSD Wk 2) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Assign a value to a variable

A

eg, x = 5

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

print values

A

print()

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

Exponents in python

A

base ** exp

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

find out the type of a variable

A

type(variable)

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

concatenate strings

A

str3 = str1 + str2

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

convert a string to a float

A

pi_float = float(“3.14”)

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

convert int or float to string

A

str(int_var) or string(numerical value)

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

can you multiply a string by a number?

A

Yes. “hey” * 2 = “heyhey”

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

what are lists?

A

lists are arrays, and can have have various data types, including other lists

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

how to assign values to a list

A

list = [var1, var2, 5, “hello”]

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

how do you create a 2 dim array with lists

A

2d = [[“val1”, var1] , [“val2”, var2]]

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

How do you retrieve individual elements of a list (subset)

A

list[0] (first element]; list[-1] (last element)

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

How do you retrieve multiple elements from a list (slice)

A

list[1:3] - returns elements 1 and 2 (inc start, exc end)

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

Do you have to specify begin and end of the indices for your list slice?

A

no. if blank, python will go to end. eg, list[ : 5] will do from begin to 5.

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

specify elements from n-dim list (eg, 2 dim)

A

list [1] [2], or list [2] [0:2] - elements 0 to 2 from list 2

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

how to replace list elements?

A

x[2:3] = [”s”, “t”]

17
Q

how to extend a list

A

list2 = list1 + [“e”, “f”]

18
Q

remove an element from a list

A

del( list [1] )&raquo_space; removes element 1, and renumbers all indexed elements after

19
Q

Point a new list at another list

A

newList = oldList (points to the same array)

20
Q

copy the values of a list into a new list (eg, new ind. data structure)

A

copiedList = oldList[ : ]

21
Q

append 4 to list [1,2,3] (appends an element to the end. )

A

list.append(4)

22
Q

iterate over values in a list

A

for i in list: print(i)

23
Q

iterate over list using range

A

for i in range(0, len(list)): print(list[i])

24
Q

lists are mutable

A

you can change the values in the list

25
Q

Retrieve and delete element with index 2 from a list

A

list.pop(2)&raquo_space; returns element 2, deletes, and shifts subsequent elements “down” in index

26
Q

remove an element with the value 33 from a list

A

list.remove(33)

27
Q

method to add one list to the end of an other

A

list.extend(list2)&raquo_space; (appends all the values from second list as values in first list)

28
Q

iterate through same position in two lists at same time

A

for x,y in zip(list1, list 2): print(x,” “,y)