Write Python in Jupyter Notebook(revised edition)| 在jupyter notebook上写Python代码(修正版)

in #utopian-io6 years ago (edited)

jupyterlogo

What Will I Learn?

  • You will learn what is jupyter
  • You will learn why we use jupyter
  • You will learn basic usage of jupyter

Requirements

  • have experience in python
  • have installed python and pip

Difficulty

  • Intermediate

Tutorial Contents

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

01

Create Python Files

We can create a notebook, a text file, a folder and even a terminal with the New button.
02
03
Then we will see our own notebook in the browser. We can name it SteemJupyter. The extension of the notebook is .ipynb.
04

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.
05
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

01

创建Python文件

在Jupyter Notebook中,我们通过这个New的按钮可以创建一个notebook,一个text文件,一个文件夹,甚至一个终端窗口。
02
03
然后我们就进入了Notebook界面。我们把这个Notebook命名为SteemJupyter。Notebook文件的扩展名是.ipynb。
04

在Notebook中运行Python

在这个界面我们就可以像写Python Shell一样了。在这里每个代码块被成为一个cell,一个cell中的代码每次都必须一起运行。

在一个cell中,我们可以进行赋值和输出变量操作,也可以定义和运行一个函数。
05
Jupyter非常强大。在下篇文章中我会介绍它的其他有趣功能。



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

You can contact us on Discord.
[utopian-moderator]

Congratulations @mucyoung! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @mucyoung! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.029
BTC 62772.50
ETH 3434.98
USDT 1.00
SBD 2.55