Data Structures Flashcards
“Type” command
Returns the class of the data, or the exact type if a Python object
Ex
Type(5)
Class ‘int’
Int, str, float
Sequences
An ordered collection of values.
Sequence types:
Strings
Tuples
Lists
Tuple
A tuple is an immutable sequence of 0 or more values.
Enclosed in round brackets and separated by commas.
Tuple functions
X in tup. - True if x is an element in tup
Len(tup) - # of elements in tup
tup.count(x) - # of times element x occurs in tup
Tup.Index(x) - index location of first occ of x, returns ValueError exception if not in tup
Lists
Lists are the same as tuples, but they are mutable- ie they can be changed.
Separated by commas, enclosed in square brackets.
Can use len, concatenate, indexing, slicing.
List functions
Most are mutating functions, ie they modify the list.
S.append (x)
Appends x to the end of s
What does “pop” refer to?
The act of removing the last element of a list.
What does “push “ refer to?
Adding an element to the same end of a list (like append)
What does “stack “ refer to?
When push and pop are used on the same list.
Items are pushed onto the top of the stack, and then popped from the top of the stack.
S.count(x)
Returns the number of times x appears in s
S.extend(lst)
Appends each item of “lst” to s
S.pop(i)
Removes and returns the item at index i in s
S.index(x)
Returns the index value of the leftmost occurrence of x
S.insert(i, x)
Inserts x before index location i (so that s[i] == x)