{ "metadata": { "name": "", "signature": "sha256:9c21d1f44f5f11828308d4a5b6ab2316e8a5d933b04912def7dc67cfcb9dbaea" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6 - Nested if Statements and Logical Operators" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 6.1, page no. 125" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your age: \",\n", "age = int(raw_input())\n", "print \"Are you a citizen (Y/N): \",\n", "choice = raw_input()\n", "if (choice == 'Y'):\n", " citizen = True\n", "else:\n", " citizen = False\n", "if (age >= 18):\n", " if citizen:\n", " print \"You are eligible to vote\"\n", " else:\n", " print \"You are not eligible to vote\"\n", "else:\n", " print \"You are not eligible to vote\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your age: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Are you a citizen (Y/N): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You are eligible to vote\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 6.2, page no. 127" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your age: \",\n", "age = int(raw_input())\n", "if (age > 12):\n", " if (age >= 65):\n", " print \"Admission is free\"\n", " else:\n", " print \"You have to pay\"\n", "else:\n", " print \"Admission is free\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your age: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Admission is free\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 6.3, page no. 129" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your age: \",\n", "age = int(raw_input())\n", "print \"Are you a citizen (Y/N): \",\n", "choice = raw_input()\n", "if (choice == 'Y'):\n", " citizen = True\n", "else:\n", " citizen = False\n", "if (age >= 18 and citizen == True):\n", " print \"You are eligible to vote\"\n", "else:\n", " print \"You are not eligible to vote\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Enter your age: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Are you a citizen (Y/N): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You are not eligible to vote\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 6.4, page no. 131" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your age: \",\n", "age = int(raw_input())\n", "if (age <= 12 or age >= 65):\n", " print \"Admission is free\"\n", "else:\n", " print \"You have to pay\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your age: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You have to pay\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 6.5, page no. 132" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter your age: \",\n", "age = int(raw_input())\n", "if not ((age > 12 and age < 65)):\n", " print \"Admission is free\"\n", "else:\n", " print \"You have to pay\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your age: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You have to pay\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }