- Sep 2020
-
greenteapress.com greenteapress.com
-
Tuples are sequence of values. Tuples are immutable. Bracket operator indexes an element,slice,relational operators which work on lists aslo work on tuples. Built-in function divmod takes two arguments and returns a tuple of two values as quotient and remainder. If you have a sequence of values you can use * operator to pass it to asunction as multiplue arguments. Operation that collects multiple arguments into a tuple is known as 'gather'. Operation which makes a sequence behave like multiple arguments is called 'scatter'. Zip is a built-in function in the lists and tuples takes two or more sequences and interleaves them.If the sequences are not of same length, the result has the length of the shorter one. Tuples has built-in funtions sorted and reversed as it does not provide methods like sort an reverse which work as the same.
-
=>Dictionary is like a list , contains a collection of indices , called keys and their corresponding values. *Mapping : A dictionary represents a route/map from keys to values. The elements of a dictionary are never indexed with integers Python dictionaries use a data structure called a hashtable =>There are 3 ways to count how many times a letter appears and the best way is implementing through dictionaries Dictionaries have a method called "get" that takes a key and a default value. =>Traverses the keys of the dictionary is possible by us ing dictionary in a for statement. =>Lookup : The operation lookup means finding the corresponding value of a key in dictionary i.e., by value v = d[k]. Reverse lookup is nothing but finding the corresponding key of a value. The raise statement causes an exception; in<br> this case it causes a LookupError. You can provide a detailed error message as<br> an optional argument , when you raise an<br> exception. A reverse lookup is much slower than a forward lookup. =>Lists can be values in a dictionary, but they cannot be keys. In the inverted dictionary there are several hits's of having same vales which are in "List" see below example
* hist = histogram('parrot') >>> hist {'a' :1,'p':1,'r':2,'t':1,'o':1} >>>inverse = invert_dict(hist) >>>inverse {1: ['a','p','t','o'], 2: ['r']} **A hash is a function that takes a value and returns an integer. **if the keys are mutable, like lists, the dictionary wouldn’t work correctly.
=>A previously computed value that is stored for later use is called a memo. =>If a global variable refers to a mutable value, you can modify the value without declaring the variable: =>There are four types of debugging Scale down the input Check summaries and types Write self-checks Format the output
Tags
Annotators
URL
-