{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programming Challenge Starting Coding Guide\n", "\n", "this is a guide to help people who have never coded before understand and write\n", "a program to figure out minimum duplication distance. \n", "This program uses python 3. and solves the problem given in IST4 hw1 problem1\n", "\n", "For questions contact chaack@caltech.edu\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Basics\n", "\n", "Below is an ipython shell, you can run and exceute the code by clicking on the cell\n", "and holding shift + return or hitting the play button.\n", "\n", "The code below assigns the string 'hello_world' to the variable first_string\n", "with the equal sign. The equal sign is used for assignment in python, which is\n", "the idea that you can assign a value to an variable.\n", "\n", "print is then called with the variable first_string causing the program\n", "to print the value stored in the variable first_string" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "first_string = 'hello world'\n", "print(first_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings\n", "\n", "strings are a useful variable type in python. They are used to store \n", "words or collections of characters. \n", "\n", "There are many different things you can do with strings in python,\n", "below i will go over a couple of very useful things you can do with\n", "them.\n", "\n", "### indexing and accessing characters.\n", "\n", "One useful thing to do with a string is to get the nth character of a string\n", "for example if we wanted to figure out what is the 5th letter of hello\n", "we could try to access the 5th letter of hello, and it would return o.\n", "\n", "In the code below we assign 'hello' to the variable s.\n", "then we access the 5th character and assign the 5th character to the variable char. \n", "Note that strings are indexed at 0, this means the first character of a string 'hello' is given by s[0].\n", "Also note we use [n] to access the n-1 element of a string.\n", "\n", "A good check to understand if you get this material is to change the \n", "program to print the character 'e'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "o\n" ] } ], "source": [ "s = 'hello'\n", "char = s[4]\n", "print(char)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More Strings\n", "\n", "Below we will go over a couple of more useful things you can do with strings.\n", "\n", "### slicing strings\n", "\n", "One nice thing to do is to slice a string so that you can get important pieces of a string.\n", "For example the code below gets the characters in a string from the 3rd character onwards.\n", "The next segment prints all of the string except the last character.\n", "\n", "A good check to see if you understand the material is to change the program to print just IST4 and the second box to just\n", "print love using slicing\n", "\n", "note you can also store numbers in variables." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "love IST4\n" ] } ], "source": [ "n = 2\n", "s = 'i love IST4'\n", "new_s = s[n:]\n", "print(new_s)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i love IST\n" ] } ], "source": [ "n = 2\n", "s = 'i love IST4'\n", "new_s = s[:len(s) -1]\n", "print(new_s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More Strings\n", "\n", "### comparing strings\n", "\n", "another important thing to do is to compare strings. If you want to compare if a string is a certain value you can do that as well\n", "for example we might want to check in a binary string if the last two values are 00.\n", "\n", "this can be done using the if then syntax. In python we use two = signs to check for equality.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "s = '1001100011'\n", "if s[len(s) -2:] == '00':\n", " print(True)\n", "else:\n", " print(False)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "s = '10011000'\n", "if s[len(s) -2:] == '00':\n", " print(True)\n", "else:\n", " print(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More Strings (one last time)\n", "\n", "### Adding more characters to strings\n", "\n", "one other thing you can do is add characters to an existing string.\n", "\n", "code to do this is shown below\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00111\n" ] } ], "source": [ "s = '001'\n", "s = s + '11'\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# looping and control\n", "\n", "one important aspect of coding is doing things repeatedly until a stop condition has been reached.\n", "\n", "for example i may want to print hello 10 times, or do key printing until some variable is equal to 5.\n", "\n", "Below I will show you how that is done\n", "\n", "## the if statetement:\n", "\n", "A boolean (you will learn about George Boole in IST4) is a statement in programming that is true or false.\n", "A variable can have a type boolean. \n", "\n", "An if statement checks to see whether a boolean is true or false, if it is true it evaluates some code, otherwise it skips that code.\n", "\n", "you can pair this with else if statements, and else statements which will execute code based on whether those conditions are satisfied " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "logic\n" ] } ], "source": [ "if 'i' in 'hello':\n", " print('magic')\n", "elif '4' in 'hello':\n", " print('Shuki said 4 is always correct')\n", "else:\n", " print('logic')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## looping \n", "loops can be useful for repeating code many times, for example if I want to print hello 5 times i could use a for loop. a for loop goes through every element in a list. (technically this isn't exactly what happens in python3, but this is a minor detail for now and one you shouldn't lose sleep over for this programming challenge)\n", "\n", "note range(5) generates a list from 1 to 5." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "hello\n", "hello\n", "hello\n", "hello\n" ] } ], "source": [ "for i in range(5):\n", " print('hello')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## looping continued:\n", "another type of loop is called a while loop. You can use while loop to do something as long as a condition is satisfied. \n", "You can also use a break statement to get out of a loop\n", "\n", "while(True) will loop forever unless a break statement is used" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "cnt = 0\n", "while(True):\n", " cnt = cnt + 1\n", " print(cnt)\n", " if cnt == 10:\n", " break\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other Datatypes \n", "\n", "## lists\n", "\n", "lists are lists of objects. They can be indexed like strings and can be used to store multiple items.\n", "\n", "for example this is a list that stores two strings 'cat' and 'dog' and then 'monkey' is added to the list\n", "\n", "lists are denoted by []" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['cat', 'dog', 'monkey']\n" ] } ], "source": [ "lst = ['cat', 'dog']\n", "lst = lst + ['monkey']\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# dictionaries\n", "\n", "dictionaries are data structures that have keys, associated with value. Think of it as a word and a definition. When you look up a word in a dictionary (key), you can get its definition (value). Dictionaries are created as follows, and you can add stuff to a dictionary by adding a key, value pair as shown below." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'chris': 'not as Awesome', 'shuki': 'Awesome'}\n", "not as Awesome\n" ] } ], "source": [ "dic = {}\n", "dic['shuki'] = 'Awesome'\n", "dic['chris'] = 'not as Awesome'\n", "print(dic)\n", "print(dic['chris'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:tensorflow]", "language": "python", "name": "conda-env-tensorflow-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }