{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 判断语句" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本用法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "判断,基于一定的条件,决定是否要执行特定的一段代码,例如判断一个数是不是正数:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey!\n", "x is positive\n" ] } ], "source": [ "x = 0.5\n", "if x > 0:\n", " print \"Hey!\"\n", " print \"x is positive\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在这里,如果 `x > 0` 为 `False` ,那么程序将不会执行两条 `print` 语句。\n", "\n", "虽然都是用 `if` 关键词定义判断,但与**C,Java**等语言不同,**Python**不使用 `{}` 将 `if` 语句控制的区域包含起来。**Python**使用的是缩进方法。同时,也不需要用 `()` 将判断条件括起来。\n", "\n", "上面例子中的这两条语句:\n", "```python\n", " print \"Hey!\"\n", " print \"x is positive\"\n", "```\n", "就叫做一个代码块,同一个代码块使用同样的缩进值,它们组成了这条 `if` 语句的主体。\n", "\n", "不同的缩进值表示不同的代码块,例如:\n", "\n", "`x > 0` 时:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey!\n", "x is positive\n", "This is still part of the block\n", "This isn't part of the block, and will always print.\n" ] } ], "source": [ "x = 0.5\n", "if x > 0:\n", " print \"Hey!\"\n", " print \"x is positive\"\n", " print \"This is still part of the block\"\n", "print \"This isn't part of the block, and will always print.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`x < 0` 时:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This isn't part of the block, and will always print.\n" ] } ], "source": [ "x = -0.5\n", "if x > 0:\n", " print \"Hey!\"\n", " print \"x is positive\"\n", " print \"This is still part of the block\"\n", "print \"This isn't part of the block, and will always print.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在这两个例子中,最后一句并不是`if`语句中的内容,所以不管条件满不满足,它都会被执行。\n", "\n", "一个完整的 `if` 结构通常如下所示(注意:条件后的 `:` 是必须要的,缩进值需要一样):\n", " \n", " if :\n", " \n", " \n", " elif : \n", " \n", " else:\n", " \n", "\n", "当条件1被满足时,执行 `if` 下面的语句,当条件1不满足的时候,转到 `elif` ,看它的条件2满不满足,满足执行 `elif` 下面的语句,不满足则执行 `else` 下面的语句。\n", "\n", "对于上面的例子进行扩展:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is zero\n" ] } ], "source": [ "x = 0\n", "if x > 0:\n", " print \"x is positive\"\n", "elif x == 0:\n", " print \"x is zero\"\n", "else:\n", " print \"x is negative\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`elif` 的个数没有限制,可以是1个或者多个,也可以没有。\n", "\n", "`else` 最多只有1个,也可以没有。\n", "\n", "可以使用 `and` , `or` , `not` 等关键词结合多个判断条件:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10\n", "y = -5\n", "x > 0 and y < 0" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not x > 0" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x < 0 or y < 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这里使用这个简单的例子,假如想判断一个年份是不是闰年,按照闰年的定义,这里只需要判断这个年份是不是能被4整除,但是不能被100整除,或者正好被400整除:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is not a leap year.\n" ] } ], "source": [ "year = 1900\n", "if year % 400 == 0:\n", " print \"This is a leap year!\"\n", "# 两个条件都满足才执行\n", "elif year % 4 == 0 and year % 100 != 0:\n", " print \"This is a leap year!\"\n", "else:\n", " print \"This is not a leap year.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 值的测试" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**不仅仅可以使用布尔型变量作为条件,它可以直接在`if`中使用任何表达式作为条件:\n", "\n", "大部分表达式的值都会被当作`True`,但以下表达式值会被当作`False`:\n", "\n", "- False\n", "- None\n", "- 0\n", "- 空字符串,空列表,空字典,空集合" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first element is: 3\n" ] } ], "source": [ "mylist = [3, 1, 4, 1, 5, 9]\n", "if mylist:\n", " print \"The first element is:\", mylist[0]\n", "else:\n", " print \"There is no first element.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "修改为空列表:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There is no first element.\n" ] } ], "source": [ "mylist = []\n", "if mylist:\n", " print \"The first element is:\", mylist[0]\n", "else:\n", " print \"There is no first element.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当然这种用法并不推荐,推荐使用 `if len(mylist) > 0:` 来判断一个列表是否为空。" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }