Unit 3 Flashcards
Types
What is a string?
an immutable sequence of characters stored in a variable
3.1 - String Basics
How do you find the length of a string?
len(string)
3.1 - String Basics
What is string concatenation?
combining string values in an output statement
3.2 - List Basics
What is a container?
a construct which groups related objects together
individual components are called elements
3.2 - List Basics
What is a list?
an ordered mutable container, uses square brackets []
3.2 - List Basics
How do you concatenate lists?
use ‘list1’ and ‘list2’ as list names
list1 + list2
3.2 - List Basics
What are the four commands you can do for lists?
format answers as code
list.append(v)
adds new elementlist.pop(i)
removes an element at index (i)list.remove(v)
removes an element with value (v)list.index(v)
finds a value (v)’s index
3.3 - Tuple Basics
What is a tuple?
an ordered immutable container, uses ()
3.3 - Tuple Basics
What does a named tuple do?
allows a programmer to name each element
Car = namedtuple('Car', ['model', 'price']
blazer = Car('Chevrolet', 32000')
3.4 - Set Basics
What is a set?
a mutable, unordered container with unique elements; uses set()
or {}
{} used for literals
3.4 - Set Basics
What are the four commands you can do for sets?
use ‘setA’, ‘setB, etc. for set examples
set.intersection(setA, setB)
set w/ elements in commonset.union(setA, setB)
set w/ unique elementssetA.difference(setB, setC)
set w/ unique elements from setAsetA.symmetric_difference(setB)
set w/ elements that appear only once in setA/setB
3.5 - Dictionary Basics
What is a dictionary?
a mutable key/value paired collection, uses {'a': 1}
‘a’: 1 is a sample element for a dictionary
3.5 - Dictionary Basics
What is the difference between a key and a value?
key: index, like a term
value: mutable value, like a definition
3.5 - Dictionary Basics
What are the three commands you can use for dictionaries?
dictionary name: numbers
key: ‘one’, ‘two’, etc.
value: 1, 2, etc.
numbers["one"]
references value from keynumbers['three'] = 3
creates/updates entriesdel numbers['two']
removes an entry
3.6 - Common Data Types
What are the 5 data types?
string: sequence type, text
list: sequence type, ordered elements, mutable
tuple: sequence type, ordered elements, immutable
set: set type, unordered/unique elements, mutable
dict: mapping type, key/value elements, mutable