{ "cells": [ { "cell_type": "markdown", "id": "0af955cc", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python from Scratch\n", "\n", "Feng Li\n", "\n", "School of Statistics and Mathematics\n", "\n", "Central University of Finance and Economics\n", "\n", "[feng.li@cufe.edu.cn](mailto:feng.li@cufe.edu.cn)\n", "\n", "[https://feng.li/python](https://feng.li/python)" ] }, { "cell_type": "markdown", "id": "571b1bd8", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Using Python as a calculator" ] }, { "cell_type": "code", "execution_count": 1, "id": "81acf6d1", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 3 " ] }, { "cell_type": "code", "execution_count": 2, "id": "7c76d33a", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17 - 4" ] }, { "cell_type": "code", "execution_count": 3, "id": "af405ad7", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4/2" ] }, { "cell_type": "code", "execution_count": 3, "id": "f2b9d073", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "27" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 + 4)*3" ] }, { "cell_type": "markdown", "id": "05efb85e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- The remainder can be calculated with the `%` operator" ] }, { "cell_type": "code", "execution_count": 10, "id": "eff94c68", "metadata": { "scrolled": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "x = 2004\n", "print(x % 100 == 0)\n", "print(x % 4 == 0)" ] }, { "cell_type": "markdown", "id": "26437f53", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- **Powers** With Python, use the \\*\\* operator to calculate powers. " ] }, { "cell_type": "code", "execution_count": 12, "id": "ee21a4cc", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "25\n", "10\n" ] } ], "source": [ "print(5^2) # ^ XOR\n", "\n", "print(5**2)\n", "\n", "print(5*2)" ] }, { "cell_type": "markdown", "id": "7eac0b43", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- **Note** Caret (^) invokes the `exclusive OR`(XOR) of the object: a logical operation that outputs true only when both inputs differ (one is true, the other is false). " ] }, { "cell_type": "code", "execution_count": 7, "id": "a452ab59", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True^True" ] }, { "cell_type": "code", "execution_count": 8, "id": "670018f3", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False^False" ] }, { "cell_type": "code", "execution_count": 9, "id": "e21fe646", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True^False" ] }, { "cell_type": "code", "execution_count": 10, "id": "e723f904", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2^3" ] }, { "cell_type": "code", "execution_count": 11, "id": "6259cedd", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'10'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(2,'b')" ] }, { "cell_type": "code", "execution_count": 12, "id": "8b2d11a1", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'11'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(3,'b')" ] }, { "cell_type": "code", "execution_count": 13, "id": "147dc347", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('01',2)" ] }, { "cell_type": "markdown", "id": "e916737b", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt. " ] }, { "cell_type": "code", "execution_count": 13, "id": "7724f90f", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "b = 5\n", "c = a + b\n", "c" ] }, { "cell_type": "markdown", "id": "1ab77f9c", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "If you forget to assign a value, you could use the underscore (`_`) to fetch the last output. " ] }, { "cell_type": "markdown", "id": "67eabc4c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "In interactive mode, the last printed expression is assigned to the variable \\_. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations." ] }, { "cell_type": "code", "execution_count": 14, "id": "8acdf20a", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "33.333333333333336" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100/3.0" ] }, { "cell_type": "code", "execution_count": 16, "id": "814d90f3", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "33.333333333333336" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "_" ] }, { "cell_type": "markdown", "id": "683f9647", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Strings" ] }, { "cell_type": "markdown", "id": "24bdd209", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes (`'...'`) or double quotes (`\"...\"`) with the same result. " ] }, { "cell_type": "code", "execution_count": 16, "id": "83165e5d", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "LastName = \"Li\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "1959459d", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "FirstName = \"Feng\"" ] }, { "cell_type": "markdown", "id": "ee569b44", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- `\\` can be used to escape quotes:" ] }, { "cell_type": "code", "execution_count": 19, "id": "a90f8e0e", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello \n", " World!\n" ] } ], "source": [ "print(\"Hello \\n World!\")" ] }, { "cell_type": "markdown", "id": "0ce5462a", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- If you don't want characters prefaced by `\\` to be interpreted as special characters, you can use raw strings by adding an `r` before the first quote:" ] }, { "cell_type": "code", "execution_count": 3, "id": "e6b8db89", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello \\n World!\n", "Hello \\n World!\n" ] } ], "source": [ "print(r\"Hello \\n World!\")\n", "print(\"Hello \\\\n World!\")" ] }, { "cell_type": "markdown", "id": "775d4430", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Strings can be concatenated (glued together) with the `+` operator, and repeated with `*`:" ] }, { "cell_type": "code", "execution_count": 21, "id": "f2efcd39", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'I Looooove you'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I \" + 'L' + 'o'*5 + 've' + ' you'" ] }, { "cell_type": "markdown", "id": "d3dab3d7", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- The built-in function `len()` returns the length of a string:" ] }, { "cell_type": "code", "execution_count": 24, "id": "28c35a9c", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "love is \"love\" \n" ] } ], "source": [ "print(len(\"Feng Li\"))\n", "print('love is \"love\" ')" ] }, { "cell_type": "markdown", "id": "dffb650c", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated. This feature is particularly useful when you want to break long strings:" ] }, { "cell_type": "code", "execution_count": 23, "id": "990b54e6", "metadata": { "scrolled": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'FengLi'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Feng\" \"Li\"" ] }, { "cell_type": "code", "execution_count": 25, "id": "88e6ca59", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi, my name is Feng Li. And I am from Beijing.\n" ] } ], "source": [ "print(\"Hi, my name is Feng Li.\" \n", " \" And I am from Beijing.\")" ] }, { "cell_type": "markdown", "id": "57f8f83e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:" ] }, { "cell_type": "code", "execution_count": 26, "id": "53b90d5c", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name = \"Feng Li\"\n", "# 0123456\n", "# ...-4-3-2-1\n", "Name[1]" ] }, { "cell_type": "code", "execution_count": 26, "id": "eb23c9c9", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'i'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[-1]" ] }, { "cell_type": "code", "execution_count": 27, "id": "39e32ef7", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'L'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[-2]" ] }, { "cell_type": "markdown", "id": "4f5e9974", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- In addition to indexing, **slicing** is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:" ] }, { "cell_type": "code", "execution_count": 28, "id": "2a159a48", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Feng'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# \"Feng Li\"\n", "Name[0:4] # [a:b] - > [a, b)" ] }, { "cell_type": "code", "execution_count": 29, "id": "bd39d1eb", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Feng'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[:4] # 0,1,2,3 4)" ] }, { "cell_type": "code", "execution_count": 30, "id": "243b91ef", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Li'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[5:]" ] }, { "cell_type": "code", "execution_count": 31, "id": "c0590903", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Li'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[-2:]" ] }, { "cell_type": "markdown", "id": "544042a6", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- However, out of range slice indexes are handled gracefully when used for slicing:" ] }, { "cell_type": "code", "execution_count": 32, "id": "0cd70cf5", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'Li'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Name[5:100]" ] }, { "cell_type": "markdown", "id": "00646b0b", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Lists\n", "\n", "Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type." ] }, { "cell_type": "code", "execution_count": 33, "id": "f12d2336", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "values = [1,5,7,9,12]" ] }, { "cell_type": "code", "execution_count": 34, "id": "e90fb66c", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(values)" ] }, { "cell_type": "code", "execution_count": 35, "id": "9c9db6d9", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values[0]" ] }, { "cell_type": "code", "execution_count": 36, "id": "acea9ff2", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[9, 12]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values[-2:]" ] }, { "cell_type": "markdown", "id": "a90d6f81", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Lists also supports operations like concatenation:" ] }, { "cell_type": "code", "execution_count": 37, "id": "f1f37dc0", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 5, 7, 9, 12, '22', '33']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values + [\"22\",\"33\"]" ] }, { "cell_type": "markdown", "id": "bfd79229", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Lists are a mutable type, i.e. it is possible to change their content:" ] }, { "cell_type": "code", "execution_count": 5, "id": "bd4aea1c", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 67, 22]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = [1,2,3,4,67,22]\n", "values" ] }, { "cell_type": "code", "execution_count": 6, "id": "b9a97022", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 1000, 4, 67, 22]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values[2] = 1000\n", "values" ] }, { "cell_type": "markdown", "id": "b55e1290", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "You can also add new items at the end of the list, by using the append() method" ] }, { "cell_type": "code", "execution_count": 7, "id": "75b38879", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 1000, 4, 67, 22, 9999]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values.append(9999)\n", "values" ] }, { "cell_type": "markdown", "id": "cef41eee", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:\n", "\n", "[1, 2, 1000, 4, 67, 22, 9999]\n", "0 1 2 3 4 5 6\n", "[1, 2, 2, 3, 4, 67, 22, 9999]" ] }, { "cell_type": "code", "execution_count": 41, "id": "749c6672", "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 2, 3, 4, 67, 22, 9999]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values[2:4] = [2,3,4]\n", "values" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.8" }, "rise": { "auto_select": "first", "autolaunch": false, "enable_chalkboard": true, "start_slideshow_at": "selected", "theme": "black" } }, "nbformat": 4, "nbformat_minor": 5 }