Commit 5ca4d9f2 by Arucas Chacon

### Completed notebooks

parent 2820c217
This diff is collapsed.
\n", "Variables defined in one cell exist in all other cells once executed, so the relative location of cells in the notebook does\n", "not matter (i.e., cells lower down can still affect those above). Remember: Notebook cells are just a way to organize a program: as far as Python is concerned, all of the source code is one long set of instructions.\n", "
\n", "
\n", "

Exercise - Swapping Values

\n", "
\n", "
\n", " Fill the table showing the values of the variables in this program after each statement is executed.\n", "
\n", "
" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# Command # Value of x # Value of y # Value of swap #\n", "x = 1.0 # # # #\n", "y = 3.0 # # # #\n", "swap = x # # # #\n", "x = y # # # #\n", "y = swap # # # #" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise - Predicting Values

\n", "
\n", "
\n", "What is the final value of position in the program below? (Try to predict the value without running the program, then check your prediction.)\n", "\n", "
\n", "
" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "initial = \"left\"\n", "position = initial\n", "initial = \"right\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise - Choosing a name

\n", "
\n", "
\n", "Which is a better variable name, m, min, or minutes? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "ts = m * 60 + s\n", "tot_sec = min * 60 + sec\n", "total_seconds = minutes * 60 + seconds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Key Points

\n", "
\n", "
\n", "
\n", "
• Use variables to store values.
• \n", "
• Use print to display values.
• \n", "
• Variables persist between cells.
• \n", "
• Variables must be created before they are used.
• \n", "
• Variables can be used in calculations.
• \n", "
• Use an index to get a single character from a string.
• \n", "
• Use a slice to get a substring.
• \n", "
• Use the built-in function len to find the length of a string.
• \n", "
• Python is case-sensitive.
• \n", "
• Use meaningful variable names.
• \n", "
\n", "
\n", "
" ] } ], "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": 2 }
\n", "
\n", "

Exercise - Types

\n", "
\n", "
\n", " What type of value is 3.4?
\n", " What type of value is 3.25 + 4?
\n", " What type of value is 4/2?
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise - Division Types

\n", "
\n", "
\n", "In Python 3, the // operator performs integer (whole-number) floor division, the / operator performs floating-point division, and the ‘%’ (or modulo) operator calculates and returns the remainder from integer division.
\n", "\n", "However in Python2 (and other languages), the / operator between two integer types perform a floor (//) division. To perform a float division, we have to convert one of the integers to float\n", "
\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print('5 // 3 =', 5//3)\n", "print('5 / 3 =', 5/3)\n", "print('5 % 3 =', 5%3)\n", "print('float(5)/3 =', float(5) / 3 )\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "\n", "
\n", "
\n", "

Exercise - Arithmetic with Different Types

\n", "
\n", "
\n", " Which of the following will print 2.0? Note: there may be more than one right answer.
\n", " first = 1.0
\n", "second = \"1\"
\n", "third = \"1.1\"
\n", "
\n", "
\n", "1. first + float(second)
\n", "2. float(second) + float(third)
\n", "3. first + int(third)
\n", "4. first + int(float(third))
\n", "5. int(first) + int(float(third))
\n", "6. 2.0 * second\n", "
\n", "
\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "first = 1.0\n", "second = \"1\"\n", "third = \"1.1\"\n", "\n", "first + float(second)\n", "float(second) + float(third)\n", "#first + int(third)\n", "first + int(float(third))\n", "int(first) + int(float(third))\n", "2.0 * second" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Key Points

\n", "
\n", "
\n", "
\n", "
• Every value has a type.
• \n", "
• Use the built-in function type() to find the type of a value.
• \n", "
• Types control what operations can be done on values.
• \n", "
• Strings can be added and multiplied.
• \n", "
• Strings have a length (but numbers don’t).
• \n", "
• Must convert numbers to strings or vice versa when operating on them.
• \n", "
• Must convert numbers to strings or vice versa when operating on them.
• \n", "
• Can mix integers and floats freely in operations.
• \n", "
\n", "
\n", "
\n" ] } ], "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": 2 }
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Built-in Functions and Help" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Use comments to add documentation to programs." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# This is a comment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A function may take zero or more arguments.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* We've alreadys een some built-in functions\n", "* print(), len(), int(), float(), str()\n", "* Always with parenthesis, even if empty" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Commonly-used built-in functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* max() , min()\n",