Py Lists (Datacamp + UCSD Wk 2) Flashcards
Assign a value to a variable
eg, x = 5
print values
print()
Exponents in python
base ** exp
find out the type of a variable
type(variable)
concatenate strings
str3 = str1 + str2
convert a string to a float
pi_float = float(“3.14”)
convert int or float to string
str(int_var) or string(numerical value)
can you multiply a string by a number?
Yes. “hey” * 2 = “heyhey”
what are lists?
lists are arrays, and can have have various data types, including other lists
how to assign values to a list
list = [var1, var2, 5, “hello”]
how do you create a 2 dim array with lists
2d = [[“val1”, var1] , [“val2”, var2]]
How do you retrieve individual elements of a list (subset)
list[0] (first element]; list[-1] (last element)
How do you retrieve multiple elements from a list (slice)
list[1:3] - returns elements 1 and 2 (inc start, exc end)
Do you have to specify begin and end of the indices for your list slice?
no. if blank, python will go to end. eg, list[ : 5] will do from begin to 5.
specify elements from n-dim list (eg, 2 dim)
list [1] [2], or list [2] [0:2] - elements 0 to 2 from list 2