# Or, or not def or_contains(a, b): return True in (a, b) def or_add(a, b): return (a + b) >= 1 def or_cond(a, b): return True if b else a # Generator expressions bugg = 'University of California Berkeley Undergraduate Graphics Group' def iscap(s): return len(s) > 0 and s[0].isupper() def acronym_gen(name): """Return a tuple of the letters that form the acronym for name. >>> acronym_gen('University of California Berkeley') ('U', 'C', 'B') """ return tuple(w[0] for w in name.split() if iscap(w)) # List mutation suits = ['coin', 'string', 'myriad'] # A list literal suits.pop() # Removes and returns the final element suits.remove('string') # Removes the first element that equals the argument suits.append('cup') # Add an element to the end suits.extend(['sword', 'club']) # Add all elements of a list to the end suits[2] = 'spade' # Replace an element suits suits[0:2] = ['heart', 'diamond'] # Replace a slice # Identity and equality suits = ['heart', 'diamond'] s = suits t = list(suits) suits += ['spade', 'club'] t[0] = suits print(t) suits.append('Joker') print(t) t[0].pop() print(s) # Dictionaries numerals = {'I': 1.0, 'V': 5, 'X': 10} numerals['X'] numerals['I'] = 1 numerals['L'] = 50 numerals sum(numerals.values()) dict([(3, 9), (4, 16), (5, 25)]) numerals.get('A', 0) numerals.get('V', 0) {x: x*x for x in range(3,6)} # {[1]: 2} # {1: [2]} # {([1], 2): 3} # {tuple([1, 2]): 3}