{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 第一课 Python 基础" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 计算" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.8" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 / 5" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.761245674740486" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "60 / 1.7 ** 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 变量\n", "* 取有意义的变量名\n", "* 区分大小写\n", "* 用 = 进行赋值" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "weight = 60\n", "height = 1.7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`print()` 输出函数" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "60\n" ] } ], "source": [ "print(weight)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "用 `#` 进行注释" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20.761245674740486\n" ] } ], "source": [ "# Body Mass Index (normal 18.5 ~ 25)\n", "bmi = weight / height**2\n", "print(bmi)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World !\n" ] } ], "source": [ "print(\"Hello World !\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "print(bmi > 18.5)\n", "print(bmi >= 25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据类型" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "整型" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(weight)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮点型" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(bmi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('Hello World !')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "布尔型" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(bmi > 18.5)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('123')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "类型转换" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int('123'))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(1.9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.8, 1.68, 1.2, 1.5]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "family = [1.80, 1.68, 1.2, 1.5]\n", "family" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(family)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.8" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "family[0]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.2, 1.5]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "family[-2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 函数和方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "函数" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.8, 1.68, 1.2, 1.5]\n" ] } ], "source": [ "print(family)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.8" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(family)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(family)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function max in module builtins:\n", "\n", "max(...)\n", " max(iterable, *[, default=obj, key=func]) -> value\n", " max(arg1, arg2, *args, *[, key=func]) -> value\n", " \n", " With a single iterable argument, return its biggest item. The\n", " default keyword-only argument specifies an object to return if\n", " the provided iterable is empty.\n", " With two or more arguments, return the largest argument.\n", "\n" ] } ], "source": [ "help(max)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "?max" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "family.count(1.8)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.8, 1.68, 1.2, 1.5, 1.8, 1.8]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "family.append(1.8)\n", "family" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## packages 导入" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy " ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numpy.round(2.7)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(2.3)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.8 , 1.68, 1.2 , 1.5 , 1.8 , 1.8 ])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array(family)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from numpy import array" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.8 , 1.68, 1.2 , 1.5 , 1.8 , 1.8 ])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array(family)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }