Python 3 (ipykernel)
print('你好 Python')
你好 Python
print('test')
test
for i in range(10):
print(i)
0 1 2 3 4 5 6 7 8 9
x = 3.14
%run ./My_Test.py
--------------------------------------------------------------------------- OSError Traceback (most recent call last) File D:\py\Python3\lib\site-packages\IPython\core\magics\execution.py:701, in ExecutionMagics.run(self, parameter_s, runner, file_finder) 700 fpath = arg_lst[0] --> 701 filename = file_finder(fpath) 702 except IndexError as e: File D:\py\Python3\lib\site-packages\IPython\utils\path.py:90, in get_py_filename(name) 89 return py_name ---> 90 raise IOError("File `%r` not found." % name) OSError: File `'./My_Test.py'` not found. The above exception was the direct cause of the following exception: Exception Traceback (most recent call last) Cell In[5], line 1 ----> 1 get_ipython().run_line_magic('run', './My_Test.py') File D:\py\Python3\lib\site-packages\IPython\core\interactiveshell.py:2417, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth) 2415 kwargs['local_ns'] = self.get_local_scope(stack_depth) 2416 with self.builtin_trap: -> 2417 result = fn(*args, **kwargs) 2419 # The code below prevents the output from being displayed 2420 # when using magics with decodator @output_can_be_silenced 2421 # when the last Python token in the expression is a ';'. 2422 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False): File D:\py\Python3\lib\site-packages\IPython\core\magics\execution.py:712, in ExecutionMagics.run(self, parameter_s, runner, file_finder) 710 if os.name == 'nt' and re.match(r"^'.*'$",fpath): 711 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"') --> 712 raise Exception(msg) from e 713 except TypeError: 714 if fpath in sys.meta_path: Exception: File `'./My_Test.py'` not found.
# %load ./My_Test.py
print('大家好,很高兴认识你')
def my_print(x):
print(x*2)
my_print('Python好,大家好')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 my_print('Python好,大家好') NameError: name 'my_print' is not defined
from My_Test import my_print
my_print('xixi')
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[7], line 1 ----> 1 from My_Test import my_print 2 my_print('xixi') ModuleNotFoundError: No module named 'My_Test'
# notebook对同一个文件只会导入一次
%run ./My_Test.py
--------------------------------------------------------------------------- OSError Traceback (most recent call last) File D:\py\Python3\lib\site-packages\IPython\core\magics\execution.py:701, in ExecutionMagics.run(self, parameter_s, runner, file_finder) 700 fpath = arg_lst[0] --> 701 filename = file_finder(fpath) 702 except IndexError as e: File D:\py\Python3\lib\site-packages\IPython\utils\path.py:90, in get_py_filename(name) 89 return py_name ---> 90 raise IOError("File `%r` not found." % name) OSError: File `'./My_Test.py'` not found. The above exception was the direct cause of the following exception: Exception Traceback (most recent call last) Cell In[8], line 2 1 # notebook对同一个文件只会导入一次 ----> 2 get_ipython().run_line_magic('run', './My_Test.py') File D:\py\Python3\lib\site-packages\IPython\core\interactiveshell.py:2417, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth) 2415 kwargs['local_ns'] = self.get_local_scope(stack_depth) 2416 with self.builtin_trap: -> 2417 result = fn(*args, **kwargs) 2419 # The code below prevents the output from being displayed 2420 # when using magics with decodator @output_can_be_silenced 2421 # when the last Python token in the expression is a ';'. 2422 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False): File D:\py\Python3\lib\site-packages\IPython\core\magics\execution.py:712, in ExecutionMagics.run(self, parameter_s, runner, file_finder) 710 if os.name == 'nt' and re.match(r"^'.*'$",fpath): 711 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"') --> 712 raise Exception(msg) from e 713 except TypeError: 714 if fpath in sys.meta_path: Exception: File `'./My_Test.py'` not found.
my_print('Python好,大家好')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 1 ----> 1 my_print('Python好,大家好') NameError: name 'my_print' is not defined
%timeit li = [i**2 for i in range(1000)]
288 µs ± 16.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%timeit li = [i**2 for i in range(1000000)]
301 ms ± 8.75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit li = [i**2 for i in range(10)]
2.9 µs ± 312 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# %timeit 后面只跟一句代码
# 测试代码块 用%%timit
%%timeit
li = []
for i in range(1000):
li.append(i**2)
# 在python中使用列表生成式比for高效
346 µs ± 14.1 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
# %time 只会测量一次代码的执行时间
%time li = [i**2 for i in range(1000)]
CPU times: total: 0 ns Wall time: 0 ns
%%time
li = []
for i in range(1000):
li.append(i**2)
CPU times: total: 15.6 ms Wall time: 1 ms
import random
li = [random.random() for i in range(100000)]
%timeit li.sort()
1.18 ms ± 389 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
li = [random.random() for i in range(100000)]
%time li.sort()
CPU times: total: 31.2 ms Wall time: 27 ms
%time li.sort()
CPU times: total: 0 ns Wall time: 4 ms
%time li.sort()
CPU times: total: 0 ns Wall time: 5 ms
%time li.sort()
CPU times: total: 0 ns Wall time: 5 ms
%%html
<div class='mytest' style='color:red'>html content</div>
%%js
document.querySelector('.mytest').innerHTML='我成功了'
%%writefile haha.py
import random
li = [random.random() for i in range(100000)]
%timeit li.sort()
Writing haha.py
import numpy as np
a = np.array([1, 2, 3])
type(a)
numpy.ndarray
a.shape
(3,)
b = np.array([[1,2,3],
[4,5,6]])
b.shape
(2, 3)
a = np.zeros((5,5,4))
a
array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
a
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
b = a[:2, 1:3]
b
array([[2, 3], [6, 7]])
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
x + y
array([[ 6., 8.], [10., 12.]])
np.add(x, y)
array([[ 6., 8.], [10., 12.]])