PythonDataScience_00 - Jake VanderPlas Flashcards

1
Q

Wie kann ich die Dokumentation von der len-Funktion aufrufen?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Wie kann ich die Dokumentation und weitere wichtige Infos aufrufen?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Notation for multi-line strings?

A

Mit 3 Anführungszeichen

In [6]: def square(a):

….: “"”Return the square of a.”””

….: return a ** 2

….:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wie komme ich zur Dokumentation und dem Quellcode?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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:

A

In [10]: from itertools import co<tab></tab>

combinations compress

combinations_with_replacement count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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:

A

In [10]: str.*find*?

str. find
str. rfind

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Move cursor to the beginning of the line

A

Ctrl-a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Move cursor to the end of the line

A

Ctrl-e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Delete next character in line

A

Ctrl-d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Cut text from cursor to end of line

A

Ctrl-k

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Cut text from beginning of line to cursor

A

Ctrl-u

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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:

A

In [8]: %timeit L = [n ** 2 for n in range(1000)]

1000 loops, best of 3: 325 µs per loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The standard Python shell contains just one simple shortcut for accessing previous output;

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

A

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);

17
Q

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:

A

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

1: import math
2: math.sin(2)
3: math.cos(2)
4: print(In)

18
Q
A