Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FinanceData/d38168e1b53edb69b04a209b7f1155fe to your computer and use it in GitHub Desktop.
Save FinanceData/d38168e1b53edb69b04a209b7f1155fe to your computer and use it in GitHub Desktop.
Chapter_05_Conditionals_and_Control_Flow
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Chapter 05 Conditionals and Control Flow\n",
"<img align=\"left\" src=\"https://i.imgur.com/lnc0qMV.png\" />"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You've just entered the clinic!\n",
"Do you take the door on the left or the right?\n",
"Type left or right and hit 'Enter'.right\n",
"Of course this is the Argument Room, I've told you that already!\n"
]
}
],
"source": [
"# 01 흐름(flow)를 따라가세요\n",
"\n",
"def clinic():\n",
" print (\"You've just entered the clinic!\")\n",
" print (\"Do you take the door on the left or the right?\")\n",
" answer = input(\"Type left or right and hit 'Enter'.\").lower()\n",
" if answer == \"left\" or answer == \"l\":\n",
" print (\"This is the Verbal Abuse Room, you heap of parrot droppings!\")\n",
" elif answer == \"right\" or answer == \"r\":\n",
" print (\"Of course this is the Argument Room, I've told you that already!\")\n",
" else:\n",
" print (\"You didn't pick left or right! Try again.\")\n",
" clinic()\n",
"\n",
"clinic()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17 < (118 %100)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"False\n",
"False\n",
"False\n"
]
}
],
"source": [
"# 02 자세히 비교하기!\n",
"\n",
"bool_one = 17 < (118 %100)\n",
"print(bool_one)\n",
"\n",
"bool_two = 100 == ((33 * 3) + 1)\n",
"print(bool_two)\n",
"\n",
"bool_three = 19 <= 2 ** 4\n",
"print(bool_three)\n",
"\n",
"bool_four = -22 >= -18\n",
"print(bool_four)\n",
"\n",
"bool_five = 99 != 98 + 1\n",
"print(bool_five)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 03 더 자세히 비교하기!\n",
"\n",
"bool_one = 20 + -10 * 2 > 10 % 3 % 2\n",
"\n",
"bool_two = (10 + 17)**2 == 3**6\n",
"\n",
"bool_three = 1**2**3 <= -(-(-1))\n",
"\n",
"bool_four = 40 / 20 * 4 >= -4**2\n",
"\n",
"bool_five = 100**0.5 != 6 + 4"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 04 불린 연산자(Boolean Operators)\n",
"\n",
"# Make me true!\n",
"bool_one = 1 > 0\n",
"\n",
"# Make me false!\n",
"bool_two = 2 > 3\n",
"\n",
"# Make me true!\n",
"bool_three = 1 == 1\n",
"\n",
"# Make me false!\n",
"bool_four = 2 != 2\n",
"\n",
"# Make me true!\n",
"bool_five = 2 == 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"05 같거나(And), 또는(Or) 반대(Not)이거나\n",
"\n",
"Boolean Operators\n",
"\n",
"### and\n",
"\n",
"A | B | 결과\n",
"-- | -- | --\n",
"True | True | True\n",
"True | False | False\n",
"False | True | False\n",
"False | False | False\n",
"\n",
"### or\n",
"A | B | 결과\n",
"-- | -- | --\n",
"True | True | True\n",
"True | False | True\n",
"False | True | True\n",
"False | False | False\n",
"\n",
"### not\n",
"A | 결과\n",
"-- | --\n",
"Not | False\n",
"Not | True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 06 And 연산자\n",
"\n",
"bool_one = False and False\n",
"\n",
"bool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5\n",
"\n",
"bool_three = 19 % 4 != 300 / 10 / 10 and False\n",
"\n",
"bool_four = -(1**2) < 2**0 and 10 % 10 <= 20 - 10 * 2\n",
"\n",
"bool_five = True and True"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 07 Or 연산자\n",
"\n",
"bool_one = 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'\n",
"\n",
"bool_two = True or False\n",
"\n",
"bool_three = 100**0.5 >= 50 or False\n",
"\n",
"bool_four = True or True\n",
"\n",
"bool_five = 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 08 Not 연산자\n",
"\n",
"bool_one = False\n",
"\n",
"bool_two = True\n",
"\n",
"bool_three = True\n",
"\n",
"bool_four = True\n",
"\n",
"bool_five = False"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"# 09 This and That or This But Not That\n",
"\n",
"bool_one = False or not True and True\n",
"print(bool_one)\n",
"\n",
"bool_two = False and not True or True\n",
"print(bool_two)\n",
"\n",
"bool_three = True and not (False or False)\n",
"\n",
"bool_four = not not True or False and not True\n",
"\n",
"bool_five = False or not (True and True)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# 10 Mix n Match\n",
"\n",
"# Make me false!\n",
"bool_one = not True\n",
"\n",
"# Make me true!\n",
"bool_two = True and True\n",
"\n",
"# Make me false!\n",
"bool_three = False or False\n",
"\n",
"# Make me true!\n",
"bool_four = not False\n",
"\n",
"# Make me true!\n",
"bool_five = not False"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the Verbal Abuse Room, you heap of parrot droppings!\n"
]
}
],
"source": [
"# 11 조건문(Conditional Statement)의 문법\n",
"\n",
"response = \"Y\"\n",
"\n",
"answer = \"Left\"\n",
"if answer == \"Left\":\n",
" print (\"This is the Verbal Abuse Room, you heap of parrot droppings!\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success #1\n",
"Success #2\n"
]
}
],
"source": [
"# 12 만약(if) 여러분이...\n",
"\n",
"def using_control_once():\n",
" if 1 == 1:\n",
" return (\"Success #1\")\n",
"\n",
"def using_control_again():\n",
" if not False:\n",
" return (\"Success #2\")\n",
"\n",
"print (using_control_once())\n",
"print (using_control_again())"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"# 13 그 외에(else) 문제가 있다면, 유감입니다…\n",
"\n",
"answer = \"'Tis but a scratch!\"\n",
"\n",
"def black_knight():\n",
" if answer == \"'Tis but a scratch!\":\n",
" return True\n",
" else:\n",
" return False # Make sure this returns False\n",
"\n",
"def french_soldier():\n",
" if answer == \"Go away, or I shall taunt you a second time!\":\n",
" return True\n",
" else:\n",
" return False # Make sure this returns False\n",
" \n",
"print( black_knight() )\n",
"print( french_soldier() )"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1\n",
"0\n",
"1\n"
]
}
],
"source": [
"# 14 수많은 문제가 있지만, Switch는 문제가 아니죠.\n",
"\n",
"def greater_less_equal_5(answer):\n",
" if answer > 5:\n",
" return 1\n",
" elif answer < 5:\n",
" return -1\n",
" else:\n",
" return 0\n",
"\n",
"print (greater_less_equal_5(4))\n",
"print (greater_less_equal_5(5))\n",
"print (greater_less_equal_5(6))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"elif evaluated\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 15 The Big If\n",
"\n",
"def the_flying_circus():\n",
" if 1 == 2 and 3 != 4 or not 5 < 6 and 7 <= 8 or 9 > 10 and 11 >= 10:\n",
" print (\"if evaluated\")\n",
" return True\n",
" elif True:\n",
" print (\"elif evaluated\")\n",
" return True\n",
" else:\n",
" print (\"Impossible to reach here\")\n",
" return False\n",
"\n",
"the_flying_circus()"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment