Sequence Types Flashcards
Containers for collections of objects ordered by position in the sequence, where the first object has an index of 0 and subsequent elements have indices 1, 2, etc.
Sequence Types
What are all the sequence types ?
String, list, and tuples.
What are all the sequence-type methods ?
len()
+
min()
max()
sum()
index(val)
count(val)
append()
pop()
remove()
What does the method list.count(val) do ?
Count the number of occurrences of the value val in the list.
What does the method list.index(val) do ?
Find the index of the first element in the list whose value matches val.
Instructs an object to perform some action, and is executed by specifying the ___ name following a “.” symbol and an object.
Method
Which sequence-type containers are mutable ?
Lists & dicts
Which sequence-type containers are immutable ?
Strings and Tuples
What is the syntax to create a list ?
my_list = [ ]
How to access a sequence type element ?
my_sequence[i]
i = index
What is the syntax to create a tuple ?
my_tuple = (‘abc’)
Which sequence type allows the programmer to define a new simple data type that consists of named attributes ?
named tuple
Syntax to create a new named tuple Dog that has the attributes name, breed, and color.
Dog = namedtuple([‘name’, ‘gender’, ‘color’])
Create a new dog object called ‘Boston Terrier’ where name is ‘Camy’, gender is ‘female’, and color is ‘brown and white’.
Camy = Dog(‘Camy’, ‘Female’, ‘Brown and white’)
How to access a named tuple data object’s attributes ?
object_name.attribute_name
Ex) Camy.color