#!/usr/bin/env python # coding: utf-8 # # Jupyter notebooks # # Ta sẽ sử dụng notebooks để code Python. Lý do: # # - Notebooks là môi trường lập trình khá trực quan. # - Code đến đâu kết quả hiển thị đến đó. # - Không cần chạy lại chương trình từ đầu đến cuối, chỉ cần chạy lại cell cần điều chỉnh. # - Giúp mọi người theo dõi code và visualization cùng một lúc. # # Thường ta sử dụng 2 chế độ cells: # # - text cells: dùng để ghi chú headers và paragraph kể cả LaTeX format. # - code cells: dùng để code # # # In[1]: # dòng code mà ai cũng sẽ thử print("Hello, World!") # # Magic commands # # 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` # In[2]: get_ipython().run_line_magic('lsmagic', '') # In[3]: # 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 get_ipython().run_line_magic('pastebin', 'file.py') # In[3]: # 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 get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt plt.plot([[0, 0], [1, 1]], linewidth=2) plt.show() # In[5]: # 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 get_ipython().run_line_magic('matplotlib', 'notebook') import matplotlib.pyplot as plt plt.plot([[0, 0], [1, 1]], linewidth=2) plt.show() # # Debug code # In[6]: x = [1, 2, 3] y = 2 z = 5 result = y + z print(result) result2 = x + y print(result2) # In[7]: get_ipython().run_line_magic('debug', '') # # Xuất nội dung và ghi chú hiệu quả # In[8]: # with print employee_records = {"Emp ID": "101", "Emp Name": "Tom", "Project IDs": {"P1": 1308, "P2": "A104", "P4": 2}} print(employee_records) # In[9]: # 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) # **Blue Alert Box: info** # # ``` #
# Tip: Use blue boxes (alert-info) for tips and notes. # If it’s a note, you don’t have to include the word “Note”. #
# ``` # #
# Tip: Use blue boxes (alert-info) for tips and notes. # If it’s a note, you don’t have to include the word “Note”. #
# **Yellow Alert Box: Warning** # # ``` #
# Example: Yellow Boxes are generally used to include additional examples or mathematical formulas. #
# ``` # #
# Example: Yellow Boxes are generally used to include additional examples or mathematical formulas. #
# **Green Alert Box: Success** # # ``` #
# Use green box only when necessary like to display links to related content. #
# ``` # #
# Use green box only when necessary like to display links to related content. #
# **Red Alert Box: Danger** # # ``` #
# It is good to avoid red boxes but can be used to alert users to not delete some important part of code etc. #
# ``` # #
# It is good to avoid red boxes but can be used to alert users to not delete some important part of code etc. #
# # Reference # # https://www.kdnuggets.com/2019/07/10-simple-hacks-speed-data-analysis-python.html