IPython vs Python¶
Reference: https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html
Accessing help¶
As IPython is mostly an interactive shell, the question mark is a simple shortcut to get help. A question mark alone will bring up the IPython help:
?
IPython -- An enhanced Interactive Python
=========================================
IPython offers a fully compatible replacement for the standard Python
interpreter, with convenient shell features, special commands, command
history mechanism and output results caching.
At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.
GETTING HELP
------------
Within IPython you have various way to access help:
? -> Introduction and overview of IPython's features (this screen).
object? -> Details about 'object'.
object?? -> More detailed, verbose information about 'object'.
%quickref -> Quick reference of all IPython specific syntax and magics.
help -> Access Python's own help system.
If you are in terminal IPython you can quit this screen by pressing `q`.
MAIN FEATURES
-------------
* Access to the standard Python help with object docstrings and the Python
manuals. Simply type 'help' (no quotes) to invoke it.
* Magic commands: type %magic for information on the magic subsystem.
* System command aliases, via the %alias command or the configuration file(s).
:
A single question mark before, or after an object available in current namespace will show help relative to this object:
import os
os?
Type: module
String form: <module 'os' from '/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py'>
File: /usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py
Docstring:
OS routines for NT or Posix depending on what system we're on.
This exports:
- all functions from posix or nt, e.g. unlink, stat, etc.
- os.path is either posixpath or ntpath
- os.name is either 'posix' or 'nt'
- os.curdir is a string representing the current directory (always '.')
- os.pardir is a string representing the parent directory (always '..')
- os.sep is the (or a most common) pathname separator ('/' or '\\')
- os.extsep is the extension separator (always '.')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)
Programs that import and use 'os' stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
A double question mark will try to pull out more information about the object, and if possible display the python source code of this object.
def two_times(a):
b = a * 2
return b
two_times??
Signature: two_times(a)
Docstring: <no docstring>
Source:
def two_times(a):
b = a * 2
return b
File: ~/Documents/GitHub/infosec-jupyter-book/docs/getting-started/<ipython-input-5-9fc03334c94b>
Type: function
print??
Shell Assignment¶
When doing interactive computing it is common to need to access the underlying shell. This is doable through the use of the exclamation mark !
(or bang).
!ls
!pwd
/private/var/folders/lt/s5djww754m90lmrtcf_077xm0000gn/T/tmprxsj0g3r
Magics¶
Magics function are often present in the form of shell-like syntax, but are under the hood python function. The syntax and assignment possibility are similar to the one with the bang (!)
syntax, but with more flexibility and power. Magic function start with a percent sign (%)
or double percent (%%)
.
%lsmagic
Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
%time
for i in range(5, 8):
print(i, i ** 2)
CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 5.25 µs
5 25
6 36
7 49