{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Language Intro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Agenda\n", "\n", "1. Language overview\n", "2. Built-in types, operators, and constructs\n", "3. Functions, Classes, and Modules" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Language overview\n", "\n", "Note: this is *not* a language course! Though I'll cover the important bits of the language (and standard library) that are relevant to class material, I expect you to master the language on your own time.\n", "\n", "Python ...\n", "\n", "- is *interpreted*\n", "- is *dynamically-typed*\n", "- is *automatically memory-managed*\n", "- supports *procedural*, *object-oriented*, *imperative* and *functional* programming paradigms\n", "- is designed (mostly) by one man: Guido van Rossum (aka “benevolent dictator”), and therefore has a fairly *opinionated* design\n", "- has a single reference implementation (CPython)\n", "- version 3 (the most recent version) is *not backwards-compatible* with version 2, though the latter is still widely used\n", "- has an interesting programming philosophy: \"There should be one — and preferably only one — obvious way to do it.\" (a.k.a. the \"Pythonic\" way) — see [The Zen of Python](https://www.python.org/dev/peps/pep-0020/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Built in types, operators, and constructs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# integers\n", "a = 2\n", "b = 5\n", "c = a + b\n", "d = a * (b + 2)\n", "e = b // a # integer div\n", "f = b % a # modulus, remainder\n", "g = a ** b # exponentiation (power)\n", "\n", "print(c, d, e, f, g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# floating point\n", "print(b / a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# strings (`str`)\n", "name = 'John' + ' ' + 'Doe'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are an example of a *sequence* type; https://docs.python.org/3.5/library/stdtypes.html#typesseq\n", "\n", "Other sequence types are: *ranges*, *tuples* (both also immutable), and *lists* (mutable).\n", "\n", "All immutable sequences support the [common sequence operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations), and mutable sequences additionally support the [mutable sequence operations](https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ranges & Tuples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ranges\n", "r1 = range(10)\n", "r2 = range(10, 20)\n", "r3 = range(100, 0, -10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tup = ('lions', 'tigers', 'bears', (0, 1, 2), True, False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The almighty list!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = list(range(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = []\n", "for e in range(0, 10):\n", " if 2**e > 100 or 2**e < 50:\n", " l.append(2**e)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[(e, 2**e) for e in range(0, 10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[(i, j) for i in range(5) for j in range(5, 10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import random\n", "[random() for _ in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[random()] * 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = dict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions, Classes, and Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def foo():\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Foo:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "dir(random)" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }