Created
December 8, 2019 08:43
-
-
Save muratxs/0b4c2c1aa39422df64afe578a096e485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 7 Tricks to Write Better Python Code" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1 - Use \"enumerate\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cities = [\"barcelona\", \"madrid\", \"istanbul\", \"paris\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 barcelona\n", | |
"1 madrid\n", | |
"2 istanbul\n", | |
"3 paris\n" | |
] | |
} | |
], | |
"source": [ | |
"i = 0\n", | |
"\n", | |
"for city in cities:\n", | |
" print(i, city)\n", | |
" i+=1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 barcelona\n", | |
"1 madrid\n", | |
"2 istanbul\n", | |
"3 paris\n" | |
] | |
} | |
], | |
"source": [ | |
"for i, city in enumerate(cities):\n", | |
" print(i, city)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 2 - Use \"zip\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x_list = [1,2,3]\n", | |
"y_list = [2,4,6]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 2\n", | |
"2 4\n", | |
"3 6\n" | |
] | |
} | |
], | |
"source": [ | |
"for i in range(len(x_list)):\n", | |
" x = x_list[i]\n", | |
" y = y_list[i]\n", | |
" print(x, y)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 2\n", | |
"2 4\n", | |
"3 6\n" | |
] | |
} | |
], | |
"source": [ | |
"for x,y in zip(x_list, y_list):\n", | |
" print(x, y)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 3 - Use \"swap\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = 10\n", | |
"y = -10" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before : x = 10, y = -10\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Before : x = %d, y = %d\" %(x,y))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"tmp = y\n", | |
"y = x\n", | |
"x = tmp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"After : x = -10, y = 10\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"After : x = %d, y = %d\" %(x,y))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before : x = -10, y = 10\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Before : x = %d, y = %d\" %(x,y))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x, y = y, x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"After : x = 10, y = -10\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"After : x = %d, y = %d\" %(x,y))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 4 - Use \"get\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ages = {\n", | |
" \"Mary\" : 31,\n", | |
" \"Jonathan\" : 28\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# age = ages[\"Dick\"] this code has error, because dictionary has'nt \"Dick\" key." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Dick is unknown years old.\n" | |
] | |
} | |
], | |
"source": [ | |
"if \"Dick\" in ages:\n", | |
" age = ages[\"Dick\"]\n", | |
"else: \n", | |
" age = \"unknown\"\n", | |
" \n", | |
"print(\"Dick is %s years old.\"%age)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Dick is unknown years old.\n" | |
] | |
} | |
], | |
"source": [ | |
"age = ages.get(\"Dick\", \"unknown\")\n", | |
"print(\"Dick is %s years old.\"%age)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 5 - Use \"else\" after for loop." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"needle = \"e\"\n", | |
"haystack = [\"a\", \"b\", \"c\",\"d\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Not found!\n" | |
] | |
} | |
], | |
"source": [ | |
"found = False\n", | |
"\n", | |
"for letter in haystack:\n", | |
" if needle == letter:\n", | |
" print(\"Found!\")\n", | |
" found = True\n", | |
" break\n", | |
" \n", | |
"if not found:\n", | |
" print(\"Not found!\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Not found!\n" | |
] | |
} | |
], | |
"source": [ | |
"for letter in haystack:\n", | |
" if needle == letter:\n", | |
" print(\"Found!\")\n", | |
" break\n", | |
"else: # if no break occured.\n", | |
" print(\"Not found!\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 6 - Use \"with\"." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello world.\n", | |
"how areu?\n", | |
"im good.\n" | |
] | |
} | |
], | |
"source": [ | |
"f = open(\"text.txt\")\n", | |
"\n", | |
"text = f.read()\n", | |
"for line in text.split(\"\\n\"):\n", | |
" print(line)\n", | |
" \n", | |
"f.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello world.\n", | |
"\n", | |
"how areu?\n", | |
"\n", | |
"im good.\n" | |
] | |
} | |
], | |
"source": [ | |
"f = open(\"text.txt\")\n", | |
"\n", | |
"for line in f:\n", | |
" print(line)\n", | |
"\n", | |
"f.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Best Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello world.\n", | |
"\n", | |
"how areu?\n", | |
"\n", | |
"im good.\n" | |
] | |
} | |
], | |
"source": [ | |
"with open(\"text.txt\") as f:\n", | |
" for line in f:\n", | |
" print(line)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 7 - Use \"try - catch\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Converting!\n", | |
"1\n", | |
"Done\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Converting!\")\n", | |
"print(int(\"1\"))\n", | |
"print(\"Done\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### - Bad Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Converting!\n", | |
"Conversion successful!\n", | |
"Done\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Converting!\")\n", | |
"\n", | |
"try:\n", | |
" int(\"1\")\n", | |
"except:\n", | |
" print(\"Conversion failed!\")\n", | |
"else:\n", | |
" print(\"Conversion successful!\")\n", | |
"finally:\n", | |
" print(\"Done\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### + Good Way" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Converting!\n", | |
"Conversion failed!\n", | |
"Done\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"Converting!\")\n", | |
"\n", | |
"try:\n", | |
" int(\"x\")\n", | |
"except:\n", | |
" print(\"Conversion failed!\")\n", | |
"else:\n", | |
" print(\"Conversion successful!\")\n", | |
"finally:\n", | |
" print(\"Done\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Source : https://www.youtube.com/watch?v=pQmKDXUh2I8**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Source created by Sebastiaan Mathôt Youtube Channel.**" | |
] | |
} | |
], | |
"metadata": { | |
"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.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment