Coggle requires JavaScript to display documents.
if x < 0:... x = 0... print('Negative changed to zero')... elif x == 0:... print('Zero')... elif x == 1:... print('Single')... else:... print('More')...
----------- ---------- ---------- | | | | Positional or keyword | | - Keyword only -- Positional only
f = make_incrementor(42)f(0)42f(1)43
def my_function():... """Do nothing, but document it....... No, really, it doesn't do anything.... """... pass...print(my_function.doc)
def f(ham: str, eggs: str = 'eggs') -> str:... print("Annotations:", f.annotations)... print("Arguments:", ham, eggs)... return ham + ' and ' + eggs...f('spam')Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}Arguments: spam eggs'spam and eggs'
stack = [3, 4, 5]stack.append(6)stack.append(7)stack[3, 4, 5, 6, 7]stack.pop()7stack[3, 4, 5, 6]stack.pop()6stack.pop()5stack[3, 4]
queue.append("Terry") # Terry arrivesqueue.append("Graham") # Graham arrivesqueue.popleft() # The first to arrive now leaves'Eric'queue.popleft() # The second to arrive now leaves'John'queue # Remaining queue in order of arrivaldeque(['Michael', 'Terry', 'Graham'])
t(12345, 54321, 'hello!')// Tuples may be nested:... u = t, (1, 2, 3, 4, 5)u((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))// Tuples are immutable:... t[0] = 88888Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment// but they can contain mutable objects:... v = ([1, 2, 3], [3, 2, 1])v([1, 2, 3], [3, 2, 1])
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}print(basket) # show that duplicates have been removed{'orange', 'banana', 'pear', 'apple'}'orange' in basket # fast membership testingTrue'crabgrass' in basketFalse -# Demonstrate set operations on unique letters from two words...a = set('abracadabra')b = set('alacazam')a # unique letters in a{'a', 'r', 'b', 'c', 'd'}a - b # letters in a but not in b{'r', 'd', 'b'}a | b # letters in a or b or both{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}a & b # letters in both a and b{'a', 'c'}a ^ b # letters in a or b but not both{'r', 'd', 'b', 'm', 'z', 'l'}
a = {x for x in 'abracadabra' if x not in 'abc'}a{'r', 'd'}
knights = {'gallahad': 'the pure', 'robin': 'the brave'}for k, v in knights.items():... print(k, v)...gallahad the purerobin the brave
for i, v in enumerate(['tic', 'tac', 'toe']):... print(i, v)...0 tic1 tac2 toe
questions = ['name', 'quest', 'favorite color']answers = ['lancelot', 'the holy grail', 'blue']for q, a in zip(questions, answers):... print('What is your {0}? It is {1}.'.format(q, a))...What is your name? It is lancelot.What is your quest? It is the holy grail.What is your favorite color? It is blue.