Chapter 3 (strings) Flashcards
What are the types of sequences?
- String
- List
- Tuple
What are the properties of a string?
- Immutable, must be assigned a new string
- Only stores single characters in a sequence
- Can be iterated by element
What are the properties of a list?
- Mutable
- Can hold any data type
- Can hold more than one character per index
- Can be iterated by element
What are the properties of a tuple?
- Immutable
- Can hold any data type
- Can hold more than one character per index
- Can be iterated by element
What is the notation for finding the element of a sequence type by index?
sequence[index]
What is the only mapping type in Python?
Dictionary
What makes a mapping type different from a sequence type?
Both act as containers that hold values, but the mapping types are not ordered, their contents cannot be searched using an index number
- A dictionary uses a key-value pairs instead of index-value pairs
What is a set?
A separate type from mapping or sequence types, simply an unordered list of unique elements
How do you declare an empty and prefilled list?
Empty) list = []
Prefilled) list = [1,2,3,4,5]
How do you declare an empty and prefilled tuple?
empty) tuple = ()
prefilled) tuple = (1,2,3,4)
How do you declare an empty and prefilled dictionary?
Empty) dict = {}
Prefilled) dict = {‘joe’: 18, ‘Dan’: 19}
dict = {key:value}
How do you limit the number of decimal places in an output?
~ let x be the number of values after the decimal point
print(f’{value:.xf}’)
How do you find the length of a sequence?
len()
How do you find the final value in a sequence?
sequence[-1]
- indexing works as a circle, so you [0] is the first and [-1] is the last, you can keep going back the sequence as long as you don’t exceed the first value of the sequence
- e.g. name =’danial’ name[-12] would create an error
what is the function of \?
Ignore the following character in a print statement