数据科学彻底改变了这个世界¶
课程目标¶
具有Python编程方面的背景
介绍学术界/工业界流行的数据分析工具
学习如何阅读文档并快速使用新工具
学习基本的分布式计算框架、深度学习框架
These tools will be obsolete some day ... ... but not your ability to learn new frameworks and solve problems!
课程内容¶
- Python介绍
- Data types, functions, Jupyter, classes, objects
- 数值计算与数据可视化
- Numpy, scipy, matplotlib
- 结构化数据处理
- Regular expressions, retrieving web data, SQL, Python pandas, APIs
- 大数据和并行编程
- Basics of the UNIX command line, Hadoop, Spark, PyTorch
学习第一步¶
- 选择一个Python IDE或编辑器
- 文本编辑器,Jupyter、Pycharm或VS Code
- 熟悉jupyter
- 熟悉Pycharm
- 熟悉VS Code
本课程将全部使用Python3
Python简介¶
Python是一种动态类型、解释型编程语言
- 由Guido van Rossum于1991年创建
- 由Python软件基金会维护
- 动态类型:在许多编程语言中,当你声明一个变量时,必须指定变量的类型(例如:int、double、Boolean,string)。而在Python中则不需要这样做。
- 有些编程语言(如C/C++和Java)是编译型的:编写代码,然后通过编译生成一个可运行的程序。相比之下,Python是解释型的:一个称为解释器的程序逐行运行我们的代码。
设计理念:简洁、可读的代码
Python语法与R、Java、C/C++、MATLAB不同
- 使用空白符分隔
- 较少使用括号、分号等符号
Python简介¶
for (i in 1:10) {
print(i)
}
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
return 0;
}
for i = 1:10
disp(i)
end
for i in range(1, 11):
print(i)
Python简介¶
for (i in 1:10) {
print(i)
}
R
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
return 0;
}
C++
for i = 1:10
disp(i)
end
Matlab
for i in range(1, 11):
print(i)
Python
使用空白符分隔;较少使用括号、分号等符号
运行Python¶
如何运行Python
Python解释器
Jupyter:https://jupyter.org/
PythonAnywhere:https://www.pythonanywhere.com/
Visual Studio Code:https://code.visualstudio.com/
作业以Jupyter Notebook的形式提交(.ipynb)
建议使用Anaconda安装Jupyter和管理环境:https://anaconda.com/
Python Interpreter on the Command Line¶
Python Interpreter on the Command Line¶
Python in Jupyter¶
Jupyter Notebook和Jupyter Lab¶
- Jupyter Lab是Jupyter项目的新一代交互式计算环境,提供了更加现代和灵活的用户界面。Jupyter Lab不仅包含了Console、terminal和其他工具集,可以在Jupyter Lab中同时打开多个工具进行操作。
Jupyter¶
第一个Python代码¶
AI Assistant¶
- AI Assistant
- Github Copilot
- Codeium
Codeium¶
导出Html文件¶
- 打开终端,激活相应环境
- 安装nbconvert
- pip install nbconvert
- 将.ipynb文件导出为HTML
- jupyter nbconvert --to html lecture01.ipynb
Python中的算术¶
In [1]:
1 + 2 # 1 plus 2
Out[1]:
3
In [2]:
2 * 3 # 2 times 3
Out[2]:
6
In [3]:
2 ** 7 # 2 to the power of 7
Out[3]:
128
In [4]:
8 / 3 # 8 divided by 3
Out[4]:
2.6666666666666665
In [5]:
8 // 3 # 8 divided by 3 without remainder
Out[5]:
2
In [6]:
8 % 3 # 8 modulo 3
Out[6]:
2
数据类型¶
程序处理不同值,将给予不同的类型;
- 例如:
- 值 42 是一个整数
- 值2.71828 是一个浮点数(即小数)
- 值 “bird” 是一个字符串(即一串字符)
- 例如:
变量的类型决定了我们可以执行或不能执行的操作。
- 例如,2 * 3 是合理的,但 ‘cat’*‘dog’ 是什么意思?
Variables in Python¶
In [7]:
# 与其它编程语言不同的是,Python自动指定变量类型
# 这种方式叫做 Duck Typing
mystring = "hello world!"
approxi_pi = 3.1415926
number_of_planet = 9
In [8]:
# 你可以使用 type() 函数来查看变量的类型
type(mystring)
Out[8]:
str
In [9]:
type(approxi_pi)
Out[9]:
float
In [10]:
type(number_of_planet)
Out[10]:
int
Variables in Python¶
- Python变量类型的转化
In [11]:
approxi_pi = 3.1415926
type(approxi_pi)
Out[11]:
float
In [12]:
# convert a float to an integer
pi_int = int(approxi_pi)
type(pi_int)
Out[12]:
int
In [13]:
# convert an string to an integer
int_from_string = int('123456')
type(int_from_string)
Out[13]:
int
In [14]:
int_from_string
Out[14]:
123456
In [15]:
# 但是有些转换是没有意义的
goat_int = int('goat')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[15], line 2 1 # 但是有些转换是没有意义的 ----> 2 goat_int = int('goat') ValueError: invalid literal for int() with base 10: 'goat'
一些字符串运算¶
In [16]:
# 一些运算也是没有意义的
'dog' * 'cat'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[16], line 2 1 # 一些运算也是没有意义的 ----> 2 'dog' * 'cat' TypeError: can't multiply sequence by non-int of type 'str'
In [17]:
# concatenation of two strings
'dog' + 'cat'
Out[17]:
'dogcat'
In [18]:
# string concatenation with multiplication
'dog' * 3
Out[18]:
'dogdogdog'
Python中的注释¶
- 两种注释'#'和''' '''
In [ ]:
# This is a comment.
# Python doesn't process this line.
euler = 2.71828
'''Triple quotes let you write a multiline like this one.
Everything between the quotes is part of the comment.
'''
print(euler)
作业¶
安装并注册Anaconda、Jupyter、PyCharm(专业版)和VS Code
安装并注册GitHub和Codeium
阅读PyCharm或VS Code的技术文档