├── .gitignore ├── README.md ├── environment.yml ├── problem.ipynb ├── solution.ipynb └── workshop.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.ipynb_checkpoints 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PY01 - Introduction to Python Programming 2 | 3 | ## Python fundamentals 4 | 5 | Welcome to the first UCL Data Science Society Workshop of the year!! 6 | 7 | In this workshop we will set you up with the tools that we will use for the rest of term and introduce you to ***variables***, ***data types*** and ***operators*** in Python. 8 | 9 | These act as the foundations for developing more complicated and advanced workflows that you often see in Data Science and Sofware Development applications. 10 | 11 | >***Author***: Tony Wu, Head of Science (20/21), UCL Data Science Society
12 | >**Updated** Philip Wilkinson, Head of Science (21/22) UCL Data Science Society (philip.wilkinson.19@ucl.ac.uk)
13 | 14 | ## Requirements 15 | 16 | Prior to this lecture please install 17 | - [Anaconda](https://www.anaconda.com/products/individual) 18 | - [Git](https://git-scm.com/downloads) 19 | 20 | To install the environment: 21 | 22 | 1. Search your laptop for `Anaconda Prompt (Anaconda 3)` 23 | 24 | 2. Navigate to the directory you have the current documents i.e. 25 | 26 | `cd Docuemnts/Data_Science/DSS/lectures/fundamental-python-workshop` 27 | 28 | 3. Create a new environmt using the environment.yml 29 | 30 | `conda env create -f environment.yml source` 31 | 32 | 4. Create an Ipython kernal for the `DSS` environment 33 | 34 | `python -m ipykernel install --user --name DSS --display-name DSS` 35 | 36 | 5. Active the environment 37 | 38 | `conda activate DSS` 39 | 40 | 6. Launch Jupyter notebook 41 | 42 | `jupyter notebook` 43 | 44 | >***Proudly presented by the UCL Data Science Society*** 45 | 46 | 47 | ## Structure 48 | 49 | ```shell 50 | 51 | ├── PY01 - Introduction to Python Programming - Logic 52 | │ ├── README.md 53 | │ ├── problem.ipynb 54 | │ ├── solution.ipynb 55 | └── workshop.ipynb 56 | 57 | ``` 58 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: DSS 2 | channels: 3 | - conda-forge 4 | - defaults 5 | dependencies: 6 | - argon2-cffi=20.1.0=py39hb82d6ee_2 7 | - async_generator=1.10=py_0 8 | - attrs=21.2.0=pyhd8ed1ab_0 9 | - backcall=0.2.0=pyh9f0ad1d_0 10 | - backports=1.0=py_2 11 | - backports.functools_lru_cache=1.6.4=pyhd8ed1ab_0 12 | - bleach=4.0.0=pyhd8ed1ab_0 13 | - boost-cpp=1.74.0=h5b4e17d_4 14 | - bzip2=1.0.8=h8ffe710_4 15 | - ca-certificates=2021.5.30=h5b45459_0 16 | - cairo=1.16.0=hb19e0ff_1008 17 | - certifi=2021.5.30=py39hcbf5309_0 18 | - cffi=1.14.6=py39h0878f49_0 19 | - cfitsio=3.470=h0af3d06_7 20 | - click=7.1.2=pyh9f0ad1d_0 21 | - click-plugins=1.1.1=py_0 22 | - cligj=0.7.2=pyhd8ed1ab_0 23 | - colorama=0.4.4=pyh9f0ad1d_0 24 | - curl=7.78.0=h789b8ee_0 25 | - cycler=0.10.0=py_2 26 | - debugpy=1.4.1=py39h415ef7b_0 27 | - decorator=5.0.9=pyhd8ed1ab_0 28 | - defusedxml=0.7.1=pyhd8ed1ab_0 29 | - entrypoints=0.3=pyhd8ed1ab_1003 30 | - expat=2.4.1=h39d44d4_0 31 | - fiona=1.8.20=py39hea8b339_1 32 | - fontconfig=2.13.1=h1989441_1005 33 | - freetype=2.10.4=h546665d_1 34 | - freexl=1.0.6=ha8e266a_0 35 | - gdal=3.3.1=py39h6795fcd_1 36 | - geopandas=0.9.0=pyhd8ed1ab_1 37 | - geopandas-base=0.9.0=pyhd8ed1ab_1 38 | - geos=3.9.1=h39d44d4_2 39 | - geotiff=1.6.0=ha8a8a2d_6 40 | - gettext=0.19.8.1=h1a89ca6_1005 41 | - greenlet=1.1.1=py39h415ef7b_0 42 | - hdf4=4.2.15=h0e5069d_3 43 | - hdf5=1.10.6=nompi_h5268f04_1114 44 | - icu=68.1=h0e60522_0 45 | - importlib-metadata=4.6.3=py39hcbf5309_0 46 | - intel-openmp=2021.3.0=h57928b3_3372 47 | - ipykernel=6.0.3=py39h832f523_0 48 | - ipython=7.26.0=py39h832f523_0 49 | - ipython_genutils=0.2.0=py_1 50 | - ipywidgets=7.6.3=pyhd3deb0d_0 51 | - jbig=2.1=h8d14728_2003 52 | - jedi=0.18.0=py39hcbf5309_2 53 | - jinja2=3.0.1=pyhd8ed1ab_0 54 | - joblib=1.0.1=pyhd8ed1ab_0 55 | - jpeg=9d=h8ffe710_0 56 | - jsonschema=3.2.0=pyhd8ed1ab_3 57 | - jupyter=1.0.0=py39hcbf5309_6 58 | - jupyter_client=6.1.12=pyhd8ed1ab_0 59 | - jupyter_console=6.4.0=pyhd8ed1ab_0 60 | - jupyter_core=4.7.1=py39hcbf5309_0 61 | - jupyterlab_pygments=0.1.2=pyh9f0ad1d_0 62 | - jupyterlab_widgets=1.0.0=pyhd8ed1ab_1 63 | - kealib=1.4.14=h96bfa42_2 64 | - kiwisolver=1.3.1=py39h2e07f2f_1 65 | - krb5=1.19.2=hbae68bd_0 66 | - lcms2=2.12=h2a16943_0 67 | - lerc=2.2.1=h0e60522_0 68 | - libblas=3.9.0=11_win64_mkl 69 | - libcblas=3.9.0=11_win64_mkl 70 | - libclang=11.1.0=default_h5c34c98_1 71 | - libcurl=7.78.0=h789b8ee_0 72 | - libdeflate=1.7=h8ffe710_5 73 | - libffi=3.3=h0e60522_2 74 | - libgdal=3.3.1=ha6cada3_1 75 | - libglib=2.68.3=h1e62bf3_0 76 | - libiconv=1.16=he774522_0 77 | - libkml=1.3.0=h9859afa_1014 78 | - liblapack=3.9.0=11_win64_mkl 79 | - libnetcdf=4.8.0=nompi_hf689e7d_103 80 | - libpng=1.6.37=h1d00b33_2 81 | - libpq=13.3=hfcc5ef8_0 82 | - librttopo=1.1.0=hb340de5_6 83 | - libsodium=1.0.18=h8d14728_1 84 | - libspatialindex=1.9.3=h39d44d4_4 85 | - libspatialite=5.0.1=h762a7f4_5 86 | - libssh2=1.9.0=h680486a_6 87 | - libtiff=4.3.0=h0c97f57_1 88 | - libwebp-base=1.2.0=h8ffe710_2 89 | - libxml2=2.9.12=hf5bbc77_0 90 | - libzip=1.8.0=hfed4ece_0 91 | - lz4-c=1.9.3=h8ffe710_1 92 | - m2w64-gcc-libgfortran=5.3.0=6 93 | - m2w64-gcc-libs=5.3.0=7 94 | - m2w64-gcc-libs-core=5.3.0=7 95 | - m2w64-gmp=6.1.0=2 96 | - m2w64-libwinpthread-git=5.0.0.4634.697f757=2 97 | - mapclassify=2.4.3=pyhd8ed1ab_0 98 | - markupsafe=2.0.1=py39hb82d6ee_0 99 | - matplotlib=3.4.2=py39hcbf5309_0 100 | - matplotlib-base=3.4.2=py39h581301d_0 101 | - matplotlib-inline=0.1.2=pyhd8ed1ab_2 102 | - mistune=0.8.4=py39hb82d6ee_1004 103 | - mkl=2021.3.0=hb70f87d_564 104 | - msys2-conda-epoch=20160418=1 105 | - munch=2.5.0=py_0 106 | - nbclient=0.5.3=pyhd8ed1ab_0 107 | - nbconvert=6.1.0=py39hcbf5309_0 108 | - nbformat=5.1.3=pyhd8ed1ab_0 109 | - nest-asyncio=1.5.1=pyhd8ed1ab_0 110 | - networkx=2.3=py_0 111 | - notebook=6.4.3=pyha770c72_0 112 | - numpy=1.21.1=py39h6635163_0 113 | - olefile=0.46=pyh9f0ad1d_1 114 | - openjpeg=2.4.0=hb211442_1 115 | - openssl=1.1.1k=h8ffe710_0 116 | - packaging=21.0=pyhd8ed1ab_0 117 | - pandas=1.3.1=py39h2e25243_0 118 | - pandoc=2.14.1=h8ffe710_0 119 | - pandocfilters=1.4.2=py_1 120 | - parso=0.8.2=pyhd8ed1ab_0 121 | - patsy=0.5.1=py_0 122 | - pcre=8.45=h0e60522_0 123 | - pickleshare=0.7.5=py_1003 124 | - pillow=8.3.1=py39h916092e_0 125 | - pip=21.2.3=pyhd8ed1ab_0 126 | - pixman=0.40.0=h8ffe710_0 127 | - plotly=5.1.0=pyhd8ed1ab_1 128 | - poppler=21.03.0=h9ff6ed8_0 129 | - poppler-data=0.4.10=0 130 | - postgresql=13.3=h1c22c4f_0 131 | - proj=8.0.1=h1cfcee9_0 132 | - prometheus_client=0.11.0=pyhd8ed1ab_0 133 | - prompt-toolkit=3.0.19=pyha770c72_0 134 | - prompt_toolkit=3.0.19=hd8ed1ab_0 135 | - pycparser=2.20=pyh9f0ad1d_2 136 | - pygments=2.9.0=pyhd8ed1ab_0 137 | - pyparsing=2.4.7=pyh9f0ad1d_0 138 | - pyproj=3.1.0=py39ha996c60_3 139 | - pyqt=5.12.3=py39hcbf5309_7 140 | - pyqt-impl=5.12.3=py39h415ef7b_7 141 | - pyqt5-sip=4.19.18=py39h415ef7b_7 142 | - pyqtchart=5.12=py39h415ef7b_7 143 | - pyqtwebengine=5.12.1=py39h415ef7b_7 144 | - pyrsistent=0.17.3=py39hb82d6ee_2 145 | - python=3.9.6=h7840368_1_cpython 146 | - python-dateutil=2.8.2=pyhd8ed1ab_0 147 | - python_abi=3.9=2_cp39 148 | - pytz=2021.1=pyhd8ed1ab_0 149 | - pywin32=301=py39hb82d6ee_0 150 | - pywinpty=1.1.3=py39h99910a6_0 151 | - pyzmq=22.2.1=py39he46f08e_0 152 | - qt=5.12.9=h5909a2a_4 153 | - qtconsole=5.1.1=pyhd8ed1ab_0 154 | - qtpy=1.9.0=py_0 155 | - rtree=0.9.7=py39h09fdee3_2 156 | - scikit-learn=0.24.2=py39he931e04_1 157 | - scipy=1.7.1=py39hc0c34ad_0 158 | - seaborn=0.11.1=hd8ed1ab_1 159 | - seaborn-base=0.11.1=pyhd8ed1ab_1 160 | - send2trash=1.8.0=pyhd8ed1ab_0 161 | - setuptools=49.6.0=py39hcbf5309_3 162 | - shapely=1.7.1=py39haadaec5_5 163 | - six=1.16.0=pyh6c4a22f_0 164 | - sqlalchemy=1.4.22=py39hb82d6ee_0 165 | - sqlite=3.36.0=h8ffe710_0 166 | - statsmodels=0.12.2=py39h5d4886f_0 167 | - tbb=2021.3.0=h2d74725_0 168 | - tenacity=8.0.1=pyhd8ed1ab_0 169 | - terminado=0.10.1=py39hcbf5309_0 170 | - testpath=0.5.0=pyhd8ed1ab_0 171 | - threadpoolctl=2.2.0=pyh8a188c0_0 172 | - tiledb=2.3.3=h78dabda_0 173 | - tk=8.6.10=h8ffe710_1 174 | - tornado=6.1=py39hb82d6ee_1 175 | - traitlets=5.0.5=py_0 176 | - tzdata=2021a=he74cb21_1 177 | - ucrt=10.0.20348.0=h57928b3_0 178 | - vc=14.2=hb210afc_5 179 | - vs2015_runtime=14.29.30037=h902a5da_5 180 | - wcwidth=0.2.5=pyh9f0ad1d_2 181 | - webencodings=0.5.1=py_1 182 | - wheel=0.37.0=pyhd8ed1ab_0 183 | - widgetsnbextension=3.5.1=py39hcbf5309_4 184 | - wincertstore=0.2=py39hcbf5309_1006 185 | - winpty=0.4.3=4 186 | - xerces-c=3.2.3=h0e60522_2 187 | - xz=5.2.5=h62dcd97_1 188 | - zeromq=4.3.4=h0e60522_0 189 | - zipp=3.5.0=pyhd8ed1ab_0 190 | - zlib=1.2.11=h62dcd97_1010 191 | - zstd=1.5.0=h6255e5f_0 192 | prefix: C:\Users\phili\anaconda3\envs\DSS 193 | -------------------------------------------------------------------------------- /problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Data Science Workshop 1: Fundamentals of Python (Solutions)\n", 8 | "\n", 9 | "Feel free to go through to refer back to the workshop content when solving these problems!\n", 10 | "\n", 11 | "## Debugging problems\n", 12 | "The following problems will have written code that is incorrect in some way. Your job will be to debug it and fix it so that it works as intended. When debugging code the error messages can often tell you where a problem may lie. Here the comments indicate what the code is intended to do.\n", 13 | "\n", 14 | "### 1) \n", 15 | "\n", 16 | "Print:\n", 17 | "\n", 18 | "```\n", 19 | "Hello World\n", 20 | "\n", 21 | "My name is: \n", 22 | "\n", 23 | "I am studying: \n", 24 | "\n", 25 | "I am excited to learn Data Science\n", 26 | "```" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "print(\"Hello World\")\n", 36 | "\n", 37 | "print(\"My name is: \", ??)\n", 38 | "\n", 39 | "print(\"I am studying:\", ??)\n", 40 | "\n", 41 | "print(\"I am excited to learn Data Science\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "### 2) \n", 49 | "\n", 50 | "Assign \"3\", 6.2, False to a, b and c and print the results:" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "a, b , ?? = \"3\", ??, False\n", 60 | "\n", 61 | "print(a)\n", 62 | "\n", 63 | "print(b)\n", 64 | "\n", 65 | "print(??)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "### 3)\n", 73 | "\n", 74 | "Perform the following mathematical operations:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "#divide 56 by 7\n", 84 | "print(56??7)\n", 85 | "\n", 86 | "#Find the remainder of 131 divided by 13\n", 87 | "print(131??13)\n", 88 | "\n", 89 | "#find how many whole times 56 goes into 15426\n", 90 | "print(15426??56)\n", 91 | "\n", 92 | "#find 7 to the power of 3\n", 93 | "print(7??3)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "## Coding from scratch\n", 101 | "\n", 102 | "This section will include problems where no code is given.\n", 103 | "\n", 104 | "### 1) \n", 105 | "\n", 106 | "Define a variable `c` to be a string of numbers $367$. Multiply this by 2. Convert the result to a integer in a variable `b` and divide by $6*12$ and print the result." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3 (ipykernel)", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.9.6" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 2 138 | } 139 | -------------------------------------------------------------------------------- /solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Data Science Workshop 1: Fundamentals of Python (Solutions)\n", 8 | "\n", 9 | "Feel free to go through to refer back to the workshop content when solving these problems!\n", 10 | "\n", 11 | "## Debugging problems\n", 12 | "The following problems will have written code that is incorrect in some way. Your job will be to debug it and fix it so that it works as intended. When debugging code the error messages can often tell you where a problem may lie. Here the comments indicate what the code is intended to do.\n", 13 | "\n", 14 | "### 1) \n", 15 | "\n", 16 | "Print:\n", 17 | "\n", 18 | "```\n", 19 | "Hello World\n", 20 | "\n", 21 | "My name is: \n", 22 | "\n", 23 | "I am studying: \n", 24 | "\n", 25 | "I am excited to learn Data Science\n", 26 | "```" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "print(\"Hello World\")\n", 36 | "\n", 37 | "print(\"My name is: \", \"Philip Wilkinson\")\n", 38 | "\n", 39 | "print(\"I am studying:\", \"Advanced Spatital Analysis\")\n", 40 | "\n", 41 | "print(\"I am excited to learn Data Science\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "### 2) \n", 49 | "\n", 50 | "Assign \"3\", 6.2, False to a, b and c and print the results:" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "a, b ,c = \"3\", 6.2, False\n", 60 | "\n", 61 | "print(a)\n", 62 | "\n", 63 | "print(b)\n", 64 | "\n", 65 | "print(c)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "### 3)\n", 73 | "\n", 74 | "Perform the following mathematical operations:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "#divide 56 by 7\n", 84 | "print(56/7)\n", 85 | "\n", 86 | "#Find the remainder of 131 divided by 13\n", 87 | "print(131%13)\n", 88 | "\n", 89 | "#find how many whole times 56 goes into 15426\n", 90 | "print(15426//56)\n", 91 | "\n", 92 | "#find 7 to the power of 3\n", 93 | "print(7**3)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "## Coding from scratch\n", 101 | "\n", 102 | "This section will include problems where no code is given.\n", 103 | "\n", 104 | "### 1) \n", 105 | "\n", 106 | "Define a variable `c` to be a string of numbers $367$. Multiply this by 2. Convert the result to a integer in a variable `b` and divide by $6*12$ and print the result." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "#define the variable\n", 116 | "c = \"367\"\n", 117 | "\n", 118 | "#multiple this by 2\n", 119 | "c = c*2\n", 120 | "\n", 121 | "#convert this to a variable as `b`\n", 122 | "b = int(c)\n", 123 | "\n", 124 | "#divide by 563/12\n", 125 | "b = b/(6*12)\n", 126 | "\n", 127 | "#print the result\n", 128 | "print(b)" 129 | ] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 3 (ipykernel)", 135 | "language": "python", 136 | "name": "python3" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 3 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython3", 148 | "version": "3.9.6" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 2 153 | } 154 | -------------------------------------------------------------------------------- /workshop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# [PY01] Introduction to Python Programming - workshop\n", 8 | "\n", 9 | "## Fundamentals of Python\n", 10 | "\n", 11 | "**Author:** Tony Wu, Head of Science (20/21), UCL DSS\n", 12 | "\n", 13 | "**Updated by:** Philip Wilkinson, Head of Science (21/22), UCL DSS\n", 14 | "\n", 15 | "**Date** 24 September 2021\n", 16 | "\n", 17 | "***Proudly presented by the UCL data Science Society***\n", 18 | "\n", 19 | "Acknowledgement The content of this workshop is inspired by [W3 schools](https://www.w3schools.com/python/default.asp)\n", 20 | "\n", 21 | "### Introduction\n", 22 | "\n", 23 | "Welcome to the first Data Science Society workshop on Python! The aim of these workshops will be to teach you enough Python so that you'll be able to use machine learning algorithms on various datasets for data analysis. We will be avoiding the more complex maths behind these algorithms leaning instead towards packages like Scikit Learn which already provide us with these algorithms. To begin with we will cover the very basics of Python. \n", 24 | "\n", 25 | "For the first term, it is expected that we will follow the following workshop outlines:\n", 26 | "\n", 27 | "- [Workshop 1: Python fundamentals](https://github.com/UCL-DSS/fundamental-python-workshop)\n", 28 | "- [Workshop 2: Python Data structures](https://github.com/UCL-DSS/python-sequence-workshop)\n", 29 | "- [Workshop 3: Python Logic](https://github.com/UCL-DSS/python-logic-workshop)\n", 30 | "- [Workshop 4: Github](https://github.com/UCL-DSS/git-workshop)\n", 31 | "- [Workshop 5: SQL](https://github.com/UCL-DSS/SQL_workshop)\n", 32 | "- [Workshop 6: Numpy](https://github.com/UCL-DSS/numpy-workshop)\n", 33 | "- [Workshop 7: Pandas](https://github.com/UCL-DSS/pandas-workshop)\n", 34 | "- [Workshop 8: Matplotlib](https://github.com/UCL-DSS/matplotlib-workshop)\n", 35 | "- [Workshop 9: Linear regression](https://github.com/UCL-DSS/linear-regression-workshop)\n", 36 | "- Workshop 10: TBC\n", 37 | "\n", 38 | "The first of these workshops is therefore Python fundamentals, with the purpose to introduce you to some of the tools we will be working with over the next two terms and, if we have, to introduce some of the basics of Python and how it works.\n", 39 | "\n", 40 | "This workshop specifcally covers:\n", 41 | "\n", 42 | "- [Jupter Notebook](#JN)\n", 43 | "- [Mardown Cells](#MC)\n", 44 | "- [Code Cells](#CC)\n", 45 | "- [Commenting](#COM)\n", 46 | "- [Print Statements](#PRINT)\n", 47 | "- [Python Variables](#PYVAR)\n", 48 | "- [Data Types](#DTYP)\n", 49 | "- [Basic Operations](#BO)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "### Jupyter Notebook\n", 64 | "Throughout these workshops we will be using the Jupyter Notebook editor. In this editor code is run in code cells and text is run in markdown cells. You can change the type of cell using at the top of the page below the words Kernel and Widgets. The options to edit cells are found at the top of the page: \"+\" to create a new cell, scissors to cut a cell etc." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### Markdown Cells\n", 79 | "The text you are reading now is in a markdown cell. To edit the text double click on the cell. To run the cell press `Shift + Enter`. These cells don't actually run code but mainly serve as a means of allowing you to describe what your code is supposed to do, so that you can inform someone else or remind yourself of what your code is doing when you revisit it. These cells also allow for the formatting of equations when you place something in between two single dollar signs. For instance: $ E = mc^2 $. Doing so between two double dollar signs places the equation on a new line like so: \n", 80 | "\n", 81 | "$$ \\nabla \\cdot B = 0. $$ \n", 82 | "\n", 83 | "Don't worry about what these equations mean but you see what can be done! \n", 84 | "\n", 85 | "In these workshops you will see much of a jupyter notebook taken up by markdown code telling you about what the next code cell is expected to do or what you should be inputting into that cell. This is unique to JupyterNotebook and is the reason why this is often used to introduce people to Python and Data Science in general." 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "### Code Cells\n", 100 | "\n", 101 | "The other type of cell in a jupyter notebook then is a Code Cell and as the name suggests, is where you actually write the code you are going to run. As with a markdown cell, you can make it work by pressing `shift + Enter` to run the code in that cell. When you run a cell, the output will be displayed below the code cell. \n", 102 | "\n", 103 | "Below is a code cell for you to run, don't worry about the code in it for now but just run it and see what happens: " 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "print(\"If you are reading this, this cell has been run!\")" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "### Commenting \n", 127 | "Inside code cells you can write notes by using the `#` symbol. Putting it in front of a line of code means that code won't be run when running the cell. This is useful when you want to see what removing a line of code briefly without deleting the line entirely and having to rewrite it again. Additionally, the # symbol is used in commenting on your code so that another user, or yourself when you return to the code can figure out what that code does. It is good practice to leave comments in your code however they should be consise to avoid overcommenting that may make your code harder to read. If ever in doubt imagine that if you ever undercomment two kittens die and if you ever overcomment one kitten dies. Below is a code cell identical to the one above save for the fact the # symbol is in front of the line of code. If you run the code you will not see any output." 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# print(\"If you are reading this, this cell has been run!\")" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "### Print Statements\n", 151 | "\n", 152 | "With that in mind then, we can start to understand some of the usefulness of Python and why we use it. One of the first funcionalities of Python is the `print` statement we used above. As the name suggests it \"prints\" the argument you pass to it. \n", 153 | "\n", 154 | "We can first see what happens if we run the original code that we ran without the `print` statement?" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "\"If you are reading this, this cell has been run!\"" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "We can see that the cell still produces the output as we did with the print statement. However, this changes when we have two lines of results we want to print:" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [ 179 | "\"Line 1\"\n", 180 | "\"Line 2\"" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "From this only the second line was printed. This is because Python, in Jupyter Notebooks, will only show the final line of output unless stated otherwise. The way to get around this therefore is to have each line inside the `print` statements to ensure that both pieces are outputted:" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "print(\"Line 1\")\n", 197 | "print(\"Line 2\")" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "Commas in ````print```` statements and indeed in all functions are used to seperate different arguments. In ````print```` statements you typically do this when \"printing\" different data types. Passing ````print(\"The population of London is\", 8.136, \"million\")```` will output ````The population of London is 8.136 million````. As can be seen each argument is printed with a space in between them. For this example the arguments in quotation marks are strings; essentially a set of characters, while the second argument is a float; essentially a number. We will discuss data types in more detail later." 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "print(\"The population of London is\", 8.136, \"million\")" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "Now use the print statement in the code cell below to print a message of your choosing." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "print()" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "### Python Variables\n", 244 | "\n", 245 | "While so far we have just printed what we wanted in the print statement, Python can also be used to store information in things called variables. Essentially these act as contains to store data variables and this is done using the `=` operator to assign the data to the specific variable. We can see this by assigning a number and a name to values:\n" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [ 254 | "#store the data in variables\n", 255 | "x = 10\n", 256 | "y = \"Peter\"\n", 257 | "\n", 258 | "#print the values held in the vraibles\n", 259 | "print(x)\n", 260 | "print(y)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "We can see that we have now stored data in variables `x` and `y` and these can be \"called\" later on to output the required information. This is useful when we may want to change the information latter on, such as by adding one, or by using the stored value mutiple times, such as using the same name over and over again. \n", 268 | "\n", 269 | "We can see this as below:" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "# Defining a variable x as 5\n", 279 | "x = 5\n", 280 | "print(\"x =\", x)\n", 281 | "\n", 282 | "# Redefining a variable x as a string \"string\"\n", 283 | "x = \"string\"\n", 284 | "print(\"x =\", x)\n", 285 | "\n", 286 | "# Redefining a variable x as 7\n", 287 | "x = 7 \n", 288 | "print(\"x =\", x)\n", 289 | "\n", 290 | "# Redefining x as 8 by adding 1 (The same can be done for other mathmatical operations)\n", 291 | "x = x + 1\n", 292 | "print(\"x =\", x)\n", 293 | "\n", 294 | "# Redefining x as 9 by adding 1 (The same can be done for other mathmatical operations)\n", 295 | "x += 1\n", 296 | "print(\"x =\", x)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "An import part of this is variable naming convention.\n", 304 | "\n", 305 | "A variable can have a short name, such as `x` and `y` that we have already done, or a more descrptive name such as `first_name`, `last_name` or `car_age`. However there are rules for this:\n", 306 | "\n", 307 | "- A variable name must start with a letter or the underscore character\n", 308 | "- A variable name cannot start with a number\n", 309 | "- A varibale name can only contain alpha-numeric characteristics and underscores (A_z, 0-9, and _)\n", 310 | "- Varibale names are case sensitive\n", 311 | "\n", 312 | "Examples that we can use are:" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "metadata": {}, 319 | "outputs": [], 320 | "source": [ 321 | "myvar = \"Peter\"\n", 322 | "my_var = \"Peter\"\n", 323 | "_my_var = \"Peter\"\n", 324 | "myVar = \"Peter\"\n", 325 | "MYVAR = \"Peter\"\n", 326 | "myvar2 = \"Peter\"" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "Examples that would produce an error would include:" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "2myvar = \"Peter\"\n", 343 | "my-var = \"Peter\"\n", 344 | "my var = \"Peter\"" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "As we get to more complicated names however that contain multiple names, we can use different conventions to make then easier to read:" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": {}, 358 | "outputs": [], 359 | "source": [ 360 | "#Camel Case\n", 361 | "#each word, except the first, starts with a capital letter\n", 362 | "firstName = \"Peter\"\n", 363 | "\n", 364 | "#Pascal Case\n", 365 | "#each word starts with a capital letter\n", 366 | "FirstName = \"Peter\"\n", 367 | "\n", 368 | "#Snake case\n", 369 | "#each word is seperated by an underscore character\n", 370 | "first_name = \"Peter\"" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "metadata": {}, 376 | "source": [ 377 | "Finally, we can also assign multiple values to multiple variables in one line:" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [ 386 | "x, y, z = \"Orange\", \"Banana\", \"Cherry\"\n", 387 | "print(x)\n", 388 | "print(y)\n", 389 | "print(z)" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "Can you use this to assign your first name, last name, age and course to four apropriate named variables and print out the results?" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": null, 402 | "metadata": {}, 403 | "outputs": [], 404 | "source": [ 405 | "print()" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "### Data Types\n", 420 | "\n", 421 | "We can see above that we have defined some variables as numbers without using speech marks, while we have defined words with speech marks. This is important as it helps us to distinguish between different datatypes, which in programming is a very important concept. \n", 422 | "\n", 423 | "Variables are used to store information, as we have already seen, but the type of that data is important for what you can do with that specific varable i.e. addition, subtraction, multiplication or list slicing. By default, Python has the following built in datatypes:\n", 424 | "\n", 425 | "- Text type: `str`\n", 426 | "- Numeric Types: `int`, `float`, `complex`\n", 427 | "- Sequence Types: `list`, `tuple`, `range`\n", 428 | "- Mapping Type: `dict`\n", 429 | "- Set Types: `set`, `frozenset`\n", 430 | "- Boolean Type: `bool`\n", 431 | "- Binary Types: `bytes`, `bytearray`, `memoryview`\n", 432 | "\n", 433 | "For our purposes here we will cover `str`, `int`, `float` and `boolean` but other datatypes are coevred later on.\n", 434 | "\n", 435 | "Firstly, we can define variables of each type and then we get the datatype of that object to see what it is:" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "a = 2\n", 445 | "b = 2.0\n", 446 | "c = \"Hello World\"\n", 447 | "d = True" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": null, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "print(type(a))\n", 457 | "print(type(b))\n", 458 | "print(type(c))\n", 459 | "print(type(d))" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "From this we can see that `a` is an `int`, `b` is a `float`, `c` is a `str` and `d` is `bool`.\n", 467 | "\n", 468 | "What this means is that:\n", 469 | "\n", 470 | "- `int` is an integer value i.e. with no decimal place\n", 471 | "- `float` is a numerial value with decimal places\n", 472 | "- `str` is a string value\n", 473 | "- `bool` can only take on `True` or `False`\n", 474 | "\n", 475 | "To make this simple and to reduce ambiguity you can set the datatype itself:" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": null, 481 | "metadata": {}, 482 | "outputs": [], 483 | "source": [ 484 | "a = int(2)\n", 485 | "b = float(2)\n", 486 | "c = str(\"Hello World\")\n", 487 | "d = bool(True)" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": null, 493 | "metadata": {}, 494 | "outputs": [], 495 | "source": [ 496 | "print(type(a))\n", 497 | "print(type(b))\n", 498 | "print(type(c))\n", 499 | "print(type(d))" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "But we can also use this method (which is known as casting) to change the datatype of a specific value. For example:" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": null, 512 | "metadata": {}, 513 | "outputs": [], 514 | "source": [ 515 | "a = 2\n", 516 | "print(type(a))\n", 517 | "b = float(a)\n", 518 | "print(type(b))" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": null, 524 | "metadata": {}, 525 | "outputs": [], 526 | "source": [ 527 | "a = \"2\"\n", 528 | "print(type(a))\n", 529 | "b = float(a)\n", 530 | "print(type(b))" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [ 539 | "a = True\n", 540 | "print(type(a))\n", 541 | "b = str(a)\n", 542 | "print(type(b))" 543 | ] 544 | }, 545 | { 546 | "cell_type": "markdown", 547 | "metadata": {}, 548 | "source": [ 549 | "Of course, in order to cast a variable we need to ensure that it is compatible otherwise an error with be thrown.\n", 550 | "\n", 551 | "Can you cast the following float to an integer and string and print out the result?" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": null, 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [ 560 | "a = 27.0\n", 561 | "print(a)\n", 562 | "b = \n", 563 | "print()\n", 564 | "c = " 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "metadata": {}, 570 | "source": [ 571 | "What happens when you try to case `a` to a `bool`?" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": null, 577 | "metadata": {}, 578 | "outputs": [], 579 | "source": [ 580 | "d = \n", 581 | "print()" 582 | ] 583 | }, 584 | { 585 | "cell_type": "markdown", 586 | "metadata": {}, 587 | "source": [ 588 | "" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "## Basic Operations\n", 596 | "\n", 597 | "As part of Python's basic functionality we also have basic operations that can be perfomed for example addition. Python divides operators into the following groups:\n", 598 | "\n", 599 | "- Arithmetic operators\n", 600 | "- Assignment operators\n", 601 | "- Comparison operators\n", 602 | "- Logical operators\n", 603 | "- Identity operators\n", 604 | "- Membership operators\n", 605 | "- Bitwise operators\n", 606 | "\n", 607 | "so far we have already seen assignment operators in effect, by assiging a value to a variable. For now we will focus on Arithmateic and Comparison operators, whereby other operators will be introduced in future lecture series.\n", 608 | "\n", 609 | "For this, basic arithemtic operators include:\n", 610 | "- `+` for addition\n", 611 | "- `-` for subtraction\n", 612 | "- `*` for multiplication\n", 613 | "- `/` for division\n", 614 | "- `%` for modulos\n", 615 | "- `**` for exponentiation\n", 616 | "- `//` for floor division\n", 617 | "\n", 618 | "We can see how these perform below:" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": null, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "# Addition\n", 628 | "print(\"Addition:\", 2 + 2)\n", 629 | "\n", 630 | "# Subtraction \n", 631 | "print(\"Subtraction:\", 5 - 2)\n", 632 | "\n", 633 | "# Multiplication\n", 634 | "print(\"Multiplication:\", 2*4)\n", 635 | "\n", 636 | "# Division\n", 637 | "print(\"Division:\", 6/3)\n", 638 | "\n", 639 | "# Powers\n", 640 | "print(\"Powers:\", 5**3)\n", 641 | "\n", 642 | "# Division without remainder \n", 643 | "print(\"Divison without remainder:\", 7//3)\n", 644 | "\n", 645 | "# Returns remainder\n", 646 | "print(\"Division returning the remainder:\", 7%3)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": {}, 652 | "source": [ 653 | "Be aware that is often easy to make syntax errors when performing mathmatical operations. Sometimes this involves writing something to the effect of ````2x```` instead of ````2*x```` where the former would look for a variable defined as ````2x```` and the latter would multiply a variable ````x```` by $2$. In large calculations when using brackets it is very easy to make mistakes so be alert for potential mistakes. Clarity of code is important to help make it easier to read and spot mistakes. Often rather than having an entire calculation in one messy line of code its often best to split the calculation into several lines that are easier to read. \n", 654 | "\n", 655 | "In the code cell below print the result of the following operations: $$ 4^7 $$ $$ 8\\times123 $$ $$ \\frac{4523}{84} $$" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": null, 661 | "metadata": {}, 662 | "outputs": [], 663 | "source": [ 664 | "# 4 to the power of 7\n", 665 | "print()\n", 666 | "\n", 667 | "# 8 x 123\n", 668 | "print()\n", 669 | "\n", 670 | "# 4523 divided by 84\n", 671 | "print()" 672 | ] 673 | }, 674 | { 675 | "cell_type": "markdown", 676 | "metadata": {}, 677 | "source": [ 678 | "Then we also have comparison operators that are used to compare values. These include:\n", 679 | "\n", 680 | "- `==` for Equal\n", 681 | "- `!=` for Not Equal\n", 682 | "- `<` for Less than\n", 683 | "- `>` for Greater Than\n", 684 | "- `>=` for Greater than or equal to\n", 685 | "- `<=` for Less than or equal to\n", 686 | "\n", 687 | "Which can be tested as follows:" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": null, 693 | "metadata": {}, 694 | "outputs": [], 695 | "source": [ 696 | "#for equal\n", 697 | "print(\"5 is equal to 5:\", 5 == 5)\n", 698 | "\n", 699 | "#for not equal\n", 700 | "print(\"5 is not equal to 4:\", 5 != 4)\n", 701 | "\n", 702 | "#for less than\n", 703 | "print(\"3 is less than 5:\", 3 < 5)\n", 704 | "\n", 705 | "#for greater than\n", 706 | "print(\"5 is greater than 3:\", 5 > 3)\n", 707 | "\n", 708 | "#for greater than or equal to\n", 709 | "print(\"5 is greater than or equal to 3:\", 5 >= 3)\n", 710 | "\n", 711 | "#for less than or equal to\n", 712 | "print(\"3 is less than or equal to 5:\", 3 <= 5)" 713 | ] 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": {}, 718 | "source": [ 719 | "Can you see what has happened here and why they have behaved this way?\n", 720 | "\n", 721 | "Can you then change these so instead of `True` they all return False?" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": null, 727 | "metadata": {}, 728 | "outputs": [], 729 | "source": [ 730 | "#for equal\n", 731 | "print(\"5 is equal to 5:\", 5 == 5)\n", 732 | "\n", 733 | "#for not equal\n", 734 | "print(\"5 is not equal to 4:\", 5 != 4)\n", 735 | "\n", 736 | "#for less than\n", 737 | "print(\"3 is less than 5:\", 3 < 5)\n", 738 | "\n", 739 | "#for greater than\n", 740 | "print(\"5 is greater than 3:\", 5 > 3)\n", 741 | "\n", 742 | "#for greater than or equal to\n", 743 | "print(\"5 is greater than or equal to 3:\", 5 >= 3)\n", 744 | "\n", 745 | "#for less than or equal to\n", 746 | "print(\"3 is less than or equal to 5:\", 3 <= 5)" 747 | ] 748 | }, 749 | { 750 | "cell_type": "markdown", 751 | "metadata": {}, 752 | "source": [ 753 | "Of course, each of these operations and what can be done with them will depend on the data type you are working with. While addition may work well for `floats` and `intergers` for example, it can behave very differently with `strings`.\n", 754 | "\n", 755 | "For example, is the following output what you would expect?" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": null, 761 | "metadata": {}, 762 | "outputs": [], 763 | "source": [ 764 | "#additions\n", 765 | "print(\"Hello\" + \"World\")\n", 766 | "\n", 767 | "#multiplication\n", 768 | "print(\"Hello world \" * 3)\n", 769 | "\n", 770 | "#comparison\n", 771 | "print(\"A\" == \"A\")\n", 772 | "print(\"A\" == \"a\")\n", 773 | "\n", 774 | "#less than\n", 775 | "print(\"A\" < \"a\")\n", 776 | "\n", 777 | "#greater than\n", 778 | "print(\"c\" > \"b\")" 779 | ] 780 | }, 781 | { 782 | "cell_type": "markdown", 783 | "metadata": {}, 784 | "source": [ 785 | "## Further Work (Optional)\n", 786 | "At the end of the workshops I may include content that I feel is too maths oriented for the workshops. The idea here is that you are free to explore these but they will not be covered in workshops. \n", 787 | "### Complex Numbers\n", 788 | "In addition to the float and integer data types there is also a complex number data type. The function ````complex(u,v)```` generates a complex number with a real component ````u```` and imaginary component ````v````. Note that $j$ is used as the imaginary unit in Python." 789 | ] 790 | }, 791 | { 792 | "cell_type": "code", 793 | "execution_count": null, 794 | "metadata": {}, 795 | "outputs": [], 796 | "source": [ 797 | "# Creates a complex number z = 2 + 4j\n", 798 | "z = complex(2,4)\n", 799 | "print(z)\n", 800 | "\n", 801 | "# Alternatively \n", 802 | "z = 2 + 4j\n", 803 | "print(z)\n", 804 | "\n", 805 | "# Real part of z\n", 806 | "z_real = z.real\n", 807 | "print(z_real)\n", 808 | "\n", 809 | "# Imaginary part of z\n", 810 | "z_imag = z.imag\n", 811 | "print(z_imag)\n", 812 | "\n", 813 | "# Returns the conjugate of the complex number\n", 814 | "z_conj = z.conjugate()\n", 815 | "print(z_conj)" 816 | ] 817 | } 818 | ], 819 | "metadata": { 820 | "kernelspec": { 821 | "display_name": "DSS", 822 | "language": "python", 823 | "name": "dss" 824 | }, 825 | "language_info": { 826 | "codemirror_mode": { 827 | "name": "ipython", 828 | "version": 3 829 | }, 830 | "file_extension": ".py", 831 | "mimetype": "text/x-python", 832 | "name": "python", 833 | "nbconvert_exporter": "python", 834 | "pygments_lexer": "ipython3", 835 | "version": "3.9.6" 836 | } 837 | }, 838 | "nbformat": 4, 839 | "nbformat_minor": 2 840 | } 841 | --------------------------------------------------------------------------------