{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "01_Python",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"metadata": {
"id": "bOChJSNXtC9g",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# Python简介"
]
},
{
"metadata": {
"id": "OLIxEDq6VhvZ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"\n",
"\n",
"在本节中,我们将学习Python编程语言的基础知识(版本3)。我们不会学习Python的所有知识点,但足以做一些基础的机器学习。\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"metadata": {
"id": "VoMq0eFRvugb",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 变量"
]
},
{
"metadata": {
"id": "qWro5T5qTJJL",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"变量(variables)是python中的对象,可以存储数值或字符串。我们来看看如何创建一些变量。"
]
},
{
"metadata": {
"id": "0-dXQiLlTIgz",
"colab_type": "code",
"outputId": "b9874721-864d-4204-c24b-7789686ceff8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 数值示例\n",
"x = 5\n",
"print (x)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"5\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "5Ym0owFxTkjo",
"colab_type": "code",
"outputId": "877a09a0-b74c-4fee-8ead-b86e8d508ed8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 字符串示例\n",
"x = \"hello\"\n",
"print (x)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"hello\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "1a4ZhMV1T1-0",
"colab_type": "code",
"outputId": "f089ba0e-ec2a-43b5-ab75-ac6b8d8f0f17",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 变量可以相互使用\n",
"a = 1\n",
"b = 2\n",
"c = a + b\n",
"print (c)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "nbKV4aTdUC1_",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"变量可以有很多不同的类型。 即使在数值变量中,也可以使用整数(int),浮点数(float)等。所有基于字符串的变量都是string(str)类型。我们可以通过打印它的类型来查看变量的类型。"
]
},
{
"metadata": {
"id": "c3NJmfO4Uc6V",
"colab_type": "code",
"outputId": "0ca2821e-0a17-46f2-d380-e6b0ba6549dd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 152
}
},
"cell_type": "code",
"source": [
"# int 变量\n",
"x = 5\n",
"print (x)\n",
"print (type(x))\n",
"\n",
"# float 变量\n",
"x = 5.0\n",
"print (x)\n",
"print (type(x))\n",
"\n",
"# text 变量(即为str)\n",
"x = \"5\" \n",
"print (x)\n",
"print (type(x))\n",
"\n",
"# boolean 变量\n",
"x = True\n",
"print (x)\n",
"print (type(x))"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"5\n",
"\n",
"5.0\n",
"\n",
"5\n",
"\n",
"True\n",
"\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "6HPtavfdU8Ut",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"知道变量的类型是一种很好的做法。当你想在那时使用数值运算时,它们需要类型兼容。"
]
},
{
"metadata": {
"id": "8pr1-i7IVD-h",
"colab_type": "code",
"outputId": "d7807ee0-d227-4591-af5e-2668ffe1b2b2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"cell_type": "code",
"source": [
"# int 变量\n",
"a = 5\n",
"b = 3\n",
"print (a + b)\n",
"\n",
"# string 变量\n",
"a = \"5\"\n",
"b = \"3\"\n",
"print (a + b)"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"8\n",
"53\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "q4R_UF6PVw4V",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 列表"
]
},
{
"metadata": {
"id": "LvGsQBj4VjMl",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"列表(lists)是python中的对象,可以包含有序的数字序列**和**字符串。"
]
},
{
"metadata": {
"id": "9iPESkq9VvlX",
"colab_type": "code",
"outputId": "726c2997-2b58-4d6e-cb04-e66966cc578c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 创建列表\n",
"list_x = [3, \"hello\", 1]\n",
"print (list_x)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"[3, 'hello', 1]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "0xC6WvuwbGDg",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
""
]
},
{
"metadata": {
"id": "7lbajc-zV515",
"colab_type": "code",
"outputId": "18053361-d915-4f05-d786-21c6b1544366",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 向列表中添加元素\n",
"list_x.append(7)\n",
"print (list_x)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"[3, 'hello', 1, 7]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "W0xpIryJWCN9",
"colab_type": "code",
"outputId": "e2736f73-8fa3-4240-ca0d-c251658bddf6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
}
},
"cell_type": "code",
"source": [
"# 访问列表中特定位置的元素\n",
"print (\"list_x[0]: \", list_x[0])\n",
"print (\"list_x[1]: \", list_x[1])\n",
"print (\"list_x[2]: \", list_x[2])\n",
"print (\"list_x[-1]: \", list_x[-1]) # 索引值为-1时,表示访问最后的元素\n",
"print (\"list_x[-2]: \", list_x[-2]) # 索引值为-2时,表示访问倒数第二个元素"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"list_x[0]: 3\n",
"list_x[1]: hello\n",
"list_x[2]: 1\n",
"list_x[-1]: 7\n",
"list_x[-2]: 1\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "VSu_HNrnc1WK",
"colab_type": "code",
"outputId": "aeb31316-0445-4e97-91d6-4695ddf56a3d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 84
}
},
"cell_type": "code",
"source": [
"# 切片\n",
"print (\"list_x[:]: \", list_x[:])\n",
"print (\"list_x[2:]: \", list_x[2:])\n",
"print (\"list_x[1:3]: \", list_x[1:3])\n",
"print (\"list_x[:-1]: \", list_x[:-1])"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"list_x[:]: [3, 'hello', 1, 7]\n",
"list_x[2:]: [1, 7]\n",
"list_x[1:3]: ['hello', 1]\n",
"list_x[:-1]: [3, 'hello', 1]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "dImY-hVzWxB4",
"colab_type": "code",
"outputId": "152f6a17-4cb8-4878-9e72-cee22b7cbfc7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 列表的长度\n",
"len(list_x)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {
"tags": []
},
"execution_count": 10
}
]
},
{
"metadata": {
"id": "3-reXDniW_sm",
"colab_type": "code",
"outputId": "fbbe3416-857d-4265-9939-e30ce8cfc5ba",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 替换列表中的元素\n",
"list_x[1] = \"hi\"\n",
"print (list_x)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"[3, 'hi', 1, 7]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "X8T5I3bjXJ0S",
"colab_type": "code",
"outputId": "845d934a-8946-4030-e3c1-f7dd6356929b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 组合列表\n",
"list_y = [2.4, \"world\"]\n",
"list_z = list_x + list_y\n",
"print (list_z)"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"[3, 'hi', 1, 7, 2.4, 'world']\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "ddpIO6LLVzh0",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 元组"
]
},
{
"metadata": {
"id": "CAZblq7oXY3s",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"元组(tuples)也是python中可以保存数据但是不能替换值的对象(因此,元组称为不可变,而列表称为可变)。"
]
},
{
"metadata": {
"id": "G95lu8xWXY90",
"colab_type": "code",
"outputId": "e3192ff1-6f24-414e-9733-f00fcfcd0991",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 创建元组\n",
"tuple_x = (3.0, \"hello\")\n",
"print (tuple_x)"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
"(3.0, 'hello')\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "kq23Bej1acAP",
"colab_type": "code",
"outputId": "91de445d-c9d5-4009-f94d-e0c3393c203e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# 添加元素到元组\n",
"tuple_x = tuple_x + (5.6,)\n",
"print (tuple_x)"
],
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": [
"(3.0, 'hello', 5.6)\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "vyTmOc6BXkge",
"colab_type": "code",
"outputId": "929c7835-4539-4207-f27c-4ff9d29ce58a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
}
},
"cell_type": "code",
"source": [
"# 试图改变元组的值(事实上不可行)\n",
"tuple_x[1] = \"world\""
],
"execution_count": 15,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple_x\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"world\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
]
},
{
"metadata": {
"id": "UdlJHkwZV3Mz",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 字典"
]
},
{
"metadata": {
"id": "azp3AoxYXS26",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"字典(dictionaries) 是包含键值对(key-value)的python对象。在下面的字典示例中,键是“name”和“eye_color”变量。它们各自都有与之相关的值。 字典不能有两个相同的键。"
]
},
{
"metadata": {
"id": "pXhNLbzpXXSk",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 创建字典\n",
"goku = {\"name\": \"Goku\",\n",
" \"eye_color\": \"brown\"}\n",
"print (goku)\n",
"print (goku[\"name\"])\n",
"print (goku[\"eye_color\"])\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "1HXtX8vQYjXa",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 改变键的值\n",
"goku[\"eye_color\"] = \"green\"\n",
"print (goku)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "qn33iB0MY5dT",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 添加新的键值对\n",
"goku[\"age\"] = 24\n",
"print (goku)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "g9EYmzMKa9YV",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 字典的长度\n",
"print (len(goku))"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "B-DInx_Xo2vJ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# If 语句"
]
},
{
"metadata": {
"id": "ZG_ICGRGo4tY",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"您可以使用if语句有条件地执行某些操作。"
]
},
{
"metadata": {
"id": "uob9lQuKo4Pg",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# If 语句\n",
"x = 4\n",
"if x < 1:\n",
" score = \"low\"\n",
"elif x <= 4:\n",
" score = \"medium\"\n",
"else:\n",
" score = \"high\"\n",
"print (score)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "vwsQaZqIpfJ3",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 带有布尔值的if语句\n",
"x = True\n",
"if x:\n",
" print (\"it worked\")"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "sJ7NPGEKV6Ik",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 循环语句"
]
},
{
"metadata": {
"id": "YRVxhVCkn0vc",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"您可以在python中使用for或while循环来重复执行某些操作,直到满足条件为止。"
]
},
{
"metadata": {
"id": "OB5PtyqAn8mj",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# for循环\n",
"x = 1\n",
"for i in range(3): # 从 i=0 到 i=2\n",
" x += 1 # 等价于 x = x + 1\n",
" print (\"i={0}, x={1}\".format(i, x)) # 打印多个变量"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "6XyhCrFeoGj4",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 循环遍历列表中的元素\n",
"x = 1\n",
"for i in [0, 1, 2]:\n",
" x += 1\n",
" print (\"i={0}, x={1}\".format(i, x))"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "5Tf2x4okp3fH",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# while循环\n",
"x = 3\n",
"while x > 0:\n",
" x -= 1 # 等价于 x = x - 1\n",
" print (x)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "gJw-EDO9WBL_",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 函数"
]
},
{
"metadata": {
"id": "hDIOUdWCqBwa",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"函数(functions)是模块化可重用代码块的一种方法。"
]
},
{
"metadata": {
"id": "iin1ZXmMqA0y",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 创建函数\n",
"def add_two(x):\n",
" x += 2\n",
" return x\n",
"\n",
"# 调用函数\n",
"score = 0\n",
"score = add_two(x=score)\n",
"print (score)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "DC6x3DMrqlE3",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 具有多输入的函数\n",
"def join_name(first_name, last_name):\n",
" joined_name = first_name + \" \" + last_name\n",
" return joined_name\n",
"\n",
"# 调用函数\n",
"first_name = \"Goku\"\n",
"last_name = \"Mohandas\"\n",
"joined_name = join_name(first_name=first_name, last_name=last_name)\n",
"print (joined_name)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "lBLa1n54WEd2",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 类"
]
},
{
"metadata": {
"id": "mGua8QnArAZh",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"类(classes) 是面向对象的Python编程的基础部分。"
]
},
{
"metadata": {
"id": "DXmPwI1frAAd",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 创建函数\n",
"class Pets(object):\n",
" \n",
" # 类初始化\n",
" def __init__(self, species, color, name):\n",
" self.species = species\n",
" self.color = color\n",
" self.name = name\n",
"\n",
" # 用于打印\n",
" def __str__(self):\n",
" return \"{0} {1} named {2}.\".format(self.color, self.species, self.name)\n",
"\n",
" # 示例函数\n",
" def change_name(self, new_name):\n",
" self.name = new_name"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "ezQq_Fhhrqrv",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 创建类的实例:对象\n",
"my_dog = Pets(species=\"dog\", color=\"orange\", name=\"Guiness\",)\n",
"print (my_dog)\n",
"print (my_dog.name)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "qTinlRj1szc5",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# 调用类的函数\n",
"my_dog.change_name(new_name=\"Charlie\")\n",
"print (my_dog)\n",
"print (my_dog.name)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "kiWtd0aJtNtY",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 其它资源"
]
},
{
"metadata": {
"id": "cfLF4ktmtSC3",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"本文是对python的快速浏览,我们将在未来的课程中学到更多东西。如果您想在进入机器学习之前了解关于python的更多信息,请查看此免费课程: [Free Python Course](https://www.codecademy.com/learn/learn-python)"
]
},
{
"metadata": {
"id": "MDbjB8ju3E9L",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}