{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 数字" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 整型 Integers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "整型运算,加减乘:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 - 4" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 * 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在**Python 2.7**中,整型的运算结果只能返回整型,**除法**的结果也不例外。\n", "\n", "例如`12 / 5`返回的结果并不是2.4,而是2:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 / 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "幂指数:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "取余:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "32 % 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "赋值给变量:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用`type()`函数来查看变量类型:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "整型数字的最大最小值:\n", "\n", "在 32 位系统中,一个整型 4 个字节,最小值 `-2,147,483,648`,最大值 `2,147,483,647`。\n", "\n", "在 64 位系统中,一个整型 8 个字节,最小值 `-9,223,372,036,854,775,808`,最大值 `9,223,372,036,854,775,807`。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2147483647" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.maxint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 长整型 Long Integers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当整型超出范围时,**Python**会自动将整型转化为长整型,不过长整型计算速度会比整型慢。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "a = sys.maxint + 1\n", "print type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "长整型的一个标志是后面以字母L结尾:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2147483648L" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以在赋值时强制让类型为长整型:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "long" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 1234L\n", "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "长整型可以与整型在一起进行计算,返回的类型还是长整型:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "2147483644L" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a - 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 浮点数 Floating Point Numbers" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.4\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在之前的除法例子`12 / 5`中,假如想要使返回的结果为2.4,可以将它们写成浮点数的形式:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.4" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12.0 / 5.0" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.4" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 / 5.0" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "2.4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12.0 / 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "上面的例子说明,浮点数与整数进行运算时,返回的仍然是浮点数:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7.4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 + 2.4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮点数也可以进行与整数相似的运算,甚至可以取余:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.19999999999999973" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.4 - 3.2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "44.7" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12.3 + 32.4" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6.25" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.5 ** 2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.2999999999999998" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.4 % 2.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**的浮点数标准与**C**,**Java**一致,都是[IEEE 754 floating point standard](http://en.wikipedia.org/wiki/IEEE_floating_point)。\n", "\n", "注意看 `3.4 - 3.2` 的结果并不是我们预期的`0.2`,这是因为浮点数本身储存方式引起的,浮点数本身会存在一点误差。\n", "\n", "事实上,**Python** 中储存的值为'0.199999999999999733546474089962430298328399658203125',因为这是最接近0.2的浮点数。|" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0.199999999999999733546474089962430298328399658203125'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{:.52}'.format(3.4 - 3.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当我们使用`print`显示时,**Python**会自动校正这个结果" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.2\n" ] } ], "source": [ "print 3.4 - 3.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以用`sys.float_info`来查看浮点数的信息:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.float_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "例如浮点数能表示的最大值:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.7976931348623157e+308" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.max" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮点数能表示的最接近0的值:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.2250738585072014e-308" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.min" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮点数的精度:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.220446049250313e-16" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.float_info.epsilon" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 复数 Complex Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python** 使用 `j` 来表示复数的虚部:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "complex" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1 + 2j\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以查看它的实部,虚部以及共轭:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.real" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.imag" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1-2j)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.conjugate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##交互计算" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以将复杂的表达式放在一起计算:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-27" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在**Python**中运算是有优先级的,优先级即算术的先后顺序,比如“先乘除后加减”和“先算括号里面的”都是两种优先级的规则,优先级从高到低排列如下:\n", "\n", "- `( )` 括号\n", "- `**` 幂指数运算\n", "- `* / // %` 乘,除,整数除法,取余运算\n", "- '+ -' 加减\n", "\n", "整数除法,返回的是比结果小的最大整数值:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12.3 // 5.2" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-4.0" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12.3 // -4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 简单的数学函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "绝对值:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "12.4" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-12.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "取整:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "22.0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(21.6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "最大最小值:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n" ] } ], "source": [ "print min(2, 3, 4, 5)\n", "print max(2, 4, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 变量名覆盖" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "不要用内置的函数来命名变量,否则会出现意想不到的结果:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(max)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "不要这样做!!!" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max = 1\n", "type(max)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'int' object is not callable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'int' object is not callable" ] } ], "source": [ "max(4, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 类型转换" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮点数转整型,只保留整数部分:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "-3\n" ] } ], "source": [ "print int(12.324)\n", "print int(-3.32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "整型转浮点型:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2\n" ] } ], "source": [ "print float(1.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他表示" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "除了10进制外,整数还有其他类型的表示方法。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "科学计数法:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1e-06" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1e-6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "16进制,前面加`0x`修饰,后面使用数字0-9A-F:" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0xFF" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "8进制,前面加`0`或者`0o`修饰,后面使用数字0-7:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "067" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2进制,前面加`0b`修饰,后面使用数字0或1:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b101010" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 原地计算 In-place" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**可以使用下面的形式进行原地计算:" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.5\n", "9.0\n", "6.0\n" ] } ], "source": [ "b = 2.5\n", "b += 2\n", "print b\n", "b *= 2\n", "print b\n", "b -= 3\n", "print b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 布尔型 Boolean Data Type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "布尔型可以看成特殊的二值变量,其取值为`True`和`False`:" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = True\n", "type(q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以用表达式构建布尔型变量:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "q = 1 > 2\n", "print q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "常用的比较符号包括:\n", "\n", " <, >, <=, >=, ==, !=\n", "\n", "**Python**支持链式比较:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2 \n", "1 < x <= 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }