第一章 Jupyter Notebook基础实操

1 Jupyter Notebook 基础

1.1 基本概念

每一个有代码的格子在jupyter中叫做cell,比如下方这个cell中声明了两个变量,并输出其相加的值

python 复制代码
a = 1
b = 2
print(a+b)
复制代码
3

我们可以通过cell左边框的颜色来判断当前选中的cell,以及当前的状态,

比如下图表示我们选中了对应的cell,而旁边的蓝色代表当前处于命令模式

而当cell旁边的颜色为绿色时,这表明当前处于编辑模式,我们编辑修改cell中的内容,处于编辑模式时,可以看到我们的光标在闪烁


当我们选中某个cell并且处于命令模式时,就可以采取多种方式来运行选中的cell,常用的几种方式有:

  • 选中cell然后点击界面上的按钮,
  • 使用Ctrl+Enter快捷键,这个快捷键会运行选中的cell
  • 使用Shift+Enter快捷键,这个快捷键会运行选中的cell并在cell下方创建一个空的cell

1.2 cell的基本操作

增加和删除cell

在命令模式下,可以通过以下按键来新增和删除以及恢复cell:

  • a在当前cell上方增加一个cell
  • b在当前cell下方增加一个cell
  • dd删除当前的cell
  • z恢复通过dd删除的cell

python 复制代码
current

复制和粘贴cell

同样在命令模式下,可以通过cx以及v来进行复制、剪切以及粘贴等操作:

  • 命令模式下按c可以复制当前的cell
  • 命令模式下按x可以剪切当前的cell
  • 命令模式下按v可以在当前cell下方粘贴cell,而V可以当前cell上方粘贴cell
python 复制代码
code

转换cell类型

在命令模式可以通过命令来快速转换cell的类型:

  • code: cell中的内容为代码,运行cell就会运行其中的代码,出现对应的输出,命令模式下按y切换,新建cell默认就是code模式
  • markdown:用来编写markdown格式的文档,运行后就会出现按照markdown格式渲染的文字,命令模式下按m切换
python 复制代码
print("code type cell")
复制代码
code type cell

markdown type

bold

1.3 其他操作

切换输出

当一些cell的输出非常长时,可以通过一些快捷键来设置其是否显示或者切换显示效果:

  • 命令模式按o可以隐藏/显示对应cell的输出
  • 命令模式按O可以切换输出的显示方式,是否滚动
python 复制代码
print("Python is an easy to learn, \npowerful programming language. \nIt has efficient high-level data structures and a simple but effective approach to \nobject-oriented programming. Python's elegant syntax and dynamic typing, \ntogether with its interpreted nature, make it an ideal language for \nscripting and rapid application development in many \nareas on most platforms.")
print("The Python interpreter and \nthe extensive standard library are \nfreely available in source or binary form for all major platforms from the Python web site, \nhttps://www.python.org/, and may be freely distributed. The same site also contains distributions of \nand pointers to many free third party Python modules, programs and tools, \nand additional documentation.")
print("The Python interpreter is \neasily extended with new functions and \ndata types implemented in C or C++ (or other languages callable from C). \nPython is also suitable as an extension language for customizable applications.")
print("This tutorial introduces the reader \ninformally to the basic concepts and features \nof the Python language and system. It helps to have a \nPython interpreter handy for hands-on experience, \nbut all examples are self-contained, so the tutorial can be read off-line as well.")
复制代码
Python is an easy to learn, 
powerful programming language. 
It has efficient high-level data structures and a simple but effective approach to 
object-oriented programming. Python's elegant syntax and dynamic typing, 
together with its interpreted nature, make it an ideal language for 
scripting and rapid application development in many 
areas on most platforms.
The Python interpreter and 
the extensive standard library are 
freely available in source or binary form for all major platforms from the Python web site, 
https://www.python.org/, and may be freely distributed. The same site also contains distributions of 
and pointers to many free third party Python modules, programs and tools, 
and additional documentation.
The Python interpreter is 
easily extended with new functions and 
data types implemented in C or C++ (or other languages callable from C). 
Python is also suitable as an extension language for customizable applications.
This tutorial introduces the reader 
informally to the basic concepts and features 
of the Python language and system. It helps to have a 
Python interpreter handy for hands-on experience, 
but all examples are self-contained, so the tutorial can be read off-line as well.

切换行号

另一个比较实用的操作是切换显示cell的行号:

  • 在命令模式下可以按l切换当前cell是否显示行号
  • 在命令模式下可以按L切换所有的cell是否显示行号

line1

line2

重启内核

当我们想要重启当前notebook的内核时(重启内核会清空所有变量),可以使用下面两种办法:

  • 在命令模式连着输入两个0,即可重启内核
  • 在页面上点击如下位置,同时也可以点击1处,可以直接重启内核然后运行所有的cell

1.2 查看库的帮助信息

在jupyter中编写代码时,可以通过jupyter提供的?来快速查看库,变量以及其他内容的信息

运行上方的代码,就会出现一个弹出框来显示os库的信息。

python 复制代码
import os
os?

或者通过将鼠标移动函数的两个括号中间,然后输入Shift+Tab来查看对应的注释信息

python 复制代码
os.chdir()
相关推荐
人道领域2 小时前
2026技术展望】Python与AI的深度融合:从“能用”到“好用”的质变之年
人工智能·python·大模型·agent
chushiyunen2 小时前
python异常模拟工具类(异常生成工具类)
开发语言·python
测试19982 小时前
python+selenium 定位到元素,无法点击的解决方法
自动化测试·软件测试·python·selenium·测试工具·测试用例·压力测试
Deadly_Bug_GF2 小时前
PID 控制器的核心原理
开发语言·python
狗都不学爬虫_2 小时前
JS逆向 - Akamai阿迪达斯(三次) 补环境、纯算
javascript·爬虫·python·网络爬虫·wasm
深蓝海拓2 小时前
基于QtPy (PySide6) 的PLC-HMI工程项目(一)使用自定义socket协议的基本方法
python·plc
秃头狂魔3 小时前
【HOT100】DAY2
python·算法
程序员三藏3 小时前
接口自动化测试思路和实战:编写线性测试脚本实战
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
丁当粑粑3 小时前
Pydantic的主要用法
python