PythonDataScience_00 - Jake VanderPlas Flashcards
Wie kann ich die Dokumentation von der len-Funktion aufrufen?
In [1]: help(len)
Help on built-in function len in module builtins:
len(…)
len(object) -> integer
Return the number of items of a sequence or mapping.
Wie kann ich die Dokumentation und weitere wichtige Infos aufrufen?
Mit ?
In [2]: len?
Type: builtin_function_or_method
String form: <built-in></built-in>
Namespace: Python builtin
Docstring: l
en(object) -> integer
Return the number of items of a sequence or mapping
Notation for multi-line strings?
Mit 3 Anführungszeichen
In [6]: def square(a):
….: “"”Return the square of a.”””
….: return a ** 2
….:
Wie komme ich zur Dokumentation und dem Quellcode?
Mit ?? Fragezeichen
If you play with this much, you’ll notice that sometimes the ?? suffix doesn’t display any source code: this is generally because the object in question is not implemented in Python, but in C or some other compiled extension language. If this is the case, the ?? suffix gives the same output as the ? suffix.
Tab completion is also useful when importing objects from packages. Here we’ll use it to find all possible imports in the itertools package that start with co:
In [10]: from itertools import co<tab></tab>
combinations compress
combinations_with_replacement count
Tab completion is useful if you know the first few characters of the object or attribute you’re looking for, but is little help if you’d like to match characters at the middle or end of the word.
For this use-case, IPython provides a means of wildcard matching for names using the * character.
For example, we can use this to list every object in the namespace that ends with Warning:
In [10]: *Warning?
BytesWarning RuntimeWarning DeprecationWarning SyntaxWarning FutureWarning UnicodeWarning ImportWarning UserWarning PendingDeprecationWarning Warning ResourceWarning
Similarly, suppose we are looking for a string method that contains the word find somewhere in its name. We can search for it this way:
In [10]: str.*find*?
str. find
str. rfind
Move cursor to the beginning of the line
Ctrl-a
Move cursor to the end of the line
Ctrl-e
Delete next character in line
Ctrl-d
Cut text from cursor to end of line
Ctrl-k
Cut text from beginning of line to cursor
Ctrl-u
Another example of a useful magic function is %timeit, which will automatically determine the execution time of the single-line Python statement that follows it. For example, we may want to check the performance of a list comprehension:
In [8]: %timeit L = [n ** 2 for n in range(1000)]
1000 loops, best of 3: 325 µs per loop
We’ve imported the built-in math package, then computed the sine and the cosine of the number 2. These inputs and outputs are displayed in the shell with In/Out labels, but there’s more–IPython actually creates some Python variables called In and Out that are automatically updated to reflect this history:
In [1]: import math
In [2]: math.sin(2)
Out[2]: 0.9092974268256817
In [3]: math.cos(2)
Out[3]: -0.4161468365471424
In [4]: print(In)
[’’, ‘import math’, ‘math.sin(2)’, ‘math.cos(2)’, ‘print(In)’]
In [5]: Out
Out[5]: {2: 0.9092974268256817, 3: -0.4161468365471424}
The standard Python shell contains just one simple shortcut for accessing previous output;
the variable _ (i.e., a single underscore) is kept updated with the previous output; this works in IPython as well:
In [9]: print(_)
1.0
But IPython takes this a bit further—you can use a double underscore to access the second-to-last output, and a triple underscore to access the third-to-last output (skipping any commands with no output):
In [10]: print(__)
-0.4161468365471424
In [11]: print(___)
0.9092974268256817