[Tutorial] Write Python in Jupyter Notebook | 在jupyter notebook上写Python代码
[Tutorial] Write Python in Jupyter Notebook | 在jupyter notebook上写Python代码
Jupyter Notebook used to be known as the IPython Notebook. It is an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media. We can get to know it on the offical site http://jupyter.org/. The Notebook has support for over 40 programming languages, including Python, R, Julia, and Scala.However, it is most used in Python.
Why
Python is an object-oriented language, but it is widely used in statistics and machine learning now. As we all know, Matlab was most used in these areas in early years. How can python do it? If we write Python just like Java, we can't know the value of each step.
Fortunately, Python is also a scripting language. There is no compilation process in python. We can dynamically create variables in a session. So How can we easily view each variable? It's what Jupyter Notebook does.
Install
Jupyter Notebook supports both python 2 and python 3.
First, the package of jupyter is already included in Anaconda.
If we don't want to install Anaconda.We can install it with pip easily.
Python 2 installed:
python -m pip install --upgrade pip
python -m pip install jupyter
Python 3 installed:
python3 -m pip install --upgrade pip
python3 -m pip install jupyter
Run Jupyter
Type in the command below, then Jupyter will run. We can see it on http://localhost:8888/tree.
jupyter notebook
Create Python Files
We can create a notebook, a text file, a folder and even a terminal with the New button.
Then we will see our own notebook in the browser. We can name it SteemJupyter. The extension of the notebook is .ipynb.
Run Python in the notebook
Then we can use it just like python shell. In a notebook, a code block is called a cell. All the code in a cell will be run together.In a cell, we can assign values and print the value of a variable.We can also define and run a function in it.
Jupyter is so powerful. I will introduce more interesting functions of it in the next post.
Jupyter Notebook曾经很长一段时间被称为Ipython Notebook。它是一个可以将代码执行、富文本、数学公式、画图和富媒体(视频等)结合在一起的交互式计算环境。我们可以在它的官网http://jupyter.org/ 进行详细了解。Jupyter Notebook已经能够支持40多种编程语言,包括Python、R、Julia和Scala等。但是应用的最广泛的还属Python。
为什么
Python是一门面向对象语言,但是它被广泛地应用在了统计和机器学习领域(现在又开始入侵我的优化算法领域了=_=)。众所周知,早年间Matlab才是这个领域的老大。Python是怎么做到的呢?如果我们像写Java一样写Python,我们不可能像Matlab一样知道每一步的值。
幸运的是,Python还是一门脚本语言,在Python中是没有编译这个过程的。所以我们可以在一个会话中动态地创建变量。所以我们怎么才能更好地查看自己创建的每一个变量呢?这时候Jupyter Notebook就应运而生了。
安装
Jupyter Notebook同时支持Python2和Python3。
首先,jupyter的包是被囊括在Anaconda环境中的,可以直接安装Anaconda环境。
如果不想安装Anaconda,则可以用过pip来简单安装。
Python 2 installed:
python -m pip install --upgrade pip
python -m pip install jupyter
Python 3 installed:
python3 -m pip install --upgrade pip
python3 -m pip install jupyter
运行Jupyter
输入下面的命令,Jupyter就会运行,我们可以在http://localhost:8888/tree 中看到它。
jupyter notebook
创建Python文件
在Jupyter Notebook中,我们通过这个New的按钮可以创建一个notebook,一个text文件,一个文件夹,甚至一个终端窗口。
然后我们就进入了Notebook界面。我们把这个Notebook命名为SteemJupyter。Notebook文件的扩展名是.ipynb。
在Notebook中运行Python
在这个界面我们就可以像写Python Shell一样了。在这里每个代码块被成为一个cell,一个cell中的代码每次都必须一起运行。在一个cell中,我们可以进行赋值和输出变量操作,也可以定义和运行一个函数。
Jupyter非常强大。在下篇文章中我会介绍它的其他有趣功能。
Posted on Utopian.io - Rewarding Open Source Contributors
你好!cn区点赞机器人 @cnbuddy 感谢你对cn区作出成长的贡献。 @cnbuddy 很开心能够和您一起为cn区快速发展,贡献自己的力量。我的主页会定期总结cn区活动动态,欢迎围观。欢迎关注我们的大股东 @skenan,并注册使用由其开发的 CNsteem.com。如果不想再收到我的留言,请回复“取消”。