In [1]:
help(len)
In [2]:
len?
In [3]:
# ? works for many kind of objects
L = [1, 2, 3, 2]
In [4]:
L?
In [5]:
# works for function
L.insert?
In [6]:
# even function user created.
def square(x):
'''Return square of the value'''
return x ** 2
In [7]:
square?
In [8]:
#reach the source code
square??
In [9]:
len??
In [10]:
L.count(2)
Out[10]:
In [11]:
# press tab after import to see all possible imports
#from itertools import
In [13]:
# press tab after import to see all library in the system
#import
In [14]:
# list all objects ends with Warning
*Warning?
In [15]:
# string method contains the find word in it
str.*find*?
1- Line magics denoted by %
2- Cell magics indicated by %%
In [16]:
# run the code that you saved in the text editor in the notebook
%run myScript.py
In [17]:
%%timeit
L = [n ** 2 for n in range(1000)]
In [18]:
%%timeit
L = []
for i in range(1000):
L.append(i ** 2)
In [19]:
# help on magic functions
%timeit?
In [20]:
#general info about magic functions
%magic
In [21]:
# available magic functions
%lsmagic
Out[21]:
In [22]:
In
Out[22]:
In [23]:
Out
Out[23]:
In [26]:
In[10]
Out[26]:
In [28]:
# _ prints the previous output
_
Out[28]:
In [29]:
3 + 4
Out[29]:
In [31]:
# suppress the output with ;
3 + 4 ;
In [32]:
%history?
In [36]:
%history -n 30-32
In [37]:
%rerun
In [38]:
!pwd
In [39]:
directory = !pwd
directory
Out[39]:
In [43]:
# use shell command in the notebook. automagic has to be on.
In [42]:
ls
In [48]:
def func1(a,b):
return a / b
def func2(x):
a = x
b = x - 1
return func1(a,b)
In [50]:
func2(1)
In [51]:
%xmode Plain
In [52]:
func2(1)
In [53]:
%xmode Verbose
In [54]:
func2(1)
In [55]:
%debug
In [56]:
%pdb on
In [57]:
%timeit sum(range(100))
In [58]:
#profiler
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
total += sum(L)
return total
In [60]:
%prun sum_of_lists(1000000)
In [63]:
#line by line profiler
!pip install line_profiler
In [65]:
%load_ext line_profiler
In [66]:
%lprun -f sum_of_lists sum_of_lists(5000)
In [68]:
#memory porfiler
!pip install memory_profiler
In [69]:
%load_ext memory_profiler
In [70]:
%memit sum_of_lists(1000000)
In [71]:
pwd
Out[71]:
In [73]:
%%file mprun_demo.py
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
total += sum(L)
return total
In [75]:
from mprun_demo import sum_of_lists
%mprun -f sum_of_lists sum_of_lists(10000)
In [76]:
import this
In [ ]: