Chapter 3 CS453 Python
What do Dictionaries do
They map keys to values; no ordering is maintained between entries
What does list.index(val) do?
This function finds the index of the first element within the list whose value matches the val
What is a String
a String is a series of characters,like the text Mary, that can be stored in a variable
What is a word for a term that can be located on a dictionary
a key
What is a method?
a method instructs an object to perform some action and is executed by specifying the method name following a "."
what is the result of a set([100, 200, 100, 200, 300])
a set containing elements 100, 200, 300,
Since sets are unordered, this means you can't
access the element within the set by using index because it does not have an index
Since sets are mutable elements can be
added or removed using set methods
what does set1.update(set2)
adds the elements in set2 to set 1
How do you access list elemetns
by using indexing expression
attribute error
can be defined as an error that is raised when an attribute reference or assignment fails.
set.clear( )
clears all elements from the set
Element index
contained elements are ordered by position in the list
A value
describes some data associated with a key
How do you add new entries to a dictionary
dict[k]= v: Adds the new key-value pair k-v, if dict[k] already exist
Simply passing a list into a set ( ) will cause any
duplicates to be omitted in the created set.
the \ characters after the string literal _______-
extends the string to the following line
Container
is a construct used to group related values together and contains references to other objects instead of data
List
is a container created by surround a sequence of variables of literals with brackets []
element
is a list item
Dictionary
is a python container used to describe associative relationships it is represented by the dict object type
What is String literal
is a string value specified in the source code of the program example: 'MARY' OR "MARY" usually have single or double quotes
Sequence type
is a type that specifies a collection of objects ordered form left to right.
String concatenation
is the process of adding new characters to a string
len() function
is used to find the length of a string
What function is used to find the length of a list
len(list)
What does the append method look like?
list.append(value): Example list.append('abc')
What does the pop method look like in python
list.pop(i): Removes the element at index i from list.
What does the remove method look like?
list.remove(v): Removes the first element value is v
what function causes the concatenation of one or more list t
list1 + list2
How do you find the element with the largest value on a list
max(list)
Create a set using the set function
num_christians= set( 30, 32, 53)
What does namedtuple( ) do?
only creates new simple data type, and does not create new data objects Above, a new object is not created until car( ) is called appropriate values.
The add ( ) method
places a new element within a set if the set does not contain an element with the provided value
you can use the remove and pop methods to
remove an element in a set
Set.pop(
removes a random element from the set
set.remove(value)
removes value from the set. Raises KeyError if value is not found.
What is an example of adding elements to a set
set.add(value): Add value into the set Ex: my_set.add( 'abc')
What are some examples of removing elements form a set:
set.remove(value): Remove the element with the given value from the set. Raises Key Error if the value is not found. set.pop(value) removes a random element in the set
How do you find the sum of all the elements of a list?
sum(list)
How can a named tuple be created
the named tuple package must be imported to create a new named tuple.
Elements can be removed using
the pop() or remove methods
Set is often used to___
to reduce a list of items that potentially contains duplicates into a collection of unique values
How do you remove entries from the dictionary
using the Del keyword del prices [ 'papaya'] removes the entry whose key is 'papaya'
Are dictionaries mutable?
yes,
What is an example of a named tuple
A Car named tuple with fields like Car.price and Car.horsepower would more clearly represent a car object than a list.
what does len(set) do
Finds the length of the set
set.union(set_a, set_b, set_c...)
Returns a new set containing all of the unique elements in all sets.
String type is a special construct known as a _____________
Sequence type
what are sequence type functions
They are built in functions that act on list and strings
How to add elements to list
Use the append list method is used to add new elements to a list
set.add(value)
adds value into the set
What does len(set1)
looks at the length of set1
Create a set using a set literal
num_Christians = {30, 32, 55}
Important note: an empty set can only be created by using
set ( )
What are the two ways to create a set
using the set ( ) function or by using a set literal
What is the main difference between a tuple and a list
A tuple is immutable whereas a list is mutable
How do you build a dictionary
Curly Braces { }
What is an unordered collection of unique elements
Set
What is this an example of "New" + "York"
String concatenation
What are sequence-type methods
They are methods built into the class definition of sequences like strings and list
What are so special about list
They are mutable, this means that a programmer can use methods to add and remove elements for ma list as needed
How do you find the the element with the smallest value in a list
min(list)
You can use negative indexes
-1 means last character in a string
How can you make a named tuple of a NBA and NFL player
1. Import named tuple 2. Player = namedtuple( 'Player', [ 'name', 'number', 'position', 'team'] 3 cam= Player( 'Cam Newton', '1', 'Quarterback', 'Carolina Panthers 4. lebron= Player( 'Lebron James', '23', 'Small Forward', 'Los Angeles lakers') 5. print (cam.name + '( ' # + cam.number + ')' + ' is a ' + cam.position + ' for the ' + cam.team + '.') 6. print (lebron.name + ' ( ' # + lebron.number + ' ) ' + 'is a ' + lebron.position + ' for the' + lebron.team + ' . ')
What are some qualities of a set
1. The elements are unordered: So the elements do not have a position or an index. 2. Elements are unique: No elements in the set share the same value
How do you access key entry
A key is placed in brakcets [ ] to acces a speicific dictionary entry.
How is a dictionary entry added
A new dictionary entry is added by using backets to specify the key: prices ['banana'] =1.49.
How do you create a tuple?
A tuple is created by creating a list with comma-separated values such as 5, 15, 20
what is associative relationships
Associative relationships exist between records that are conceptually close, but where the relationship is neither equivalent nor hierarchical. The most basic type of associative relationship is simply related to.
Why are list so important
Because it is essential for reducing the number of variables in a program by storing an entire collection of related variables
Why must you use an assignment statement when updating a string
Because strings are immutable objects
How come String concatenation doesn't contradict the immutability of strings
Because the result of a string concatenation is a new string; the original string is not altered.
How can you access the elements in a list?
By accessing the index function in the list Example: List[0] will show the value stored in element 0 of List1
How do you create a new named tuple
By importing the named-tuple package Example: Import namedtuple
How do you create a set
By using the function set( )
what does list.count(val)
Count the number of occurrences of the value val in list
Players = { 'Lionel Messi': 10, 'Cristiano Ronaldo': 7 }
Creates a dictionary called players with two keys: 'Lionel Messi' and 'Cristiano Ronaldo' and values 10 and 7
What is so important about a named tuple
It allows the programmer to define a new simple data type that consists of named attributes.
what is so important about the input function in python
It can be used to get the strings from the user
When would using a tuple be important
It can be used when element position, and not just relative ordering of elements is important
A set is similar to a list because
It does not matter what order the elements are in
How is a dict object created
It is created by using curly braces { } to surround the key:value pairs that comprises the dictionary contents
How do you create an empty dictionary
It is created with the expression player = { }
Set. intersection (set_a, set_b, set_c)
Returns a new set containing only the elements in common between set and all provided sets.
set_a.symmetric_difference(set_b)
Returns a set containing only elements that appear in exactly one of set_a or set_b
set.difference(set_a, set_b, set_c...)
Returns a set containing only the elements of set that are not found in any of the provided sets.
What does the set function accept
The set function accepts a sequence-type iterable object (list, tuple, string, etc )`