Skip to content

Instantly share code, notes, and snippets.

@lordpretzel
Created January 1, 2021 04:44
Show Gist options
  • Save lordpretzel/55bfee9a0655ea8d12b0705411ba68be to your computer and use it in GitHub Desktop.
Save lordpretzel/55bfee9a0655ea8d12b0705411ba68be to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "# Python Language Intro (Part 3)"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "## Agenda\n\n1. Language overview\n2. White space sensitivity\n3. Basic Types and Operations\n4. Statements & Control Structures\n5. Functions\n6. OOP (Classes, Methods, etc.)\n7. Immutable Sequence Types (Strings, Ranges, Tuples)\n8. Mutable data structures: Lists, Sets, Dictionaries"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "# by default, only the result of the last expression in a cell is displayed after evaluation.\n# the following forces display of *all* self-standing expressions in a cell.\n\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "## 7. Immutable Sequence Types: Strings, Ranges, Tuples"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "Recall: All immutable sequences support the [common sequence operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations). For many sequence types, there are constructors that allow us to create them from other sequence types."
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Strings"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s = 'hello'",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "s[0]\ns[1:3]\n'e' in s\ns + s",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "s[0] = 'j'",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "t = s\ns += s # not mutating the string!",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "t\ns",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Ranges"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "r = range(150, 10, -8)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "r[2]\nr[3:7]\n94 in r",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Tuples"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "(1, 2, 3)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "('a', 10, False, 'hello')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "tuple(range(10))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "tuple('hello')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"state": "normal"
},
"cell_type": "code",
"source": "t = tuple('hello')\n'e' in t\nt[::-1]\nt * 3",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "## 8. Mutable data structures: Lists, Sets, Dicts"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Lists"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "This list supports the [mutable sequence operations](https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types) in addition to the [common sequence operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations)."
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l = [1, 2, 1, 1, 2, 3, 3, 1]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "len(l)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l[5]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l[1:-1]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l + ['hello', 'world']",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l # `+` does *not* mutate the list!",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l * 3",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "sum = 0\nfor x in l:\n sum += x\nsum",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "#### Mutable list operations"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l = list('hell')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l.append('o')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l.append(' there')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "del l[-1]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l.extend(' there')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l[2:7]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "del l[2:7]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "l[:]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "#### List comprehensions"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "[x for x in range(10)]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "[2*x+1 for x in range(10)] # odd numbers",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "adjs = ('hot', 'blue', 'quick')\nnouns = ('table', 'fox', 'sky')\n[adj + ' ' + noun for adj in adjs for noun in nouns]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "# pythagorean triples\nn = 50\n[(a,b,c) for a in range(1,n) \n for b in range(a,n) \n for c in range(b,n) \n if a**2 + b**2 == c**2]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Sets"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "A [set](https://docs.python.org/3.7/library/stdtypes.html#set-types-set-frozenset) is a data structure that represents an *unordered* collection of unique objects (like the mathematical set). "
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s = {1, 2, 1, 1, 2, 3, 3, 1}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "t = {2, 3, 4, 5}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s.union(t)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s.difference(t)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "s.intersection(t)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "### Dicts"
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "A [dictionary](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict) is a data structure that contains a set of unique key → value mappings. "
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "d = {\n 'Superman': 'Clark Kent',\n 'Batman': 'Bruce Wayne',\n 'Spiderman': 'Peter Parker',\n 'Ironman': 'Tony Stark'\n}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "d['Ironman']",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "d['Ironman'] = 'James Rhodes'",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "d",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal"
},
"cell_type": "markdown",
"source": "#### Dictionary comprehensions"
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "{e:2**e for e in range(0,100,10)}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "{x:y for x in range(3) for y in range(10)}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"state": "normal",
"trusted": true
},
"cell_type": "code",
"source": "sentence = 'a man a plan a canal panama'\n{w:w[::-1] for w in sentence.split()}",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"mimir": {
"project_id": "a76b0e47-b067-496f-bf4a-a4f0d87f58d9",
"last_submission_id": "",
"data": {}
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment