Skip to content

Instantly share code, notes, and snippets.

@bryanyang0528
Created June 30, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanyang0528/350886672d43eb5292df to your computer and use it in GitHub Desktop.
Save bryanyang0528/350886672d43eb5292df to your computer and use it in GitHub Desktop.
Python Course 2
{
"metadata": {
"name": "",
"signature": "sha256:f844c7076bd537dedadb2fe248e886e56fe450fd60c794d4148ed4a0ace2f581"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"\u6d41\u7a0b\u63a7\u5236"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## \u6bd4\u8f03\u51fd\u793a\n",
"'''\n",
"Equal to (==)\n",
"Not equal to (!=)\n",
"Less than (<)\n",
"Less than or equal to (<=)\n",
"Greater than (>)\n",
"Greater than or equal to (>=)\n",
"'''\n",
"##\u6bd4\u8f03\u51fd\u793a\u6703\u5410\u51faTrue or False\n",
"\n",
"a = 5 > 4\n",
"print a\n",
"\n",
"b = 5 == 4\n",
"print b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 129
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## and \u5169\u8005\u7686\u771f\u70ba\u771f\n",
"## or \u5169\u8005\u6709\u4e00\u771f\u70ba\u771f\n",
"## not \u5426\u5b9a\n",
"#\u771f\u503c\u8868\n",
"\n",
"\n",
"a = True and True\n",
"b = True and False\n",
"c = False and False\n",
"d = True or True\n",
"e = True or False\n",
"f = False or False\n",
"g = not False\n",
"\n",
"print a, b, c, d, e, f, g"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True False False True True False True\n"
]
}
],
"prompt_number": 130
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# if, elif and else\n",
"\n",
"a = -1\n",
"\n",
"\n",
"if a > 3:\n",
" print \"a > 3\"\n",
"elif a == 3:\n",
" print \"a = 3\"\n",
"else:\n",
" print \"a < 3\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a < 3\n"
]
}
],
"prompt_number": 131
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"\u7269\u4ef6\u8207\u65b9\u6cd5"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Python \u662f\u7269\u4ef6\u5c0e\u5411\u8a9e\u8a00\n",
"#\u6bcf\u500b\u6771\u897f\u90fd\u662f\u7269\u4ef6\uff0c\u4e0d\u540c\u7684\u7269\u4ef6\u4e4b\u4e2d\u6709\u4e0d\u540c\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\n",
"\n",
"class car:\n",
" wheel = 4\n",
" max_speed = 90\n",
" \n",
" #\u5efa\u7acb\u7269\u4ef6\u6642\u81ea\u52d5\u547c\u53eb\u7684\u65b9\u6cd5\uff0c\u53c3\u6578\u9810\u8a2d\u8981\u52a0\u5165\"self\"\n",
" def __init__(self):\n",
" print \"Let's Go!\"\n",
" \n",
" def get_wheel(self):\n",
" print \"wheel = \", self.wheel\n",
" \n",
" def set_max_speed(self,speed):\n",
" if speed > self.max_speed:\n",
" print \"Too Fast!\"\n",
" elif (speed <= self.max_speed) & (speed > 0):\n",
" print \"Safe!\"\n",
" else:\n",
" print \"Stop\"\n",
" \n",
"\n",
"#\u6211\u5011\u4e0d\u80fd\u76f4\u63a5\u64cd\u4f5c\u7269\u4ef6\uff0c\u8981\u64cd\u4f5c\u7269\u4ef6\u4e4b\u524d\uff0c\u5fc5\u9808\u5c07\u7269\u4ef6\u5be6\u9ad4\u5316\n",
"car = car()\n",
"car.get_wheel()\n",
"car.set_max_speed(0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Let's Go!\n",
"wheel = 4\n",
"Stop\n"
]
}
],
"prompt_number": 132
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"##\u5efa\u7acb\u65b9\u6cd5\n",
"\n",
"def test():\n",
" print \"It's my first function in Python!\"\n",
" \n",
"test()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"It's my first function in Python!\n"
]
}
],
"prompt_number": 133
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## \u52a0\u5165\u53c3\u6578\n",
"\n",
"def tax(income):\n",
" tax = 1.035\n",
" return income * tax\n",
"\n",
"\n",
"tax(50000)\n",
"# str(tax(50000))\n",
"\n",
"#\u6709\u95dc\u6d6e\u9ede\u6578\u904b\u7b97\u7684\u8aa4\u5dee\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7:\n",
"#http://www.codedata.com.tw/python/python-tutorial-the-2nd-class-1-numeric-types-and-string/"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 134,
"text": [
"51749.99999999999"
]
}
],
"prompt_number": 134
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#PYTHON\u5728\u8a2d\u5b9a\u53c3\u6578\u7684\u6642\u5019\uff0c\u4e0d\u7528\u6307\u5b9a\u53c3\u6578\u578b\u614b\n",
"\n",
"def super_print(x):\n",
" print x\n",
"\n",
"super_print(\"cool\")\n",
"super_print(13+5)\n",
"super_print([1,2,3,4,5])\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cool\n",
"18\n",
"[1, 2, 3, 4, 5]\n"
]
}
],
"prompt_number": 135
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def super_print(x):\n",
" for i in x:\n",
" print i\n",
"\n",
"super_print(\"cool\")\n",
"#super_print(13+5)\n",
"super_print([1,2,3,4,5])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"c\n",
"o\n",
"o\n",
"l\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"prompt_number": 136
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"\u7368\u7279\u7684\u8cc7\u6599\u578b\u614b:List\u548cDictionary"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#list \u5c31\u662f array\n",
"zoo_animals = [\"pangolin\", \"cassowary\", \"sloth\", \"cool\"];\n",
"\n",
"\n",
"if len(zoo_animals) > 3:\n",
"\tprint \"The first animal at the zoo is the \" + zoo_animals[0]\n",
"\tprint \"The second animal at the zoo is the \" + zoo_animals[1]\n",
"\tprint \"The third animal at the zoo is the \" + zoo_animals[2]\n",
"\tprint \"The fourth animal at the zoo is the \" + zoo_animals[3]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The first animal at the zoo is the pangolin\n",
"The second animal at the zoo is the cassowary\n",
"The third animal at the zoo is the sloth\n",
"The fourth animal at the zoo is the cool\n"
]
}
],
"prompt_number": 137
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numbers = [1,2,3,4]\n",
"print \"\u6578\u5217\u9577\u5ea6: \",len(numbers)\n",
"\n",
"print numbers[0]\n",
"print numbers[1]\n",
"print numbers[2]\n",
"print numbers[3] + numbers[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\u6578\u5217\u9577\u5ea6: 4\n",
"1\n",
"2\n",
"3\n",
"6\n"
]
}
],
"prompt_number": 138
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## change the content of a list\n",
"numbers[3] = 5\n",
"print numbers\n",
"\n",
"numbers.append(10)\n",
"print \"append : \", numbers\n",
"\n",
"numbers.insert(2,100)\n",
"print \"insert : \",numbers\n",
"\n",
"##\u53ea\u80fd\u79fb\u9664\u7b2c\u4e00\u500b\u78b0\u5230\u7684\u5143\u7d20\n",
"numbers.remove(100)\n",
"print \"remove : \",numbers\n",
"\n",
"numbers.sort()\n",
"print \"sort : \",numbers"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1, 2, 3, 5]\n",
"append : [1, 2, 3, 5, 10]\n",
"insert : [1, 2, 100, 3, 5, 10]\n",
"remove : [1, 2, 3, 5, 10]\n",
"sort : [1, 2, 3, 5, 10]\n"
]
}
],
"prompt_number": 139
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## cut the lis\n",
"numbers_a = numbers[1:4]\n",
"print numbers_a\n",
"\n",
"numbers_b = numbers[:4]\n",
"print numbers_b\n",
"\n",
"numbers_c = numbers[1::2]\n",
"print numbers_c\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[2, 3, 5]\n",
"[1, 2, 3, 5]\n",
"[2, 5]\n"
]
}
],
"prompt_number": 140
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"## \u4e09\u500d\u9577\u5ea6\n",
"print 3 * numbers\n",
"\n",
"## \u5206\u5225\u64cd\u4f5clist\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\n",
"for i in numbers:\n",
" print i \n",
" \n",
"for i in numbers:\n",
" print i * 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1, 2, 3, 5, 10, 1, 2, 3, 5, 10, 1, 2, 3, 5, 10]\n",
"1\n",
"2\n",
"3\n",
"5\n",
"10\n",
"3\n",
"6\n",
"9\n",
"15\n",
"30\n"
]
}
],
"prompt_number": 141
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Dictionary \u5c31\u662fkey and value\n",
"\n",
"residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}\n",
"\n",
"print residents['Puffin'] # Prints Puffin's room number\n",
"print residents[\"Sloth\"]\n",
"print residents[\"Burmese Python\"]\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"104\n",
"105\n",
"106\n"
]
}
],
"prompt_number": 142
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#\u5efa\u7acb\u65b0\u7684key\n",
"residents['Bryan']=107\n",
"print \"create a new key: \", residents\n",
"\n",
"#\u6539\u8b8avalue\n",
"residents['Bryan']=207\n",
"print \"change a key: \",residents['Bryan']\n",
"\n",
"#\u79fb\u9664key\n",
"\n",
"del residents[\"Bryan\"]\n",
"print \"remove a key: \",residents"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"create a new key: {'Sloth': 105, 'Puffin': 104, 'Burmese Python': 106, 'Bryan': 107}\n",
"change a key: 207\n",
"remove a key: {'Sloth': 105, 'Puffin': 104, 'Burmese Python': 106}\n"
]
}
],
"prompt_number": 143
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# \u7528for\u8ff4\u5708\u64cd\u4f5clist\n",
"\n",
"for i in residents:\n",
" print i, residents[i]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sloth 105\n",
"Puffin 104\n",
"Burmese Python 106\n"
]
}
],
"prompt_number": 144
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def room(name):\n",
" for n in residents:\n",
" if n == name:\n",
" print name, \"is living in room\",residents[n]\n",
" ##\u82e5\u5df2\u7d93\u627e\u5230\u5c0d\u8c61\uff0c\u5247\u8df3\u812b\u8ff4\u5708\n",
" break\n",
" ##\u5982\u679cfor\u8ff4\u5708\u7d50\u675f\u5f8c\u6c92\u6709\u7d50\u679c\uff0c\u5247\u8df3\u5230else\n",
" else:\n",
" print \"There is no this man\"\n",
"\n",
"room(\"Puffin\")\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Puffin is living in room 104\n"
]
}
],
"prompt_number": 145
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Dictionary \u5167\u90e8\u7684value\u53ef\u4ee5\u653e\u7684\u6771\u897f\n",
"inventory = {\n",
" 'gold' : 500,\n",
" 'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key\n",
" 'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']\n",
"}\n",
"\n",
"\n",
"# Adding a key 'burlap bag' and assigning a list to it\n",
"inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']\n",
"\n",
"# Sorting the list found under the key 'pouch'\n",
"inventory['pouch'].sort() \n",
"\n",
"# Your code here\n",
"inventory[\"pocket\"] = [\"seashell\",\"strange berry\", \"lint\"]\n",
"inventory[\"backpack\"].sort()\n",
"inventory[\"backpack\"].remove(\"dagger\")\n",
"inventory[\"gold\"]+=50\n",
"\n",
"print inventory"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'pocket': ['seashell', 'strange berry', 'lint'], 'backpack': ['bedroll', 'bread loaf', 'xylophone'], 'pouch': ['flint', 'gemstone', 'twine'], 'burlap bag': ['apple', 'small ruby', 'three-toed sloth'], 'gold': 550}\n"
]
}
],
"prompt_number": 146
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment