Ta sẽ sử dụng notebooks để code Python. Lý do:
Thường ta sử dụng 2 chế độ cells:
# dòng code mà ai cũng sẽ thử
print("Hello, World!")
Hello, World!
Tập hợp các lệnh tiện ích cho việc phân tích dữ liệu trong Notebooks. Bạn có thể liệt kê danh sách magic commands thông qua lệnh %lsmagic
%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.
# upload code lên https://pastebin.com/ và trả về URL.
# Đây là nơi lưu trữ snippets trực tuyến, bạn có thể dùng URL bên dưới để chia sẻ code của mình
%pastebin file.py
'http://dpaste.com/0KPTZ7Y'
# dùng notebook thay vì inline sẽ cho phép bạn thực hiện zoom và thay đổi kích thước biểu đồ của mình
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([[0, 0], [1, 1]], linewidth=2)
plt.show()
# dùng notebook thay vì inline sẽ cho phép bạn thực hiện zoom và thay đổi kích thước biểu đồ của mình
%matplotlib notebook
import matplotlib.pyplot as plt
plt.plot([[0, 0], [1, 1]], linewidth=2)
plt.show()
x = [1, 2, 3]
y = 2
z = 5
result = y + z
print(result)
result2 = x + y
print(result2)
7
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-9d8c4c27419e> in <module> 5 result = y + z 6 print(result) ----> 7 result2 = x + y 8 print(result2) TypeError: can only concatenate list (not "int") to list
%debug
> <ipython-input-6-9d8c4c27419e>(7)<module>() 4 5 result = y + z 6 print(result) ----> 7 result2 = x + y 8 print(result2) ipdb> x [1, 2, 3] ipdb> y 2 ipdb> result 7 ipdb> y = [1, 2] ipdb> result2 = x + y ipdb> result2 [1, 2, 3, 1, 2] ipdb> q
# with print
employee_records = {"Emp ID": "101", "Emp Name": "Tom",
"Project IDs": {"P1": 1308, "P2": "A104", "P4": 2}}
print(employee_records)
{'Emp ID': '101', 'Emp Name': 'Tom', 'Project IDs': {'P1': 1308, 'P2': 'A104', 'P4': 2}}
# with pretty print
import pprint
employee_records = {"Emp ID": "101", "Emp Name": "Tom",
"Project IDs": {"P1": 1308, "P2": "A104", "P4": 2}}
pprint.pprint(employee_records)
{'Emp ID': '101', 'Emp Name': 'Tom', 'Project IDs': {'P1': 1308, 'P2': 'A104', 'P4': 2}}
Blue Alert Box: info
<div class="alert alert-block alert-info">
<b>Tip:</b> Use blue boxes (alert-info) for tips and notes.
If it’s a note, you don’t have to include the word “Note”.
</div>
Yellow Alert Box: Warning
<div class="alert alert-block alert-warning">
<b>Example:</b> Yellow Boxes are generally used to include additional examples or mathematical formulas.
</div>
Green Alert Box: Success
<div class="alert alert-block alert-success">
Use green box only when necessary like to display links to related content.
</div>
Red Alert Box: Danger
<div class="alert alert-block alert-danger">
It is good to avoid red boxes but can be used to alert users to not delete some important part of code etc.
</div>