{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## while循环:计算10以内自然数的和" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s=45\n" ] } ], "source": [ "i = 1\n", "s = 0\n", "while i<10:\n", " s += i\n", " i += 1\n", "print(\"s=\"+`s`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## while循环:输入一个自然数,求其除了1和本身外的所有因子" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入一个自然数,程序将求出它所有的因子14\n", "因子是:2 7 \n" ] } ], "source": [ "i = 0\n", "j = 2\n", "stro = '因子是:'\n", "i = input(\"输入一个自然数,程序将求出它所有的因子\")\n", "while j < i:\n", " if i % j == 0:\n", " stro += `j` + ' '\n", " j+=1\n", "print stro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## break语句演示" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B\n", "B\n", "B\n", "B\n", "B\n", "B\n", "B\n", "B\n", "B\n", "B\n", "I\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n", "T\n" ] } ], "source": [ "for s in \"BIT\":\n", " for i in range(10):\n", " print s\n", " if s == \"I\":\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## continue语句演示" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P\n", "Y\n", "H\n", "O\n", "N\n" ] } ], "source": [ "for s in \"PYTHON\":\n", " if s == \"T\":\n", " continue\n", " print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 列表" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100011\n", "Beijing\n", "3.14\n" ] } ], "source": [ "items = [] #声明一个空列表\n", "items.append(100011)\n", "items.append('Beijing')\n", "items.append(3.14)\n", "print items[0]\n", "print items[1]\n", "print items[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### append 与 extend 方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### append 表示把某个数据当做新元素 追加 到列表的最后面,它的参数可以是任意对象" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, [4, 5]]\n" ] } ], "source": [ "x = [1, 2, 3]\n", "y = [4, 5]\n", "x.append(y)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### extend 的参数必须是一个可迭代对象,表示把该对象里面的所有元素逐个地追加到列表的后面" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n" ] } ], "source": [ "x = [1, 2, 3]\n", "y = [4, 5]\n", "x.extend(y)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 向列表中插入元素" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100011, 'Beijing', '\\xe5\\x9c\\x86\\xe5\\x91\\xa8\\xe7\\x8e\\x87', 3.14]\n" ] } ], "source": [ "items = []\n", "items.append(100011)\n", "items.append('Beijing')\n", "items.append(3.14)\n", "items.insert(2,'圆周率')\n", "print items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 删除或清空列表中的记录" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'd']\n", "['a', 'b', 'd', 'a']\n", "['b', 'd', 'a']\n" ] } ], "source": [ "my_list = ['a', 'b', 'c', 'd']\n", "my_list.pop(2) #删除索引为 2 的元素\n", "print my_list\n", "my_list.append('a')\n", "print my_list\n", "my_list.remove('a') #remove 移除某个元素,而且只能移除第一次出现的元素\n", "print my_list" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c']\n" ] } ], "source": [ "my_list = ['a', 'b', 'c', 'd']\n", "my_list.pop() #删除尾部元素\n", "print my_list" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [4, 3, 5]\n", "a.pop(1)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [4, 3, 5]\n", "del a[1]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd']\n", "[]\n" ] }, { "ename": "NameError", "evalue": "name 'my_list' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-56-3938ed1f7307>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mmy_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mmy_list\u001b[0m \u001b[0;31m#删除列表\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mmy_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'my_list' is not defined" ] } ], "source": [ "my_list = ['a', 'b', 'c', 'd']\n", "print my_list\n", "my_list = [] #清空列表\n", "print my_list\n", "del my_list #删除列表\n", "print my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 二级索引" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Triangle:', '3,4,5', 6.0]\n", "6.0\n" ] } ], "source": [ "lst_shape = [['Triangle:','3,4,5',6.0],['Rect:','5,6',30],['Circle:','5',78.5]]\n", "print lst_shape[0]\n", "print lst_shape[0][2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 检查列表是否为空" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "if len(items) == 0:\n", " print(\"空列表\")" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "if items == []:\n", " print(\"空列表\")" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "if not items:\n", " print(\"空列表\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 切片" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "切片用于获取列表中指定范的子集,语法非常简单:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "items[start:end:step] #从 start 到 end-1 位置之间的元素。step 表示步长,默认为1,表示连续获取,如果 step 为 2 就表示每隔一个元素获取。" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6, 7, 8]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[3:8] # 第3到第8位置之间的元素" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 6, 8]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[3:8:2] # 第3到第8位置之间的元素,每隔一个元素获取" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[:5] #省略start表示从第0个元素开始" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[3:] #省略end表示到最后一个元素" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[::] #都省略相当于拷贝一个列表" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 获取列表中的最后一个元素" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[-1]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "a[len(a)-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 连接两个列表" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "listone = [1, 2, 3]\n", "listtwo = [4, 5, 6]\n", "\n", "mergedlist = listone + listtwo\n", "\n", "print(mergedlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for循环" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for i in range(N):\n", " 循环体LB\n", "#函数range(N)的意义在于生成一个0到N-1的数字列表\n", "for i in range(N,M)#range(N,M)的范围是从N到M-1。其中i的取值范围是[N,M)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "153\n", "370\n", "371\n", "407\n" ] } ], "source": [ "# 输出水仙花数\n", "for i in range(100,1000):\n", " hun = i/100\n", " ten = (i-hun*100)/10\n", " num = i%10\n", " if i == hun**3 + ten**3 + num**3:\n", " print i" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (<ipython-input-62-9fd3efae8c5f>, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-62-9fd3efae8c5f>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m for rec in My_list:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "for rec in My_list:\n", " 循环体LB\n", "#用for循环遍历列表" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "for r in a:\n", " print r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "补充:eval()函数:可简单理解为将string变成算术表达式来执行" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('3+5')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在Python3中可以用eval(input())将输入的内容转换为数字" ] }, { "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.13" }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }