PythonDataScience_00 - Jake VanderPlas Flashcards Preview

Machine Learning - Jibun > PythonDataScience_00 - Jake VanderPlas > Flashcards

Flashcards in PythonDataScience_00 - Jake VanderPlas Deck (18)
Loading 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(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:

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

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*?




Move cursor to the beginning of the line



Move cursor to the end of the line



Delete next character in line



Cut text from cursor to end of line



Cut text from beginning of line to cursor



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(_)


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(__)


In [11]: print(___)



Sometimes you might wish to suppress the output of a statement (this is perhaps most common with the plotting commands that we'll explore in Introduction to Matplotlib). Or maybe the command you're executing produces a result that you'd prefer not like to store in your output history, perhaps so that it can be deallocated when other references are removed. 

The easiest way to suppress the output of a command is to add a semicolon to the end of the line:

In [14]: math.sin(2) + math.cos(2);


For accessing a batch of previous inputs at once, the %history magic command is very helpful. Here is how you can print the first four inputs:

In [16]: %history -n 1-4

1: import math

2: math.sin(2)

3: math.cos(2)

4: print(In)