{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Lecture 1\n", "==========\n", "\n", "Hello, welcome to UCI Math 10 Introduction to Programming for Data Science.\n", "\n", "This is an iPython notebook file. It consists of individual cells. \n", "\n", "In this class, we will use Jupyter to edit and run these cells.\n", "\n", "Each cell contains either a code snippet or markdown cell (explanatory using markdown enabling $\\LaTeX$).\n", "\n", "Double-click (or press enter) to edit a cell. Now you are in **edit mode**, which is indicated by a green cell border and a prompt showing in the editor area. \n", "\n", "Press Shift+Enter to run a cell including markdown (and move to the next one), press Ctrl+Enter to run a cell and stay at that cell. \n", "\n", "Once you have run a cell, you have exited the edit mode and entered **command mode**, which is indicated by a grey cell border with a blue left margin.\n", "\n", "More hacks and shortcuts using Jupyter will be featured in Lab 1.\n", "\n", "------" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this is a code cell\n", "# press shift+enter to run it when it is selected\n", "# for example, the following code computes the product of 2 and 4\n", "2*4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# By the way, lines that start with sharp key are comments\n", "# comments are ignored by python\n", "# 2*4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------\n", "\n", "## Markdown format \n", "\n", "Markdown is a way to format text easily:\n", "Choose in menu above: Cell -> Cell Type to toggle markdown or code\n", "\n", "In command mode, press M to change a cell to Markdown, press Y to change it back to code mode.\n", "\n", "Double click this cell to see the code for it. \n", "\n", "# Title\n", "## Subtitle\n", "### Subsubtitle\n", "#### Subsubsubtitle\n", "##### Subsubsubsubsubtitle\n", "###### Subsubsubsubsubsubtitle\n", "####### It gave up at this point" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last word of this sentence is *emphasized*. \n", "The last word of this sentence is **booooold**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

good old html code also works

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Math can also be included using latex, e.g. $e^{i\\pi} = -1$\n", "$$\\frac{d}{dx} x^3 = 3{x^2}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's a quick markdown tutorial: [link](http://www.markitdown.net/markdown)\n", "\n", "------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code blocks, expressions\n", "Press Shift+Enter to run the code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's the best calculator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "3*5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3/4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the cell that displays `27 % 11`, enter command mode, press A. Then move the newly generated cell, enter command mode, press D, D. See what happens." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# modulo operator\n", "27 % 11" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# integer division\n", "27 // 11" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The two operations above are related to the division with remainders: $m = qd + r$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# exponentiation\n", "2**10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prior of mathematical operators\n", "### multiplication/division before addition/subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "4 * 2 - 5 * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "4 * (2 - 5) * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 / 2 * 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 / (2 * 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplication has priority. Between * and / it's left to right." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Calling built-in functions:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# single variable\n", "abs(-5.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# multi-variable\n", "max(3,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "cos(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "whatt?! it doesn't know cos?!?!! \n", "
\n", "it's because cos is in an extra module we need to import" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "math.cos(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# no need to import again\n", "math.cos(math.pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------\n", "\n", "### Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"This is a string\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# adding strings is string concatenation\n", "\"hel\" + \"lo\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Weird stuff with string:\n", "5*\"hello.\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# strings format using floats\n", "'Our float value is %s. Our int value is %s.' % (3.14,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Upon finishing Lecture 1's notebook, if you are done with Jupyter, save first and you can close the browser window, and then use the keyboard shortcut `Ctrl+c` in the Anaconda prompt window (Windows) or in the terminal window (Mac OS/Linux)." ] } ], "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 }