assign the number 8 to a variable eight.
set b equal to eight.
print b.
Write a boolean expression that will return true if x is 'a' or 'b' and false otherwise.
Write a boolean expression that returns true if and only if x is greater than ten and x is odd.
write a function that takes a parameter, n, and then returns n (unchanged).
write a function that takes a string, str_, and prints the string three times (once per line).
Write a program to prompt the user for hours and rate per hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
given a str1 = "Hello " and a str2 = "World", how can we concatenate (join together) str1 to str2?
given a str1 = "Hello", how can we index str1 to get the 'o'? Give two different ways.
given a str1 = "Hi", what operation can we do to the string to output "HiHiHiHi"?
make a list, lst, containing the numbers 0 through 10.
how can you check if 5 is in the lst (your expression should return True if 5 is in the lst, and False otherwise)
write a loop that prints each element from 0 through 9
write a loop that prints each element from your lst.
write a loop that prints out the element multiplied by two for each element from 0 through 9.
write a loop that will count from 0 to infinity.
write a statement that checks if a variable var is empty.
make a tuple containing a single element 'a'
make a tuple containing two elements, 'a' and 'b'
given a tuple containing 'Dicaprio' and 43, unpack the tuple with the variables name and age.
make an empty dictionary, dct.
add the key value pairs 'one'/1, 'two'/2, 'three'/3, 'four'/4
delete the key value pair 'two'/2.
write the following loops over dct:
why might we use a dictionary over a list of tuples?
Give a definition of the following:
For each of the following datatypes, write M for mutable or I for immutable, HO for homogeneous or HE for heterogenous: ex. blub: MHO (note blub is not a datatype we will be going over in this class)
what is the difference between printing output from a function vs. returning output from a function?
In [4]:
# How to make a set
a = {1, 2, 3}
type(a)
# Getting a set from a list
b = set([1, 2, 3])
a == b
# How to make a frozen set
a = frozenset({1, 2, 3})
# Getting a set from a list
b = frozenset([1, 2, 3])
Out[4]:
In [12]:
# Getting a set from a string
set("obtuse")
Out[12]:
In [6]:
# Getting a set from a dictionary
c = set({'a':1, 'b':2})
type(c)
Out[6]:
In [8]:
# Getting a set from a tuple
c = set(('a','b'))
type(c)
Out[8]:
In [7]:
# Sets do not contain duplicates
a = {1, 2, 2, 3, 3, 3, 3}
a
Out[7]:
In [10]:
# Sets do not support indexing (because they don't preserve order)
a[2]
In [ ]:
# Sets cannot be used for dictionary keys because they are mutable but frozensets can be used for dictionary keys.
In [14]:
# Adding elements to a set
s = set([12, 26, 54])
s.add(32)
s # If we try to add 32 again, nothing will happen
Out[14]:
In [15]:
# Updating a set using an iterable
s.update([26, 12, 9, 14]) # once again, note that adding duplicates has no effect.
s
Out[15]:
In [ ]:
# making copies of sets
s2 = s.copy()
In [16]:
32 in s
Out[16]:
In [17]:
55 in s
Out[17]:
In [18]:
s.issubset(set([32, 8, 9, 12, 14, -4, 54, 26, 19]))
Out[18]:
In [19]:
s.issuperset(set([9, 12]))
Out[19]:
In [20]:
# Note that subset and superset testing works on other iterables
s.issuperset([32, 9])
Out[20]:
In [ ]:
# We can also use <= and >= respectively for subset and superset testing
set([4, 5, 7]) <= set([4, 5, 7, 9])
set([9, 12, 15]) >= set([9, 12])
In [21]:
s = set([1,2,3,4,5,6])
s.pop()
Out[21]:
In [ ]:
s.remove(3)
In [ ]:
s.remove(9) # Removing an item that isn't in the set causes an error
In [22]:
s.discard(9) # discard is the same as remove but doesn't throw an error
In [24]:
s.clear() # removes everything
s
Out[24]:
In [25]:
s = set("blerg")
for char in s:
print(char)
In [27]:
s1 = set([4, 6, 9])
s2 = set([1, 6, 8])
s1.intersection(s2)
Out[27]:
In [28]:
s1 & s2
Out[28]:
In [29]:
s1.intersection_update(s2) # updates s1 with the intersection of s1 and s2
s1
Out[29]:
In [30]:
s1 = set([4, 6, 9])
s2 = set([1, 6, 8])
s1.union(s2)
Out[30]:
In [31]:
s1 | s2
Out[31]:
In [ ]:
# To update using union, simply use update
In [48]:
s1 = {8, 1, 6, 5, 3}
print(s1)
s2.update([7])
print(s2)
In [49]:
s1.symmetric_difference(s2)
Out[49]:
In [56]:
(s1 | s2) - (s1 & s2) == s1 ^ s2
Out[56]:
In [33]:
s1 ^ s2
Out[33]:
In [37]:
s1.symmetric_difference_update(s2)
s1
Out[37]:
In [35]:
s1 = set([4, 6, 9])
s2 = set([1, 6, 8])
s1.difference(s2)
Out[35]:
In [36]:
s1 - s2
Out[36]:
In [38]:
s1.difference_update(s2)
s1
Out[38]:
Create a set containing the frozenset fs, it should look like {frozenset({'cat', 2, 3, 1})}.
create two new sets a and b such that
In [ ]: