diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..f6b3ed7 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,183 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "house1.owner = Braun Brelin\n", + "house1.address = 1234 Little Box Lane\n", + "house1.taxtopay = 200.0\n", + "The static value of numhouses is: 1\n", + "\n", + "house2.owner = Jon Brelin\n", + "house2.address = 1235 Little Box Lane\n", + "house2.taxtopay = 200.0\n", + "The static value of numhouses is: 2\n", + "\n", + "The static value of numhouses is: 2\n" + ] + } + ], + "source": [ + "class House(object):\n", + " numhouses=0\n", + " def __init__(self,owner,address):\n", + " self.owner = owner\n", + " self.address = address\n", + " self.value = 100000.00\n", + " House.numhouses +=1\n", + " def calculate_tax(self):\n", + " return self.value * .002\n", + " \n", + " @staticmethod\n", + " def mow_lawn():\n", + " call_gardener_app()\n", + " \n", + "\n", + "house1 = House('Braun Brelin','1234 Little Box Lane')\n", + "print ('house1.owner = ',house1.owner)\n", + "print ('house1.address = ',house1.address)\n", + "print ('house1.taxtopay = ',house1.calculate_tax())\n", + "print ('The static value of numhouses is:',house1.numhouses)\n", + "print()\n", + "house2 = House('Jon Brelin','1235 Little Box Lane')\n", + "print ('house2.owner = ',house2.owner)\n", + "print ('house2.address = ',house2.address)\n", + "print ('house2.taxtopay = ',house2.calculate_tax())\n", + "print ('The static value of numhouses is:',house2.numhouses)\n", + "print()\n", + "print ('The static value of numhouses is:',house1.numhouses)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Foo\n" + ] + } + ], + "source": [ + "class Foo(object):\n", + " def __init__(self,cs):\n", + " self.class_string = cs\n", + " \n", + " def returnstring(self):\n", + " return self.class_string\n", + "\n", + "class Bar(Foo):\n", + " def __init__(self,class_string = 'Foo'):\n", + " super().__init__(class_string)\n", + " pass\n", + " \n", + "b = Bar()\n", + "print (b.returnstring())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class BankBranch(object):\n", + " def __init__(self,branchnum):\n", + " self.branchnum = branchnum\n", + " \n", + " def getBranchData(self):\n", + " return get_branch_data(branchnum)\n", + " \n", + "class BankAccount(object):\n", + " def __init__(self,acctholder,bankacctnum,balance):\n", + " self.acctholder = acctholder\n", + " self.bankacctnum = bankacctnum\n", + " self.balance = balance\n", + "\n", + "class SavingsAccount(BankAccount,BankBranch):\n", + " def __init__(self,acctholder,bankacctnum,balance,branchnum):\n", + " super(BankAccount,self).__init__(acctholder,bankacctnum,balance)\n", + " super(BankBranch,self).__init__(branchnum)\n", + " self.rate_of_interest = .04\n", + "\n", + "class CurrentAccount(BankAccount)\n", + " def __init__(self,acctholder,bankacctnum,balance):\n", + " super().__init__(acctholder,bankacctnum,balance)\n", + " self.rate_of_interest = .02\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x: 3 y: 4\n" + ] + } + ], + "source": [ + "class Vector(object):\n", + " def __init__(self,x,y):\n", + " self.x = x\n", + " self.y = y\n", + " \n", + " def __str__(self):\n", + " return ('x: %d y: %d' % (self.x,self.y))\n", + " \n", + " def __add__(self,other):\n", + " return Vector(self.x + other.x, self.y + other.y)\n", + " \n", + "V1 = Vector(1,2)\n", + "V2 = Vector(2,2)\n", + "\n", + "V3 = V1 + V2\n", + "\n", + "print (V3)" + ] + } + ], + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/data/....functional_python b/data/....functional_python new file mode 100644 index 0000000..1383121 --- /dev/null +++ b/data/....functional_python @@ -0,0 +1,5 @@ +George Washington +John Adams +Thomas Jefferson +James Madison +James Monroe diff --git "a/data/c\357\200\272usersbbrelfunctional_pythondata" "b/data/c\357\200\272usersbbrelfunctional_pythondata" new file mode 100644 index 0000000..1383121 --- /dev/null +++ "b/data/c\357\200\272usersbbrelfunctional_pythondata" @@ -0,0 +1,5 @@ +George Washington +John Adams +Thomas Jefferson +James Madison +James Monroe diff --git a/data/ip.dat b/data/ip.dat new file mode 100644 index 0000000..8a779be --- /dev/null +++ b/data/ip.dat @@ -0,0 +1,5 @@ +192.168.100.1 +255.255.255.0 +1111.2.3.4 +1.2.3.4444 +192_168_100_1 diff --git a/data/output_example.dat b/data/output_example.dat index 09ed199..d4cdb20 100644 --- a/data/output_example.dat +++ b/data/output_example.dat @@ -1 +1 @@ -Writing some output \ No newline at end of file +Writing some new output \ No newline at end of file diff --git a/docs/.ipynb_checkpoints/13-6-2022-checkpoint.ipynb b/docs/.ipynb_checkpoints/13-6-2022-checkpoint.ipynb new file mode 100644 index 0000000..a190032 --- /dev/null +++ b/docs/.ipynb_checkpoints/13-6-2022-checkpoint.ipynb @@ -0,0 +1,1772 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "if x == 10:\n", + " print ('x is 10')\n", + "print ('x is not 10')" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n", + " This is the docstring for func1\n", + " \n" + ] + } + ], + "source": [ + "# this is a comment\n", + "print ('Hello') # This is a comment here. \n", + "\n", + "''' Everything in here is a string\n", + "[ We use this for multiline comments.\n", + "'''\n", + "\n", + "def func1(a):\n", + " ''' This is the docstring for func1\n", + " '''\n", + " \n", + " return a +1\n", + "\n", + "print (func1.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World') # Single quotes\n", + "print (\"Hello World\") # Double quotes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First set of object types\n", + "\n", + "Numbers\n", + "\n", + " 1 Integers\n", + " 2. Floating point (Implemented as double precision)\n", + " 3. Complex\n", + " 4. Binary\n", + " 5. Hexadecimal\n", + " 6. Octal. \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "y = 2.5\n", + "c = 2j\n", + "b = 0b1010111\n", + "h = 0xffff\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "a,b,c = 1,2,3\n", + "print (a,b,c)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 5 5\n" + ] + } + ], + "source": [ + "x = y = z = 5\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Numbers\n", + "Strings\n", + "Lists\n", + "Tuples\n", + "Sets \n", + "Dictionaries\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** \n", + "* / //\n", + "+ - \n", + "%\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0\n", + "3.0\n" + ] + } + ], + "source": [ + "a = 6.0\n", + "b = 2.0\n", + "c = 2\n", + "print (a/b)\n", + "print (a // c)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "a = 5\n", + "b = 2\n", + "print (a % b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = a + 1 # Both of these statements do the same thing. \n", + "a += 1 #This iis a shortcut\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Boolean type\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = True,1,'Non empty string'\n", + "b = False, 0,'' \n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = True\n", + "if a:\n", + " print ('True')\n", + "if a == True:\n", + " print ('True')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if not a:\n", + " print ('False')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if a and not b:\n", + " print ('Foo')\n", + " \n", + "if a or not b:\n", + " print ('Bar')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "import math\n", + "print (math.pi)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number: 5\n", + "5\n" + ] + } + ], + "source": [ + "x = input (\"Please type in a number: \")\n", + "print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Goodbye World\n" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "print (s)\n", + "s = \"Goodbye World\"\n", + "print (s)\n", + "\n", + "# Strings are immutable.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Hello World\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "s [0] = 'J'\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HELLO WORLD\n", + "hello world\n" + ] + } + ], + "source": [ + "print (s.upper())\n", + "print (s.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "print (s[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "e\n" + ] + } + ], + "source": [ + "print (s[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ello\n" + ] + } + ], + "source": [ + "print (s[1:5]) #This is called a slice." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HloWrd\n" + ] + } + ], + "source": [ + "print (s[::2])" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dlroW olleH\n" + ] + } + ], + "source": [ + "print (s[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World\"\n", + "\n", + "s3 = s1 + s2\n", + "print (s3)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Hello Hello Hello Hello \n" + ] + } + ], + "source": [ + "s4 = \"Hello \" * 5\n", + "print (s4)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "data = 'Braun Brelin, 1234 Main Street, Anytown, USA'\n", + "print (data.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number 5\n", + "\n", + "\n", + "25\n" + ] + } + ], + "source": [ + "x = input(\"Please type in a number \")\n", + "print (type(x))\n", + "x = int(x)\n", + "print (type(x))\n", + "print (x**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number b\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please type in a number \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input(\"Please type in a number \"))\n", + "print (x ** 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type ib some input: Hello\n", + "The length of your string is: 5\n" + ] + } + ], + "source": [ + "s = input (\"Please type ib some input: \")\n", + "print (\"The length of your string is: \", len(s))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings are also *iterable* objects. " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n" + ] + } + ], + "source": [ + "for c in s:\n", + " print (c)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,'a','b','c']\n", + "for o in mylist:\n", + " print (o)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 'a']\n" + ] + } + ], + "source": [ + "print (mylist[1:4])" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['c', 'b', 'a', 3, 2, 1]\n" + ] + } + ], + "source": [ + "print (mylist[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(mylist)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd']\n" + ] + } + ], + "source": [ + "mylist.append('d')\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "source": [ + "print (len(mylist))" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n" + ] + } + ], + "source": [ + "mylist1 = ['e','f','g']\n", + "mylist = mylist + mylist1\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]\n" + ] + } + ], + "source": [ + "mylist2 = [1,2,3]\n", + "mylist3 = mylist2 * 5\n", + "print (mylist3)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [[1,2,3],['a','b','c']]\n", + "print (mylist[0][2])\n", + "print (mylist[1][2])" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "words = ['fee','fi','fo','fum']\n", + "upper_words = [word.upper() for word in words] # List Comprehension\n", + " print (upper_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums if num % 2 == 0]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "t = (0,1,2,3,4,5)\n", + "print (t[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2, 3, 4)" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[1:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 1, 2, 3, 4, 5)" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(t)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun 21 Male\n" + ] + } + ], + "source": [ + "name = \"Braun\"\n", + "age = 21\n", + "gender = \"Male\"\n", + "\n", + "t = (name,age,gender) # Tuple packing.\n", + "\n", + "#print (t)\n", + "\n", + "(x,y,z) = t # Tuple Unpacking\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 'a')\n", + "(1, 'b')\n", + "(2, 'c')\n", + "(3, 'd')\n", + "(4, 'e')\n" + ] + } + ], + "source": [ + "example_list = ['a','b','c','d','e']\n", + "\n", + "for i in enumerate(example_list):\n", + " print (i)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 10), ('b', 11), ('c', 12), ('d', 13)]\n" + ] + } + ], + "source": [ + "example_list2 = [10,11,12,13]\n", + "\n", + "example_list3 = zip(example_list, example_list2)\n", + "print (list(example_list3))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Apple', 'Orange', 'Banana'}\n" + ] + } + ], + "source": [ + "myset = {'Apple','Orange','Banana','Apple'}\n", + "print (myset)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is neither 0 nor 1\n" + ] + } + ], + "source": [ + "x = 2\n", + "if not x:\n", + " print (\"x is 0\" )\n", + "elif x == 1:\n", + " print (\"x is 1\")\n", + "else:\n", + " print (\"x is neither 0 nor 1\")" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid IP Address\n" + ] + } + ], + "source": [ + "ipaddr = \"257.168.100.1\"\n", + "for octet in ipaddr.split(\".\"):\n", + " if int(octet) >255:\n", + " print (\"Invalid IP Address\")\n", + " break\n", + "else:\n", + " print (\"Valid IP Address\")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "x = 10\n", + "while x > 0:\n", + " print (x)\n", + " x -= 1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "num is 5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "for num in nums:\n", + " if num == 5:\n", + " print (\"num is 5\")\n", + " continue\n", + " else:\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "def func1(x: int = 2)-> int:\n", + " return x**2\n", + "\n", + "print (func1())\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n", + "d\n" + ] + } + ], + "source": [ + "def func2(*args: list) -> str: \n", + " for arg in args:\n", + " print (arg)\n", + " \n", + "func2(\"a\",\"b\",\"c\",\"d\")" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a foo\n", + "b bar\n", + "c baz\n" + ] + } + ], + "source": [ + "def func3(**kwargs: dict) -> None:\n", + " for (key,value) in kwargs.items():\n", + " print (key,value)\n", + " \n", + "func3(a = \"foo\", b = \"bar\",c = \"baz\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'key1':'value1', 'key2':'value2', 'key3':'value3'}\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "4\n" + ] + } + ], + "source": [ + "dow_dict = {}\n", + "list1 = ['Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday','Sunday']\n", + "list2 = [1,2,3,4,5,6,7]\n", + "dow_dict = { k:v for (k,v) in zip(list1,list2)} # Dictionary comprehension\n", + "\n", + "print (type(dow_dict))\n", + "print (dow_dict['Thursday'])" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['Monday', 'Tuesday', 'Wedneseday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])\n", + "dict_values([1, 2, 3, 4, 5, 6, 7])\n", + "dict_items([('Monday', 1), ('Tuesday', 2), ('Wedneseday', 3), ('Thursday', 4), ('Friday', 5), ('Saturday', 6), ('Sunday', 7)])\n" + ] + } + ], + "source": [ + "print (dow_dict.keys())\n", + "print (dow_dict.values())\n", + "print (dow_dict.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid key!\n" + ] + } + ], + "source": [ + "if 'Braunsday' in dow_dict:\n", + " print (dow_dict['Braunsday'])\n", + "else:\n", + " print ('Invalid key!')" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter day of week: Wednesday\n", + "3\n" + ] + } + ], + "source": [ + "while True:\n", + " key = input(\"Enter day of week: \")\n", + " if key not in dow_dict:\n", + " print (\"Invalid key! Try again\")\n", + " continue\n", + " else:\n", + " print (dow_dict[key])\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a numberb\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input (\"Please enter a number\"))\n", + "print (x + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: b\n", + "Invalid number!\n", + "Please enter a number: c\n", + "Invalid number!\n", + "Please enter a number: 4\n", + "5\n" + ] + } + ], + "source": [ + "while True: \n", + " try:\n", + " x = int(input (\"Please enter a number: \")) \n", + " except ValueError:\n", + " print (\"Invalid number!\")\n", + " continue\n", + " print (x + 1)\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " # Execute some function here \n", + "except:\n", + " # If the function throws an exception, the code for this event ia handled here. \n", + "else:\n", + " # Only runs this code if the function suceeeded. \n", + "finally:\n", + " # Always run this code when the try block is finished. \n", + " # This is called try with resources. \n", + " \n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\bbrel\\\\basicpython\\\\docs'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from os import getcwd\n", + "\n", + "getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "George Washington\n", + "John Adams\n", + "Thomas Jefferson\n", + "James Madison\n", + "James Monroe\n" + ] + } + ], + "source": [ + "with open('c:/users/bbrel/basicpython/labs/lab4/data/test.dat') as f:\n", + " for line in f:\n", + " print (line.strip())" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x before function call is: 10\n", + "x inside the function is 11\n", + "x after function call is: 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def funct(y):\n", + " y += 1\n", + " print (\"y inside the function is \",y\n", + " return y\n", + "\n", + "print (\"x before function call is: \",x)\n", + "funct(x)\n", + "print (\"x after function call is: \",x)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "This is known as scope, or alternatively lexical scope. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for num in nums:\n", + " y = 20\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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/.ipynb_checkpoints/Basic Python-checkpoint.ipynb b/docs/.ipynb_checkpoints/Basic Python-checkpoint.ipynb index 8fb5616..625438f 100644 --- a/docs/.ipynb_checkpoints/Basic Python-checkpoint.ipynb +++ b/docs/.ipynb_checkpoints/Basic Python-checkpoint.ipynb @@ -31,7 +31,26 @@ "10. [Dictionaries](#Dictionaries)\n", "11. [Sets](#Sets)\n", "12. [Comprehensions](#Comprehensions)\n", - "13. [Problem Analysis](#Problem Analysis)\n", + "13. [Problem Analysis](#Problem Analysis)

Chapter 1

\n", + "

About Python

\n", + "\n", + "Python was created in the late 1980's by Guido Van Rossum when he worked as a researcher at Centrum Wiskunde & Informatica. In 2000. Python 2.0 was released. The 2.0 release has gone through a number of major versions, ending in version 2.7. In 2008, Python 3.0 was released. As of this writing, the latest stable version, 3.6, was released in December 2016. \n", + "\n", + "Python is a programming language designed to be run on multiple operating systems, including Linux, Unix, MacOS X and Microsoft Windows. The language supports a number of programming paradigms, including *Functional*, *Imperative* and *Object Oriented* styles.\n", + "\n", + "The core philosophy of Python is summarized in a paper entitled [*The zen of python*](#https://www.python.org/dev/peps/pep-0020/)\n", + "\n", + "In brief, \n", + "- Beautiful is better than ugly. \n", + "- Explicit is better than implicit.\n", + "- Simple is better than complex\n", + "- Complex is better than complicated.\n", + "- There should be one—and preferably only one—obvious way to do it.\n", + "\n", + "[https://docs.python.org](#http://docs.python.org) is the URL for the repository of all the offical Python documentation, including a tutorial, API documentation, and much more. \n", + "\n", + "This course will focus on the Python 3.x implementation of the language. Although Python 2 is still in wide release, it is no longer being updated with the new features of Python 3 and there is a wide push among the Python developers to have existing Python code ported from version 2 to version 3 as well as suggesting that all new Python code be developed with the latest stable release of Python 3. \n", + "\n", "14. [Introduction to Object Oriented Programming](#Introduction to Object Oriented Programming)\n", "15. [Classes](#Classes)\n", " 1. [Inheritance](#Inheritance)\n", @@ -66,6 +85,33 @@ " \n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n" + ] + } + ], + "source": [ + "print('Hello')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a text note. I love python a lot!\n", + "

\n", + "This is an html paragraph Yay!\n", + "

" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -135,6 +181,30 @@ "3. Print the string \"Hello Python\" using the print() function in Python." ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "kjhkkhkhkhkhjk" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -144,7 +214,7 @@ "\n", "While this sounds like unnecessary complexity, in fact, there are many advantages to this type of system. Before the concept of a virtual machine, programmers needed to keep track of all computer memory allocated and de-allocated in their program. Often, mistakes in the program would lead to crashes and indeterministic behavior on part of the program due to a flaw in the program's allocation of memory. With a virtual machine, the VM itself takes care of the allocation and de-allocation of memory so the programmer no longer needs to worry about it. This leads to far more robust programs since an entire class of potential error has been removed. Following is a graphic illustration of how the Python Virtual Machine works. \n", "\n", - " \n", + " \n", "\n", "\n" ] @@ -242,6 +312,26 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is 1\n" + ] + } + ], + "source": [ + "x = 1\n", + "if (x == 2):\n", + " print ('x is 2')\n", + "print ('x is 1')" + ] + }, { "cell_type": "markdown", "metadata": { @@ -337,9 +427,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -389,22 +477,20 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "1 1 1\n", + "2 2 2\n", "1 2 Foo\n" ] } ], "source": [ - "a=b=c=1\n", + "a=b=c=2\n", "print (a,b,c)\n", "a,b,c = 1,2,'Foo'\n", "print (a,b,c)" @@ -456,11 +542,12 @@ "\n", " \n", " \n", - " \n", + " <\n", + " td> a - b\n", " \n", " \n", " \n", - " \n", + " \n", " \n", "
Operator Operation Example
+ Add two numbers a + b
- Subtract two numbers a - b
- Subtract two numbers
\\* Multiply two numbers a * b
/ Floating point division a / b
// Integer (Floor) divisiona // b
\\*\\* Exponentiation a ** 2
\\*\\*\\* Exponentiation a ** 2
% Modulo (Remainder) a % b
\n", "














\n", @@ -478,6 +565,25 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n" + ] + } + ], + "source": [ + "a = 2\n", + "b = 2 ** 3\n", + "print (b)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -498,6 +604,47 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n" + ] + } + ], + "source": [ + "a = 1\n", + "a = a + 1 \n", + "print (a)\n", + "a += 1\n", + "print (a)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "a=4\n", + "b=2\n", + "print (a//b)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -518,16 +665,13 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "a is 5\n", "b is 10\n", "They are both True\n", "One or both are True\n", @@ -542,8 +686,8 @@ "b = 10\n", "\n", "# Now, let's do some boolean tests\n", - "if a == 5:\n", - " print ('a is 5')\n", + "if a == 65:\n", + " print ('a is 65')\n", "if b == 10:\n", " print ('b is 10')\n", "\n", @@ -583,19 +727,153 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140102643103032\n" + ] + } + ], + "source": [ + "s1 = 'Hello'\n", + "s2 = 'Hello'\n", + "\n", + "print (id(s1))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is not 5\n" + ] + } + ], + "source": [ + "a = 6\n", + "if a == 5:\n", + " print (a)\n", + "elif a != 5:\n", + " print ('a is not 5')\n", + "else:\n", + " print ('else')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Red\n", + "Green\n", + "Blue\n" + ] + } + ], + "source": [ + "colors = ['Red','Green','Blue']\n", + "for i in range(len(colors)):\n", + " print (colors[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "id of a is 10914656\n", - "id of b is 10914688\n", - "id of c is 10914496\n", - "id of d is 10914496\n", + "Red\n", + "Blue\n" + ] + } + ], + "source": [ + "colors = ['Red','Green','Blue']\n", + "\n", + "\n", + "for color in colors:\n", + " \n", + " if color == 'Green':\n", + " continue\n", + " print (color)\n", + "else:\n", + " print ('I did not find magenta')\n", + " \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ip = \"192.168.100.1\"\n", + "\n", + "for octet in ip.split('.'):\n", + " if octet > 255:\n", + " break\n", + "else:\n", + " print (\"The octet is valid\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n" + ] + } + ], + "source": [ + "x = 10\n", + "while x > 1:\n", + " print (x)\n", + " x -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "id of a is 10055840\n", + "id of b is 10055872\n", + "id of c is 10055680\n", + "id of d is 10055680\n", "a is not the same as b\n", "c is the same as d\n", "These two strings do not have the same identity\n", @@ -646,12 +924,240 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 16, "metadata": {}, - "source": [ - "

Operator Precedence

\n", - "\n", - "Operators in Python have a precedence order, that is, given a statement with multiple operators, Python will decide which operator to do first based on this order. For example given the statement \n", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value: b\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Please enter a value: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "a = input('Please enter a value: ')\n", + "print (int(a) + 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "113.0973372\n", + "113.0973372\n", + "18.8495562\n", + " This function calculates the volume of a sphere \n" + ] + } + ], + "source": [ + "pi=3.1415927\n", + "r = 3\n", + "\n", + "def Volume(r):\n", + " ''' This function calculates the volume of a sphere '''\n", + " return 4/3* pi * r**3 \n", + "\n", + "def Area(r):\n", + " ''' This function calculates the area of a sphere '''\n", + " return 4 * pi * r **2\n", + "\n", + "def Circumference(r):\n", + " ''' This function calcuates the circumference of a sphere '''\n", + " return 2 * pi * r\n", + " \n", + " \n", + "# volume = 4/3* pi * r**3\n", + "# area = 4 * pi *r **2\n", + "# circumference = 2 * pi * r\n", + "print (Volume(r))\n", + "print (Area(r))\n", + "print (Circumference(r))\n", + "print (Volume.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 4\n", + "1. Calculate volume\n", + "2. Calculate area\n", + "3. Calculate circumference\n", + "4. Exit\n", + "Please enter your choice: 1\n", + "268.08257706666666\n", + "\n", + "\n", + "1. Calculate volume\n", + "2. Calculate area\n", + "3. Calculate circumference\n", + "4. Exit\n" + ] + } + ], + "source": [ + "import sys\n", + "\n", + "pi = 3.1415927\n", + "radius = int(input('Please enter a number: '))\n", + "\n", + "def Volume(r):\n", + " ''' This function calculates the volume of a sphere '''\n", + " return 4/3* pi * r**3 \n", + "\n", + "def Area(r):\n", + " ''' This function calculates the area of a sphere '''\n", + " return 4 * pi * r **2\n", + "\n", + "def Circumference(r):\n", + " ''' This function calcuates the circumference of a sphere '''\n", + " return 2 * pi * r\n", + "\n", + "funclist = [Volume,Area,Circumference]\n", + "\n", + "while (True):\n", + " print ('1. Calculate volume')\n", + " print ('2. Calculate area')\n", + " print ('3. Calculate circumference')\n", + " print ('4. Exit')\n", + " \n", + " choice = input('Please enter your choice: ')\n", + " \n", + " if choice == 4:\n", + " sys.exit()\n", + " else:\n", + " print(funclist[int(choice)-1](radius))\n", + " print ('\\n')\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = range(0,10)\n", + "list(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 10\n", + "10\n" + ] + }, + { + "ename": "TypeError", + "evalue": "must be str, not int", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please enter a number: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mv\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: must be str, not int" + ] + } + ], + "source": [ + "v = input(\"Please enter a number: \")\n", + "print (v)\n", + "v += 1\n", + "print (v)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import math \n", + "pi = 3.1415927\n", + "radius = input('Please enter the radius')\n", + "radius = int(radius)\n", + "volume = 4 /3 * pi * radius ** 3\n", + "print ('Volume = ',volume)\n", + "circumference = 2 * pi * radius\n", + "print ('Circumference = ',circumference)\n", + "area = 4 * pi * radius ** 2\n", + "print ('Area = ', area)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The previous cell is an example of calculating stuff.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Operator Precedence

\n", + "\n", + "Operators in Python have a precedence order, that is, given a statement with multiple operators, Python will decide which operator to do first based on this order. For example given the statement \n", "\n", "\n", "
\n", @@ -737,12 +1243,287 @@ "
" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dlroW olleH'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"Hello World\"\n", + "# slice\n", + "s[::-1] " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Wor\n" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "print (s[-6:-2])" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dlroW olleH\n" + ] + } + ], + "source": [ + "datastring = 'Hello World'\n", + "print (datastring[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun', 'Brelin', '1234 Main Street', ' 21', ' Male']\n" + ] + } + ], + "source": [ + "personnel_record = 'Braun,Brelin,1234 Main Street, 21, Male\\n'\n", + "pers_array = personnel_record.strip().split(',')\n", + "print (pers_array)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Goodbye World\n" + ] + } + ], + "source": [ + "datastring = 'Hello World'\n", + "datastring = 'Goodbye World'\n", + "print (datastring)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "datastring ='Hello World'\n", + "dir (datastring)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], + "source": [ + "datastring ='Hello World'\n", + "print (datastring.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['12345', 'Braun', 'Brelin', '1234 Main Street', 'Anytown', ' USA']\n" + ] + } + ], + "source": [ + "person_rec = '12345,Braun,Brelin,1234 Main Street,Anytown, USA'\n", + "person_list = person_rec.split(',')\n", + "print (person_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Goodbye World\n", + "Good\n", + "dlroW eybdooG\n" + ] + } + ], + "source": [ + "s1 = 'Hello World'\n", + "print (s1)\n", + "s1 = 'Goodbye World'\n", + "print (s1)\n", + "print (s1[0:4])\n", + "print (s1[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello', 'World']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = open('some_file')\n", + "for line in f:\n", + " r = line.strip().split(',')\n", + " \n" + ] + }, { "cell_type": "code", "execution_count": 20, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -804,9 +1585,7 @@ { "cell_type": "code", "execution_count": 21, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "ename": "TypeError", @@ -831,7 +1610,7 @@ "source": [ "

Python String Operators


\n", "Python has two string operators. The + and the *. \n", - "+ allows concatenation of two strings. * is the repitition operator. For example:\n", + "\\+ allows concatenation of two strings. \\** is the repitition operator. For example:\n", "\n", "\n", "\n", @@ -855,9 +1634,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -880,6 +1657,25 @@ "print (string1)" ] }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value 5\n", + "5\n" + ] + } + ], + "source": [ + "myvar = input('Please enter a value ')\n", + "print (myvar)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -937,9 +1733,7 @@ { "cell_type": "code", "execution_count": 28, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1005,9 +1799,7 @@ { "cell_type": "code", "execution_count": 29, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1026,11 +1818,95 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mystring = 'Hello'\n", "dir(mystring)" @@ -1046,6 +1922,35 @@ "3. Print out the number of characters in this string." ] }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "this is an ex-parrot\n", + "20\n" + ] + } + ], + "source": [ + "mystring = 'This is an ex-parrot'\n", + "print (mystring.lower())\n", + "print (len(mystring))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "mylist = [1,2,3,4.5,'foo','bar','baz']\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1111,12 +2016,162 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 'foo', 'bar', 'baz']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [1,2,3,'foo','bar','baz']\n", + "mylist" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = []\n", + "dir(mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6, 5, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "mylist.append(6)\n", + "mylist.reverse()\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [[1,2,3],[4,5,6]]\n", + "mylist\n", + "mylist[0][1]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Hello World\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "s[0] = 'J'\n", + "s" + ] + }, { "cell_type": "code", "execution_count": 39, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1157,6 +2212,34 @@ "print (mylist)" ] }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "mylist= [1,2,3,4,5]\n", + "mylist[0] = 10\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -1186,9 +2269,7 @@ { "cell_type": "code", "execution_count": 55, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1237,9 +2318,7 @@ { "cell_type": "code", "execution_count": 58, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1259,6 +2338,141 @@ "print (mylist + mylist2)" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on list object:\n", + "\n", + "class list(object)\n", + " | list() -> new empty list\n", + " | list(iterable) -> new list initialized from iterable's items\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __delitem__(self, key, /)\n", + " | Delete self[key].\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(...)\n", + " | x.__getitem__(y) <==> x[y]\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iadd__(self, value, /)\n", + " | Implement self+=value.\n", + " | \n", + " | __imul__(self, value, /)\n", + " | Implement self*=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self None -- append object to end\n", + " | \n", + " | clear(...)\n", + " | L.clear() -> None -- remove all items from L\n", + " | \n", + " | copy(...)\n", + " | L.copy() -> list -- a shallow copy of L\n", + " | \n", + " | count(...)\n", + " | L.count(value) -> integer -- return number of occurrences of value\n", + " | \n", + " | extend(...)\n", + " | L.extend(iterable) -> None -- extend list by appending elements from the iterable\n", + " | \n", + " | index(...)\n", + " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n", + " | Raises ValueError if the value is not present.\n", + " | \n", + " | insert(...)\n", + " | L.insert(index, object) -- insert object before index\n", + " | \n", + " | pop(...)\n", + " | L.pop([index]) -> item -- remove and return item at index (default last).\n", + " | Raises IndexError if list is empty or index is out of range.\n", + " | \n", + " | remove(...)\n", + " | L.remove(value) -> None -- remove first occurrence of value.\n", + " | Raises ValueError if the value is not present.\n", + " | \n", + " | reverse(...)\n", + " | L.reverse() -- reverse *IN PLACE*\n", + " | \n", + " | sort(...)\n", + " | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "mylist = []\n", + "help(mylist)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1289,9 +2503,7 @@ { "cell_type": "code", "execution_count": 60, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1315,6 +2527,58 @@ "print (outer[1][3])" ] }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "#for item in mylist:\n", + "# print (item)\n", + "\n", + "#mystring = 'Hello World'\n", + "#for char in mystring:\n", + "# print (char)\n", + " \n", + "for i in range(6):\n", + " print (i)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(2,6):\n", + " fiblist.append(fiblist[i-2] + fiblist[i-1])\n", + "print (fiblist)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1323,6 +2587,183 @@ "1. The fibonacci sequence is a well known mathematical sequence where an element is the sum of the previous two elements. Create a list that will contain the first five elements of this sequence.\n" ] }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "x = fiblist[0] + fiblist[1]\n", + "fiblist.append(x)\n", + "x = fiblist[1] + fiblist[2]\n", + "fiblist.append(x)\n", + "x = fiblist[2] + fiblist[3]\n", + "fiblist.append(x)\n", + "x = fiblist[3] + fiblist[4]\n", + "fiblist.append(x)\n", + "x = fiblist[4] + fiblist[5]\n", + "fiblist.append(x)\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(5):\n", + " x = fiblist[i] + fiblist[i+1]\n", + " fiblist.append(x)\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(5):\n", + " fiblist.append(fiblist[i] + fiblist[i+1])\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Valid\n" + ] + } + ], + "source": [ + "ipaddr = '192.168.100.1'\n", + "octets = ipaddr.split('.')\n", + "for octet in octets:\n", + " if int(octet) > 255:\n", + " print ('Invalid')\n", + " break\n", + "else: \n", + " print ('Valid')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Hello World'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0ms1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s1 = 'Hello World'\n", + "s1[0] = 'J'\n", + "print (s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = [1,2,3,4,5]\n", + "list2 = ['a','b','c','d','e']\n", + "list3 = zip (list1,list2)\n", + "list(list3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Quick Exercise:\n", + "Create a sample list of potential ipaddresses, some of which have invalid octets. Write a small program that iterates over each ip address and prints out whether or not the ip address is valid." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good ip address!\n", + "Bad ipaddress!\n", + "Good ip address!\n" + ] + } + ], + "source": [ + "ipaddrs = ['192.168.100.1','255.262.1.2','10.1.2.3']\n", + "\n", + "for ipaddr in ipaddrs:\n", + " octets = ipaddr.split('.')\n", + " for octet in octets:\n", + " if int(octet) > 255:\n", + " print ('Bad ipaddress!')\n", + " break\n", + " else:\n", + " print ('Good ip address!') \n", + " \n", + " " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1361,10 +2802,8 @@ }, { "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1379,24 +2818,54 @@ } ], "source": [ - "# Let's define a tuple.\n", - "mytuple = (1,2,3,4,5)\n", - "# Let's print out the second through the fifth element.\n", - "print (mytuple[1:6])\n", - "# Let's define a second tuple and add it to the first. \n", - "mytuple1 = ('a','b','c','d','e')\n", - "mytuple = mytuple + mytuple1\n", - "print (mytuple)\n", - "# Let's pack a tuple t with the values of x, y and z. These become the elements of the new tuple t. \n", - "x = 1\n", - "y = 2\n", - "z = 3\n", - "t = (x,y,z)\n", - "print (t)\n", - "# Now let's unpack a tuple.\n", - "(name,age) = ('Braun Brelin',21)\n", - "print ('name = ', name)\n", - "print ('age = ',age)" + "# Let's define a tuple.\n", + "mytuple = (1,2,3,4,5)\n", + "# Let's print out the second through the fifth element.\n", + "print (mytuple[1:6])\n", + "# Let's define a second tuple and add it to the first. \n", + "mytuple1 = ('a','b','c','d','e')\n", + "mytuple = mytuple + mytuple1\n", + "print (mytuple)\n", + "# Let's pack a tuple t with the values of x, y and z. These become the elements of the new tuple t. \n", + "x = 1\n", + "y = 2\n", + "z = 3\n", + "t = (x,y,z)\n", + "print (t)\n", + "# Now let's unpack a tuple.\n", + "(name,age) = ('Braun Brelin',21)\n", + "print ('name = ', name)\n", + "print ('age = ',age)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "fo\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmytuple\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'fee'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fi'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fo'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fum'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmytuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmytuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'foo'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "mytuple = ('fee','fi','fo','fum')\n", + "print(mytuple[2])\n", + "mytuple[2] = 'foo' " ] }, { @@ -1443,9 +2912,9 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 8, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { @@ -1473,6 +2942,44 @@ "print (t1)" ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('12345', 'Braun', 'Brelin', '1234 Main Street', ' Anytown', ' USA', ' 12345')\n" + ] + } + ], + "source": [ + "emp_record = '12345,Braun,Brelin,1234 Main Street, Anytown, USA, 12345'\n", + "\n", + "emp_tuple = tuple(emp_record.split(','))\n", + "print (emp_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "import math\n", + "print (math.sqrt(4))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1505,12 +3012,128 @@ "
\n" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "even_squares = [num **2 for num in nums if num % 2 == 0]\n", + "#for num in nums:\n", + "# squares.append(num **2)\n", + "\n", + "print (even_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "words = ['fee','fi','fo','fum']\n", + "upperwords = [word.upper() for word in words]\n", + "print (upperwords)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + }, + { + "data": { + "text/plain": [ + "['__doc__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " 'acos',\n", + " 'acosh',\n", + " 'asin',\n", + " 'asinh',\n", + " 'atan',\n", + " 'atan2',\n", + " 'atanh',\n", + " 'ceil',\n", + " 'copysign',\n", + " 'cos',\n", + " 'cosh',\n", + " 'degrees',\n", + " 'e',\n", + " 'erf',\n", + " 'erfc',\n", + " 'exp',\n", + " 'expm1',\n", + " 'fabs',\n", + " 'factorial',\n", + " 'floor',\n", + " 'fmod',\n", + " 'frexp',\n", + " 'fsum',\n", + " 'gamma',\n", + " 'hypot',\n", + " 'isfinite',\n", + " 'isinf',\n", + " 'isnan',\n", + " 'ldexp',\n", + " 'lgamma',\n", + " 'log',\n", + " 'log10',\n", + " 'log1p',\n", + " 'log2',\n", + " 'modf',\n", + " 'pi',\n", + " 'pow',\n", + " 'radians',\n", + " 'sin',\n", + " 'sinh',\n", + " 'sqrt',\n", + " 'tan',\n", + " 'tanh',\n", + " 'trunc']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import math\n", + "print (math.pi)\n", + "dir (math)" + ] + }, { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1535,6 +3158,93 @@ " print (element)" ] }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "squares" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num **2 for num in nums]\n", + "squares\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "for num in nums:\n", + " if num % 2== 0: \n", + " squares.append(num ** 2)\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [num ** 2 for num in nums if num %2 == 0]\n", + "print (squares)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1572,54 +3282,251 @@ "# the [] is *not* an index value, but the value of the key.
\n", "print (mydict1[3])
\n", "\n", - "# Any immutable type can be a key, for example, integers, strings or even tuples.
\n", - "# Let's see an example of using a tuple as a key.
\n", - "
\n", - "keytuple = ('12345','Braun Brelin','12345 Main Street')
\n", - "mydict[keytuple] = 'Record for Braun Brelin'
\n", - "
\n", - "print (mydict[keytuple])
\n", - "

\n", - "\n", - "\n", - "" + "# Any immutable type can be a key, for example, integers, strings or even tuples.
\n", + "# Let's see an example of using a tuple as a key.
\n", + "
\n", + "keytuple = ('12345','Braun Brelin','12345 Main Street')
\n", + "mydict[keytuple] = 'Record for Braun Brelin'
\n", + "
\n", + "print (mydict[keytuple])
\n", + "

\n", + "\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mydict = {}\n", + "dir(mydict)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Brad Pitt, 1235 Main Street, Anytown, USA, 11111\n" + ] + } + ], + "source": [ + "Person = {12345:'Braun Brelin,1234 Main Street,Anytown, USA, 11111',\n", + " 12346:'Brad Pitt, 1235 Main Street, Anytown, USA, 11111',\n", + " 12348:'Al Pacino,12347 Broadway, New York, USA, 111112'}\n", + "\n", + "\n", + "print (Person[12346])" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wednesday\n", + "Record for Braun Brelin\n" + ] + } + ], + "source": [ + "# Let's create our first, empty, dictionary. \n", + "mydict = {}\n", + "# Let's create another dictionary with some key value pairs. Note that the syntax is key:value\n", + "# Also note that keys *must* be unique. I.e. I can't have two entries with a key value of 1. \n", + "\n", + "mydict1 = {'one':'Monday','two':'Tuesday','three':'Wednesday','four':'Thursday','five':'Friday','six':'Saturday','seven':'Sunday'}\n", + "\n", + "# Let's print out the value associated with key 3 in the second dictionary. Note that the value here in \n", + "# the [] is *not* an index value, but the value of the key. \n", + "print (mydict1['three'])\n", + "\n", + "# Any immutable type can be a key, for example, integers, strings or even tuples.\n", + "# Let's see an example of using a tuple as a key.\n", + "\n", + "keytuple = ('12345','Braun Brelin','12345 Main Street')\n", + "mydict[keytuple] = 'Record for Braun Brelin'\n", + "\n", + "print (mydict[keytuple])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mydict = {}\n", + "dir(mydict)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B\n", + "G\n", + "R\n", + "Blue\n", + "Green\n", + "Red\n", + "('B', 'Blue')\n", + "('G', 'Green')\n", + "('R', 'Red')\n" + ] + } + ], + "source": [ + "mydict = {'R':'Red','G':'Green','B':'Blue'}\n", + "\n", + "for key in mydict.keys():\n", + " print (key)\n", + "\n", + "for value in mydict.values():\n", + " print(value)\n", + " \n", + "for item in mydict.items():\n", + " print (item)\n", + " \n", + "if 'magenta' in mydict:\n", + " print (mydict['magenta'])" ] }, { "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, + "execution_count": 29, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Wednesday\n", - "Record for Braun Brelin\n" + "No such key found!\n" ] } ], "source": [ - "# Let's create our first, empty, dictionary. \n", - "mydict = {}\n", - "# Let's create another dictionary with some key value pairs. Note that the syntax is key:value\n", - "# Also note that keys *must* be unique. I.e. I can't have two entries with a key value of 1. \n", - "\n", - "mydict1 = {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday',6:'Saturday',7:'Sunday'}\n", - "\n", - "# Let's print out the value associated with key 3 in the second dictionary. Note that the value here in \n", - "# the [] is *not* an index value, but the value of the key. \n", - "print (mydict1[3])\n", - "\n", - "# Any immutable type can be a key, for example, integers, strings or even tuples.\n", - "# Let's see an example of using a tuple as a key.\n", - "\n", - "keytuple = ('12345','Braun Brelin','12345 Main Street')\n", - "mydict[keytuple] = 'Record for Braun Brelin'\n", - "\n", - "print (mydict[keytuple])" + "colors = {'R':'Red','G':'Green','B':'Blue'}\n", + " \n", + "if 'M' in colors.keys():\n", + " print (colors['M'])\n", + "else:\n", + " print ('No such key found!')" ] }, { @@ -1713,9 +3620,7 @@ { "cell_type": "code", "execution_count": 81, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1756,6 +3661,27 @@ " print (\"Hello is in s but World is not in s\")" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No key found\n" + ] + } + ], + "source": [ + "mydict = {2:'Red',1:'Green',3:'Blue'}\n", + "if 4 not in mydict:\n", + " print ('No key found')\n", + "else:\n", + " print (mydict[4])\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1812,9 +3738,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1882,6 +3806,24 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "x = range(10)\n", + "print (list(x))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1934,9 +3876,7 @@ { "cell_type": "code", "execution_count": 17, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1975,6 +3915,30 @@ " print ('element position: ',element[0],' element value: ',element[1])" ] }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 'Mercury', 2: 'Venus', 3: 'Earth', 4: 'Mars', 5: 'Jupiter', 6: 'Saturn', 7: 'Uranus', 8: 'Neptune'}\n" + ] + } + ], + "source": [ + "thekeys=[1,2,3,4,5,6,7,8,9]\n", + "thevalues = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']\n", + "theplanets = zip(thekeys,thevalues)\n", + "tmp = list(theplanets)\n", + "planetdict =dict(tmp)\n", + "print (planetdict)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2001,9 +3965,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2089,12 +4051,59 @@ "" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192', '168', '100', '1']\n" + ] + } + ], + "source": [ + "ipaddr = '192.168.100.1'\n", + "octets = ipaddr.split('.')\n", + "print (octets)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "myipaddr = '256.0.1.2'\n", + "\n", + "octets = myipaddr.split('.')\n", + "for octet in octets:\n", + " if octet > 255:\n", + " break\n", + "else:\n", + " print ('Ipaddr is good!')\n", + " \n", + "\n", + "\n" + ] + }, { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2227,6 +4236,25 @@ "1. The previous quick exercise asked for the fibonacci sequence. Re-do this exercise, however use a loop to calculate the first 10 elements of the fibonacci sequence." ] }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25]\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "mysquares = [num ** 2 for num in mylist]\n", + "print (mysquares)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2262,10 +4290,32 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "2\n" + ] + } + ], + "source": [ + "x = 2\n", + "def square(x):\n", + " x = x ** 2\n", + " return (x)\n", + "\n", + "print (square(x))\n", + "print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2318,10 +4368,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2378,9 +4426,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2444,9 +4490,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2503,9 +4547,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2566,9 +4608,7 @@ { "cell_type": "code", "execution_count": 17, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2596,9 +4636,9 @@ "\n", "

\n", "\n", - "def myfunction(a,b,*args):
\n", + "def myfunction(a,b,\\*args):
\n", "    print ('a = ',a,' b = ',b)
\n", - "    for arg in args:
\n", + "    for arg in \\*args:
\n", "        print (arg)
\n", "    return
\n", "
\n", @@ -2633,9 +4673,7 @@ { "cell_type": "code", "execution_count": 22, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2659,9 +4697,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2684,6 +4720,100 @@ "myfunction(1,2,param1='a',param2='b',param3='c')" ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "foo!\n", + "bar!\n", + "baz!\n" + ] + } + ], + "source": [ + "def foo():\n", + " print (\"foo!\")\n", + "def bar():\n", + " print (\"bar!\")\n", + "def baz():\n", + " print (\"baz!\")\n", + " \n", + "funclist = [foo,bar,baz]\n", + "\n", + "for func in funclist:\n", + " func()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'function' object has no attribute 'a'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'foo'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'a'" + ] + } + ], + "source": [ + "def foo():\n", + " foo.a = 10\n", + " print ('foo')\n", + "\n", + "print (foo.a)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a divisor1\n", + "dividing 2 by 1 gives me: 2.0\n", + "The input was a number and it was not zero!\n", + "We got to the end!\n" + ] + } + ], + "source": [ + "try:\n", + " divisor = int(input ('Please enter a divisor'))\n", + " numerator = 2\n", + " print ('dividing 2 by ', divisor, ' gives me: ', numerator/divisor)\n", + "except ValueError:\n", + " print ('Sorry, you did not enter an integer')\n", + "except ZeroDivisionError:\n", + " print ('Sorry, you entered an invalid number')\n", + "else:\n", + " print ('The input was a number and it was not zero!')\n", + "finally:\n", + " print('We got to the end!')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -2743,9 +4873,7 @@ { "cell_type": "code", "execution_count": 26, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2771,9 +4899,7 @@ { "cell_type": "code", "execution_count": 27, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2895,7 +5021,6 @@ "cell_type": "code", "execution_count": 66, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -2921,9 +5046,7 @@ { "cell_type": "code", "execution_count": 35, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2945,9 +5068,7 @@ { "cell_type": "code", "execution_count": 65, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3038,9 +5159,73 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The int conversion failed\n" + ] + } + ], + "source": [ + "import sys\n", + "\n", + "a = '5nlah'\n", + "try:\n", + " b = int(a)\n", + "except:\n", + " print ('The int conversion failed')\n", + "\n", + "while (True):\n", + " key = input('Team name')\n", + " if key == 'x':\n", + " break\n", + " try: \n", + " print (team_dict[key])\n", + " except KeyError:\n", + " print ('Invalid team name, please re-enter')\n", + " continue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "colors - {'R':'Red', 'G':'Green', 'B':'Blue'}\n", + "if 'M' in colors:\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/home/bbrelin/src/repos/basicpython/docs\n" + ] + } + ], + "source": [ + "import os\n", + "print (os.getcwd())\n", + "open ('../labs/lab4/data/ALbb.salaries.2003.formatted.csc')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -3050,13 +5235,13 @@ "text": [ "Name of the file: ../data/presidents.dat\n", "Closed or not : False\n", - "Opening mode : r\n" + "Opening mode : a\n" ] } ], "source": [ "# Here we open a file with the default arguments for the mode and buffer.\n", - "myfileobject = open('../data/presidents.dat')\n", + "myfileobject = open('../data/presidents.dat','a')\n", "\n", "# Once we've obtained the file object, we can access a number of methods \n", "# and attributes.\n", @@ -3096,11 +5281,35 @@ "" ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "George Washington\n", + "John Adams\n", + "Thomas Jefferson\n", + "James Madison\n", + "James Monroe\n", + "\n" + ] + } + ], + "source": [ + "myfileobject = open('../data/presidents.dat')\n", + "datalist = myfileobject.read()\n", + "print (datalist)\n", + "myfileobject.close()\n" + ] + }, { "cell_type": "code", "execution_count": 53, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -3129,6 +5338,33 @@ " data = myfileobject.readline()" ] }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First name = George Last name = Washington\n", + "First name = John Last name = Adams\n", + "First name = Thomas Last name = Jefferson\n", + "First name = James Last name = Madison\n", + "First name = James Last name = Monroe\n" + ] + } + ], + "source": [ + "myfileobject = open('../data/presidents.dat')\n", + "\n", + "for line in myfileobject:\n", + " (fname,lname) = line.strip().split()\n", + " print ('First name = ',fname, 'Last name = ',lname)\n", + " \n", + "myfileobject.close()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -3159,9 +5395,7 @@ { "cell_type": "code", "execution_count": 55, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3210,9 +5444,7 @@ { "cell_type": "code", "execution_count": 58, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3270,16 +5502,68 @@ }, { "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/home/bbrelin/src/repos/basicpython/docs\n" + ] + } + ], + "source": [ + "import os\n", + "print (os.getcwd())" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "i = 2\n", + "def isPrime(n):\n", + " global i\n", + " if n == 0 or n == 1:\n", + " return(False)\n", + " elif (n % i == 0) and (n == i):\n", + " i = 2\n", + " return(True)\n", + " elif n % i == 0:\n", + " i = 2\n", + " return(False)\n", + " else:\n", + " i += 1\n", + " return(isPrime(n))\n", + "\n", + "print (isPrime(17))\n", + "print (isPrime(4))\n", + "print (isPrime(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Writing some output\n" + "Writing some new output\n" ] } ], @@ -3288,7 +5572,7 @@ "# Write mode will automatically delete any existing data in the file before\n", "# opening it. \n", "myfileobject = open('../data/output_example.dat','w')\n", - "myfileobject.write('Writing some output')\n", + "myfileobject.write('Writing some new output')\n", "myfileobject.close()\n", "\n", "# Now let's reopen for read only.\n", @@ -3350,12 +5634,39 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 5\n", + "5\n" + ] + } + ], + "source": [ + "while (True):\n", + " try:\n", + " a = int(input ('Please enter a number '))\n", + " except ValueError:\n", + " print (\"You entered an invalid number!\")\n", + " print ('Please re-enter!')\n", + " finally:\n", + " break\n", + " \n", + "b = 0\n", + "b = a + b\n", + "print (b)" + ] + }, { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3405,6 +5716,75 @@ " myfileobject.close()\n" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "from math import pi,cos,sin,tan,log\n", + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib as plt \n", + "\n", + "np.array\n", + "\n", + "print (pi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import sys\n", + "try:\n", + " f = open('baseballsalaries.csv')\n", + "except OSError:\n", + " print ('File not found!')\n", + " sys.exit(1)\n", + " \n", + "\n", + "salaries = {}\n", + "try:\n", + " for line in f:\n", + " record = line.strip().split(',')\n", + " if record[0] not in salaries:\n", + " try:\n", + " salaries[record[0]] = int(record[3])\n", + " except ValueError:\n", + " continue\n", + " else:\n", + " try:\n", + " salaries[record[0]] += int(record[3])\n", + " except ValueError:\n", + " continue\n", + "except IOError:\n", + " print ('IO Error! Program exiting!')\n", + " sys.exit(1)\n", + " \n", + "finally:\n", + " f.close()\n", + " \n", + " \n", + "team = input('Please enter a team name: ')\n", + "try:\n", + " print (salaries[team])\n", + "except KeyError:\n", + " print ('Invalid key!')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -3482,12 +5862,114 @@ "- The code will run significantly faster in the Python Virtual Machine, often 30-40 per cent faster. " ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "newnums = [ num ** 2 for num in nums if num % 2 == 0 ]\n", + "print (newnums)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[18.333333333333336, -6.666666666666667, 37.77777777777778, 0.0]\n" + ] + } + ], + "source": [ + "fahrenheit = [65,20,100,32]\n", + "celsius = [ 5/9 * (f - 32) for f in fahrenheit ]\n", + "print (celsius)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat'), (7, 'Sun')]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8]\n", + "daysofweek = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']\n", + "\n", + "print (list(zip(nums,daysofweek)))\n", + "#dowdict = {pair[0]:pair[1] for pair in zip(nums,daysofweek)}\n", + "#print (dowdict[4])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number is divisible by two\n" + ] + } + ], + "source": [ + "x = 2\n", + "\n", + "if x % 2 == 0:\n", + " print ('Number is divisible by two')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "mylist = ['Fee','Fi','Fo','Fum']\n", + "myupperlist = [ word.upper() for word in mylist if 'Fee' not in word ]\n", + "\n", + "print (myupperlist)\n", + "#for word in mylist:\n", + "# myupperlist.append(word.upper())\n", + " \n", + "\n" + ] + }, { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3515,9 +5997,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3541,9 +6021,7 @@ { "cell_type": "code", "execution_count": 12, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3592,9 +6070,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3628,11 +6104,18 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the gemstone: Tanzanite\n", + "The price for that gemstone is: 1000.0\n" + ] + } + ], "source": [ "# Here are the two lists that we want to combine into a dictionary. \n", "gemstonenames = ['Tanzanite','Taaffeite','Black Opal', 'Benitoite', 'Red Beryl', 'Alexandrite', \\\n", @@ -3641,14 +6124,407 @@ "\n", "# Write a dictionary comprehension to combine the two lists. The names will be the keys, the price will be the values. \n", "# Refer to the Python documentation for the zip() built in function. \n", + "\n", + "gemstonedict ={}\n", + "#gemstonelist = zip(gemstonenames, gemstoneprice)\n", + "#for gems in gemstonelist:\n", + "# gemstonedict [gems[0]] = gems[1]\n", + "gemstonedict = {gems[0]:gems[1] for gems in zip(gemstonenames,gemstoneprice)}\n", + "gemkey = input ('Please enter the gemstone: ')\n", + "print ('The price for that gemstone is: ',gemstonedict[gemkey])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'d': 4, 'e': 5, 'b': 2, 'c': 3, 'a': 1}\n" + ] + } + ], + "source": [ + "list1 = ['a','b','c','d','e']\n", + "list2 = [1,2,3,4,5]\n", + "list3 = list(zip(list1,list2))\n", + "d = {}\n", + "d = {pair[0]:pair[1] for pair in list3}\n", + "print (d)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 27, 16, 125, 36, 343, 64, 729, 100]\n" + ] + } + ], + "source": [ + "mynums = [1,2,3,4,5,6,7,8,9,10]\n", + "mysquares = [num **2 if num %2 == 0 else num **3 for num in mynums]\n", + "print (mysquares)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "def square(x):\n", + " return x**2\n", + "\n", + "def get_even(x):\n", + " if x % 2 == 0:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "mynums = [1,2,3,4,5,6,7,8,9,10]\n", + "\n", + "mysquares = map(square,filter(get_even,mynums))\n", + "print (list(mysquares))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Looking for this in Does this text match the pattern\n", + "Found a match!\n", + "The match started at position: 5 and ended at position: 9\n", + "\n", + "Looking for that in Does this text match the pattern\n", + "No match found!\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "patterns = ['this','that']\n", + "text = 'Does this text match the pattern'\n", + "\n", + "for pattern in patterns:\n", + " print ('Looking for %s in %s' %(pattern,text))\n", + " searchResult = re.search(pattern,text)\n", + " if searchResult:\n", + " print ('Found a match!')\n", + " print ('The match started at position: %d and ended at position: %d\\n'% (searchResult.start(),searchResult.end()))\n", + " else:\n", + " print ('No match found!')" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account Name\tAccount Balance\n", + "-------------------------------\n", + "Braun Brelin\t10000.00\n", + "Joe Blow\t500.41\n", + "Jack Green\t251.24\n" + ] + } + ], + "source": [ + "name = 'Braun Brelin'\n", + "age = 21\n", + "\n", + "acctlist = [('Braun Brelin',10000.00052),('Joe Blow',500.4102),('Jack Green',251.2351)]\n", + "print ('Account Name\\tAccount Balance')\n", + "print ('-'*31)\n", + "for (acctname,acctbal) in acctlist:\n", + " print ('%s\\t%.2f' % (acctname,acctbal))\n", + " \n", + " \n", + "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "r = 2.236 t = 1.107\n", + "r = 7.810 t = 0.876\n", + "r = 3.162 t = -1.249\n", + "r = 5.657 t = 0.785\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def convert_to_polar(coordt):\n", + " x,y = coordt\n", + " radius = math.sqrt(x**2 + y**2)\n", + " theta = math.atan(y/x)\n", + " return (radius,theta)\n", + "\n", + "coordlist = [(1,2),(5,6),(-1,3),(4,4)]\n", + "polarlist = map(convert_to_polar,coordlist)\n", + "for coord in polarlist:\n", + " print ('r = %.3f t = %.3f' % (coord[0],coord[1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n" + ] + } + ], + "source": [ + "def myfunc(a,b=1):\n", + " print (a,b)\n", + " return\n", + "\n", + "myfunc(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n" + ] + } + ], + "source": [ + "class Foo:\n", + " def __init__(self,a,b):\n", + " self.first = a\n", + " self.second = b\n", + " \n", + " def returna(self):\n", + " return self.first\n", + " \n", + " def returnb(self):\n", + " return self.second\n", + " \n", + "f = Foo(1,2)\n", + "print (f.returna())\n", + "print (f.returnb())" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Speak!\n", + "3\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "class Animal:\n", + " \n", + " ''' This is my animal class. It has a speak method '''\n", + " \n", + " def __init__(self,n,a,b):\n", + " self.name = n\n", + " self.age = a\n", + " self.breed = b\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def setAge(self,newAge):\n", + " self.age = newAge\n", + " return\n", + " \n", + " def speak(self):\n", + " return ('Speak!')\n", + " \n", + "a = Animal('Rex',2,'German Shepherd')\n", + "a1 = Animal('Bowser',3,'Boxer')\n", + "a2 = Animal('Fido',4,'Poodle')\n", + "\n", + "print (a.speak())\n", + "a.setAge(3)\n", + "print (a.getAge())\n", + "\n", + "print (a1.getAge())\n", + "print (a2.getAge())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100.0\n", + "500.0\n" + ] + } + ], + "source": [ + "class BankAcct:\n", + " def __init__(self,balance):\n", + " self.balance = balance\n", + " \n", + " def getBalance(self):\n", + " return (self.balance)\n", + " \n", + " def Deposit(self,deposit):\n", + " self.balance += newbal\n", + " \n", + " def Withdrawal(self,withdrawal):\n", + " self.balance -= withdrawal\n", + " \n", + " def setBalance(self,newbal):\n", + " self.balance = newbal\n", + " \n", + "\n", + "braun_checking = BankAcct(100.00)\n", + "braun_saving = BankAcct(500.00)\n", + "braun_ccacct = BankAcct(250.00)\n", + "print (braun_checking.getBalance())\n", + "braun_checking.setBalance(500.00)\n", + "print (braun_checking.getBalance()) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "class Light:\n", + " \n", + " ''' This is the Light class.\n", + " There are two methods, switch and getState \n", + " and there is one variable, state \n", + " '''\n", + " def __init__(self,s=False):\n", + " self.state=s\n", + " \n", + " def switch(self):\n", + " if (self.state == False):\n", + " self.state = True\n", + " else:\n", + " self.state = False\n", + " \n", + " def getState(self):\n", + " return self.state\n", + "\n", + " \n", + "light1 = Light(True)\n", + "light2 = Light()\n", + "\n", + "light1.switch()\n", + "light2.switch()\n", + "print (light1.getState())\n", + "print (light2.getState())" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Match found!\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "ipfile = open('ipaddrs.txt')\n", + "\n", + "repattern = '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'\n", + "validipaddrs = 0\n", + "invalidipaddrs=0\n", + "for ipaddr in ipfile:\n", + " match_object = re.search(repattern,ipaddr\n", + "\n", + "if match_object is None:\n", + " invalidipaddrs +=1 \n", + "else:\n", + " validipaddrs +=1\n", + " \n", + "print ('Found %d valid ipaddresses' %(validipaddrs))\n", + "print ('Found %d invalid ipaddresses' %(invalidipaddrs))\n", + "print ('Processes %d total ipaddresses' %(validipaddrs + invalidipaddrs)) " + ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [default]", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -3662,7 +6538,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.6.7" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false } }, "nbformat": 4, diff --git a/docs/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/.ipynb_checkpoints/Untitled1-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled1-checkpoint.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled1-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/.ipynb_checkpoints/Untitled2-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled2-checkpoint.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled2-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/.ipynb_checkpoints/Untitled3-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled3-checkpoint.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled3-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/.ipynb_checkpoints/Untitled4-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled4-checkpoint.ipynb new file mode 100644 index 0000000..7fec515 --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled4-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/.ipynb_checkpoints/Untitled5-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled5-checkpoint.ipynb new file mode 100644 index 0000000..7fec515 --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled5-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/.ipynb_checkpoints/Untitled6-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled6-checkpoint.ipynb new file mode 100644 index 0000000..7fec515 --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled6-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/.ipynb_checkpoints/Untitled7-checkpoint.ipynb b/docs/.ipynb_checkpoints/Untitled7-checkpoint.ipynb new file mode 100644 index 0000000..7fec515 --- /dev/null +++ b/docs/.ipynb_checkpoints/Untitled7-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/.ipynb_checkpoints/notes-7.4-checkpoint.2022 b/docs/.ipynb_checkpoints/notes-7.4-checkpoint.2022 new file mode 100644 index 0000000..5df84cf --- /dev/null +++ b/docs/.ipynb_checkpoints/notes-7.4-checkpoint.2022 @@ -0,0 +1,2121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4c512f51", + "metadata": {}, + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e5699b81", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], + "source": [ + "x = 10\n", + "x = x + 1\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "f7e78254", + "metadata": {}, + "source": [ + "# This is a header\n", + "## This is a sub header\n", + "### Third header.\n", + "\n", + "**This is bolded**
\n", + "*This is italicized*" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "97ad1400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], + "source": [ + "import this" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a4389c89", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello from Python\n" + ] + } + ], + "source": [ + "print ('Hello from Python')" + ] + }, + { + "cell_type": "markdown", + "id": "2ba69ab4", + "metadata": {}, + "source": [ + "# Data types in Python\n", + "\n", + "1. Numbers.\n", + " A. Integers\n", + " B. Fixed Point\n", + " C. Complex Numbers. (a + bi)\n", + " D. Octal Numbers. \n", + " E. Hexadecimal Numbers. \n", + " 2. Strings.\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d903c5d3", + "metadata": {}, + "outputs": [], + "source": [ + "x = 10" + ] + }, + { + "cell_type": "markdown", + "id": "8770c190", + "metadata": {}, + "source": [ + "Object reference ID ----> The object\n", + "x = 10 This is an object. Specifically, it's a numeric object. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a201e3ab", + "metadata": {}, + "outputs": [], + "source": [ + "y = 20.0\n", + "c = 10j\n", + "o = 010\n", + "x = 0xff" + ] + }, + { + "cell_type": "markdown", + "id": "9a4d56ea", + "metadata": {}, + "source": [ + "** -> Exponentiation operator.\n", + "*\n", + "/ -> Fixed point division\n", + "// -> Integer division\n", + "% -> Find the remainder.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "8a7b3006", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.3333333333333335\n", + "3.0\n", + "-3.3333333333333335\n", + "-4.0\n" + ] + } + ], + "source": [ + "x = 10.0\n", + "y = 3\n", + "print (x / y)\n", + "print (x // y) # Floor division\n", + "y = -3\n", + "print (x / y)\n", + "print (x // y) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "25e8596e", + "metadata": {}, + "outputs": [], + "source": [ + "x = True\n", + "y = False\n", + "a = 5\n", + "if a > 5:\n", + " print ('A is bigger than 5')\n", + " print ('foo')\n", + "print ('Not in the block anymore')" + ] + }, + { + "cell_type": "markdown", + "id": "a03b20b0", + "metadata": {}, + "source": [ + "if (a > 5) {\n", + " print ('A is bigger than 5');\n", + "}\n", + "\n", + "if (a>5) {printf(\"A is bigger than 5\\n\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "386f83fb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n" + ] + } + ], + "source": [ + "print ('Hello')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0c7227ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 4\n", + "4\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "2b6283b4", + "metadata": {}, + "source": [ + "Every type of input comes in as a string, not a number. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e7e39b0c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 7\n", + "11\n", + "11.0\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "y = int(x) # Int is a built in function. It converts datatypes to numeric (integer) objects. \n", + "print (y + 4)\n", + "z = float(x)\n", + "print (z + 4)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5d5c95a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/1375056562.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "t = \"Hello World\"\n", + "z = \"\"\" This is a multi line string. \n", + " This is line 2 of the string.\n", + " And so on...\n", + " \"\"\"\n", + "# This is a single line comment. \n", + "# Strings are immutable.\n", + "\n", + "print (s)\n", + "s [0] = 'J'\n" + ] + }, + { + "cell_type": "markdown", + "id": "4c47351a", + "metadata": {}, + "source": [ + "These are examples of string slicing. " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "09b6b757", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "H\n", + "e\n", + "Hello\n", + "Hlo\n", + "drWolH\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s[0])\n", + "print (s[1]) #This concept is called a 'slice'\n", + "print (s[0:5]) # Start at the beginning and go to the end - 1 character.\n", + "print (s[0:5:2]) # The last value is a stepper value. \n", + "print (s[::-1])" + ] + }, + { + "cell_type": "markdown", + "id": "56610e95", + "metadata": {}, + "source": [ + "What is an object?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "38095079", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "417fa3ff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "HELLO WORLD\n", + "hello world\n", + "11\n", + "11\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s.upper())\n", + "print (s.lower())\n", + "print (len(s))\n", + "print (s.__len__())" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "19038cce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is less than 10\n" + ] + } + ], + "source": [ + "x = 9\n", + "l_counter = 0\n", + "if x == 10:\n", + " print ('x is 10')\n", + "elif x < 10:\n", + " print ('x is less than 10')\n", + "else:\n", + " print (\"x is greater than 10\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6d67acf6", + "metadata": {}, + "source": [ + "A string is an example of an *iterable* object. \n", + "We can use a looping construct to iterate over the object. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "ce320277", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for i in range(len(s)):\n", + " print (s[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "e5208dad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "print (list(range(10)))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "14bc20de", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for elem in s:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "1b621a14", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World \n", + "Hello World Hello World Hello World Hello World Hello World \n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World \"\n", + "s = s1 + s2 # + is string concatenation for strings. \n", + "print (s)\n", + "print (s * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "225da735", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "187e7215", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "s = \"Braun Brelin, 1234 Main Street, Anytown, USA\"\n", + "print (s.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c5d8823c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class str in module builtins:\n", + "\n", + "class str(object)\n", + " | str(object='') -> str\n", + " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", + " | \n", + " | Create a new string object from the given object. If encoding or\n", + " | errors is specified, then the object must expose a data buffer\n", + " | that will be decoded using the given encoding and error handler.\n", + " | Otherwise, returns the result of object.__str__() (if defined)\n", + " | or repr(object).\n", + " | encoding defaults to sys.getdefaultencoding().\n", + " | errors defaults to 'strict'.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __format__(self, format_spec, /)\n", + " | Return a formatted version of the string as described by format_spec.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(self, key, /)\n", + " | Return self[key].\n", + " | \n", + " | __getnewargs__(...)\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __hash__(self, /)\n", + " | Return hash(self).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self int\n", + " | \n", + " | Return the number of non-overlapping occurrences of substring sub in\n", + " | string S[start:end]. Optional arguments start and end are\n", + " | interpreted as in slice notation.\n", + " | \n", + " | encode(self, /, encoding='utf-8', errors='strict')\n", + " | Encode the string using the codec registered for encoding.\n", + " | \n", + " | encoding\n", + " | The encoding in which to encode the string.\n", + " | errors\n", + " | The error handling scheme to use for encoding errors.\n", + " | The default is 'strict' meaning that encoding errors raise a\n", + " | UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", + " | 'xmlcharrefreplace' as well as any other name registered with\n", + " | codecs.register_error that can handle UnicodeEncodeErrors.\n", + " | \n", + " | endswith(...)\n", + " | S.endswith(suffix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S ends with the specified suffix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | suffix can also be a tuple of strings to try.\n", + " | \n", + " | expandtabs(self, /, tabsize=8)\n", + " | Return a copy where all tab characters are expanded using spaces.\n", + " | \n", + " | If tabsize is not given, a tab size of 8 characters is assumed.\n", + " | \n", + " | find(...)\n", + " | S.find(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | format(...)\n", + " | S.format(*args, **kwargs) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from args and kwargs.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | format_map(...)\n", + " | S.format_map(mapping) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from mapping.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | index(...)\n", + " | S.index(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | isalnum(self, /)\n", + " | Return True if the string is an alpha-numeric string, False otherwise.\n", + " | \n", + " | A string is alpha-numeric if all characters in the string are alpha-numeric and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isalpha(self, /)\n", + " | Return True if the string is an alphabetic string, False otherwise.\n", + " | \n", + " | A string is alphabetic if all characters in the string are alphabetic and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isascii(self, /)\n", + " | Return True if all characters in the string are ASCII, False otherwise.\n", + " | \n", + " | ASCII characters have code points in the range U+0000-U+007F.\n", + " | Empty string is ASCII too.\n", + " | \n", + " | isdecimal(self, /)\n", + " | Return True if the string is a decimal string, False otherwise.\n", + " | \n", + " | A string is a decimal string if all characters in the string are decimal and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isdigit(self, /)\n", + " | Return True if the string is a digit string, False otherwise.\n", + " | \n", + " | A string is a digit string if all characters in the string are digits and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isidentifier(self, /)\n", + " | Return True if the string is a valid Python identifier, False otherwise.\n", + " | \n", + " | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n", + " | such as \"def\" or \"class\".\n", + " | \n", + " | islower(self, /)\n", + " | Return True if the string is a lowercase string, False otherwise.\n", + " | \n", + " | A string is lowercase if all cased characters in the string are lowercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | isnumeric(self, /)\n", + " | Return True if the string is a numeric string, False otherwise.\n", + " | \n", + " | A string is numeric if all characters in the string are numeric and there is at\n", + " | least one character in the string.\n", + " | \n", + " | isprintable(self, /)\n", + " | Return True if the string is printable, False otherwise.\n", + " | \n", + " | A string is printable if all of its characters are considered printable in\n", + " | repr() or if it is empty.\n", + " | \n", + " | isspace(self, /)\n", + " | Return True if the string is a whitespace string, False otherwise.\n", + " | \n", + " | A string is whitespace if all characters in the string are whitespace and there\n", + " | is at least one character in the string.\n", + " | \n", + " | istitle(self, /)\n", + " | Return True if the string is a title-cased string, False otherwise.\n", + " | \n", + " | In a title-cased string, upper- and title-case characters may only\n", + " | follow uncased characters and lowercase characters only cased ones.\n", + " | \n", + " | isupper(self, /)\n", + " | Return True if the string is an uppercase string, False otherwise.\n", + " | \n", + " | A string is uppercase if all cased characters in the string are uppercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | join(self, iterable, /)\n", + " | Concatenate any number of strings.\n", + " | \n", + " | The string whose method is called is inserted in between each given string.\n", + " | The result is returned as a new string.\n", + " | \n", + " | Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n", + " | \n", + " | ljust(self, width, fillchar=' ', /)\n", + " | Return a left-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | lower(self, /)\n", + " | Return a copy of the string converted to lowercase.\n", + " | \n", + " | lstrip(self, chars=None, /)\n", + " | Return a copy of the string with leading whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | partition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string. If the separator is found,\n", + " | returns a 3-tuple containing the part before the separator, the separator\n", + " | itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing the original string\n", + " | and two empty strings.\n", + " | \n", + " | removeprefix(self, prefix, /)\n", + " | Return a str with the given prefix string removed if present.\n", + " | \n", + " | If the string starts with the prefix string, return string[len(prefix):].\n", + " | Otherwise, return a copy of the original string.\n", + " | \n", + " | removesuffix(self, suffix, /)\n", + " | Return a str with the given suffix string removed if present.\n", + " | \n", + " | If the string ends with the suffix string and that suffix is not empty,\n", + " | return string[:-len(suffix)]. Otherwise, return a copy of the original\n", + " | string.\n", + " | \n", + " | replace(self, old, new, count=-1, /)\n", + " | Return a copy with all occurrences of substring old replaced by new.\n", + " | \n", + " | count\n", + " | Maximum number of occurrences to replace.\n", + " | -1 (the default value) means replace all occurrences.\n", + " | \n", + " | If the optional argument count is given, only the first count occurrences are\n", + " | replaced.\n", + " | \n", + " | rfind(...)\n", + " | S.rfind(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | rindex(...)\n", + " | S.rindex(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | rjust(self, width, fillchar=' ', /)\n", + " | Return a right-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | rpartition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string, starting at the end. If\n", + " | the separator is found, returns a 3-tuple containing the part before the\n", + " | separator, the separator itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing two empty strings\n", + " | and the original string.\n", + " | \n", + " | rsplit(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | Splits are done starting at the end of the string and working to the front.\n", + " | \n", + " | rstrip(self, chars=None, /)\n", + " | Return a copy of the string with trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | split(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | splitlines(self, /, keepends=False)\n", + " | Return a list of the lines in the string, breaking at line boundaries.\n", + " | \n", + " | Line breaks are not included in the resulting list unless keepends is given and\n", + " | true.\n", + " | \n", + " | startswith(...)\n", + " | S.startswith(prefix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S starts with the specified prefix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | prefix can also be a tuple of strings to try.\n", + " | \n", + " | strip(self, chars=None, /)\n", + " | Return a copy of the string with leading and trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | swapcase(self, /)\n", + " | Convert uppercase characters to lowercase and lowercase characters to uppercase.\n", + " | \n", + " | title(self, /)\n", + " | Return a version of the string where each word is titlecased.\n", + " | \n", + " | More specifically, words start with uppercased characters and all remaining\n", + " | cased characters have lower case.\n", + " | \n", + " | translate(self, table, /)\n", + " | Replace each character in the string using the given translation table.\n", + " | \n", + " | table\n", + " | Translation table, which must be a mapping of Unicode ordinals to\n", + " | Unicode ordinals, strings, or None.\n", + " | \n", + " | The table must implement lookup/indexing via __getitem__, for instance a\n", + " | dictionary or list. If this operation raises LookupError, the character is\n", + " | left untouched. Characters mapped to None are deleted.\n", + " | \n", + " | upper(self, /)\n", + " | Return a copy of the string converted to uppercase.\n", + " | \n", + " | zfill(self, width, /)\n", + " | Pad a numeric string with zeros on the left, to fill a field of the given width.\n", + " | \n", + " | The string is never truncated.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | maketrans(...)\n", + " | Return a translation table usable for str.translate().\n", + " | \n", + " | If there is only one argument, it must be a dictionary mapping Unicode\n", + " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", + " | Character keys will be then converted to ordinals.\n", + " | If there are two arguments, they must be strings of equal length, and\n", + " | in the resulting dictionary, each character in x will be mapped to the\n", + " | character at the same position in y. If there is a third argument, it\n", + " | must be a string, whose characters will be mapped to None in the result.\n", + "\n" + ] + } + ], + "source": [ + "help(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "113cc64e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (str)" + ] + }, + { + "cell_type": "markdown", + "id": "957d378a", + "metadata": {}, + "source": [ + "The List data type. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "e9f462b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "s = \"1\"\n", + "l = [1,2,3,4,5,6,7,8,9,10] # Is a list, an iterable object?\n", + "print (l)\n", + "for elem in l:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "e641abe4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (list)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "572cb928", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l\n", + "l.append(11)\n", + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "daf174d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "[1, 2, 3, 4]\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "9\n" + ] + } + ], + "source": [ + "print (l[0])\n", + "print (l[0:4])\n", + "#print (l[::-1])\n", + "print (l)\n", + "print (l.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "051158ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']\n", + "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "print (len (l))\n", + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = l1 + l2\n", + "print (l3)\n", + "print (l1 * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "61b6c9d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "3f6e10c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "b4bcd51d", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "dis() got an unexpected keyword argument 'squares'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/266041033.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m print (dis(\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0msquares\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;33m]\u001b[0m \u001b[1;31m# List comprehension\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m ))\n", + "\u001b[1;31mTypeError\u001b[0m: dis() got an unexpected keyword argument 'squares'" + ] + } + ], + "source": [ + "from dis import dis\n", + "\n", + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums if num % 2 == 0 ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "410cd78f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "jack_list = [\"Fee\",\"Fi\",\"Fo\",\"Fum\"]\n", + "jack_upper_list = [ word.upper() for word in jack_list ]\n", + "print (jack_upper_list)" + ] + }, + { + "cell_type": "markdown", + "id": "1d20fdbc", + "metadata": {}, + "source": [ + "Tuples. Tuples are similar to lists, except that, like strings, they are immutable. \n", + "We use the round braces () to specify a tuple.." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "702f7ab3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/4289262067.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = (1,2,3,4,5)\n", + "print (t)\n", + "\n", + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "823197e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]\n", + "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]\n" + ] + } + ], + "source": [ + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = zip(l1,l2)\n", + "print (list(l3))\n", + "print (list(enumerate(l2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "4b97b974", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "1\n" + ] + } + ], + "source": [ + "print (max(l1))\n", + "print (min(l1))\n", + "# tuple() converts objects into tuples. " + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "54fd10d5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 12)\n", + "Fee fi fo fum\n" + ] + } + ], + "source": [ + "x = 5\n", + "y = 6\n", + "\n", + "t = (x+5,y+6) #tuple packing. \n", + "(x+5,y+6) = t1\n", + "print (t)\n", + "s = \"Fee,fi,fo,fum\"\n", + "(first,second,third,fourth) = s.split(',')\n", + "print (first,second,third,fourth)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "ec7c3800", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Graduate\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/3873200294.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0mmovie_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mt1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = ('The Graduate', 'Dustin Hoffman','Anne Bancroft',1967)\n", + "\n", + "\n", + "'''\n", + "movie_name = t[0]\n", + "actor = t[1]\n", + "actress = t[2]\n", + "date = t[3]\n", + "'''\n", + "movie_name, actor, actress, date = t # This is tuple unpacking.\n", + "print (movie_name)\n", + "t1 = (movie_name,actor,actress,date) # Tuple packing.\n", + "print (t1)\n", + "movie_name = \"Papillon\"\n", + "print (t1)\n", + "t1[0] = \"Papillon\" \n" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "3c68c436", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'orange', 'apple', 'banana', 'grapefruit'}\n" + ] + } + ], + "source": [ + "list1 = ['apple','banana','orange','apple','banana','grapefruit','apple']\n", + "set1 = set(list1)\n", + "print (set1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "66ef816a", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'pop from an empty set'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/2821507018.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mset1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;31m#print (set1.pop())\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'" + ] + } + ], + "source": [ + "print (set1.pop())\n", + "#print (set1.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "77949490", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "\n", + "\n" + ] + } + ], + "source": [ + "x = 1\n", + "print (str(x))\n", + "y = str(x)\n", + "print (type(x))\n", + "print (type(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c723273e", + "metadata": {}, + "outputs": [], + "source": [ + "from math import sqrt,atan\n", + "\n", + "sqrt()\n", + "atan()\n" + ] + }, + { + "cell_type": "markdown", + "id": "88db836e", + "metadata": {}, + "source": [ + "Dictionary. Stores key/value pairs in Python...\n" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "82ed62a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "value1\n" + ] + } + ], + "source": [ + "d = {'key1':'value1','key2':'value2','key3':'value3'}\n", + "\n", + "print (d['key1'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a53af4c0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "6e7c1277", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values(['value1', 'value2', 'value3'])\n" + ] + } + ], + "source": [ + "print(d.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "2278b952", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['key1', 'key2', 'key3']\n" + ] + } + ], + "source": [ + "print (list(d.keys()))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37cc6ae4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "4a731d45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])\n" + ] + } + ], + "source": [ + "print (d.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "06f6658d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "6fca4445", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No such color\n" + ] + } + ], + "source": [ + "colors = {'Red':1,'Green':2,'Blue':3}\n", + "if 'Yellow' in colors:\n", + " print (colors['Yellow'])\n", + "else:\n", + " print ('No such color') \n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "69358712", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(colors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc366635", + "metadata": {}, + "outputs": [], + "source": [ + "def myfunc():\n", + " a = 1\n", + " b = 0\n", + " c = a + b\n", + " return c\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "e9764c55", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "X before calling myfunc = 10\n", + "In the function, x = 11\n", + "X after calling myfunc = 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def myfunc(x):\n", + " # x = x + 1 #These two statements are identical. \n", + " x += 1\n", + " print (\"In the function, x = \",x)\n", + " \n", + "print (\"X before calling myfunc = \",x)\n", + "myfunc(x)\n", + "print (\"X after calling myfunc = \",x)\n" + ] + }, + { + "cell_type": "markdown", + "id": "1894c841", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "Scoping \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4655084", + "metadata": {}, + "outputs": [], + "source": [ + "myfunc(a,b=0)" + ] + } + ], + "metadata": { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/13-6-2022.ipynb b/docs/13-6-2022.ipynb new file mode 100644 index 0000000..a190032 --- /dev/null +++ b/docs/13-6-2022.ipynb @@ -0,0 +1,1772 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "if x == 10:\n", + " print ('x is 10')\n", + "print ('x is not 10')" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n", + " This is the docstring for func1\n", + " \n" + ] + } + ], + "source": [ + "# this is a comment\n", + "print ('Hello') # This is a comment here. \n", + "\n", + "''' Everything in here is a string\n", + "[ We use this for multiline comments.\n", + "'''\n", + "\n", + "def func1(a):\n", + " ''' This is the docstring for func1\n", + " '''\n", + " \n", + " return a +1\n", + "\n", + "print (func1.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World') # Single quotes\n", + "print (\"Hello World\") # Double quotes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First set of object types\n", + "\n", + "Numbers\n", + "\n", + " 1 Integers\n", + " 2. Floating point (Implemented as double precision)\n", + " 3. Complex\n", + " 4. Binary\n", + " 5. Hexadecimal\n", + " 6. Octal. \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "y = 2.5\n", + "c = 2j\n", + "b = 0b1010111\n", + "h = 0xffff\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "a,b,c = 1,2,3\n", + "print (a,b,c)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 5 5\n" + ] + } + ], + "source": [ + "x = y = z = 5\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Numbers\n", + "Strings\n", + "Lists\n", + "Tuples\n", + "Sets \n", + "Dictionaries\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** \n", + "* / //\n", + "+ - \n", + "%\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0\n", + "3.0\n" + ] + } + ], + "source": [ + "a = 6.0\n", + "b = 2.0\n", + "c = 2\n", + "print (a/b)\n", + "print (a // c)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "a = 5\n", + "b = 2\n", + "print (a % b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = a + 1 # Both of these statements do the same thing. \n", + "a += 1 #This iis a shortcut\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Boolean type\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = True,1,'Non empty string'\n", + "b = False, 0,'' \n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = True\n", + "if a:\n", + " print ('True')\n", + "if a == True:\n", + " print ('True')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if not a:\n", + " print ('False')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if a and not b:\n", + " print ('Foo')\n", + " \n", + "if a or not b:\n", + " print ('Bar')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "import math\n", + "print (math.pi)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number: 5\n", + "5\n" + ] + } + ], + "source": [ + "x = input (\"Please type in a number: \")\n", + "print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Goodbye World\n" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "print (s)\n", + "s = \"Goodbye World\"\n", + "print (s)\n", + "\n", + "# Strings are immutable.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Hello World\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "s [0] = 'J'\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HELLO WORLD\n", + "hello world\n" + ] + } + ], + "source": [ + "print (s.upper())\n", + "print (s.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "print (s[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "e\n" + ] + } + ], + "source": [ + "print (s[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ello\n" + ] + } + ], + "source": [ + "print (s[1:5]) #This is called a slice." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HloWrd\n" + ] + } + ], + "source": [ + "print (s[::2])" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dlroW olleH\n" + ] + } + ], + "source": [ + "print (s[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World\"\n", + "\n", + "s3 = s1 + s2\n", + "print (s3)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Hello Hello Hello Hello \n" + ] + } + ], + "source": [ + "s4 = \"Hello \" * 5\n", + "print (s4)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "data = 'Braun Brelin, 1234 Main Street, Anytown, USA'\n", + "print (data.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number 5\n", + "\n", + "\n", + "25\n" + ] + } + ], + "source": [ + "x = input(\"Please type in a number \")\n", + "print (type(x))\n", + "x = int(x)\n", + "print (type(x))\n", + "print (x**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number b\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please type in a number \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input(\"Please type in a number \"))\n", + "print (x ** 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type ib some input: Hello\n", + "The length of your string is: 5\n" + ] + } + ], + "source": [ + "s = input (\"Please type ib some input: \")\n", + "print (\"The length of your string is: \", len(s))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings are also *iterable* objects. " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n" + ] + } + ], + "source": [ + "for c in s:\n", + " print (c)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,'a','b','c']\n", + "for o in mylist:\n", + " print (o)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 'a']\n" + ] + } + ], + "source": [ + "print (mylist[1:4])" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['c', 'b', 'a', 3, 2, 1]\n" + ] + } + ], + "source": [ + "print (mylist[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(mylist)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd']\n" + ] + } + ], + "source": [ + "mylist.append('d')\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "source": [ + "print (len(mylist))" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n" + ] + } + ], + "source": [ + "mylist1 = ['e','f','g']\n", + "mylist = mylist + mylist1\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]\n" + ] + } + ], + "source": [ + "mylist2 = [1,2,3]\n", + "mylist3 = mylist2 * 5\n", + "print (mylist3)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [[1,2,3],['a','b','c']]\n", + "print (mylist[0][2])\n", + "print (mylist[1][2])" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "words = ['fee','fi','fo','fum']\n", + "upper_words = [word.upper() for word in words] # List Comprehension\n", + " print (upper_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums if num % 2 == 0]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "t = (0,1,2,3,4,5)\n", + "print (t[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2, 3, 4)" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[1:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 1, 2, 3, 4, 5)" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(t)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun 21 Male\n" + ] + } + ], + "source": [ + "name = \"Braun\"\n", + "age = 21\n", + "gender = \"Male\"\n", + "\n", + "t = (name,age,gender) # Tuple packing.\n", + "\n", + "#print (t)\n", + "\n", + "(x,y,z) = t # Tuple Unpacking\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 'a')\n", + "(1, 'b')\n", + "(2, 'c')\n", + "(3, 'd')\n", + "(4, 'e')\n" + ] + } + ], + "source": [ + "example_list = ['a','b','c','d','e']\n", + "\n", + "for i in enumerate(example_list):\n", + " print (i)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 10), ('b', 11), ('c', 12), ('d', 13)]\n" + ] + } + ], + "source": [ + "example_list2 = [10,11,12,13]\n", + "\n", + "example_list3 = zip(example_list, example_list2)\n", + "print (list(example_list3))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Apple', 'Orange', 'Banana'}\n" + ] + } + ], + "source": [ + "myset = {'Apple','Orange','Banana','Apple'}\n", + "print (myset)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is neither 0 nor 1\n" + ] + } + ], + "source": [ + "x = 2\n", + "if not x:\n", + " print (\"x is 0\" )\n", + "elif x == 1:\n", + " print (\"x is 1\")\n", + "else:\n", + " print (\"x is neither 0 nor 1\")" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid IP Address\n" + ] + } + ], + "source": [ + "ipaddr = \"257.168.100.1\"\n", + "for octet in ipaddr.split(\".\"):\n", + " if int(octet) >255:\n", + " print (\"Invalid IP Address\")\n", + " break\n", + "else:\n", + " print (\"Valid IP Address\")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "x = 10\n", + "while x > 0:\n", + " print (x)\n", + " x -= 1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "num is 5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "for num in nums:\n", + " if num == 5:\n", + " print (\"num is 5\")\n", + " continue\n", + " else:\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "def func1(x: int = 2)-> int:\n", + " return x**2\n", + "\n", + "print (func1())\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n", + "d\n" + ] + } + ], + "source": [ + "def func2(*args: list) -> str: \n", + " for arg in args:\n", + " print (arg)\n", + " \n", + "func2(\"a\",\"b\",\"c\",\"d\")" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a foo\n", + "b bar\n", + "c baz\n" + ] + } + ], + "source": [ + "def func3(**kwargs: dict) -> None:\n", + " for (key,value) in kwargs.items():\n", + " print (key,value)\n", + " \n", + "func3(a = \"foo\", b = \"bar\",c = \"baz\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'key1':'value1', 'key2':'value2', 'key3':'value3'}\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "4\n" + ] + } + ], + "source": [ + "dow_dict = {}\n", + "list1 = ['Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday','Sunday']\n", + "list2 = [1,2,3,4,5,6,7]\n", + "dow_dict = { k:v for (k,v) in zip(list1,list2)} # Dictionary comprehension\n", + "\n", + "print (type(dow_dict))\n", + "print (dow_dict['Thursday'])" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['Monday', 'Tuesday', 'Wedneseday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])\n", + "dict_values([1, 2, 3, 4, 5, 6, 7])\n", + "dict_items([('Monday', 1), ('Tuesday', 2), ('Wedneseday', 3), ('Thursday', 4), ('Friday', 5), ('Saturday', 6), ('Sunday', 7)])\n" + ] + } + ], + "source": [ + "print (dow_dict.keys())\n", + "print (dow_dict.values())\n", + "print (dow_dict.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid key!\n" + ] + } + ], + "source": [ + "if 'Braunsday' in dow_dict:\n", + " print (dow_dict['Braunsday'])\n", + "else:\n", + " print ('Invalid key!')" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter day of week: Wednesday\n", + "3\n" + ] + } + ], + "source": [ + "while True:\n", + " key = input(\"Enter day of week: \")\n", + " if key not in dow_dict:\n", + " print (\"Invalid key! Try again\")\n", + " continue\n", + " else:\n", + " print (dow_dict[key])\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a numberb\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input (\"Please enter a number\"))\n", + "print (x + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: b\n", + "Invalid number!\n", + "Please enter a number: c\n", + "Invalid number!\n", + "Please enter a number: 4\n", + "5\n" + ] + } + ], + "source": [ + "while True: \n", + " try:\n", + " x = int(input (\"Please enter a number: \")) \n", + " except ValueError:\n", + " print (\"Invalid number!\")\n", + " continue\n", + " print (x + 1)\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " # Execute some function here \n", + "except:\n", + " # If the function throws an exception, the code for this event ia handled here. \n", + "else:\n", + " # Only runs this code if the function suceeeded. \n", + "finally:\n", + " # Always run this code when the try block is finished. \n", + " # This is called try with resources. \n", + " \n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\bbrel\\\\basicpython\\\\docs'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from os import getcwd\n", + "\n", + "getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "George Washington\n", + "John Adams\n", + "Thomas Jefferson\n", + "James Madison\n", + "James Monroe\n" + ] + } + ], + "source": [ + "with open('c:/users/bbrel/basicpython/labs/lab4/data/test.dat') as f:\n", + " for line in f:\n", + " print (line.strip())" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x before function call is: 10\n", + "x inside the function is 11\n", + "x after function call is: 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def funct(y):\n", + " y += 1\n", + " print (\"y inside the function is \",y\n", + " return y\n", + "\n", + "print (\"x before function call is: \",x)\n", + "funct(x)\n", + "print (\"x after function call is: \",x)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "This is known as scope, or alternatively lexical scope. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for num in nums:\n", + " y = 20\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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/ALbb.salaries.2003.formatted.csv b/docs/ALbb.salaries.2003.formatted.csv new file mode 100644 index 0000000..097d5c8 --- /dev/null +++ b/docs/ALbb.salaries.2003.formatted.csv @@ -0,0 +1,383 @@ +New York Yankees,"Acevedo, Juan",900000,Pitcher +New York Yankees,"Anderson, Jason",300000,Pitcher +New York Yankees,"Clemens, Roger",10100000,Pitcher +New York Yankees,"Contreras, Jose",5500000,Pitcher +New York Yankees,"Flaherty, John",750000,Catcher +New York Yankees,"Giambi, Jason",11428571,First Baseman +New York Yankees,"Hammond, Chris",2200000,Pitcher +New York Yankees,"Hitchcock, Sterling",6000000,Pitcher +New York Yankees,"Jeter, Derek",15600000,Shortstop +New York Yankees,"Johnson, Nick",364100,First Baseman +New York Yankees,"Karsay, Steve",5000000,Pitcher +New York Yankees,"Latham, Chris",400000,Outfielder +New York Yankees,"Liever, Jon",550000,Pitcher +New York Yankees,"Matsui, Hideki",6000000,Outfielder +New York Yankees,"Mondesi, Raul",13000000,Outfielder +New York Yankees,"Mussina, Mike",12000000,Pitcher +New York Yankees,"Osuna, Antonio",2400000,Pitcher +New York Yankees,"Pettitte, Andy",11500000,Pitcher +New York Yankees,"Posada, Jorge",8000000,Catcher +New York Yankees,"Rivera, Mariano",10500000,Pitcher +New York Yankees,"Soriano, Alfonso",800000,Second Baseman +New York Yankees,"Trammell, Bubba",2500000,Outfielder +New York Yankees,"Ventura, Robin",5000000,Third Baseman +New York Yankees,"Weaver, Jeff",4150000,Pitcher +New York Yankees,"Wells, David",3250000,Pitcher +New York Yankees,"Williams, Bernie",12357143,Outfielder +New York Yankees,"Wilson, Enrique",700000,Shortstop +New York Yankees,"Zeile, Todd",1500000,Third Baseman +Anaheim Angels,"Anderson, Garret",5350000,Outfielder +Anaheim Angels,"Appier, Kevin",11500000,Pitcher +Anaheim Angels,"Callaway, Mickey",302500,Pitcher +Anaheim Angels,"Donnelly, Brendan",325000,Pitcher +Anaheim Angels,"Eckstein, David",425000,Shortstop +Anaheim Angels,"Erstad, Darin",7250000,Outfielder +Anaheim Angels,"Fullmer, Brad",1000000,First Baseman +Anaheim Angels,"Gil, Benji",725000,Shortstop +Anaheim Angels,"Glaus, Troy",7250000,Third Baseman +Anaheim Angels,"Kennedy, Adam",2270000,Second Baseman +Anaheim Angels,"Lackey, John",315000,Pitcher +Anaheim Angels,"Molina, Benjie",1425000,Catcher +Anaheim Angels,"Molina, Jose",320000,Catcher +Anaheim Angels,"Ortiz, Ramon",2266667,Pitcher +Anaheim Angels,"Owens, Eric",925000,Outfielder +Anaheim Angels,"Percival, Troy",7833333,Pitcher +Anaheim Angels,"Ramirez, Julio",300000,Outfielder +Anaheim Angels,"Rodriquez, Francisco",312500,Pitcher +Anaheim Angels,"Salmon, Tim",9900000,Outfielder +Anaheim Angels,"Schoeneweis, Scott",1425000,Pitcher +Anaheim Angels,"Sele, Aaron",8166667,Pitcher +Anaheim Angels,"Shields, Scot",305000,Pitcher +Anaheim Angels,"Spiezio, Scott",4250000,First Baseman +Anaheim Angels,"Washburn, Jarrod",3875000,Pitcher +Anaheim Angels,"Weber, Ben",375000,Pitcher +Anaheim Angels,"Wise, Matt",302500,Pitcher +Anaheim Angels,"Wooten, Shawn",337500,Catcher +Boston Red Sox,"Burkett, John",5500000,Pitcher +Boston Red Sox,"Damon, Johnny",7500000,Outfielder +Boston Red Sox,"Embree, Alan",3000000,Pitcher +Boston Red Sox,"Fossum, Casey",324500,Pitcher +Boston Red Sox,"Fox, Chad",500000,Pitcher +Boston Red Sox,"Garciaparra, Nomar",11000000,Shortstop +Boston Red Sox,"Giambi, Jeremy",2000000,Outfielder +Boston Red Sox,"Gonzalez, Dicky",300000,Pitcher +Boston Red Sox,"Hillenbrand, Shea",407500,Third Baseman +Boston Red Sox,"Howry, Bobby",1700000,Pitcher +Boston Red Sox,"Jackson, Damian",625000,Shortstop +Boston Red Sox,"Lowe, Derek",3625000,Pitcher +Boston Red Sox,"Lyon, Brandon",309500,Pitcher +Boston Red Sox,"Martinez, Pedro",15500000,Pitcher +Boston Red Sox,"Mendoza, Ramiro",2900000,Pitcher +Boston Red Sox,"Millar, Kevin",2000000,First Baseman +Boston Red Sox,"Mirabelli, Doug",805000,Catcher +Boston Red Sox,"Mueller, Bill",2100000,Third Baseman +Boston Red Sox,"Nixon, Trot",4000000,Outfielder +Boston Red Sox,"Ortiz, David",1250000,First Baseman +Boston Red Sox,"Person, Robert",300000,Pitcher +Boston Red Sox,"Ramirez, Manny",20000000,Outfielder +Boston Red Sox,"Timlin, Mike",1850000,Pitcher +Boston Red Sox,"Varitek, Jason",4700000,Catcher +Boston Red Sox,"Wakefield, Tim",4000000,Pitcher +Boston Red Sox,"Walker, Todd",3450000,Second Baseman +Boston Red Sox,"White, Matt",300000,Pitcher +Cleveland Indians,"Anderson, Brian",1500000,Pitcher +Cleveland Indians,"Baez, Danys",5125000,Pitcher +Cleveland Indians,"Bard, Josh",302100,Catcher +Cleveland Indians,"Bere, Jason",1000000,Pitcher +Cleveland Indians,"Blake, Casey",330000,Third Baseman +Cleveland Indians,"Bradley, Milton",314300,Outfielder +Cleveland Indians,"Broussard, Benjamin",303000,First Baseman +Cleveland Indians,"Burks, Ellis",7166667,Outfielder +Cleveland Indians,"Davis, Jason",301100,Pitcher +Cleveland Indians,"Garcia, Karim",900000,Outfielder +Cleveland Indians,"Gutierrez, Ricky",3916667,Shortstop +Cleveland Indians,"Hafner, Travis",302200,First Baseman +Cleveland Indians,"Laker, Tim",400000,Catcher +Cleveland Indians,"Lawton, Matt",6750000,Outfielder +Cleveland Indians,"Lee, Cliff",300900,Pitcher +Cleveland Indians,"McDonald, John",314400,Shortstop +Cleveland Indians,"Mulholland, Terry",500000,Pitcher +Cleveland Indians,"Myette, Aaron",307500,Pitcher +Cleveland Indians,"Phillips, Brandon",300900,Shortstop +Cleveland Indians,"Riske, David",314000,Pitcher +Cleveland Indians,"Rodriguez, Ricardo",302400,Pitcher +Cleveland Indians,"Sabathia, CC",1100000,Pitcher +Cleveland Indians,"Sadler, Carl",303200,Pitcher +Cleveland Indians,"Santiago, Jose",600000,Pitcher +Cleveland Indians,"Selby, Bill",325000,Second Baseman +Cleveland Indians,"Spencer, Shane",600000,Outfielder +Cleveland Indians,"Traber, Billy",300000,Pitcher +Cleveland Indians,"Vizquel, Omar",5500000,Shortstop +Cleveland Indians,"Westbrook, Jake",305500,Pitcher +Cleveland Indians,"Wickman, Bob",6000000,Pitcher +Cleveland Indians,"Wohlers, Mark",2600000,Pitcher +Toronto Blue Jays ,"Berg, Dave",700000,Shortstop +Toronto Blue Jays ,"Bordick, Mike",1000000,Shortstop +Toronto Blue Jays ,"Catalanotto, Frank",2200000,Outfielder +Toronto Blue Jays ,"Creek, Doug",700000,Pitcher +Toronto Blue Jays ,"Delgado, Carlos",18700000,First Baseman +Toronto Blue Jays ,"Escobar, Kelvim",3900000,Pitcher +Toronto Blue Jays ,"File, Bob",310000,Pitcher +Toronto Blue Jays ,"Halladay, Roy",3825000,Pitcher +Toronto Blue Jays ,"Hendrickson, Mark",302000,Pitcher +Toronto Blue Jays ,"Hinske, Eric",600000,Third Baseman +Toronto Blue Jays ,"Huckaby, Ken",313000,Catcher +Toronto Blue Jays ,"Hudson, Orlando",313000,Second Baseman +Toronto Blue Jays ,"Lidle, Cory",5350000,Pitcher +Toronto Blue Jays ,"Linton, Doug",350000,Pitcher +Toronto Blue Jays ,"Lopez, Aquilino",300000,Pitcher +Toronto Blue Jays ,"Miller, Trever",305000,Pitcher +Toronto Blue Jays ,"Myers, Greg",800000,Catcher +Toronto Blue Jays ,"Phelps, Josh",320000,First Baseman +Toronto Blue Jays ,"Politte, Cliff",845000,Pitcher +Toronto Blue Jays ,"Stewart, Shannon",6200000,Outfielder +Toronto Blue Jays ,"Sturtze, Tanyon",1000000,Pitcher +Toronto Blue Jays ,"Tam, Jeff",600000,Pitcher +Toronto Blue Jays ,"Walker, Pete",425000,Pitcher +Toronto Blue Jays ,"Wells, Vernon",520000,Outfielder +Toronto Blue Jays ,"Werth, Jayson",300000,Catcher +Toronto Blue Jays ,"Wilson, Tom",316000,Catcher +Toronto Blue Jays ,"Woodward, Chris",775000,Shortstop +Baltimore Orioles,"Batista, Tony",6400000,Third Baseman +Baltimore Orioles,"Bauer, Rick",325000,Pitcher +Baltimore Orioles,"Bedard, Erik",300000,Pitcher +Baltimore Orioles,"Belle, Albert",13000000,Outfielder +Baltimore Orioles,"Conine, Jeff",4250000,Outfielder +Baltimore Orioles,"Cordova, Marty",3100000,Outfielder +Baltimore Orioles,"Cruz, Deivi",1000000,Shortstop +Baltimore Orioles,"Daal, Omar",3000000,Pitcher +Baltimore Orioles,"Erickson, Scott",7030000,Pitcher +Baltimore Orioles,"Fordyce, Brook",3500000,Catcher +Baltimore Orioles,"Gibbons, Jay",375000,First Baseman +Baltimore Orioles,"Gil, Geronimo",330000,Catcher +Baltimore Orioles,"Groom, Buddy",3000000,Pitcher +Baltimore Orioles,"Hairston, Jerry",1550000,Shortstop +Baltimore Orioles,"Helling, Rick",1000000,Pitcher +Baltimore Orioles,"Hentgen, Pat",1200000,Pitcher +Baltimore Orioles,"Johson, Jason",2900000,Pitcher +Baltimore Orioles,"Julio, Jorge",350000,Pitcher +Baltimore Orioles,"Leon, Jose",305000,Third Baseman +Baltimore Orioles,"Ligtenberg, Kerry",1200000,Pitcher +Baltimore Orioles,"Lopez, Rodrigo",325000,Pitcher +Baltimore Orioles,"Matthews, Gary",900000,Outfielder +Baltimore Orioles,"Mora, Melvin",1725000,Outfielder +Baltimore Orioles,"Morban, Jose",300000,Shortstop +Baltimore Orioles,"Ponson, Sidney",4250000,Pitcher +Baltimore Orioles,"Roberts, Willis",3500000,Pitcher +Baltimore Orioles,"Ryan, BJ",762500,Pitcher +Baltimore Orioles,"Segui, David",7000000,First Baseman +Baltimore Orioles,"Surhoff, BJ",1000000,First Baseman +Tampa Bay Devil Rays,"Abernathy, Brent",300000,Second Baseman +Tampa Bay Devil Rays,"Anderson, Marlon",600000,Second Baseman +Tampa Bay Devil Rays,"Baldelli, Rocco",300000,Outfielder +Tampa Bay Devil Rays,"Bierbrodt, Nick",300000,Pitcher +Tampa Bay Devil Rays,"Carter, Lance",300000,Pitcher +Tampa Bay Devil Rays,"Colome, Jesus",300000,Pitcher +Tampa Bay Devil Rays,"Crawford, Carl",300000,Outfielder +Tampa Bay Devil Rays,"Grieve, Ben",5500000,Outfielder +Tampa Bay Devil Rays,"Hall, Toby",300000,Catcher +Tampa Bay Devil Rays,"Harper, Travis",325000,Pitcher +Tampa Bay Devil Rays,"Huff, Aubrey",325000,Third Baseman +Tampa Bay Devil Rays,"Kennedy, Joe",300000,Pitcher +Tampa Bay Devil Rays,"Lee, Travis",500000,First Baseman +Tampa Bay Devil Rays,"Martin, Al",300000,Outfielder +Tampa Bay Devil Rays,"McClung, Seth",300000,Pitcher +Tampa Bay Devil Rays,"Ordonez, Rey",6500000,Shortstop +Tampa Bay Devil Rays,"Parque, Jim",400000,Pitcher +Tampa Bay Devil Rays,"Parris, Steve",400000,Pitcher +Tampa Bay Devil Rays,"Rolls, Damian",300000,Third Baseman +Tampa Bay Devil Rays,"Seay, Bobby",300000,Pitcher +Tampa Bay Devil Rays,"Shumpert, Terry",300000,Second Baseman +Tampa Bay Devil Rays,"Sosa, Jorge",300000,Pitcher +Tampa Bay Devil Rays,"Valentin, Javier",300000,Catcher +Tampa Bay Devil Rays,"Venafro, Mike",300000,Pitcher +Tampa Bay Devil Rays,"Zambrano, Victor",300000,Pitcher +Kansas City Royals,"Affeldt, Jeremy",313000,Pitcher +Kansas City Royals,"Asencio, Miguel",314000,Pitcher +Kansas City Royals,"Beltran, Carlos",6000000,Outfielder +Kansas City Royals,"Berger, Grandon",304000,Outfielder +Kansas City Royals,"Berroa, Angel",302000,Shortstop +Kansas City Royals,"Brown, Dermal",309500,Outfielder +Kansas City Royals,"Bukvich, Ryan",304500,Pitcher +Kansas City Royals,"Carrasco, DJ",300000,Pitcher +Kansas City Royals,"Difelice, Mike",625000,Catcher +Kansas City Royals,"Febles, Carlos",775000,Second Baseman +Kansas City Royals,"George, Chris",303500,Pitcher +Kansas City Royals,"Grimsley, Jason",2000000,Pitcher +Kansas City Royals,"Harvey, Ken",300000,First Baseman +Kansas City Royals,"Hernandez, Runelvys",305500,Pitcher +Kansas City Royals,"Ibanez, Raul",3000000,Outfielder +Kansas City Royals,"Johnson, Rontrez",300000,Outfielder +Kansas City Royals,"Lopez, Albie",1500000,Pitcher +Kansas City Royals,"Lopez, Mendy",300000,Shortstop +Kansas City Royals,"MacDougal, Mike",301000,Pitcher +Kansas City Royals,"May, Darrell",450000,Pitcher +Kansas City Royals,"Mayne, Brent",2750000,Catcher +Kansas City Royals,"Randa, Joe",4500000,Third Baseman +Kansas City Royals,"Relaford, Desi",900000,Shortstop +Kansas City Royals,"Sweeney, Mike",11000000,First Baseman +Kansas City Royals,"Tucker, Michael",2750000,Outfielder +Kansas City Royals,"Wilson, Kris",311000,Pitcher +Minnesota Twins,"Cuddyer, Michael",302500,Outfielder +Minnesota Twins,"Fetters, Mike",500000,Pitcher +Minnesota Twins,"Fiore, Tony",330000,Pitcher +Minnesota Twins,"Gomez, Chris",500000,Shortstop +Minnesota Twins,"Guardado, Eddie",2700000,Pitcher +Minnesota Twins,"Guzman, Cristian",2525000,Shortstop +Minnesota Twins,"Hawkins, Latroy",3000000,Pitcher +Minnesota Twins,"Hocking, Denny",1000000,Second Baseman +Minnesota Twins,"Hunter, Torii",4750000,Outfielder +Minnesota Twins,"Jones, Jacque",2750000,Outfielder +Minnesota Twins,"Kielty, Bobby",325000,Outfielder +Minnesota Twins,"Koskie, Corey",3400000,Third Baseman +Minnesota Twins,"Lecroy, Matt",312500,Catcher +Minnesota Twins,"Lohse, Kyle",330000,Pitcher +Minnesota Twins,"Mays, Joe",4150000,Pitcher +Minnesota Twins,"Mientkiewicz, Doug",1750000,First Baseman +Minnesota Twins,"Milton, Eric",6000000,Pitcher +Minnesota Twins,"Mohr, Dustan",315000,Outfielder +Minnesota Twins,"Pierzynski, AJ",365000,Catcher +Minnesota Twins,"Prince, Tom",450000,Catcher +Minnesota Twins,"Radke, Brad",8750000,Pitcher +Minnesota Twins,"Reed, Rick",8000000,Pitcher +Minnesota Twins,"Rivas, Luis",340000,Second Baseman +Minnesota Twins,"Rogers, Kenny",2000000,Pitcher +Minnesota Twins,"Romero, JC",325000,Pitcher +Minnesota Twins,"Santana, Johan",335000,Pitcher +Chicago White Sox,"Alomar Jr, Sandy",700000,Catcher +Chicago White Sox,"Buehrle, Mark",445000,Pitcher +Chicago White Sox,"Colon, Bartolo",8250000,Pitcher +Chicago White Sox,"Crede, Joe",315000,Third Baseman +Chicago White Sox,"Daubach, Brian",450000,First Baseman +Chicago White Sox,"Garland, Jon",375000,Pitcher +Chicago White Sox,"Glover, Gary",330000,Pitcher +Chicago White Sox,"Gordon, Tom",1400000,Pitcher +Chicago White Sox,"Graffanino, Tony",675000,Second Baseman +Chicago White Sox,"Jimenez, D'angelo",345000,Shortstop +Chicago White Sox,"Koch, Billy",4250000,Pitcher +Chicago White Sox,"Konerko, Paul",6250000,First Baseman +Chicago White Sox,"Lee, Carlos",4200000,Outfielder +Chicago White Sox,"Loaiza, Esteban",500000,Pitcher +Chicago White Sox,"Marte, Damaso",330000,Pitcher +Chicago White Sox,"Olivo, Miguel",300000,Catcher +Chicago White Sox,"Ordonez, Magglio",9000000,Outfielder +Chicago White Sox,"Paul, Josh",325000,Catcher +Chicago White Sox,"Rios, Armando",450000,Outfielder +Chicago White Sox,"Rowand, Aaron",320000,Outfielder +Chicago White Sox,"Stewart, Josh",300000,Pitcher +Chicago White Sox,"Thomas, Frank",5000000,First Baseman +Chicago White Sox,"Valentin, Jose",5000000,Outfielder +Chicago White Sox,"White, Ribk",600000,Pitcher +Chicago White Sox,"Wright, Danny",325000,Pitcher +Chicago White Sox,"Wunsch, Kelly",575000,Pitcher +Detroit Tigers,"Anderson, Matt",3200000,Pitcher +Detroit Tigers,"Bernero, Adam",314000,Pitcher +Detroit Tigers,"Bocachica, Hiram",325000,Outfielder +Detroit Tigers,"Bonderman, Jeremy",300000,Pitcher +Detroit Tigers,"Cornejo, Nate",303000,Pitcher +Detroit Tigers,"German, Franklyn",300000,Pitcher +Detroit Tigers,"Halter, Shane",2150000,Shortstop +Detroit Tigers,"Higginson, Bobby",11850000,Outfielder +Detroit Tigers,"Infante, Omar",300000,Shortstop +Detroit Tigers,"Inge, Brandon",315000,Catcher +Detroit Tigers,"Kingsale, Eugene",340000,Outfielder +Detroit Tigers,"Knotts, Gary",305000,Pitcher +Detroit Tigers,"Ledezma, Wilfredo",300000,Pitcher +Detroit Tigers,"Maroth, Mike",309000,Pitcher +Detroit Tigers,"Munson, Eric",1700000,Third Baseman +Detroit Tigers,"Palmer, Dean",8500000,Third Baseman +Detroit Tigers,"Paquette, Craig",2625000,Third Baseman +Detroit Tigers,"Patterson, Danny",2500000,Pitcher +Detroit Tigers,"Pena, Carlos",310000,First Baseman +Detroit Tigers,"Roney, Matt",300000,Pitcher +Detroit Tigers,"Santiago, Ramon",307000,Second Baseman +Detroit Tigers,"Sparks, Steve",4500000,Pitcher +Detroit Tigers,"Spurling, Chris",305000,Pitcher +Detroit Tigers,"Walbeck, Matt",400000,Catcher +Detroit Tigers,"Walker, Jamie",360000,Pitcher +Detroit Tigers,"Young, Dmitri",6750000,Outfielder +Seattle Mariners,"Bloomquist, Willie",300000,Second Baseman +Seattle Mariners,"Boone, Bret",8000000,Second Baseman +Seattle Mariners,"Borders, Pat",500000,Catcher +Seattle Mariners,"Cameron, Mike",7416667,Outfielder +Seattle Mariners,"Carrara, Giovanni",400000,Pitcher +Seattle Mariners,"Cirillo, Jeff",6725000,Third Baseman +Seattle Mariners,"Colbrunn, Greg",750000,First Baseman +Seattle Mariners,"Davis, Ben",1000000,Catcher +Seattle Mariners,"Franklin, Ryan",425000,Pitcher +Seattle Mariners,"Garcia, Freddy",6875000,Pitcher +Seattle Mariners,"Guillen, Carlos",2500000,Shortstop +Seattle Mariners,"Hasegawa, Shigetoshi",1800000,Pitcher +Seattle Mariners,"Mabry, John",600000,First Baseman +Seattle Mariners,"Martinez, Edgar",4000000,First Baseman +Seattle Mariners,"Mateo, Julio",302500,Pitcher +Seattle Mariners,"McLemore, Mark",3150000,Second Baseman +Seattle Mariners,"Meche, Gil",325000,Pitcher +Seattle Mariners,"Moyer, Jamie",6500000,Pitcher +Seattle Mariners,"Nelson, Jeff",3983333,Pitcher +Seattle Mariners,"Olerud, John",7700000,First Baseman +Seattle Mariners,"Pineiro, Joel",440000,Pitcher +Seattle Mariners,"Rhodes, Arthur",3500000,Pitcher +Seattle Mariners,"Sasaki, Kazuhiro",8000000,Pitcher +Seattle Mariners,"Snelling, Chris",300000,Outfielder +Seattle Mariners,"Suzuki, Ichiro",4666667,Outfielder +Seattle Mariners,"Wilson, Dan",3500000,Catcher +Seattle Mariners,"Winn, Randy",3300000,Outfielder +Oakland Athletics,"Bowie, Micah",304000,Pitcher +Oakland Athletics,"Bradford, Chad",331000,Pitcher +Oakland Athletics,"Byrnes, Eric",300000,Outfielder +Oakland Athletics,"Chavez, Eric",3675000,Third Baseman +Oakland Athletics,"Durazo, Erubiel",1065000,First Baseman +Oakland Athletics,"Dye, Jermaine",11666667,Outfielder +Oakland Athletics,"Ellis, Mark",307500,Shortstop +Oakland Athletics,"Fikac, Jeremy",316000,Pitcher +Oakland Athletics,"Foulke, Keith",6000000,Pitcher +Oakland Athletics,"Gant, Ron",350000,Outfielder +Oakland Athletics,"Halama, John",750000,Pitcher +Oakland Athletics,"Hatteberg, Scott",1750000,Catcher +Oakland Athletics,"Hernandez, Ramon",1887500,Catcher +Oakland Athletics,"Hudson, Tim",2700000,Pitcher +Oakland Athletics,"Johnson, Mark L",500000,Catcher +Oakland Athletics,"Lilly, Ted",335000,Pitcher +Oakland Athletics,"Long, Terrence",2175000,Outfielder +Oakland Athletics,"Mecir, Jim",3216667,Pitcher +Oakland Athletics,"Menechino, Frank",334500,Second Baseman +Oakland Athletics,"Mulder, Mark",2650000,Pitcher +Oakland Athletics,"Neu, Mike",300000,Pitcher +Oakland Athletics,"Piatt, Adam",322000,Third Baseman +Oakland Athletics,"Rincon, Ricardo",1700000,Pitcher +Oakland Athletics,"Singleton, Chris",1200000,Outfielder +Oakland Athletics,"Tejada, Miguel",5125000,Shortstop +Oakland Athletics,"Zito, Barry",1000000,Pitcher +Texas Rangers,"Blalock, Hank",302500,Third Baseman +Texas Rangers,"Clark, Jermaine",300000,Second Baseman +Texas Rangers,"Cordero, Francisco",900000,Pitcher +Texas Rangers,"Diaz, Einar",1837500,Catcher +Texas Rangers,"Everett, Carl",9150000,Outfielder +Texas Rangers,"Fultz, Aaron",600000,Pitcher +Texas Rangers,"Garcia, Reynaldo",300000,Pitcher +Texas Rangers,"Glanville, Doug",1000000,Outfielder +Texas Rangers,"Gonzalez, Juan",13000000,Outfielder +Texas Rangers,"Greene, Todd",750000,Catcher +Texas Rangers,"Greer, Rusty",7000000,Outfielder +Texas Rangers,"Kreuter, Chad",750000,Catcher +Texas Rangers,"Lamb, Mike",440000,Third Baseman +Texas Rangers,"Lewis, Colby",302500,Pitcher +Texas Rangers,"Mench, Kevin",327500,Outfielder +Texas Rangers,"Nitkowski, CJ",550000,Pitcher +Texas Rangers,"Palmeiro, Rafael",9000000,First Baseman +Texas Rangers,"Park, Chan Ho",13000000,Pitcher +Texas Rangers,"Perry, Herbert",1300000,Third Baseman +Texas Rangers,"Powell, Jay",3250000,Pitcher +Texas Rangers,"Rodriguez, Alex",22000000,Shortstop +Texas Rangers,"Sierra, Ruben",600000,Outfielder +Texas Rangers,"Teixeira, Mark",750000,Third Baseman +Texas Rangers,"Thomson, John",1300000,Pitcher +Texas Rangers,"Urbina, Ugueth",4500000,Pitcher +Texas Rangers,"Valdes, Ismael",2500000,Pitcher +Texas Rangers,"Van Poppel, Todd",2500000,Pitcher +Texas Rangers,"Yan, Esteban",1500000,Pitcher +Texas Rangers,"Young, Mike B",415000,Shortstop +Texas Rangers,"Zimmerman, Jeff",3366667,Pitcher +,,, +Source: USA Today,,, diff --git a/docs/Basic Python.ipynb b/docs/Basic Python.ipynb index 8fb5616..fc76429 100644 --- a/docs/Basic Python.ipynb +++ b/docs/Basic Python.ipynb @@ -31,7 +31,26 @@ "10. [Dictionaries](#Dictionaries)\n", "11. [Sets](#Sets)\n", "12. [Comprehensions](#Comprehensions)\n", - "13. [Problem Analysis](#Problem Analysis)\n", + "13. [Problem Analysis](#Problem Analysis)

Chapter 1

\n", + "

About Python

\n", + "\n", + "Python was created in the late 1980's by Guido Van Rossum when he worked as a researcher at Centrum Wiskunde & Informatica. In 2000. Python 2.0 was released. The 2.0 release has gone through a number of major versions, ending in version 2.7. In 2008, Python 3.0 was released. As of this writing, the latest stable version, 3.6, was released in December 2016. \n", + "\n", + "Python is a programming language designed to be run on multiple operating systems, including Linux, Unix, MacOS X and Microsoft Windows. The language supports a number of programming paradigms, including *Functional*, *Imperative* and *Object Oriented* styles.\n", + "\n", + "The core philosophy of Python is summarized in a paper entitled [*The zen of python*](#https://www.python.org/dev/peps/pep-0020/)\n", + "\n", + "In brief, \n", + "- Beautiful is better than ugly. \n", + "- Explicit is better than implicit.\n", + "- Simple is better than complex\n", + "- Complex is better than complicated.\n", + "- There should be one—and preferably only one—obvious way to do it.\n", + "\n", + "[https://docs.python.org](#http://docs.python.org) is the URL for the repository of all the offical Python documentation, including a tutorial, API documentation, and much more. \n", + "\n", + "This course will focus on the Python 3.x implementation of the language. Although Python 2 is still in wide release, it is no longer being updated with the new features of Python 3 and there is a wide push among the Python developers to have existing Python code ported from version 2 to version 3 as well as suggesting that all new Python code be developed with the latest stable release of Python 3. \n", + "\n", "14. [Introduction to Object Oriented Programming](#Introduction to Object Oriented Programming)\n", "15. [Classes](#Classes)\n", " 1. [Inheritance](#Inheritance)\n", @@ -66,6 +85,33 @@ " \n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n" + ] + } + ], + "source": [ + "print('Hello')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a text note. I love python a lot!\n", + "

\n", + "This is an html paragraph Yay!\n", + "

" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -135,6 +181,30 @@ "3. Print the string \"Hello Python\" using the print() function in Python." ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "kjhkkhkhkhkhjk" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -144,7 +214,7 @@ "\n", "While this sounds like unnecessary complexity, in fact, there are many advantages to this type of system. Before the concept of a virtual machine, programmers needed to keep track of all computer memory allocated and de-allocated in their program. Often, mistakes in the program would lead to crashes and indeterministic behavior on part of the program due to a flaw in the program's allocation of memory. With a virtual machine, the VM itself takes care of the allocation and de-allocation of memory so the programmer no longer needs to worry about it. This leads to far more robust programs since an entire class of potential error has been removed. Following is a graphic illustration of how the Python Virtual Machine works. \n", "\n", - " \n", + " \n", "\n", "\n" ] @@ -242,6 +312,26 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is 1\n" + ] + } + ], + "source": [ + "x = 1\n", + "if (x == 2):\n", + " print ('x is 2')\n", + "print ('x is 1')" + ] + }, { "cell_type": "markdown", "metadata": { @@ -337,9 +427,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -389,22 +477,20 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "1 1 1\n", + "2 2 2\n", "1 2 Foo\n" ] } ], "source": [ - "a=b=c=1\n", + "a=b=c=2\n", "print (a,b,c)\n", "a,b,c = 1,2,'Foo'\n", "print (a,b,c)" @@ -456,11 +542,12 @@ "\n", " \n", " \n", - " \n", + " <\n", + " td> a - b\n", " \n", " \n", " \n", - " \n", + " \n", " \n", "
Operator Operation Example
+ Add two numbers a + b
- Subtract two numbers a - b
- Subtract two numbers
\\* Multiply two numbers a * b
/ Floating point division a / b
// Integer (Floor) divisiona // b
\\*\\* Exponentiation a ** 2
\\*\\*\\* Exponentiation a ** 2
% Modulo (Remainder) a % b
\n", "














\n", @@ -478,6 +565,25 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n" + ] + } + ], + "source": [ + "a = 2\n", + "b = 2 ** 3\n", + "print (b)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -498,6 +604,47 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n" + ] + } + ], + "source": [ + "a = 1\n", + "a = a + 1 \n", + "print (a)\n", + "a += 1\n", + "print (a)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "a=4\n", + "b=2\n", + "print (a//b)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -518,16 +665,13 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "a is 5\n", "b is 10\n", "They are both True\n", "One or both are True\n", @@ -542,8 +686,8 @@ "b = 10\n", "\n", "# Now, let's do some boolean tests\n", - "if a == 5:\n", - " print ('a is 5')\n", + "if a == 65:\n", + " print ('a is 65')\n", "if b == 10:\n", " print ('b is 10')\n", "\n", @@ -583,19 +727,153 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140102643103032\n" + ] + } + ], + "source": [ + "s1 = 'Hello'\n", + "s2 = 'Hello'\n", + "\n", + "print (id(s1))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is not 5\n" + ] + } + ], + "source": [ + "a = 6\n", + "if a == 5:\n", + " print (a)\n", + "elif a != 5:\n", + " print ('a is not 5')\n", + "else:\n", + " print ('else')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Red\n", + "Green\n", + "Blue\n" + ] + } + ], + "source": [ + "colors = ['Red','Green','Blue']\n", + "for i in range(len(colors)):\n", + " print (colors[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "id of a is 10914656\n", - "id of b is 10914688\n", - "id of c is 10914496\n", - "id of d is 10914496\n", + "Red\n", + "Blue\n" + ] + } + ], + "source": [ + "colors = ['Red','Green','Blue']\n", + "\n", + "\n", + "for color in colors:\n", + " \n", + " if color == 'Green':\n", + " continue\n", + " print (color)\n", + "else:\n", + " print ('I did not find magenta')\n", + " \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ip = \"192.168.100.1\"\n", + "\n", + "for octet in ip.split('.'):\n", + " if octet > 255:\n", + " break\n", + "else:\n", + " print (\"The octet is valid\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n" + ] + } + ], + "source": [ + "x = 10\n", + "while x > 1:\n", + " print (x)\n", + " x -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "id of a is 10055840\n", + "id of b is 10055872\n", + "id of c is 10055680\n", + "id of d is 10055680\n", "a is not the same as b\n", "c is the same as d\n", "These two strings do not have the same identity\n", @@ -646,12 +924,240 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 16, "metadata": {}, - "source": [ - "

Operator Precedence

\n", - "\n", - "Operators in Python have a precedence order, that is, given a statement with multiple operators, Python will decide which operator to do first based on this order. For example given the statement \n", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value: b\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Please enter a value: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "a = input('Please enter a value: ')\n", + "print (int(a) + 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "113.0973372\n", + "113.0973372\n", + "18.8495562\n", + " This function calculates the volume of a sphere \n" + ] + } + ], + "source": [ + "pi=3.1415927\n", + "r = 3\n", + "\n", + "def Volume(r):\n", + " ''' This function calculates the volume of a sphere '''\n", + " return 4/3* pi * r**3 \n", + "\n", + "def Area(r):\n", + " ''' This function calculates the area of a sphere '''\n", + " return 4 * pi * r **2\n", + "\n", + "def Circumference(r):\n", + " ''' This function calcuates the circumference of a sphere '''\n", + " return 2 * pi * r\n", + " \n", + " \n", + "# volume = 4/3* pi * r**3\n", + "# area = 4 * pi *r **2\n", + "# circumference = 2 * pi * r\n", + "print (Volume(r))\n", + "print (Area(r))\n", + "print (Circumference(r))\n", + "print (Volume.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 4\n", + "1. Calculate volume\n", + "2. Calculate area\n", + "3. Calculate circumference\n", + "4. Exit\n", + "Please enter your choice: 1\n", + "268.08257706666666\n", + "\n", + "\n", + "1. Calculate volume\n", + "2. Calculate area\n", + "3. Calculate circumference\n", + "4. Exit\n" + ] + } + ], + "source": [ + "import sys\n", + "\n", + "pi = 3.1415927\n", + "radius = int(input('Please enter a number: '))\n", + "\n", + "def Volume(r):\n", + " ''' This function calculates the volume of a sphere '''\n", + " return 4/3* pi * r**3 \n", + "\n", + "def Area(r):\n", + " ''' This function calculates the area of a sphere '''\n", + " return 4 * pi * r **2\n", + "\n", + "def Circumference(r):\n", + " ''' This function calcuates the circumference of a sphere '''\n", + " return 2 * pi * r\n", + "\n", + "funclist = [Volume,Area,Circumference]\n", + "\n", + "while (True):\n", + " print ('1. Calculate volume')\n", + " print ('2. Calculate area')\n", + " print ('3. Calculate circumference')\n", + " print ('4. Exit')\n", + " \n", + " choice = input('Please enter your choice: ')\n", + " \n", + " if choice == 4:\n", + " sys.exit()\n", + " else:\n", + " print(funclist[int(choice)-1](radius))\n", + " print ('\\n')\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r = range(0,10)\n", + "list(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 10\n", + "10\n" + ] + }, + { + "ename": "TypeError", + "evalue": "must be str, not int", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please enter a number: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mv\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: must be str, not int" + ] + } + ], + "source": [ + "v = input(\"Please enter a number: \")\n", + "print (v)\n", + "v += 1\n", + "print (v)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import math \n", + "pi = 3.1415927\n", + "radius = input('Please enter the radius')\n", + "radius = int(radius)\n", + "volume = 4 /3 * pi * radius ** 3\n", + "print ('Volume = ',volume)\n", + "circumference = 2 * pi * radius\n", + "print ('Circumference = ',circumference)\n", + "area = 4 * pi * radius ** 2\n", + "print ('Area = ', area)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The previous cell is an example of calculating stuff.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Operator Precedence

\n", + "\n", + "Operators in Python have a precedence order, that is, given a statement with multiple operators, Python will decide which operator to do first based on this order. For example given the statement \n", "\n", "\n", "
\n", @@ -737,12 +1243,287 @@ "
" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dlroW olleH'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"Hello World\"\n", + "# slice\n", + "s[::-1] " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Wor\n" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "print (s[-6:-2])" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dlroW olleH\n" + ] + } + ], + "source": [ + "datastring = 'Hello World'\n", + "print (datastring[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun', 'Brelin', '1234 Main Street', ' 21', ' Male']\n" + ] + } + ], + "source": [ + "personnel_record = 'Braun,Brelin,1234 Main Street, 21, Male\\n'\n", + "pers_array = personnel_record.strip().split(',')\n", + "print (pers_array)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Goodbye World\n" + ] + } + ], + "source": [ + "datastring = 'Hello World'\n", + "datastring = 'Goodbye World'\n", + "print (datastring)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "datastring ='Hello World'\n", + "dir (datastring)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], + "source": [ + "datastring ='Hello World'\n", + "print (datastring.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['12345', 'Braun', 'Brelin', '1234 Main Street', 'Anytown', ' USA']\n" + ] + } + ], + "source": [ + "person_rec = '12345,Braun,Brelin,1234 Main Street,Anytown, USA'\n", + "person_list = person_rec.split(',')\n", + "print (person_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Goodbye World\n", + "Good\n", + "dlroW eybdooG\n" + ] + } + ], + "source": [ + "s1 = 'Hello World'\n", + "print (s1)\n", + "s1 = 'Goodbye World'\n", + "print (s1)\n", + "print (s1[0:4])\n", + "print (s1[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello', 'World']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = open('some_file')\n", + "for line in f:\n", + " r = line.strip().split(',')\n", + " \n" + ] + }, { "cell_type": "code", "execution_count": 20, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -804,9 +1585,7 @@ { "cell_type": "code", "execution_count": 21, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "ename": "TypeError", @@ -831,7 +1610,7 @@ "source": [ "

Python String Operators


\n", "Python has two string operators. The + and the *. \n", - "+ allows concatenation of two strings. * is the repitition operator. For example:\n", + "\\+ allows concatenation of two strings. \\** is the repitition operator. For example:\n", "\n", "\n", "\n", @@ -855,9 +1634,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -880,6 +1657,25 @@ "print (string1)" ] }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value 5\n", + "5\n" + ] + } + ], + "source": [ + "myvar = input('Please enter a value ')\n", + "print (myvar)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -937,9 +1733,7 @@ { "cell_type": "code", "execution_count": 28, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1005,9 +1799,7 @@ { "cell_type": "code", "execution_count": 29, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1026,11 +1818,95 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mystring = 'Hello'\n", "dir(mystring)" @@ -1046,6 +1922,35 @@ "3. Print out the number of characters in this string." ] }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "this is an ex-parrot\n", + "20\n" + ] + } + ], + "source": [ + "mystring = 'This is an ex-parrot'\n", + "print (mystring.lower())\n", + "print (len(mystring))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "mylist = [1,2,3,4.5,'foo','bar','baz']\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1111,12 +2016,162 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 'foo', 'bar', 'baz']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [1,2,3,'foo','bar','baz']\n", + "mylist" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = []\n", + "dir(mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6, 5, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "mylist.append(6)\n", + "mylist.reverse()\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [[1,2,3],[4,5,6]]\n", + "mylist\n", + "mylist[0][1]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Hello World\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "s[0] = 'J'\n", + "s" + ] + }, { "cell_type": "code", "execution_count": 39, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1157,6 +2212,34 @@ "print (mylist)" ] }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "mylist= [1,2,3,4,5]\n", + "mylist[0] = 10\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -1186,9 +2269,7 @@ { "cell_type": "code", "execution_count": 55, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1237,9 +2318,7 @@ { "cell_type": "code", "execution_count": 58, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1259,6 +2338,141 @@ "print (mylist + mylist2)" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on list object:\n", + "\n", + "class list(object)\n", + " | list() -> new empty list\n", + " | list(iterable) -> new list initialized from iterable's items\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __delitem__(self, key, /)\n", + " | Delete self[key].\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(...)\n", + " | x.__getitem__(y) <==> x[y]\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iadd__(self, value, /)\n", + " | Implement self+=value.\n", + " | \n", + " | __imul__(self, value, /)\n", + " | Implement self*=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self None -- append object to end\n", + " | \n", + " | clear(...)\n", + " | L.clear() -> None -- remove all items from L\n", + " | \n", + " | copy(...)\n", + " | L.copy() -> list -- a shallow copy of L\n", + " | \n", + " | count(...)\n", + " | L.count(value) -> integer -- return number of occurrences of value\n", + " | \n", + " | extend(...)\n", + " | L.extend(iterable) -> None -- extend list by appending elements from the iterable\n", + " | \n", + " | index(...)\n", + " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n", + " | Raises ValueError if the value is not present.\n", + " | \n", + " | insert(...)\n", + " | L.insert(index, object) -- insert object before index\n", + " | \n", + " | pop(...)\n", + " | L.pop([index]) -> item -- remove and return item at index (default last).\n", + " | Raises IndexError if list is empty or index is out of range.\n", + " | \n", + " | remove(...)\n", + " | L.remove(value) -> None -- remove first occurrence of value.\n", + " | Raises ValueError if the value is not present.\n", + " | \n", + " | reverse(...)\n", + " | L.reverse() -- reverse *IN PLACE*\n", + " | \n", + " | sort(...)\n", + " | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "mylist = []\n", + "help(mylist)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1289,9 +2503,7 @@ { "cell_type": "code", "execution_count": 60, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1315,6 +2527,58 @@ "print (outer[1][3])" ] }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "#for item in mylist:\n", + "# print (item)\n", + "\n", + "#mystring = 'Hello World'\n", + "#for char in mystring:\n", + "# print (char)\n", + " \n", + "for i in range(6):\n", + " print (i)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(2,6):\n", + " fiblist.append(fiblist[i-2] + fiblist[i-1])\n", + "print (fiblist)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1323,6 +2587,183 @@ "1. The fibonacci sequence is a well known mathematical sequence where an element is the sum of the previous two elements. Create a list that will contain the first five elements of this sequence.\n" ] }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "x = fiblist[0] + fiblist[1]\n", + "fiblist.append(x)\n", + "x = fiblist[1] + fiblist[2]\n", + "fiblist.append(x)\n", + "x = fiblist[2] + fiblist[3]\n", + "fiblist.append(x)\n", + "x = fiblist[3] + fiblist[4]\n", + "fiblist.append(x)\n", + "x = fiblist[4] + fiblist[5]\n", + "fiblist.append(x)\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(5):\n", + " x = fiblist[i] + fiblist[i+1]\n", + " fiblist.append(x)\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8]\n" + ] + } + ], + "source": [ + "fiblist = [0,1]\n", + "for i in range(5):\n", + " fiblist.append(fiblist[i] + fiblist[i+1])\n", + "print (fiblist)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Valid\n" + ] + } + ], + "source": [ + "ipaddr = '192.168.100.1'\n", + "octets = ipaddr.split('.')\n", + "for octet in octets:\n", + " if int(octet) > 255:\n", + " print ('Invalid')\n", + " break\n", + "else: \n", + " print ('Valid')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Hello World'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0ms1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s1 = 'Hello World'\n", + "s1[0] = 'J'\n", + "print (s1)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = [1,2,3,4,5]\n", + "list2 = ['a','b','c','d','e']\n", + "list3 = zip (list1,list2)\n", + "list(list3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Quick Exercise:\n", + "Create a sample list of potential ipaddresses, some of which have invalid octets. Write a small program that iterates over each ip address and prints out whether or not the ip address is valid." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good ip address!\n", + "Bad ipaddress!\n", + "Good ip address!\n" + ] + } + ], + "source": [ + "ipaddrs = ['192.168.100.1','255.262.1.2','10.1.2.3']\n", + "\n", + "for ipaddr in ipaddrs:\n", + " octets = ipaddr.split('.')\n", + " for octet in octets:\n", + " if int(octet) > 255:\n", + " print ('Bad ipaddress!')\n", + " break\n", + " else:\n", + " print ('Good ip address!') \n", + " \n", + " " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1361,10 +2802,8 @@ }, { "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1379,24 +2818,54 @@ } ], "source": [ - "# Let's define a tuple.\n", - "mytuple = (1,2,3,4,5)\n", - "# Let's print out the second through the fifth element.\n", - "print (mytuple[1:6])\n", - "# Let's define a second tuple and add it to the first. \n", - "mytuple1 = ('a','b','c','d','e')\n", - "mytuple = mytuple + mytuple1\n", - "print (mytuple)\n", - "# Let's pack a tuple t with the values of x, y and z. These become the elements of the new tuple t. \n", - "x = 1\n", - "y = 2\n", - "z = 3\n", - "t = (x,y,z)\n", - "print (t)\n", - "# Now let's unpack a tuple.\n", - "(name,age) = ('Braun Brelin',21)\n", - "print ('name = ', name)\n", - "print ('age = ',age)" + "# Let's define a tuple.\n", + "mytuple = (1,2,3,4,5)\n", + "# Let's print out the second through the fifth element.\n", + "print (mytuple[1:6])\n", + "# Let's define a second tuple and add it to the first. \n", + "mytuple1 = ('a','b','c','d','e')\n", + "mytuple = mytuple + mytuple1\n", + "print (mytuple)\n", + "# Let's pack a tuple t with the values of x, y and z. These become the elements of the new tuple t. \n", + "x = 1\n", + "y = 2\n", + "z = 3\n", + "t = (x,y,z)\n", + "print (t)\n", + "# Now let's unpack a tuple.\n", + "(name,age) = ('Braun Brelin',21)\n", + "print ('name = ', name)\n", + "print ('age = ',age)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "fo\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmytuple\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'fee'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fi'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fo'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'fum'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmytuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmytuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'foo'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "mytuple = ('fee','fi','fo','fum')\n", + "print(mytuple[2])\n", + "mytuple[2] = 'foo' " ] }, { @@ -1443,9 +2912,9 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 8, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { @@ -1473,6 +2942,44 @@ "print (t1)" ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('12345', 'Braun', 'Brelin', '1234 Main Street', ' Anytown', ' USA', ' 12345')\n" + ] + } + ], + "source": [ + "emp_record = '12345,Braun,Brelin,1234 Main Street, Anytown, USA, 12345'\n", + "\n", + "emp_tuple = tuple(emp_record.split(','))\n", + "print (emp_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "import math\n", + "print (math.sqrt(4))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1505,12 +3012,128 @@ "
\n" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "even_squares = [num **2 for num in nums if num % 2 == 0]\n", + "#for num in nums:\n", + "# squares.append(num **2)\n", + "\n", + "print (even_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "words = ['fee','fi','fo','fum']\n", + "upperwords = [word.upper() for word in words]\n", + "print (upperwords)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + }, + { + "data": { + "text/plain": [ + "['__doc__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " 'acos',\n", + " 'acosh',\n", + " 'asin',\n", + " 'asinh',\n", + " 'atan',\n", + " 'atan2',\n", + " 'atanh',\n", + " 'ceil',\n", + " 'copysign',\n", + " 'cos',\n", + " 'cosh',\n", + " 'degrees',\n", + " 'e',\n", + " 'erf',\n", + " 'erfc',\n", + " 'exp',\n", + " 'expm1',\n", + " 'fabs',\n", + " 'factorial',\n", + " 'floor',\n", + " 'fmod',\n", + " 'frexp',\n", + " 'fsum',\n", + " 'gamma',\n", + " 'hypot',\n", + " 'isfinite',\n", + " 'isinf',\n", + " 'isnan',\n", + " 'ldexp',\n", + " 'lgamma',\n", + " 'log',\n", + " 'log10',\n", + " 'log1p',\n", + " 'log2',\n", + " 'modf',\n", + " 'pi',\n", + " 'pow',\n", + " 'radians',\n", + " 'sin',\n", + " 'sinh',\n", + " 'sqrt',\n", + " 'tan',\n", + " 'tanh',\n", + " 'trunc']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import math\n", + "print (math.pi)\n", + "dir (math)" + ] + }, { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1535,6 +3158,93 @@ " print (element)" ] }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "squares" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num **2 for num in nums]\n", + "squares\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "for num in nums:\n", + " if num % 2== 0: \n", + " squares.append(num ** 2)\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [num ** 2 for num in nums if num %2 == 0]\n", + "print (squares)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1572,54 +3282,251 @@ "# the [] is *not* an index value, but the value of the key.
\n", "print (mydict1[3])
\n", "\n", - "# Any immutable type can be a key, for example, integers, strings or even tuples.
\n", - "# Let's see an example of using a tuple as a key.
\n", - "
\n", - "keytuple = ('12345','Braun Brelin','12345 Main Street')
\n", - "mydict[keytuple] = 'Record for Braun Brelin'
\n", - "
\n", - "print (mydict[keytuple])
\n", - "

\n", - "\n", - "\n", - "" + "# Any immutable type can be a key, for example, integers, strings or even tuples.
\n", + "# Let's see an example of using a tuple as a key.
\n", + "
\n", + "keytuple = ('12345','Braun Brelin','12345 Main Street')
\n", + "mydict[keytuple] = 'Record for Braun Brelin'
\n", + "
\n", + "print (mydict[keytuple])
\n", + "

\n", + "\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mydict = {}\n", + "dir(mydict)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Brad Pitt, 1235 Main Street, Anytown, USA, 11111\n" + ] + } + ], + "source": [ + "Person = {12345:'Braun Brelin,1234 Main Street,Anytown, USA, 11111',\n", + " 12346:'Brad Pitt, 1235 Main Street, Anytown, USA, 11111',\n", + " 12348:'Al Pacino,12347 Broadway, New York, USA, 111112'}\n", + "\n", + "\n", + "print (Person[12346])" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wednesday\n", + "Record for Braun Brelin\n" + ] + } + ], + "source": [ + "# Let's create our first, empty, dictionary. \n", + "mydict = {}\n", + "# Let's create another dictionary with some key value pairs. Note that the syntax is key:value\n", + "# Also note that keys *must* be unique. I.e. I can't have two entries with a key value of 1. \n", + "\n", + "mydict1 = {'one':'Monday','two':'Tuesday','three':'Wednesday','four':'Thursday','five':'Friday','six':'Saturday','seven':'Sunday'}\n", + "\n", + "# Let's print out the value associated with key 3 in the second dictionary. Note that the value here in \n", + "# the [] is *not* an index value, but the value of the key. \n", + "print (mydict1['three'])\n", + "\n", + "# Any immutable type can be a key, for example, integers, strings or even tuples.\n", + "# Let's see an example of using a tuple as a key.\n", + "\n", + "keytuple = ('12345','Braun Brelin','12345 Main Street')\n", + "mydict[keytuple] = 'Record for Braun Brelin'\n", + "\n", + "print (mydict[keytuple])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mydict = {}\n", + "dir(mydict)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B\n", + "G\n", + "R\n", + "Blue\n", + "Green\n", + "Red\n", + "('B', 'Blue')\n", + "('G', 'Green')\n", + "('R', 'Red')\n" + ] + } + ], + "source": [ + "mydict = {'R':'Red','G':'Green','B':'Blue'}\n", + "\n", + "for key in mydict.keys():\n", + " print (key)\n", + "\n", + "for value in mydict.values():\n", + " print(value)\n", + " \n", + "for item in mydict.items():\n", + " print (item)\n", + " \n", + "if 'magenta' in mydict:\n", + " print (mydict['magenta'])" ] }, { "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, + "execution_count": 29, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Wednesday\n", - "Record for Braun Brelin\n" + "No such key found!\n" ] } ], "source": [ - "# Let's create our first, empty, dictionary. \n", - "mydict = {}\n", - "# Let's create another dictionary with some key value pairs. Note that the syntax is key:value\n", - "# Also note that keys *must* be unique. I.e. I can't have two entries with a key value of 1. \n", - "\n", - "mydict1 = {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday',6:'Saturday',7:'Sunday'}\n", - "\n", - "# Let's print out the value associated with key 3 in the second dictionary. Note that the value here in \n", - "# the [] is *not* an index value, but the value of the key. \n", - "print (mydict1[3])\n", - "\n", - "# Any immutable type can be a key, for example, integers, strings or even tuples.\n", - "# Let's see an example of using a tuple as a key.\n", - "\n", - "keytuple = ('12345','Braun Brelin','12345 Main Street')\n", - "mydict[keytuple] = 'Record for Braun Brelin'\n", - "\n", - "print (mydict[keytuple])" + "colors = {'R':'Red','G':'Green','B':'Blue'}\n", + " \n", + "if 'M' in colors.keys():\n", + " print (colors['M'])\n", + "else:\n", + " print ('No such key found!')" ] }, { @@ -1713,9 +3620,7 @@ { "cell_type": "code", "execution_count": 81, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1756,6 +3661,27 @@ " print (\"Hello is in s but World is not in s\")" ] }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No key found\n" + ] + } + ], + "source": [ + "mydict = {2:'Red',1:'Green',3:'Blue'}\n", + "if 4 not in mydict:\n", + " print ('No key found')\n", + "else:\n", + " print (mydict[4])\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1812,9 +3738,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1882,6 +3806,24 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "x = range(10)\n", + "print (list(x))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1934,9 +3876,7 @@ { "cell_type": "code", "execution_count": 17, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1975,6 +3915,30 @@ " print ('element position: ',element[0],' element value: ',element[1])" ] }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 'Mercury', 2: 'Venus', 3: 'Earth', 4: 'Mars', 5: 'Jupiter', 6: 'Saturn', 7: 'Uranus', 8: 'Neptune'}\n" + ] + } + ], + "source": [ + "thekeys=[1,2,3,4,5,6,7,8,9]\n", + "thevalues = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']\n", + "theplanets = zip(thekeys,thevalues)\n", + "tmp = list(theplanets)\n", + "planetdict =dict(tmp)\n", + "print (planetdict)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2001,9 +3965,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2089,12 +4051,59 @@ "" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192', '168', '100', '1']\n" + ] + } + ], + "source": [ + "ipaddr = '192.168.100.1'\n", + "octets = ipaddr.split('.')\n", + "print (octets)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "myipaddr = '256.0.1.2'\n", + "\n", + "octets = myipaddr.split('.')\n", + "for octet in octets:\n", + " if octet > 255:\n", + " break\n", + "else:\n", + " print ('Ipaddr is good!')\n", + " \n", + "\n", + "\n" + ] + }, { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2227,6 +4236,25 @@ "1. The previous quick exercise asked for the fibonacci sequence. Re-do this exercise, however use a loop to calculate the first 10 elements of the fibonacci sequence." ] }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25]\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,4,5]\n", + "mysquares = [num ** 2 for num in mylist]\n", + "print (mysquares)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2262,10 +4290,32 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "2\n" + ] + } + ], + "source": [ + "x = 2\n", + "def square(x):\n", + " x = x ** 2\n", + " return (x)\n", + "\n", + "print (square(x))\n", + "print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2318,10 +4368,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2378,9 +4426,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2444,9 +4490,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2503,9 +4547,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2566,9 +4608,7 @@ { "cell_type": "code", "execution_count": 17, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2596,9 +4636,9 @@ "\n", "

\n", "\n", - "def myfunction(a,b,*args):
\n", + "def myfunction(a,b,\\*args):
\n", "    print ('a = ',a,' b = ',b)
\n", - "    for arg in args:
\n", + "    for arg in \\*args:
\n", "        print (arg)
\n", "    return
\n", "
\n", @@ -2633,9 +4673,7 @@ { "cell_type": "code", "execution_count": 22, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2659,9 +4697,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2684,6 +4720,100 @@ "myfunction(1,2,param1='a',param2='b',param3='c')" ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "foo!\n", + "bar!\n", + "baz!\n" + ] + } + ], + "source": [ + "def foo():\n", + " print (\"foo!\")\n", + "def bar():\n", + " print (\"bar!\")\n", + "def baz():\n", + " print (\"baz!\")\n", + " \n", + "funclist = [foo,bar,baz]\n", + "\n", + "for func in funclist:\n", + " func()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'function' object has no attribute 'a'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'foo'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'a'" + ] + } + ], + "source": [ + "def foo():\n", + " foo.a = 10\n", + " print ('foo')\n", + "\n", + "print (foo.a)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a divisor1\n", + "dividing 2 by 1 gives me: 2.0\n", + "The input was a number and it was not zero!\n", + "We got to the end!\n" + ] + } + ], + "source": [ + "try:\n", + " divisor = int(input ('Please enter a divisor'))\n", + " numerator = 2\n", + " print ('dividing 2 by ', divisor, ' gives me: ', numerator/divisor)\n", + "except ValueError:\n", + " print ('Sorry, you did not enter an integer')\n", + "except ZeroDivisionError:\n", + " print ('Sorry, you entered an invalid number')\n", + "else:\n", + " print ('The input was a number and it was not zero!')\n", + "finally:\n", + " print('We got to the end!')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -2743,9 +4873,7 @@ { "cell_type": "code", "execution_count": 26, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2771,9 +4899,7 @@ { "cell_type": "code", "execution_count": 27, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2895,7 +5021,6 @@ "cell_type": "code", "execution_count": 66, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -2921,9 +5046,7 @@ { "cell_type": "code", "execution_count": 35, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2945,9 +5068,7 @@ { "cell_type": "code", "execution_count": 65, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3038,9 +5159,73 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The int conversion failed\n" + ] + } + ], + "source": [ + "import sys\n", + "\n", + "a = '5nlah'\n", + "try:\n", + " b = int(a)\n", + "except:\n", + " print ('The int conversion failed')\n", + "\n", + "while (True):\n", + " key = input('Team name')\n", + " if key == 'x':\n", + " break\n", + " try: \n", + " print (team_dict[key])\n", + " except KeyError:\n", + " print ('Invalid team name, please re-enter')\n", + " continue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "colors - {'R':'Red', 'G':'Green', 'B':'Blue'}\n", + "if 'M' in colors:\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/home/bbrelin/src/repos/basicpython/docs\n" + ] + } + ], + "source": [ + "import os\n", + "print (os.getcwd())\n", + "open ('../labs/lab4/data/ALbb.salaries.2003.formatted.csc')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -3050,13 +5235,13 @@ "text": [ "Name of the file: ../data/presidents.dat\n", "Closed or not : False\n", - "Opening mode : r\n" + "Opening mode : a\n" ] } ], "source": [ "# Here we open a file with the default arguments for the mode and buffer.\n", - "myfileobject = open('../data/presidents.dat')\n", + "myfileobject = open('../data/presidents.dat','a')\n", "\n", "# Once we've obtained the file object, we can access a number of methods \n", "# and attributes.\n", @@ -3096,11 +5281,35 @@ "" ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "George Washington\n", + "John Adams\n", + "Thomas Jefferson\n", + "James Madison\n", + "James Monroe\n", + "\n" + ] + } + ], + "source": [ + "myfileobject = open('../data/presidents.dat')\n", + "datalist = myfileobject.read()\n", + "print (datalist)\n", + "myfileobject.close()\n" + ] + }, { "cell_type": "code", "execution_count": 53, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -3129,6 +5338,33 @@ " data = myfileobject.readline()" ] }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First name = George Last name = Washington\n", + "First name = John Last name = Adams\n", + "First name = Thomas Last name = Jefferson\n", + "First name = James Last name = Madison\n", + "First name = James Last name = Monroe\n" + ] + } + ], + "source": [ + "myfileobject = open('../data/presidents.dat')\n", + "\n", + "for line in myfileobject:\n", + " (fname,lname) = line.strip().split()\n", + " print ('First name = ',fname, 'Last name = ',lname)\n", + " \n", + "myfileobject.close()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -3159,9 +5395,7 @@ { "cell_type": "code", "execution_count": 55, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3210,9 +5444,7 @@ { "cell_type": "code", "execution_count": 58, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3270,16 +5502,68 @@ }, { "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/home/bbrelin/src/repos/basicpython/docs\n" + ] + } + ], + "source": [ + "import os\n", + "print (os.getcwd())" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "i = 2\n", + "def isPrime(n):\n", + " global i\n", + " if n == 0 or n == 1:\n", + " return(False)\n", + " elif (n % i == 0) and (n == i):\n", + " i = 2\n", + " return(True)\n", + " elif n % i == 0:\n", + " i = 2\n", + " return(False)\n", + " else:\n", + " i += 1\n", + " return(isPrime(n))\n", + "\n", + "print (isPrime(17))\n", + "print (isPrime(4))\n", + "print (isPrime(5))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Writing some output\n" + "Writing some new output\n" ] } ], @@ -3288,7 +5572,7 @@ "# Write mode will automatically delete any existing data in the file before\n", "# opening it. \n", "myfileobject = open('../data/output_example.dat','w')\n", - "myfileobject.write('Writing some output')\n", + "myfileobject.write('Writing some new output')\n", "myfileobject.close()\n", "\n", "# Now let's reopen for read only.\n", @@ -3350,12 +5634,39 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 5\n", + "5\n" + ] + } + ], + "source": [ + "while (True):\n", + " try:\n", + " a = int(input ('Please enter a number '))\n", + " except ValueError:\n", + " print (\"You entered an invalid number!\")\n", + " print ('Please re-enter!')\n", + " finally:\n", + " break\n", + " \n", + "b = 0\n", + "b = a + b\n", + "print (b)" + ] + }, { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3405,6 +5716,75 @@ " myfileobject.close()\n" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "from math import pi,cos,sin,tan,log\n", + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib as plt \n", + "\n", + "np.array\n", + "\n", + "print (pi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import sys\n", + "try:\n", + " f = open('baseballsalaries.csv')\n", + "except OSError:\n", + " print ('File not found!')\n", + " sys.exit(1)\n", + " \n", + "\n", + "salaries = {}\n", + "try:\n", + " for line in f:\n", + " record = line.strip().split(',')\n", + " if record[0] not in salaries:\n", + " try:\n", + " salaries[record[0]] = int(record[3])\n", + " except ValueError:\n", + " continue\n", + " else:\n", + " try:\n", + " salaries[record[0]] += int(record[3])\n", + " except ValueError:\n", + " continue\n", + "except IOError:\n", + " print ('IO Error! Program exiting!')\n", + " sys.exit(1)\n", + " \n", + "finally:\n", + " f.close()\n", + " \n", + " \n", + "team = input('Please enter a team name: ')\n", + "try:\n", + " print (salaries[team])\n", + "except KeyError:\n", + " print ('Invalid key!')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -3482,12 +5862,114 @@ "- The code will run significantly faster in the Python Virtual Machine, often 30-40 per cent faster. " ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "newnums = [ num ** 2 for num in nums if num % 2 == 0 ]\n", + "print (newnums)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[18.333333333333336, -6.666666666666667, 37.77777777777778, 0.0]\n" + ] + } + ], + "source": [ + "fahrenheit = [65,20,100,32]\n", + "celsius = [ 5/9 * (f - 32) for f in fahrenheit ]\n", + "print (celsius)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat'), (7, 'Sun')]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8]\n", + "daysofweek = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']\n", + "\n", + "print (list(zip(nums,daysofweek)))\n", + "#dowdict = {pair[0]:pair[1] for pair in zip(nums,daysofweek)}\n", + "#print (dowdict[4])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number is divisible by two\n" + ] + } + ], + "source": [ + "x = 2\n", + "\n", + "if x % 2 == 0:\n", + " print ('Number is divisible by two')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "mylist = ['Fee','Fi','Fo','Fum']\n", + "myupperlist = [ word.upper() for word in mylist if 'Fee' not in word ]\n", + "\n", + "print (myupperlist)\n", + "#for word in mylist:\n", + "# myupperlist.append(word.upper())\n", + " \n", + "\n" + ] + }, { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3515,9 +5997,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3541,9 +6021,7 @@ { "cell_type": "code", "execution_count": 12, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3592,9 +6070,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -3628,11 +6104,18 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the gemstone: Tanzanite\n", + "The price for that gemstone is: 1000.0\n" + ] + } + ], "source": [ "# Here are the two lists that we want to combine into a dictionary. \n", "gemstonenames = ['Tanzanite','Taaffeite','Black Opal', 'Benitoite', 'Red Beryl', 'Alexandrite', \\\n", @@ -3641,14 +6124,407 @@ "\n", "# Write a dictionary comprehension to combine the two lists. The names will be the keys, the price will be the values. \n", "# Refer to the Python documentation for the zip() built in function. \n", + "\n", + "gemstonedict ={}\n", + "#gemstonelist = zip(gemstonenames, gemstoneprice)\n", + "#for gems in gemstonelist:\n", + "# gemstonedict [gems[0]] = gems[1]\n", + "gemstonedict = {gems[0]:gems[1] for gems in zip(gemstonenames,gemstoneprice)}\n", + "gemkey = input ('Please enter the gemstone: ')\n", + "print ('The price for that gemstone is: ',gemstonedict[gemkey])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'d': 4, 'e': 5, 'b': 2, 'c': 3, 'a': 1}\n" + ] + } + ], + "source": [ + "list1 = ['a','b','c','d','e']\n", + "list2 = [1,2,3,4,5]\n", + "list3 = list(zip(list1,list2))\n", + "d = {}\n", + "d = {pair[0]:pair[1] for pair in list3}\n", + "print (d)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 27, 16, 125, 36, 343, 64, 729, 100]\n" + ] + } + ], + "source": [ + "mynums = [1,2,3,4,5,6,7,8,9,10]\n", + "mysquares = [num **2 if num %2 == 0 else num **3 for num in mynums]\n", + "print (mysquares)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "def square(x):\n", + " return x**2\n", + "\n", + "def get_even(x):\n", + " if x % 2 == 0:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "mynums = [1,2,3,4,5,6,7,8,9,10]\n", + "\n", + "mysquares = map(square,filter(get_even,mynums))\n", + "print (list(mysquares))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Looking for this in Does this text match the pattern\n", + "Found a match!\n", + "The match started at position: 5 and ended at position: 9\n", + "\n", + "Looking for that in Does this text match the pattern\n", + "No match found!\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "patterns = ['this','that']\n", + "text = 'Does this text match the pattern'\n", + "\n", + "for pattern in patterns:\n", + " print ('Looking for %s in %s' %(pattern,text))\n", + " searchResult = re.search(pattern,text)\n", + " if searchResult:\n", + " print ('Found a match!')\n", + " print ('The match started at position: %d and ended at position: %d\\n'% (searchResult.start(),searchResult.end()))\n", + " else:\n", + " print ('No match found!')" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account Name\tAccount Balance\n", + "-------------------------------\n", + "Braun Brelin\t10000.00\n", + "Joe Blow\t500.41\n", + "Jack Green\t251.24\n" + ] + } + ], + "source": [ + "name = 'Braun Brelin'\n", + "age = 21\n", + "\n", + "acctlist = [('Braun Brelin',10000.00052),('Joe Blow',500.4102),('Jack Green',251.2351)]\n", + "print ('Account Name\\tAccount Balance')\n", + "print ('-'*31)\n", + "for (acctname,acctbal) in acctlist:\n", + " print ('%s\\t%.2f' % (acctname,acctbal))\n", + " \n", + " \n", + "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "r = 2.236 t = 1.107\n", + "r = 7.810 t = 0.876\n", + "r = 3.162 t = -1.249\n", + "r = 5.657 t = 0.785\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def convert_to_polar(coordt):\n", + " x,y = coordt\n", + " radius = math.sqrt(x**2 + y**2)\n", + " theta = math.atan(y/x)\n", + " return (radius,theta)\n", + "\n", + "coordlist = [(1,2),(5,6),(-1,3),(4,4)]\n", + "polarlist = map(convert_to_polar,coordlist)\n", + "for coord in polarlist:\n", + " print ('r = %.3f t = %.3f' % (coord[0],coord[1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n" + ] + } + ], + "source": [ + "def myfunc(a,b=1):\n", + " print (a,b)\n", + " return\n", + "\n", + "myfunc(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n" + ] + } + ], + "source": [ + "class Foo:\n", + " def __init__(self,a,b):\n", + " self.first = a\n", + " self.second = b\n", + " \n", + " def returna(self):\n", + " return self.first\n", + " \n", + " def returnb(self):\n", + " return self.second\n", + " \n", + "f = Foo(1,2)\n", + "print (f.returna())\n", + "print (f.returnb())" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Speak!\n", + "3\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "class Animal:\n", + " \n", + " ''' This is my animal class. It has a speak method '''\n", + " \n", + " def __init__(self,n,a,b):\n", + " self.name = n\n", + " self.age = a\n", + " self.breed = b\n", + " \n", + " def getAge(self):\n", + " return self.age\n", + " \n", + " def setAge(self,newAge):\n", + " self.age = newAge\n", + " return\n", + " \n", + " def speak(self):\n", + " return ('Speak!')\n", + " \n", + "a = Animal('Rex',2,'German Shepherd')\n", + "a1 = Animal('Bowser',3,'Boxer')\n", + "a2 = Animal('Fido',4,'Poodle')\n", + "\n", + "print (a.speak())\n", + "a.setAge(3)\n", + "print (a.getAge())\n", + "\n", + "print (a1.getAge())\n", + "print (a2.getAge())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100.0\n", + "500.0\n" + ] + } + ], + "source": [ + "class BankAcct:\n", + " def __init__(self,balance):\n", + " self.balance = balance\n", + " \n", + " def getBalance(self):\n", + " return (self.balance)\n", + " \n", + " def Deposit(self,deposit):\n", + " self.balance += newbal\n", + " \n", + " def Withdrawal(self,withdrawal):\n", + " self.balance -= withdrawal\n", + " \n", + " def setBalance(self,newbal):\n", + " self.balance = newbal\n", + " \n", + "\n", + "braun_checking = BankAcct(100.00)\n", + "braun_saving = BankAcct(500.00)\n", + "braun_ccacct = BankAcct(250.00)\n", + "print (braun_checking.getBalance())\n", + "braun_checking.setBalance(500.00)\n", + "print (braun_checking.getBalance()) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "class Light:\n", + " \n", + " ''' This is the Light class.\n", + " There are two methods, switch and getState \n", + " and there is one variable, state \n", + " '''\n", + " def __init__(self,s=False):\n", + " self.state=s\n", + " \n", + " def switch(self):\n", + " if (self.state == False):\n", + " self.state = True\n", + " else:\n", + " self.state = False\n", + " \n", + " def getState(self):\n", + " return self.state\n", + "\n", + " \n", + "light1 = Light(True)\n", + "light2 = Light()\n", + "\n", + "light1.switch()\n", + "light2.switch()\n", + "print (light1.getState())\n", + "print (light2.getState())" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Match found!\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "ipfile = open('ipaddrs.txt')\n", + "\n", + "repattern = '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'\n", + "validipaddrs = 0\n", + "invalidipaddrs=0\n", + "for ipaddr in ipfile:\n", + " match_object = re.search(repattern,ipaddr\n", + "\n", + "if match_object is None:\n", + " invalidipaddrs +=1 \n", + "else:\n", + " validipaddrs +=1\n", + " \n", + "print ('Found %d valid ipaddresses' %(validipaddrs))\n", + "print ('Found %d invalid ipaddresses' %(invalidipaddrs))\n", + "print ('Processes %d total ipaddresses' %(validipaddrs + invalidipaddrs)) " + ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [default]", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -3662,7 +6538,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false } }, "nbformat": 4, diff --git a/docs/Eur.pop.XL.zip.csv b/docs/Eur.pop.XL.zip.csv new file mode 100644 index 0000000..6569733 --- /dev/null +++ b/docs/Eur.pop.XL.zip.csv @@ -0,0 +1,24 @@ +European Population (1989-1995),,,,,,, +,,,,,,, +,01.01.89,01.01.90,01.01.91,01.01.92,01.01.93,01.01.94,01.01.95 +Austria,7602431,7660345,7790957,7860800,7909575,7943652,8054800 +Belgium,9927600,9947800,9987000,10068319,10100631,10130574,10143047 +Denmark,5129800,5135400,5146500,5162100,5180614,5191000,5251027 +Finland,4954359,4974383,4998478,5029300,5054982,5098754,5116800 +France,56269800,56577000,56893000,57217500,57529577,57847000,58265400 +Germany,61715000,62678000,79753000,80238000,81338000,81353000,81845000 +Iceland,253500,255708,259577,262193,264922,266783,267806 +Ireland,3526600,3505500,3519000,3542000,3559985,3570700,3591200 +Italy,57504700,57576400,57746200,57788200,57114161,57201800,57268578 +Luxemburg,374900,379300,384400,389800,395200,400000,412800 +Netherland,14805240,14892574,15010445,15129200,15354000,15341553,15492800 +Norway,4226901,4241473,4261930,4273634,4324577,4348410,4370000 +Portugal,10304700,9878200,9858500,9846000,9987500,9776000,9920800 +Spain,38851900,38924500,38993800,39055900,39790955,39177400,39241900 +Sweden,8458890,8527040,8590630,8644100,8700000,8749000,8837000 +Switzerland,6619973,6673850,6750693,6831900,6871500,7021200,7060400 +United Kingdom,57236200,57410600,57649200,58888800,58191230,58380000,58684000 +,,,,,,, +Total Europe,325665440,326962674,345061045,347604719,348887853,348540020,372599752 +,,,,,,, +"Sources: Eurostat, national agencies",,,,,,, diff --git a/docs/Untitled.ipynb b/docs/Untitled.ipynb new file mode 100644 index 0000000..9cf5105 --- /dev/null +++ b/docs/Untitled.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Goodbye World\n" + ] + } + ], + "source": [ + "print ('Goodbye World')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# This is a markdown cell\n", + "## I can use multiple headings\n", + "### This is heading three. \n", + "This is in *italics*" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is 5\n", + "X is not 6\n" + ] + } + ], + "source": [ + "x = 5\n", + "if x == 5:\n", + " print (\"x is 5\")\n", + " print (\"X is not 6\")\n", + "else:\n", + " print (\"x is not 5\")" + ] + } + ], + "metadata": { + "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.9.7" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/Untitled1.ipynb b/docs/Untitled1.ipynb new file mode 100644 index 0000000..28e1d2a --- /dev/null +++ b/docs/Untitled1.ipynb @@ -0,0 +1,716 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d6a6ae29", + "metadata": {}, + "source": [ + "1. Numbers\n", + "2. Strings\n", + "3. Lists\n", + "4. Tuples\n", + "5. Sets\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "0f6a8f7a", + "metadata": {}, + "source": [ + "Dictionary. \n", + "A dictionary in Python is a set of key/value pairs. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "c292962a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun Brelin\n" + ] + } + ], + "source": [ + "d = {\"12345\":\"Braun Brelin\", \"12346\":\"Joe Green\", \"12347\":\"John Brown\"}\n", + "print (d[\"12345\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c5ce978c", + "metadata": {}, + "outputs": [], + "source": [ + "d[\"12348\"] = \"Jake Smith\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "71d90eae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'12345': 'Braun Brelin', '12346': 'Joe Green', '12347': 'John Brown', '12348': 'Jake Smith'}\n" + ] + } + ], + "source": [ + "print (d)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "945d75be", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3276ad65", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['12345', '12346', '12347', '12348'])\n" + ] + } + ], + "source": [ + "print (d.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "efde4525", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values(['Braun Brelin', 'Joe Green', 'John Brown', 'Jake Smith'])\n" + ] + } + ], + "source": [ + "print (d.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "7172aea8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('12345', 'Braun Brelin'), ('12346', 'Joe Green'), ('12347', 'John Brown'), ('12348', 'Jake Smith')])\n" + ] + } + ], + "source": [ + "print (d.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "445d23c0", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'12349'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_7968/3636667402.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"12349\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m: '12349'" + ] + } + ], + "source": [ + "print (d[\"12349\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6cb4bd31", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'12345': 'Braun Brelin',\n", + " '12346': 'Joe Green',\n", + " '12347': 'John Brown',\n", + " '12348': 'Jake Smith'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "18e6e34c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun Brelin\n", + "Joe Green\n", + "John Brown\n", + "Jake Smith\n" + ] + } + ], + "source": [ + "for key in d.keys():\n", + " print (d[key])" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4b50482c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('12345', 'Braun Brelin')\n", + "('12346', 'Joe Green')\n", + "('12347', 'John Brown')\n", + "('12348', 'Jake Smith')\n" + ] + } + ], + "source": [ + "for item in d.items():\n", + " print (item)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "fd546cdb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\User\\\\basicpython\\\\docs'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "eb7cd6ef", + "metadata": {}, + "outputs": [], + "source": [ + "f = open(\"foo.txt\")" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "ae0b3f1a", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['_CHUNK_SIZE',\n", + " '__class__',\n", + " '__del__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__enter__',\n", + " '__eq__',\n", + " '__exit__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__next__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '_checkClosed',\n", + " '_checkReadable',\n", + " '_checkSeekable',\n", + " '_checkWritable',\n", + " '_finalizing',\n", + " 'buffer',\n", + " 'close',\n", + " 'closed',\n", + " 'detach',\n", + " 'encoding',\n", + " 'errors',\n", + " 'fileno',\n", + " 'flush',\n", + " 'isatty',\n", + " 'line_buffering',\n", + " 'mode',\n", + " 'name',\n", + " 'newlines',\n", + " 'read',\n", + " 'readable',\n", + " 'readline',\n", + " 'readlines',\n", + " 'reconfigure',\n", + " 'seek',\n", + " 'seekable',\n", + " 'tell',\n", + " 'truncate',\n", + " 'writable',\n", + " 'write',\n", + " 'write_through',\n", + " 'writelines']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (f)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "bc4548e7", + "metadata": {}, + "outputs": [], + "source": [ + "f = open (\"foo.txt\")" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "be15d02b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is foo.\n", + "\n" + ] + } + ], + "source": [ + "print (f.readline())" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "fbee0abe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Foo is great!\n", + "\n" + ] + } + ], + "source": [ + "print (f.readline())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "7b553f74", + "metadata": {}, + "outputs": [], + "source": [ + "f.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "c47efd7b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['12345', 'Braun', 'Brelin']\n", + "['12346', 'Joe', 'Blow']\n", + "['12347', 'James Brown']\n" + ] + } + ], + "source": [ + "for line in f:\n", + " record = line.strip().split(\",\")\n", + " print (record)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "ac723d89", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "958c2cb2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a team name: New York Yankees\n", + "Team: New York Yankees aggregated salary: $152749814.00\n", + "Please enter a team name: Anaheim Angels\n", + "Team: Anaheim Angels aggregated salary: $79031667.00\n", + "Please enter a team name: Blech\n", + "Invalid team name!\n", + "Please enter a team name: Exit\n" + ] + } + ], + "source": [ + "import sys\n", + "try: #This is an example of exception handling. \n", + " f = open(\"ALbb.salaries.2003.formatted.csv\")\n", + "except FileNotFoundError:\n", + " print (\"Error: File not found!\")\n", + " sys.exit(1)\n", + " \n", + " \n", + "team_dict = {}\n", + "try: \n", + " for data in f:\n", + " if len (data) == 4:\n", + " break\n", + " line = data.strip().split(',')\n", + " try:\n", + " if line[0] in team_dict:\n", + " team_dict[line[0]] += int(line[3])\n", + " else:\n", + " team_dict[line[0]] = int(line[3])\n", + " except ValueError:\n", + " continue\n", + "except IOError:\n", + " print (\"I/O Error!\")\n", + " sys.exit(1)\n", + "else:\n", + " pass\n", + "finally:\n", + " f.close()\n", + "\n", + "while (True):\n", + " team = input('Please enter a team name: ')\n", + " if team in team_dict:\n", + "# print (team_dict[team])\n", + " print(\"Team: %s aggregated salary: $%.2f\" % (team, team_dict[team]))\n", + " elif team == 'Exit':\n", + " break\n", + " else:\n", + " print ('Invalid team name!')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88cb6652", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "from math import atan, sqrt, pi\n", + "import pandas as pd\n", + "import numpy as np\n" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "6d39f0dd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['FEE', 'FI', 'FO', 'FUM']" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "words = ['Fee','Fi','Fo','Fum']\n", + "upper_words = [word.upper() for word in words]\n", + "upper_words" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "5e2c17e1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}\n" + ] + } + ], + "source": [ + "list1 = [1,2,3,4,5]\n", + "list2 = ['a','b','c','d','e']\n", + "dict1 = {key:value for (key,value) in zip(list1,list2)} # Dictionary Comprehension.\n", + "print(dict1)" + ] + } + ], + "metadata": { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/Untitled2.ipynb b/docs/Untitled2.ipynb new file mode 100644 index 0000000..4e9d952 --- /dev/null +++ b/docs/Untitled2.ipynb @@ -0,0 +1,527 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "id": "223ea191", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First match: I\n", + "Second match: love\n", + "Third match: Python!\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "data = \"I love Python!\"\n", + "pattern = \"^(.*)\\s(.*)\\s(.*)$\"\n", + "pattern_match = re.search(pattern,data)\n", + "#pattern_match.group(0)\n", + "print (\"First match: \",pattern_match.group(1))\n", + "print(\"Second match: \",pattern_match.group(2))\n", + "print (\"Third match: \",pattern_match.group(3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4707133", + "metadata": {}, + "outputs": [], + "source": [ + "ipaddr_list = [\"192.168.100.1\",268.100.2.1]\n", + "#Find all the IP addresses that match the pattern.\n", + "#Find all the IP addresses that are in the valid range. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f709ef84", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10100.00\n" + ] + } + ], + "source": [ + "class BankAccount:\n", + " def __init__(self,starting_balance, accountID, accountName):\n", + " self.balance = starting_balance\n", + " self.accountID = accountID\n", + " self.accountName = accountName\n", + " \n", + " def deposit(self,amt):\n", + " self.balance += amt\n", + " \n", + " def withdraw(self,amt):\n", + " self.balance -= amt\n", + " \n", + " def getBalance(self):\n", + " return self.balance\n", + " \n", + "b = BankAccount(10000.00, 12345,\"Braun Brelin\")\n", + "b.deposit(100.00)\n", + "print (\"%.2f\" % (b.getBalance()))\n", + "\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "19007625", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9000.0\n" + ] + } + ], + "source": [ + "class CheckingAccount(BankAccount):\n", + " pass\n", + " \n", + "c = CheckingAccount(10000.00,12345,\"Braun Brelin\")\n", + "c.getBalance()\n", + "c.withdraw(1000.00)\n", + "print (c.getBalance())" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "169f0523", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name = Braun Brelin Age = 21 Address = 1234 Main Street\n" + ] + } + ], + "source": [ + "class Person:\n", + "\n", + " def __init__(self,name,age, address):\n", + " self.name = name\n", + " self.age = age\n", + " self.address = address\n", + " \n", + " def changeage(self,newage):\n", + " self.age = newage\n", + " return\n", + " \n", + " def changeaddress(self,newaddr):\n", + " self.address = newaddr\n", + " \n", + " # This is known as method overriding. \n", + " def __str__(self):\n", + " return \"Name = \" + self.name + \" Age = \" + str(self.age) + \" Address = \" + self.address\n", + " \n", + " \n", + "class Employee(Person):\n", + " \n", + " def __init__(self,**kwargs):\n", + " super().__init__(kwargs[\"name\"],kwargs[\"age\"],kwargs[\"address\"])\n", + " ''' This class will contain attributes:\n", + " Employee ID\n", + " Role\n", + " Salary\n", + " \n", + " This class will contain methods:\n", + " ChangeSalary\n", + " ChangeRole\n", + " \n", + " '''\n", + " \n", + " # Roles = ('Workers', 'Supervisors', 'Managers')\n", + " \n", + "''' Now, test each method in the classes. '''\n", + " \n", + "p = Person(\"Braun Brelin\", 21, \"1234 Main Street\")\n", + "e = Employee(name=\"Braun Brelin\",age=21,address=\"1234 Main Street\",emp_id = \"7345\",role = \"Manager\",salary = 50000.00)\n", + "print (p)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "fae7f96c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(6.4031242374328485, 0.8960553845713439)\n" + ] + } + ], + "source": [ + "from math import atan, sqrt\n", + "class Vector:\n", + "\n", + " # This is a static variable.\n", + " vector_count=0\n", + "\n", + " \n", + " def __init__(self,x,y):\n", + " self.x = x\n", + " self.y = y\n", + " Vector.vector_count += 1\n", + " \n", + " \n", + " # This is method overriding.\n", + " def __str__(self):\n", + " return \"X: \" + str(self.x) + \" Y: \" + str(self.y)\n", + " \n", + " # This is called operator overloading. \n", + " def __add__(self,other):\n", + " return Vector(self.x + other.x, self.y + other.y)\n", + " \n", + " @staticmethod \n", + " def convert_to_polar(x,y):\n", + " return (sqrt(x**2 + y **2),atan(y/x))\n", + " \n", + " \n", + "V1 = Vector(2,3)\n", + "V2 = Vector (3,4)\n", + "# The following two lines are equivalent\n", + "V3 = V1 + V2\n", + "#V3 = V1.__add__(V2)\n", + "#print (V3)\n", + "#print (Vector.vector_count)\n", + "print (Vector.convert_to_polar(4,5))" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "e551b398", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n" + ] + } + ], + "source": [ + "def myfunc(a,b=0):\n", + " return a+b\n", + "\n", + "print (myfunc(5))\n", + "print (myfunc(5,1))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "8085fc62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Fee\n", + "Fi\n", + "Fo\n", + "Fum\n", + "None\n", + "\n", + "Foo\n", + "Bar\n", + "None\n" + ] + } + ], + "source": [ + "def myvarargs(*args):\n", + " print (type(args))\n", + " for arg in args:\n", + " print (arg)\n", + " \n", + " \n", + "print(myvarargs(\"Fee\",\"Fi\",\"Fo\",\"Fum\"))\n", + "print (myvarargs(\"Foo\",\"Bar\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "f2b0eba1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun Brelin\n" + ] + } + ], + "source": [ + "def myfunc(**kwargs):\n", + " print (kwargs[\"name\"])\n", + "\n", + "myfunc(name=\"Braun Brelin\",age=21,address=\"1234 Main Street\")" + ] + }, + { + "cell_type": "markdown", + "id": "259e1509", + "metadata": {}, + "source": [ + "Functional programming....\n", + "\n", + "Lambda function. " + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "e1d4b9ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", + "\n", + "range(0, 10)\n", + "\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "cubes = lambda x:x**3\n", + "squares = map(lambda x: x**2,nums)\n", + "print (list(squares))\n", + "cubes_list = map(cubes,nums)\n", + "print (cubes_list)\n", + "print (range(10))\n", + "list1 = [1,2,3,4,5]\n", + "list2 = ['a','b','c','d','e']\n", + "print (zip (list1,list2))" + ] + }, + { + "cell_type": "markdown", + "id": "8b605b86", + "metadata": {}, + "source": [ + "Is it possible to have a computer language that only understands single valued functions.\n", + "f(x)\n", + "\n", + "a = 0 No! Not allowed\n", + "if a == 0 No! Not allowed\n", + "for a in b: No! Not allowed\n", + "f(1,2,3) No!: Not allowed.\n", + "\n", + "f(1)\n", + "g(5)\n", + "h('Foobar')\n", + "f(g(h('Foobar') Yes, allowed. " + ] + }, + { + "cell_type": "markdown", + "id": "c9b98832", + "metadata": {}, + "source": [ + "Eager evaluation vs. lazy evaluation. " + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "e3d9b784", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", + "\n", + "range(0, 10)\n", + "(1, 'a')\n", + "(2, 'b')\n", + "(3, 'c')\n", + "(4, 'd')\n", + "(5, 'e')\n" + ] + } + ], + "source": [ + "\n", + "cubes = lambda x:x**3\n", + "squares = map(lambda x: x**2,nums)\n", + "print (list(squares))\n", + "cubes_list = map(cubes,nums)\n", + "print (cubes_list)\n", + "print (range(10))\n", + "list1 = [1,2,3,4,5]\n", + "list2 = ['a','b','c','d','e']\n", + "z = zip (list1,list2)\n", + "for item in z:\n", + " print (item)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "fadd1dbb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun Brelin\n", + "21\n", + "1234 Main Street\n" + ] + } + ], + "source": [ + "from collections import namedtuple\n", + "\n", + "Point = namedtuple(\"Point\", \"x y\")\n", + "\n", + "point = Point(2,4)\n", + "\n", + "#print (point[0],point[1])\n", + "#print (point.x,point.y)\n", + "\n", + "Person = namedtuple(\"Person\",\"name age address\")\n", + "\n", + "p = Person(\"Braun Brelin\",21,\"1234 Main Street\")\n", + "print (p.name)\n", + "print (p.age)\n", + "print (p.address)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "4f088299", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ERROR:root:Internal Python error in the inspect module.\n", + "Below is the traceback from this internal error.\n", + "\n", + "\n", + "KeyboardInterrupt\n", + "\n" + ] + } + ], + "source": [ + "from itertools import count\n", + "\n", + "print(list(count(0,2)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c3740a6", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "print (os.getcwd())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b530c762", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/Untitled3.ipynb b/docs/Untitled3.ipynb new file mode 100644 index 0000000..cdcb456 --- /dev/null +++ b/docs/Untitled3.ipynb @@ -0,0 +1,2121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "da9aeed2", + "metadata": {}, + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "41176800", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], + "source": [ + "x = 10\n", + "x = x + 1\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "10a67c25", + "metadata": {}, + "source": [ + "# This is a header\n", + "## This is a sub header\n", + "### Third header.\n", + "\n", + "**This is bolded**
\n", + "*This is italicized*" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9a5125d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], + "source": [ + "import this" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6c9e73cf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello from Python\n" + ] + } + ], + "source": [ + "print ('Hello from Python')" + ] + }, + { + "cell_type": "markdown", + "id": "f5b1784d", + "metadata": {}, + "source": [ + "# Data types in Python\n", + "\n", + "1. Numbers.\n", + " A. Integers\n", + " B. Fixed Point\n", + " C. Complex Numbers. (a + bi)\n", + " D. Octal Numbers. \n", + " E. Hexadecimal Numbers. \n", + " 2. Strings.\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1e2dc47", + "metadata": {}, + "outputs": [], + "source": [ + "x = 10" + ] + }, + { + "cell_type": "markdown", + "id": "04a86777", + "metadata": {}, + "source": [ + "Object reference ID ----> The object\n", + "x = 10 This is an object. Specifically, it's a numeric object. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "07fbe335", + "metadata": {}, + "outputs": [], + "source": [ + "y = 20.0\n", + "c = 10j\n", + "o = 010\n", + "x = 0xff" + ] + }, + { + "cell_type": "markdown", + "id": "98398ef4", + "metadata": {}, + "source": [ + "** -> Exponentiation operator.\n", + "*\n", + "/ -> Fixed point division\n", + "// -> Integer division\n", + "% -> Find the remainder.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "93a060c9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.3333333333333335\n", + "3.0\n", + "-3.3333333333333335\n", + "-4.0\n" + ] + } + ], + "source": [ + "x = 10.0\n", + "y = 3\n", + "print (x / y)\n", + "print (x // y) # Floor division\n", + "y = -3\n", + "print (x / y)\n", + "print (x // y) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9123d1c6", + "metadata": {}, + "outputs": [], + "source": [ + "x = True\n", + "y = False\n", + "a = 5\n", + "if a > 5:\n", + " print ('A is bigger than 5')\n", + " print ('foo')\n", + "print ('Not in the block anymore')" + ] + }, + { + "cell_type": "markdown", + "id": "5097a311", + "metadata": {}, + "source": [ + "if (a > 5) {\n", + " print ('A is bigger than 5');\n", + "}\n", + "\n", + "if (a>5) {printf(\"A is bigger than 5\\n\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "e0a771a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n" + ] + } + ], + "source": [ + "print ('Hello')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0a69945f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 4\n", + "4\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "4074fd93", + "metadata": {}, + "source": [ + "Every type of input comes in as a string, not a number. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "27814fe8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 7\n", + "11\n", + "11.0\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "y = int(x) # Int is a built in function. It converts datatypes to numeric (integer) objects. \n", + "print (y + 4)\n", + "z = float(x)\n", + "print (z + 4)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "93f123d5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/1375056562.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "t = \"Hello World\"\n", + "z = \"\"\" This is a multi line string. \n", + " This is line 2 of the string.\n", + " And so on...\n", + " \"\"\"\n", + "# This is a single line comment. \n", + "# Strings are immutable.\n", + "\n", + "print (s)\n", + "s [0] = 'J'\n" + ] + }, + { + "cell_type": "markdown", + "id": "5bd45e12", + "metadata": {}, + "source": [ + "These are examples of string slicing. " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "12d48495", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "H\n", + "e\n", + "Hello\n", + "Hlo\n", + "drWolH\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s[0])\n", + "print (s[1]) #This concept is called a 'slice'\n", + "print (s[0:5]) # Start at the beginning and go to the end - 1 character.\n", + "print (s[0:5:2]) # The last value is a stepper value. \n", + "print (s[::-1])" + ] + }, + { + "cell_type": "markdown", + "id": "335e63c3", + "metadata": {}, + "source": [ + "What is an object?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "bae46917", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "e32c0cf2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "HELLO WORLD\n", + "hello world\n", + "11\n", + "11\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s.upper())\n", + "print (s.lower())\n", + "print (len(s))\n", + "print (s.__len__())" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "900fb105", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is less than 10\n" + ] + } + ], + "source": [ + "x = 9\n", + "l_counter = 0\n", + "if x == 10:\n", + " print ('x is 10')\n", + "elif x < 10:\n", + " print ('x is less than 10')\n", + "else:\n", + " print (\"x is greater than 10\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "870b0fb2", + "metadata": {}, + "source": [ + "A string is an example of an *iterable* object. \n", + "We can use a looping construct to iterate over the object. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "b205b671", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for i in range(len(s)):\n", + " print (s[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "38a38dc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "print (list(range(10)))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "19f207cb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for elem in s:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "84385533", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World \n", + "Hello World Hello World Hello World Hello World Hello World \n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World \"\n", + "s = s1 + s2 # + is string concatenation for strings. \n", + "print (s)\n", + "print (s * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "42db4edb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "a26482bf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "s = \"Braun Brelin, 1234 Main Street, Anytown, USA\"\n", + "print (s.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "60d1ab9c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class str in module builtins:\n", + "\n", + "class str(object)\n", + " | str(object='') -> str\n", + " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", + " | \n", + " | Create a new string object from the given object. If encoding or\n", + " | errors is specified, then the object must expose a data buffer\n", + " | that will be decoded using the given encoding and error handler.\n", + " | Otherwise, returns the result of object.__str__() (if defined)\n", + " | or repr(object).\n", + " | encoding defaults to sys.getdefaultencoding().\n", + " | errors defaults to 'strict'.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __format__(self, format_spec, /)\n", + " | Return a formatted version of the string as described by format_spec.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(self, key, /)\n", + " | Return self[key].\n", + " | \n", + " | __getnewargs__(...)\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __hash__(self, /)\n", + " | Return hash(self).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self int\n", + " | \n", + " | Return the number of non-overlapping occurrences of substring sub in\n", + " | string S[start:end]. Optional arguments start and end are\n", + " | interpreted as in slice notation.\n", + " | \n", + " | encode(self, /, encoding='utf-8', errors='strict')\n", + " | Encode the string using the codec registered for encoding.\n", + " | \n", + " | encoding\n", + " | The encoding in which to encode the string.\n", + " | errors\n", + " | The error handling scheme to use for encoding errors.\n", + " | The default is 'strict' meaning that encoding errors raise a\n", + " | UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", + " | 'xmlcharrefreplace' as well as any other name registered with\n", + " | codecs.register_error that can handle UnicodeEncodeErrors.\n", + " | \n", + " | endswith(...)\n", + " | S.endswith(suffix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S ends with the specified suffix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | suffix can also be a tuple of strings to try.\n", + " | \n", + " | expandtabs(self, /, tabsize=8)\n", + " | Return a copy where all tab characters are expanded using spaces.\n", + " | \n", + " | If tabsize is not given, a tab size of 8 characters is assumed.\n", + " | \n", + " | find(...)\n", + " | S.find(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | format(...)\n", + " | S.format(*args, **kwargs) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from args and kwargs.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | format_map(...)\n", + " | S.format_map(mapping) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from mapping.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | index(...)\n", + " | S.index(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | isalnum(self, /)\n", + " | Return True if the string is an alpha-numeric string, False otherwise.\n", + " | \n", + " | A string is alpha-numeric if all characters in the string are alpha-numeric and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isalpha(self, /)\n", + " | Return True if the string is an alphabetic string, False otherwise.\n", + " | \n", + " | A string is alphabetic if all characters in the string are alphabetic and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isascii(self, /)\n", + " | Return True if all characters in the string are ASCII, False otherwise.\n", + " | \n", + " | ASCII characters have code points in the range U+0000-U+007F.\n", + " | Empty string is ASCII too.\n", + " | \n", + " | isdecimal(self, /)\n", + " | Return True if the string is a decimal string, False otherwise.\n", + " | \n", + " | A string is a decimal string if all characters in the string are decimal and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isdigit(self, /)\n", + " | Return True if the string is a digit string, False otherwise.\n", + " | \n", + " | A string is a digit string if all characters in the string are digits and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isidentifier(self, /)\n", + " | Return True if the string is a valid Python identifier, False otherwise.\n", + " | \n", + " | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n", + " | such as \"def\" or \"class\".\n", + " | \n", + " | islower(self, /)\n", + " | Return True if the string is a lowercase string, False otherwise.\n", + " | \n", + " | A string is lowercase if all cased characters in the string are lowercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | isnumeric(self, /)\n", + " | Return True if the string is a numeric string, False otherwise.\n", + " | \n", + " | A string is numeric if all characters in the string are numeric and there is at\n", + " | least one character in the string.\n", + " | \n", + " | isprintable(self, /)\n", + " | Return True if the string is printable, False otherwise.\n", + " | \n", + " | A string is printable if all of its characters are considered printable in\n", + " | repr() or if it is empty.\n", + " | \n", + " | isspace(self, /)\n", + " | Return True if the string is a whitespace string, False otherwise.\n", + " | \n", + " | A string is whitespace if all characters in the string are whitespace and there\n", + " | is at least one character in the string.\n", + " | \n", + " | istitle(self, /)\n", + " | Return True if the string is a title-cased string, False otherwise.\n", + " | \n", + " | In a title-cased string, upper- and title-case characters may only\n", + " | follow uncased characters and lowercase characters only cased ones.\n", + " | \n", + " | isupper(self, /)\n", + " | Return True if the string is an uppercase string, False otherwise.\n", + " | \n", + " | A string is uppercase if all cased characters in the string are uppercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | join(self, iterable, /)\n", + " | Concatenate any number of strings.\n", + " | \n", + " | The string whose method is called is inserted in between each given string.\n", + " | The result is returned as a new string.\n", + " | \n", + " | Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n", + " | \n", + " | ljust(self, width, fillchar=' ', /)\n", + " | Return a left-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | lower(self, /)\n", + " | Return a copy of the string converted to lowercase.\n", + " | \n", + " | lstrip(self, chars=None, /)\n", + " | Return a copy of the string with leading whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | partition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string. If the separator is found,\n", + " | returns a 3-tuple containing the part before the separator, the separator\n", + " | itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing the original string\n", + " | and two empty strings.\n", + " | \n", + " | removeprefix(self, prefix, /)\n", + " | Return a str with the given prefix string removed if present.\n", + " | \n", + " | If the string starts with the prefix string, return string[len(prefix):].\n", + " | Otherwise, return a copy of the original string.\n", + " | \n", + " | removesuffix(self, suffix, /)\n", + " | Return a str with the given suffix string removed if present.\n", + " | \n", + " | If the string ends with the suffix string and that suffix is not empty,\n", + " | return string[:-len(suffix)]. Otherwise, return a copy of the original\n", + " | string.\n", + " | \n", + " | replace(self, old, new, count=-1, /)\n", + " | Return a copy with all occurrences of substring old replaced by new.\n", + " | \n", + " | count\n", + " | Maximum number of occurrences to replace.\n", + " | -1 (the default value) means replace all occurrences.\n", + " | \n", + " | If the optional argument count is given, only the first count occurrences are\n", + " | replaced.\n", + " | \n", + " | rfind(...)\n", + " | S.rfind(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | rindex(...)\n", + " | S.rindex(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | rjust(self, width, fillchar=' ', /)\n", + " | Return a right-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | rpartition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string, starting at the end. If\n", + " | the separator is found, returns a 3-tuple containing the part before the\n", + " | separator, the separator itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing two empty strings\n", + " | and the original string.\n", + " | \n", + " | rsplit(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | Splits are done starting at the end of the string and working to the front.\n", + " | \n", + " | rstrip(self, chars=None, /)\n", + " | Return a copy of the string with trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | split(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | splitlines(self, /, keepends=False)\n", + " | Return a list of the lines in the string, breaking at line boundaries.\n", + " | \n", + " | Line breaks are not included in the resulting list unless keepends is given and\n", + " | true.\n", + " | \n", + " | startswith(...)\n", + " | S.startswith(prefix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S starts with the specified prefix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | prefix can also be a tuple of strings to try.\n", + " | \n", + " | strip(self, chars=None, /)\n", + " | Return a copy of the string with leading and trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | swapcase(self, /)\n", + " | Convert uppercase characters to lowercase and lowercase characters to uppercase.\n", + " | \n", + " | title(self, /)\n", + " | Return a version of the string where each word is titlecased.\n", + " | \n", + " | More specifically, words start with uppercased characters and all remaining\n", + " | cased characters have lower case.\n", + " | \n", + " | translate(self, table, /)\n", + " | Replace each character in the string using the given translation table.\n", + " | \n", + " | table\n", + " | Translation table, which must be a mapping of Unicode ordinals to\n", + " | Unicode ordinals, strings, or None.\n", + " | \n", + " | The table must implement lookup/indexing via __getitem__, for instance a\n", + " | dictionary or list. If this operation raises LookupError, the character is\n", + " | left untouched. Characters mapped to None are deleted.\n", + " | \n", + " | upper(self, /)\n", + " | Return a copy of the string converted to uppercase.\n", + " | \n", + " | zfill(self, width, /)\n", + " | Pad a numeric string with zeros on the left, to fill a field of the given width.\n", + " | \n", + " | The string is never truncated.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | maketrans(...)\n", + " | Return a translation table usable for str.translate().\n", + " | \n", + " | If there is only one argument, it must be a dictionary mapping Unicode\n", + " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", + " | Character keys will be then converted to ordinals.\n", + " | If there are two arguments, they must be strings of equal length, and\n", + " | in the resulting dictionary, each character in x will be mapped to the\n", + " | character at the same position in y. If there is a third argument, it\n", + " | must be a string, whose characters will be mapped to None in the result.\n", + "\n" + ] + } + ], + "source": [ + "help(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "ad8e5285", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (str)" + ] + }, + { + "cell_type": "markdown", + "id": "b1817294", + "metadata": {}, + "source": [ + "The List data type. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "2a051327", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "s = \"1\"\n", + "l = [1,2,3,4,5,6,7,8,9,10] # Is a list, an iterable object?\n", + "print (l)\n", + "for elem in l:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "5eaa54a4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (list)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "b8148196", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l\n", + "l.append(11)\n", + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "00cf1e2f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "[1, 2, 3, 4]\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "9\n" + ] + } + ], + "source": [ + "print (l[0])\n", + "print (l[0:4])\n", + "#print (l[::-1])\n", + "print (l)\n", + "print (l.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "3cb0ee8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']\n", + "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "print (len (l))\n", + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = l1 + l2\n", + "print (l3)\n", + "print (l1 * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "8f427605", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "c37c1b67", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "0a330c47", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "dis() got an unexpected keyword argument 'squares'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/266041033.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m print (dis(\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0msquares\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;33m]\u001b[0m \u001b[1;31m# List comprehension\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m ))\n", + "\u001b[1;31mTypeError\u001b[0m: dis() got an unexpected keyword argument 'squares'" + ] + } + ], + "source": [ + "from dis import dis\n", + "\n", + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums if num % 2 == 0 ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "9b98418e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "jack_list = [\"Fee\",\"Fi\",\"Fo\",\"Fum\"]\n", + "jack_upper_list = [ word.upper() for word in jack_list ]\n", + "print (jack_upper_list)" + ] + }, + { + "cell_type": "markdown", + "id": "27635e89", + "metadata": {}, + "source": [ + "Tuples. Tuples are similar to lists, except that, like strings, they are immutable. \n", + "We use the round braces () to specify a tuple.." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "4f21ce7f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/4289262067.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = (1,2,3,4,5)\n", + "print (t)\n", + "\n", + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "cb9be884", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]\n", + "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]\n" + ] + } + ], + "source": [ + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = zip(l1,l2)\n", + "print (list(l3))\n", + "print (list(enumerate(l2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "6e2a95a0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "1\n" + ] + } + ], + "source": [ + "print (max(l1))\n", + "print (min(l1))\n", + "# tuple() converts objects into tuples. " + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "789dbad4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 12)\n", + "Fee fi fo fum\n" + ] + } + ], + "source": [ + "x = 5\n", + "y = 6\n", + "\n", + "t = (x+5,y+6) #tuple packing. \n", + "(x+5,y+6) = t1\n", + "print (t)\n", + "s = \"Fee,fi,fo,fum\"\n", + "(first,second,third,fourth) = s.split(',')\n", + "print (first,second,third,fourth)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "8ac805f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Graduate\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/3873200294.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0mmovie_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mt1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = ('The Graduate', 'Dustin Hoffman','Anne Bancroft',1967)\n", + "\n", + "\n", + "'''\n", + "movie_name = t[0]\n", + "actor = t[1]\n", + "actress = t[2]\n", + "date = t[3]\n", + "'''\n", + "movie_name, actor, actress, date = t # This is tuple unpacking.\n", + "print (movie_name)\n", + "t1 = (movie_name,actor,actress,date) # Tuple packing.\n", + "print (t1)\n", + "movie_name = \"Papillon\"\n", + "print (t1)\n", + "t1[0] = \"Papillon\" \n" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "e6997b99", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'orange', 'apple', 'banana', 'grapefruit'}\n" + ] + } + ], + "source": [ + "list1 = ['apple','banana','orange','apple','banana','grapefruit','apple']\n", + "set1 = set(list1)\n", + "print (set1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "36784964", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'pop from an empty set'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/2821507018.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mset1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;31m#print (set1.pop())\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'" + ] + } + ], + "source": [ + "print (set1.pop())\n", + "#print (set1.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "e9836e53", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "\n", + "\n" + ] + } + ], + "source": [ + "x = 1\n", + "print (str(x))\n", + "y = str(x)\n", + "print (type(x))\n", + "print (type(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5187e9d5", + "metadata": {}, + "outputs": [], + "source": [ + "from math import sqrt,atan\n", + "\n", + "sqrt()\n", + "atan()\n" + ] + }, + { + "cell_type": "markdown", + "id": "42ce279f", + "metadata": {}, + "source": [ + "Dictionary. Stores key/value pairs in Python...\n" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "9f3bdced", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "value1\n" + ] + } + ], + "source": [ + "d = {'key1':'value1','key2':'value2','key3':'value3'}\n", + "\n", + "print (d['key1'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2bf8297", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "81dfd709", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values(['value1', 'value2', 'value3'])\n" + ] + } + ], + "source": [ + "print(d.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "34a32848", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['key1', 'key2', 'key3']\n" + ] + } + ], + "source": [ + "print (list(d.keys()))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a4e05ad9", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "7fc103e1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])\n" + ] + } + ], + "source": [ + "print (d.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "3cf15f7c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "0b54151f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No such color\n" + ] + } + ], + "source": [ + "colors = {'Red':1,'Green':2,'Blue':3}\n", + "if 'Yellow' in colors:\n", + " print (colors['Yellow'])\n", + "else:\n", + " print ('No such color') \n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "692d3e3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(colors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e81b842a", + "metadata": {}, + "outputs": [], + "source": [ + "def myfunc():\n", + " a = 1\n", + " b = 0\n", + " c = a + b\n", + " return c\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "7d72801e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "X before calling myfunc = 10\n", + "In the function, x = 11\n", + "X after calling myfunc = 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def myfunc(x):\n", + " # x = x + 1 #These two statements are identical. \n", + " x += 1\n", + " print (\"In the function, x = \",x)\n", + " \n", + "print (\"X before calling myfunc = \",x)\n", + "myfunc(x)\n", + "print (\"X after calling myfunc = \",x)\n" + ] + }, + { + "cell_type": "markdown", + "id": "132a4b21", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "Scoping \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "767a83f3", + "metadata": {}, + "outputs": [], + "source": [ + "myfunc(a,b=0)" + ] + } + ], + "metadata": { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/Untitled4.ipynb b/docs/Untitled4.ipynb new file mode 100644 index 0000000..719ecc2 --- /dev/null +++ b/docs/Untitled4.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], + "source": [ + "import this" + ] + } + ], + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/Untitled5.ipynb b/docs/Untitled5.ipynb new file mode 100644 index 0000000..ba7c981 --- /dev/null +++ b/docs/Untitled5.ipynb @@ -0,0 +1,34 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "mybar = \"Hello World\"" + ] + } + ], + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/Untitled6.ipynb b/docs/Untitled6.ipynb new file mode 100644 index 0000000..a190032 --- /dev/null +++ b/docs/Untitled6.ipynb @@ -0,0 +1,1772 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "if x == 10:\n", + " print ('x is 10')\n", + "print ('x is not 10')" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n", + " This is the docstring for func1\n", + " \n" + ] + } + ], + "source": [ + "# this is a comment\n", + "print ('Hello') # This is a comment here. \n", + "\n", + "''' Everything in here is a string\n", + "[ We use this for multiline comments.\n", + "'''\n", + "\n", + "def func1(a):\n", + " ''' This is the docstring for func1\n", + " '''\n", + " \n", + " return a +1\n", + "\n", + "print (func1.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Hello World\n" + ] + } + ], + "source": [ + "print ('Hello World') # Single quotes\n", + "print (\"Hello World\") # Double quotes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First set of object types\n", + "\n", + "Numbers\n", + "\n", + " 1 Integers\n", + " 2. Floating point (Implemented as double precision)\n", + " 3. Complex\n", + " 4. Binary\n", + " 5. Hexadecimal\n", + " 6. Octal. \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = 10\n", + "y = 2.5\n", + "c = 2j\n", + "b = 0b1010111\n", + "h = 0xffff\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "a,b,c = 1,2,3\n", + "print (a,b,c)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 5 5\n" + ] + } + ], + "source": [ + "x = y = z = 5\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Numbers\n", + "Strings\n", + "Lists\n", + "Tuples\n", + "Sets \n", + "Dictionaries\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** \n", + "* / //\n", + "+ - \n", + "%\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0\n", + "3.0\n" + ] + } + ], + "source": [ + "a = 6.0\n", + "b = 2.0\n", + "c = 2\n", + "print (a/b)\n", + "print (a // c)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "a = 5\n", + "b = 2\n", + "print (a % b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = a + 1 # Both of these statements do the same thing. \n", + "a += 1 #This iis a shortcut\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Boolean type\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = True,1,'Non empty string'\n", + "b = False, 0,'' \n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = True\n", + "if a:\n", + " print ('True')\n", + "if a == True:\n", + " print ('True')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if not a:\n", + " print ('False')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if a and not b:\n", + " print ('Foo')\n", + " \n", + "if a or not b:\n", + " print ('Bar')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "import math\n", + "print (math.pi)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number: 5\n", + "5\n" + ] + } + ], + "source": [ + "x = input (\"Please type in a number: \")\n", + "print (x)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Goodbye World\n" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "print (s)\n", + "s = \"Goodbye World\"\n", + "print (s)\n", + "\n", + "# Strings are immutable.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Hello World\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "s [0] = 'J'\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HELLO WORLD\n", + "hello world\n" + ] + } + ], + "source": [ + "print (s.upper())\n", + "print (s.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n" + ] + } + ], + "source": [ + "s = \"Hello World\"\n", + "\n", + "print (s[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "e\n" + ] + } + ], + "source": [ + "print (s[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ello\n" + ] + } + ], + "source": [ + "print (s[1:5]) #This is called a slice." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HloWrd\n" + ] + } + ], + "source": [ + "print (s[::2])" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dlroW olleH\n" + ] + } + ], + "source": [ + "print (s[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World\"\n", + "\n", + "s3 = s1 + s2\n", + "print (s3)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Hello Hello Hello Hello \n" + ] + } + ], + "source": [ + "s4 = \"Hello \" * 5\n", + "print (s4)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "data = 'Braun Brelin, 1234 Main Street, Anytown, USA'\n", + "print (data.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number 5\n", + "\n", + "\n", + "25\n" + ] + } + ], + "source": [ + "x = input(\"Please type in a number \")\n", + "print (type(x))\n", + "x = int(x)\n", + "print (type(x))\n", + "print (x**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type in a number b\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please type in a number \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input(\"Please type in a number \"))\n", + "print (x ** 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please type ib some input: Hello\n", + "The length of your string is: 5\n" + ] + } + ], + "source": [ + "s = input (\"Please type ib some input: \")\n", + "print (\"The length of your string is: \", len(s))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings are also *iterable* objects. " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n" + ] + } + ], + "source": [ + "for c in s:\n", + " print (c)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "a\n", + "b\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [1,2,3,'a','b','c']\n", + "for o in mylist:\n", + " print (o)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 'a']\n" + ] + } + ], + "source": [ + "print (mylist[1:4])" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['c', 'b', 'a', 3, 2, 1]\n" + ] + } + ], + "source": [ + "print (mylist[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(mylist)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd']\n" + ] + } + ], + "source": [ + "mylist.append('d')\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "source": [ + "print (len(mylist))" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n" + ] + } + ], + "source": [ + "mylist1 = ['e','f','g']\n", + "mylist = mylist + mylist1\n", + "print (mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]\n" + ] + } + ], + "source": [ + "mylist2 = [1,2,3]\n", + "mylist3 = mylist2 * 5\n", + "print (mylist3)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "c\n" + ] + } + ], + "source": [ + "mylist = [[1,2,3],['a','b','c']]\n", + "print (mylist[0][2])\n", + "print (mylist[1][2])" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "words = ['fee','fi','fo','fum']\n", + "upper_words = [word.upper() for word in words] # List Comprehension\n", + " print (upper_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 16, 36, 64, 100]\n" + ] + } + ], + "source": [ + "squares = [num **2 for num in nums if num % 2 == 0]\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "t = (0,1,2,3,4,5)\n", + "print (t[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2, 3, 4)" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[1:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 1, 2, 3, 4, 5)" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(t)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Braun 21 Male\n" + ] + } + ], + "source": [ + "name = \"Braun\"\n", + "age = 21\n", + "gender = \"Male\"\n", + "\n", + "t = (name,age,gender) # Tuple packing.\n", + "\n", + "#print (t)\n", + "\n", + "(x,y,z) = t # Tuple Unpacking\n", + "print (x,y,z)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 'a')\n", + "(1, 'b')\n", + "(2, 'c')\n", + "(3, 'd')\n", + "(4, 'e')\n" + ] + } + ], + "source": [ + "example_list = ['a','b','c','d','e']\n", + "\n", + "for i in enumerate(example_list):\n", + " print (i)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 10), ('b', 11), ('c', 12), ('d', 13)]\n" + ] + } + ], + "source": [ + "example_list2 = [10,11,12,13]\n", + "\n", + "example_list3 = zip(example_list, example_list2)\n", + "print (list(example_list3))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Apple', 'Orange', 'Banana'}\n" + ] + } + ], + "source": [ + "myset = {'Apple','Orange','Banana','Apple'}\n", + "print (myset)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is neither 0 nor 1\n" + ] + } + ], + "source": [ + "x = 2\n", + "if not x:\n", + " print (\"x is 0\" )\n", + "elif x == 1:\n", + " print (\"x is 1\")\n", + "else:\n", + " print (\"x is neither 0 nor 1\")" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid IP Address\n" + ] + } + ], + "source": [ + "ipaddr = \"257.168.100.1\"\n", + "for octet in ipaddr.split(\".\"):\n", + " if int(octet) >255:\n", + " print (\"Invalid IP Address\")\n", + " break\n", + "else:\n", + " print (\"Valid IP Address\")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "x = 10\n", + "while x > 0:\n", + " print (x)\n", + " x -= 1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "num is 5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "for num in nums:\n", + " if num == 5:\n", + " print (\"num is 5\")\n", + " continue\n", + " else:\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "def func1(x: int = 2)-> int:\n", + " return x**2\n", + "\n", + "print (func1())\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n", + "d\n" + ] + } + ], + "source": [ + "def func2(*args: list) -> str: \n", + " for arg in args:\n", + " print (arg)\n", + " \n", + "func2(\"a\",\"b\",\"c\",\"d\")" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a foo\n", + "b bar\n", + "c baz\n" + ] + } + ], + "source": [ + "def func3(**kwargs: dict) -> None:\n", + " for (key,value) in kwargs.items():\n", + " print (key,value)\n", + " \n", + "func3(a = \"foo\", b = \"bar\",c = \"baz\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'key1':'value1', 'key2':'value2', 'key3':'value3'}\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "4\n" + ] + } + ], + "source": [ + "dow_dict = {}\n", + "list1 = ['Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday','Sunday']\n", + "list2 = [1,2,3,4,5,6,7]\n", + "dow_dict = { k:v for (k,v) in zip(list1,list2)} # Dictionary comprehension\n", + "\n", + "print (type(dow_dict))\n", + "print (dow_dict['Thursday'])" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys(['Monday', 'Tuesday', 'Wedneseday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])\n", + "dict_values([1, 2, 3, 4, 5, 6, 7])\n", + "dict_items([('Monday', 1), ('Tuesday', 2), ('Wedneseday', 3), ('Thursday', 4), ('Friday', 5), ('Saturday', 6), ('Sunday', 7)])\n" + ] + } + ], + "source": [ + "print (dow_dict.keys())\n", + "print (dow_dict.values())\n", + "print (dow_dict.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid key!\n" + ] + } + ], + "source": [ + "if 'Braunsday' in dow_dict:\n", + " print (dow_dict['Braunsday'])\n", + "else:\n", + " print ('Invalid key!')" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter day of week: Wednesday\n", + "3\n" + ] + } + ], + "source": [ + "while True:\n", + " key = input(\"Enter day of week: \")\n", + " if key not in dow_dict:\n", + " print (\"Invalid key! Try again\")\n", + " continue\n", + " else:\n", + " print (dow_dict[key])\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a numberb\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'b'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'b'" + ] + } + ], + "source": [ + "x = int(input (\"Please enter a number\"))\n", + "print (x + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: b\n", + "Invalid number!\n", + "Please enter a number: c\n", + "Invalid number!\n", + "Please enter a number: 4\n", + "5\n" + ] + } + ], + "source": [ + "while True: \n", + " try:\n", + " x = int(input (\"Please enter a number: \")) \n", + " except ValueError:\n", + " print (\"Invalid number!\")\n", + " continue\n", + " print (x + 1)\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " # Execute some function here \n", + "except:\n", + " # If the function throws an exception, the code for this event ia handled here. \n", + "else:\n", + " # Only runs this code if the function suceeeded. \n", + "finally:\n", + " # Always run this code when the try block is finished. \n", + " # This is called try with resources. \n", + " \n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\bbrel\\\\basicpython\\\\docs'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from os import getcwd\n", + "\n", + "getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "George Washington\n", + "John Adams\n", + "Thomas Jefferson\n", + "James Madison\n", + "James Monroe\n" + ] + } + ], + "source": [ + "with open('c:/users/bbrel/basicpython/labs/lab4/data/test.dat') as f:\n", + " for line in f:\n", + " print (line.strip())" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x before function call is: 10\n", + "x inside the function is 11\n", + "x after function call is: 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def funct(y):\n", + " y += 1\n", + " print (\"y inside the function is \",y\n", + " return y\n", + "\n", + "print (\"x before function call is: \",x)\n", + "funct(x)\n", + "print (\"x after function call is: \",x)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "This is known as scope, or alternatively lexical scope. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for num in nums:\n", + " y = 20\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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/Untitled7.ipynb b/docs/Untitled7.ipynb new file mode 100644 index 0000000..dcded24 --- /dev/null +++ b/docs/Untitled7.ipynb @@ -0,0 +1,296 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Review of yesterday\n", + "\n", + "1. Everything in python is an object. \n", + "2. Virtual Machibe technology\n", + "3. High level code -> bytecode -> machine language. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 3 0 LOAD_FAST 0 (x)\n", + " 2 LOAD_CONST 1 (2)\n", + " 4 BINARY_POWER\n", + " 6 RETURN_VALUE\n" + ] + } + ], + "source": [ + "from dis import dis\n", + "def func1(x: int) -> int:\n", + " return x ** 2\n", + "\n", + "dis (func1)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Object types in Python\n", + " a. Numbers\n", + " 1. Integer\n", + " 2. Float\n", + " 3. Complex\n", + " 4 Hexadecimal\n", + " 5. Octal\n", + " 6. Binary\n", + " b. Boolean\n", + " c. Strings\n", + " 1. Strings have methods\n", + " a. split()\n", + " b. lower()\n", + " c upper()\n", + " d. strip()\n", + " 2. We can slice strinngs\n", + " a. We use the [] (index) operator.\n", + " b. [first:last-1:skip]\n", + " c. String operators. \n", + " 1. + -> concatenation operator\n", + " 2. * -> repeater operator\n", + " d. Strings are iterable objects\n", + " e. Strings are immutable.\n", + " 3. Lists \n", + " a. Uses the same index operator. \n", + " b. Same slicing syntax as strings. \n", + " c. Lists are iterable objects. \n", + " d. Lists are mutable. \n", + " e. List methods\n", + " 1. append()\n", + " 2. pop()\n", + " 3. sort()\n", + " 4. reverse()\n", + " \n", + " f. List operators. \n", + " 1. + -> concatenation\n", + " 2. * -> repeater\n", + " \n", + " 4. Tuple. \n", + " a. Tuples are like lists, but, tuples are immutable. \n", + " b. Tuples have same slicing index as strings and lists. \n", + " c. Cannot change the ruple once created. \n", + " d. Functions that work on tuples include min(), max(), len()\n", + " \n", + " \n", + " 5. Sets\n", + " a. Sets have unique values. \n", + " \n", + " 6. Dictionaries\n", + " a. Dictionaries are a collection of key/value pairs. \n", + " b. We use these when we want to do a lot of lookups on the data. \n", + " c. Dictioinaries have methods such as:\n", + " 1. keys() -> gives a list of the keys in the dictionary. \n", + " 2. values() -> gives a list of the values in the dictionary. \n", + " 3. items() -> gives a tuple of both keys and values. \n", + " \n", + " 7. Built-in python functions. \n", + " a. pribt()\n", + " b. input()\n", + " c. len()\n", + " d. zip()\n", + " e. enumerate()\n", + " f. int()\n", + " g. str()\n", + " h. list()\n", + " i. tuple()\n", + " j. dict()\n", + " \n", + " \n", + " 8. File I/O\n", + " a. Used the open() function to access the file. \n", + " b. File objects are iterable. \n", + " c. We use the close () method from the file object to close the file. \n", + " 9. Exceptions\n", + " Use the try/catch/else/finally blocks. \n", + " try:\n", + " \n", + " except : \n", + " \n", + " else:\n", + " \n", + " finally:\n", + " < some code that always runs at the end of the block.>\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:\\\\Users\\\\bbrel\\\\basicpython\\\\docs'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from os import getcwd\n", + "getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mteam_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a team name: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 14\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mteam_name\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mteam_dict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid name! \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mteam_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 858\u001b[0m \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 859\u001b[0m )\n\u001b[1;32m--> 860\u001b[1;33m return self._input_request(str(prompt),\n\u001b[0m\u001b[0;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 902\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 903\u001b[0m \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 905\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], + "source": [ + "try:\n", + " f = open ('c:/users/bbrel/basicpython/labs/lab4/data/albb.salaries.2003.formatted.csv')\n", + "except FileNotFoundError:\n", + " print (\"Invalid file!\")\n", + " quit()\n", + " \n", + "team_dict = {}\n", + "try:\n", + " for line in f:\n", + " if len(line) == 4:\n", + " break\n", + " data = line.strip().split(',')\n", + " try:\n", + " if data[0] not in team_dict:\n", + " team_dict[data[0]] = int(data[3])\n", + " else:\n", + " team_dict[data[0]] += int(data[3])\n", + " except ValueError:\n", + " continue\n", + "except IOError:\n", + " print (\"I/O Error raised!\")\n", + " quit()\n", + "finally:\n", + " f.close()\n", + " \n", + "team_name = input(\"Please enter a team name: \")\n", + "#if team_name not in team_dict:\n", + "# print (\"Invalid name! \", team_name)\n", + "#else:\n", + "# print (team_dict[team_name])\n", + "\n", + "try:\n", + " print (team_dict[team_name])\n", + "except KeyError:\n", + " print (\"Invalid team name\", team_name)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter name to search for: Cristobal\n", + "No match found!\n" + ] + } + ], + "source": [ + "import re\n", + "name = input(\"Enter name to search for: \")\n", + "data = \"12345,Braun Brelin, 1234 Main St., Anytown, USA\"\n", + "RE = \".*\" + name + \".*\"\n", + "if re.match(RE,data):\n", + " print (\"Found a match!\")\n", + "else:\n", + " print (\"No match found!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fouhd a match!\n", + "Braun Brelin\n", + "Employee ID n= 12345 Nane = Braun Brelin\n" + ] + } + ], + "source": [ + "data = \"12345Braun Brelin\"\n", + "match_output = re.match(\"(\\d+)(\\w+ \\w+)\",data)\n", + "if match_output:\n", + " print (\"Fouhd a match!\")\n", + " print (match_output.group(2))\n", + "else:\n", + " print (\"No match found!\") \n", + "emp_id = match_output.group(1)\n", + "name = match_output.group(2)\n", + "print (\"Employee ID n= \", emp_id, \"Nane = \",name)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "abc123abc123abc\n", + "\n", + "\"abc.*abc\"\n", + "\n", + "By default, all regular expression matches are greedy.\n", + "This means, that the RE engine will match on the largest possible size. \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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/foo.txt b/docs/foo.txt index e69de29..d620806 100644 --- a/docs/foo.txt +++ b/docs/foo.txt @@ -0,0 +1,3 @@ +12345,Braun,Brelin +12346,Joe,Blow +12347,James Brown \ No newline at end of file diff --git a/docs/graphics b/docs/graphics new file mode 120000 index 0000000..a8a47bb --- /dev/null +++ b/docs/graphics @@ -0,0 +1 @@ +../graphics/ \ No newline at end of file diff --git a/docs/notes-7.4.2022 b/docs/notes-7.4.2022 new file mode 100644 index 0000000..5df84cf --- /dev/null +++ b/docs/notes-7.4.2022 @@ -0,0 +1,2121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4c512f51", + "metadata": {}, + "source": [ + "print ('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e5699b81", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], + "source": [ + "x = 10\n", + "x = x + 1\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "f7e78254", + "metadata": {}, + "source": [ + "# This is a header\n", + "## This is a sub header\n", + "### Third header.\n", + "\n", + "**This is bolded**
\n", + "*This is italicized*" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "97ad1400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], + "source": [ + "import this" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a4389c89", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello from Python\n" + ] + } + ], + "source": [ + "print ('Hello from Python')" + ] + }, + { + "cell_type": "markdown", + "id": "2ba69ab4", + "metadata": {}, + "source": [ + "# Data types in Python\n", + "\n", + "1. Numbers.\n", + " A. Integers\n", + " B. Fixed Point\n", + " C. Complex Numbers. (a + bi)\n", + " D. Octal Numbers. \n", + " E. Hexadecimal Numbers. \n", + " 2. Strings.\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d903c5d3", + "metadata": {}, + "outputs": [], + "source": [ + "x = 10" + ] + }, + { + "cell_type": "markdown", + "id": "8770c190", + "metadata": {}, + "source": [ + "Object reference ID ----> The object\n", + "x = 10 This is an object. Specifically, it's a numeric object. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a201e3ab", + "metadata": {}, + "outputs": [], + "source": [ + "y = 20.0\n", + "c = 10j\n", + "o = 010\n", + "x = 0xff" + ] + }, + { + "cell_type": "markdown", + "id": "9a4d56ea", + "metadata": {}, + "source": [ + "** -> Exponentiation operator.\n", + "*\n", + "/ -> Fixed point division\n", + "// -> Integer division\n", + "% -> Find the remainder.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "8a7b3006", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.3333333333333335\n", + "3.0\n", + "-3.3333333333333335\n", + "-4.0\n" + ] + } + ], + "source": [ + "x = 10.0\n", + "y = 3\n", + "print (x / y)\n", + "print (x // y) # Floor division\n", + "y = -3\n", + "print (x / y)\n", + "print (x // y) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "25e8596e", + "metadata": {}, + "outputs": [], + "source": [ + "x = True\n", + "y = False\n", + "a = 5\n", + "if a > 5:\n", + " print ('A is bigger than 5')\n", + " print ('foo')\n", + "print ('Not in the block anymore')" + ] + }, + { + "cell_type": "markdown", + "id": "a03b20b0", + "metadata": {}, + "source": [ + "if (a > 5) {\n", + " print ('A is bigger than 5');\n", + "}\n", + "\n", + "if (a>5) {printf(\"A is bigger than 5\\n\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "386f83fb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello\n" + ] + } + ], + "source": [ + "print ('Hello')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0c7227ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 4\n", + "4\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "print (x)" + ] + }, + { + "cell_type": "markdown", + "id": "2b6283b4", + "metadata": {}, + "source": [ + "Every type of input comes in as a string, not a number. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e7e39b0c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number 7\n", + "11\n", + "11.0\n" + ] + } + ], + "source": [ + "x = input('Please enter a number ')\n", + "y = int(x) # Int is a built in function. It converts datatypes to numeric (integer) objects. \n", + "print (y + 4)\n", + "z = float(x)\n", + "print (z + 4)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5d5c95a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/1375056562.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'J'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = 'Hello World'\n", + "t = \"Hello World\"\n", + "z = \"\"\" This is a multi line string. \n", + " This is line 2 of the string.\n", + " And so on...\n", + " \"\"\"\n", + "# This is a single line comment. \n", + "# Strings are immutable.\n", + "\n", + "print (s)\n", + "s [0] = 'J'\n" + ] + }, + { + "cell_type": "markdown", + "id": "4c47351a", + "metadata": {}, + "source": [ + "These are examples of string slicing. " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "09b6b757", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "H\n", + "e\n", + "Hello\n", + "Hlo\n", + "drWolH\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s[0])\n", + "print (s[1]) #This concept is called a 'slice'\n", + "print (s[0:5]) # Start at the beginning and go to the end - 1 character.\n", + "print (s[0:5:2]) # The last value is a stepper value. \n", + "print (s[::-1])" + ] + }, + { + "cell_type": "markdown", + "id": "56610e95", + "metadata": {}, + "source": [ + "What is an object?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "38095079", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "417fa3ff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "HELLO WORLD\n", + "hello world\n", + "11\n", + "11\n" + ] + } + ], + "source": [ + "print (s)\n", + "print (s.upper())\n", + "print (s.lower())\n", + "print (len(s))\n", + "print (s.__len__())" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "19038cce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is less than 10\n" + ] + } + ], + "source": [ + "x = 9\n", + "l_counter = 0\n", + "if x == 10:\n", + " print ('x is 10')\n", + "elif x < 10:\n", + " print ('x is less than 10')\n", + "else:\n", + " print (\"x is greater than 10\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6d67acf6", + "metadata": {}, + "source": [ + "A string is an example of an *iterable* object. \n", + "We can use a looping construct to iterate over the object. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "ce320277", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for i in range(len(s)):\n", + " print (s[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "e5208dad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "print (list(range(10)))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "14bc20de", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "H\n", + "e\n", + "l\n", + "l\n", + "o\n", + " \n", + "W\n", + "o\n", + "r\n", + "l\n", + "d\n" + ] + } + ], + "source": [ + "for elem in s:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "1b621a14", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World \n", + "Hello World Hello World Hello World Hello World Hello World \n" + ] + } + ], + "source": [ + "s1 = \"Hello \"\n", + "s2 = \"World \"\n", + "s = s1 + s2 # + is string concatenation for strings. \n", + "print (s)\n", + "print (s * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "225da735", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (s)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "187e7215", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Braun Brelin', ' 1234 Main Street', ' Anytown', ' USA']\n" + ] + } + ], + "source": [ + "s = \"Braun Brelin, 1234 Main Street, Anytown, USA\"\n", + "print (s.split(','))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c5d8823c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class str in module builtins:\n", + "\n", + "class str(object)\n", + " | str(object='') -> str\n", + " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", + " | \n", + " | Create a new string object from the given object. If encoding or\n", + " | errors is specified, then the object must expose a data buffer\n", + " | that will be decoded using the given encoding and error handler.\n", + " | Otherwise, returns the result of object.__str__() (if defined)\n", + " | or repr(object).\n", + " | encoding defaults to sys.getdefaultencoding().\n", + " | errors defaults to 'strict'.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __format__(self, format_spec, /)\n", + " | Return a formatted version of the string as described by format_spec.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(self, key, /)\n", + " | Return self[key].\n", + " | \n", + " | __getnewargs__(...)\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __hash__(self, /)\n", + " | Return hash(self).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self int\n", + " | \n", + " | Return the number of non-overlapping occurrences of substring sub in\n", + " | string S[start:end]. Optional arguments start and end are\n", + " | interpreted as in slice notation.\n", + " | \n", + " | encode(self, /, encoding='utf-8', errors='strict')\n", + " | Encode the string using the codec registered for encoding.\n", + " | \n", + " | encoding\n", + " | The encoding in which to encode the string.\n", + " | errors\n", + " | The error handling scheme to use for encoding errors.\n", + " | The default is 'strict' meaning that encoding errors raise a\n", + " | UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", + " | 'xmlcharrefreplace' as well as any other name registered with\n", + " | codecs.register_error that can handle UnicodeEncodeErrors.\n", + " | \n", + " | endswith(...)\n", + " | S.endswith(suffix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S ends with the specified suffix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | suffix can also be a tuple of strings to try.\n", + " | \n", + " | expandtabs(self, /, tabsize=8)\n", + " | Return a copy where all tab characters are expanded using spaces.\n", + " | \n", + " | If tabsize is not given, a tab size of 8 characters is assumed.\n", + " | \n", + " | find(...)\n", + " | S.find(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | format(...)\n", + " | S.format(*args, **kwargs) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from args and kwargs.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | format_map(...)\n", + " | S.format_map(mapping) -> str\n", + " | \n", + " | Return a formatted version of S, using substitutions from mapping.\n", + " | The substitutions are identified by braces ('{' and '}').\n", + " | \n", + " | index(...)\n", + " | S.index(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the lowest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | isalnum(self, /)\n", + " | Return True if the string is an alpha-numeric string, False otherwise.\n", + " | \n", + " | A string is alpha-numeric if all characters in the string are alpha-numeric and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isalpha(self, /)\n", + " | Return True if the string is an alphabetic string, False otherwise.\n", + " | \n", + " | A string is alphabetic if all characters in the string are alphabetic and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isascii(self, /)\n", + " | Return True if all characters in the string are ASCII, False otherwise.\n", + " | \n", + " | ASCII characters have code points in the range U+0000-U+007F.\n", + " | Empty string is ASCII too.\n", + " | \n", + " | isdecimal(self, /)\n", + " | Return True if the string is a decimal string, False otherwise.\n", + " | \n", + " | A string is a decimal string if all characters in the string are decimal and\n", + " | there is at least one character in the string.\n", + " | \n", + " | isdigit(self, /)\n", + " | Return True if the string is a digit string, False otherwise.\n", + " | \n", + " | A string is a digit string if all characters in the string are digits and there\n", + " | is at least one character in the string.\n", + " | \n", + " | isidentifier(self, /)\n", + " | Return True if the string is a valid Python identifier, False otherwise.\n", + " | \n", + " | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n", + " | such as \"def\" or \"class\".\n", + " | \n", + " | islower(self, /)\n", + " | Return True if the string is a lowercase string, False otherwise.\n", + " | \n", + " | A string is lowercase if all cased characters in the string are lowercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | isnumeric(self, /)\n", + " | Return True if the string is a numeric string, False otherwise.\n", + " | \n", + " | A string is numeric if all characters in the string are numeric and there is at\n", + " | least one character in the string.\n", + " | \n", + " | isprintable(self, /)\n", + " | Return True if the string is printable, False otherwise.\n", + " | \n", + " | A string is printable if all of its characters are considered printable in\n", + " | repr() or if it is empty.\n", + " | \n", + " | isspace(self, /)\n", + " | Return True if the string is a whitespace string, False otherwise.\n", + " | \n", + " | A string is whitespace if all characters in the string are whitespace and there\n", + " | is at least one character in the string.\n", + " | \n", + " | istitle(self, /)\n", + " | Return True if the string is a title-cased string, False otherwise.\n", + " | \n", + " | In a title-cased string, upper- and title-case characters may only\n", + " | follow uncased characters and lowercase characters only cased ones.\n", + " | \n", + " | isupper(self, /)\n", + " | Return True if the string is an uppercase string, False otherwise.\n", + " | \n", + " | A string is uppercase if all cased characters in the string are uppercase and\n", + " | there is at least one cased character in the string.\n", + " | \n", + " | join(self, iterable, /)\n", + " | Concatenate any number of strings.\n", + " | \n", + " | The string whose method is called is inserted in between each given string.\n", + " | The result is returned as a new string.\n", + " | \n", + " | Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n", + " | \n", + " | ljust(self, width, fillchar=' ', /)\n", + " | Return a left-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | lower(self, /)\n", + " | Return a copy of the string converted to lowercase.\n", + " | \n", + " | lstrip(self, chars=None, /)\n", + " | Return a copy of the string with leading whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | partition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string. If the separator is found,\n", + " | returns a 3-tuple containing the part before the separator, the separator\n", + " | itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing the original string\n", + " | and two empty strings.\n", + " | \n", + " | removeprefix(self, prefix, /)\n", + " | Return a str with the given prefix string removed if present.\n", + " | \n", + " | If the string starts with the prefix string, return string[len(prefix):].\n", + " | Otherwise, return a copy of the original string.\n", + " | \n", + " | removesuffix(self, suffix, /)\n", + " | Return a str with the given suffix string removed if present.\n", + " | \n", + " | If the string ends with the suffix string and that suffix is not empty,\n", + " | return string[:-len(suffix)]. Otherwise, return a copy of the original\n", + " | string.\n", + " | \n", + " | replace(self, old, new, count=-1, /)\n", + " | Return a copy with all occurrences of substring old replaced by new.\n", + " | \n", + " | count\n", + " | Maximum number of occurrences to replace.\n", + " | -1 (the default value) means replace all occurrences.\n", + " | \n", + " | If the optional argument count is given, only the first count occurrences are\n", + " | replaced.\n", + " | \n", + " | rfind(...)\n", + " | S.rfind(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Return -1 on failure.\n", + " | \n", + " | rindex(...)\n", + " | S.rindex(sub[, start[, end]]) -> int\n", + " | \n", + " | Return the highest index in S where substring sub is found,\n", + " | such that sub is contained within S[start:end]. Optional\n", + " | arguments start and end are interpreted as in slice notation.\n", + " | \n", + " | Raises ValueError when the substring is not found.\n", + " | \n", + " | rjust(self, width, fillchar=' ', /)\n", + " | Return a right-justified string of length width.\n", + " | \n", + " | Padding is done using the specified fill character (default is a space).\n", + " | \n", + " | rpartition(self, sep, /)\n", + " | Partition the string into three parts using the given separator.\n", + " | \n", + " | This will search for the separator in the string, starting at the end. If\n", + " | the separator is found, returns a 3-tuple containing the part before the\n", + " | separator, the separator itself, and the part after it.\n", + " | \n", + " | If the separator is not found, returns a 3-tuple containing two empty strings\n", + " | and the original string.\n", + " | \n", + " | rsplit(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | Splits are done starting at the end of the string and working to the front.\n", + " | \n", + " | rstrip(self, chars=None, /)\n", + " | Return a copy of the string with trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | split(self, /, sep=None, maxsplit=-1)\n", + " | Return a list of the words in the string, using sep as the delimiter string.\n", + " | \n", + " | sep\n", + " | The delimiter according which to split the string.\n", + " | None (the default value) means split according to any whitespace,\n", + " | and discard empty strings from the result.\n", + " | maxsplit\n", + " | Maximum number of splits to do.\n", + " | -1 (the default value) means no limit.\n", + " | \n", + " | splitlines(self, /, keepends=False)\n", + " | Return a list of the lines in the string, breaking at line boundaries.\n", + " | \n", + " | Line breaks are not included in the resulting list unless keepends is given and\n", + " | true.\n", + " | \n", + " | startswith(...)\n", + " | S.startswith(prefix[, start[, end]]) -> bool\n", + " | \n", + " | Return True if S starts with the specified prefix, False otherwise.\n", + " | With optional start, test S beginning at that position.\n", + " | With optional end, stop comparing S at that position.\n", + " | prefix can also be a tuple of strings to try.\n", + " | \n", + " | strip(self, chars=None, /)\n", + " | Return a copy of the string with leading and trailing whitespace removed.\n", + " | \n", + " | If chars is given and not None, remove characters in chars instead.\n", + " | \n", + " | swapcase(self, /)\n", + " | Convert uppercase characters to lowercase and lowercase characters to uppercase.\n", + " | \n", + " | title(self, /)\n", + " | Return a version of the string where each word is titlecased.\n", + " | \n", + " | More specifically, words start with uppercased characters and all remaining\n", + " | cased characters have lower case.\n", + " | \n", + " | translate(self, table, /)\n", + " | Replace each character in the string using the given translation table.\n", + " | \n", + " | table\n", + " | Translation table, which must be a mapping of Unicode ordinals to\n", + " | Unicode ordinals, strings, or None.\n", + " | \n", + " | The table must implement lookup/indexing via __getitem__, for instance a\n", + " | dictionary or list. If this operation raises LookupError, the character is\n", + " | left untouched. Characters mapped to None are deleted.\n", + " | \n", + " | upper(self, /)\n", + " | Return a copy of the string converted to uppercase.\n", + " | \n", + " | zfill(self, width, /)\n", + " | Pad a numeric string with zeros on the left, to fill a field of the given width.\n", + " | \n", + " | The string is never truncated.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | maketrans(...)\n", + " | Return a translation table usable for str.translate().\n", + " | \n", + " | If there is only one argument, it must be a dictionary mapping Unicode\n", + " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", + " | Character keys will be then converted to ordinals.\n", + " | If there are two arguments, they must be strings of equal length, and\n", + " | in the resulting dictionary, each character in x will be mapped to the\n", + " | character at the same position in y. If there is a third argument, it\n", + " | must be a string, whose characters will be mapped to None in the result.\n", + "\n" + ] + } + ], + "source": [ + "help(str)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "113cc64e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'capitalize',\n", + " 'casefold',\n", + " 'center',\n", + " 'count',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'format_map',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isascii',\n", + " 'isdecimal',\n", + " 'isdigit',\n", + " 'isidentifier',\n", + " 'islower',\n", + " 'isnumeric',\n", + " 'isprintable',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'maketrans',\n", + " 'partition',\n", + " 'removeprefix',\n", + " 'removesuffix',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (str)" + ] + }, + { + "cell_type": "markdown", + "id": "957d378a", + "metadata": {}, + "source": [ + "The List data type. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "e9f462b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "source": [ + "s = \"1\"\n", + "l = [1,2,3,4,5,6,7,8,9,10] # Is a list, an iterable object?\n", + "print (l)\n", + "for elem in l:\n", + " print (elem)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "e641abe4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir (list)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "572cb928", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l\n", + "l.append(11)\n", + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "daf174d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "[1, 2, 3, 4]\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "9\n" + ] + } + ], + "source": [ + "print (l[0])\n", + "print (l[0:4])\n", + "#print (l[::-1])\n", + "print (l)\n", + "print (l.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "051158ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']\n", + "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "print (len (l))\n", + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = l1 + l2\n", + "print (l3)\n", + "print (l1 * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "61b6c9d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = []\n", + "\n", + "for num in nums:\n", + " squares.append(num ** 2)\n", + "print (squares)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "3f6e10c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "b4bcd51d", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "dis() got an unexpected keyword argument 'squares'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/266041033.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m print (dis(\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0msquares\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m**\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mnum\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;33m]\u001b[0m \u001b[1;31m# List comprehension\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m ))\n", + "\u001b[1;31mTypeError\u001b[0m: dis() got an unexpected keyword argument 'squares'" + ] + } + ], + "source": [ + "from dis import dis\n", + "\n", + "nums = [1,2,3,4,5,6,7,8,9,10]\n", + "squares = [ num ** 2 for num in nums if num % 2 == 0 ] # List comprehension\n", + "print (squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "410cd78f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['FEE', 'FI', 'FO', 'FUM']\n" + ] + } + ], + "source": [ + "jack_list = [\"Fee\",\"Fi\",\"Fo\",\"Fum\"]\n", + "jack_upper_list = [ word.upper() for word in jack_list ]\n", + "print (jack_upper_list)" + ] + }, + { + "cell_type": "markdown", + "id": "1d20fdbc", + "metadata": {}, + "source": [ + "Tuples. Tuples are similar to lists, except that, like strings, they are immutable. \n", + "We use the round braces () to specify a tuple.." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "702f7ab3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/4289262067.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = (1,2,3,4,5)\n", + "print (t)\n", + "\n", + "t[0] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "823197e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]\n", + "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]\n" + ] + } + ], + "source": [ + "l1 = [1,2,3,4,5]\n", + "l2 = ['a','b','c','d','e']\n", + "l3 = zip(l1,l2)\n", + "print (list(l3))\n", + "print (list(enumerate(l2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "4b97b974", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "1\n" + ] + } + ], + "source": [ + "print (max(l1))\n", + "print (min(l1))\n", + "# tuple() converts objects into tuples. " + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "54fd10d5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(10, 12)\n", + "Fee fi fo fum\n" + ] + } + ], + "source": [ + "x = 5\n", + "y = 6\n", + "\n", + "t = (x+5,y+6) #tuple packing. \n", + "(x+5,y+6) = t1\n", + "print (t)\n", + "s = \"Fee,fi,fo,fum\"\n", + "(first,second,third,fourth) = s.split(',')\n", + "print (first,second,third,fourth)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "ec7c3800", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Graduate\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n", + "('The Graduate', 'Dustin Hoffman', 'Anne Bancroft', 1967)\n" + ] + }, + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/3873200294.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0mmovie_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mt1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Papillon\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t = ('The Graduate', 'Dustin Hoffman','Anne Bancroft',1967)\n", + "\n", + "\n", + "'''\n", + "movie_name = t[0]\n", + "actor = t[1]\n", + "actress = t[2]\n", + "date = t[3]\n", + "'''\n", + "movie_name, actor, actress, date = t # This is tuple unpacking.\n", + "print (movie_name)\n", + "t1 = (movie_name,actor,actress,date) # Tuple packing.\n", + "print (t1)\n", + "movie_name = \"Papillon\"\n", + "print (t1)\n", + "t1[0] = \"Papillon\" \n" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "3c68c436", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'orange', 'apple', 'banana', 'grapefruit'}\n" + ] + } + ], + "source": [ + "list1 = ['apple','banana','orange','apple','banana','grapefruit','apple']\n", + "set1 = set(list1)\n", + "print (set1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "66ef816a", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'pop from an empty set'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_23960/2821507018.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mset1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;31m#print (set1.pop())\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'" + ] + } + ], + "source": [ + "print (set1.pop())\n", + "#print (set1.pop())" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "77949490", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "\n", + "\n" + ] + } + ], + "source": [ + "x = 1\n", + "print (str(x))\n", + "y = str(x)\n", + "print (type(x))\n", + "print (type(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c723273e", + "metadata": {}, + "outputs": [], + "source": [ + "from math import sqrt,atan\n", + "\n", + "sqrt()\n", + "atan()\n" + ] + }, + { + "cell_type": "markdown", + "id": "88db836e", + "metadata": {}, + "source": [ + "Dictionary. Stores key/value pairs in Python...\n" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "82ed62a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "value1\n" + ] + } + ], + "source": [ + "d = {'key1':'value1','key2':'value2','key3':'value3'}\n", + "\n", + "print (d['key1'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a53af4c0", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "6e7c1277", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_values(['value1', 'value2', 'value3'])\n" + ] + } + ], + "source": [ + "print(d.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "2278b952", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['key1', 'key2', 'key3']\n" + ] + } + ], + "source": [ + "print (list(d.keys()))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37cc6ae4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "4a731d45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])\n" + ] + } + ], + "source": [ + "print (d.items())" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "06f6658d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "6fca4445", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No such color\n" + ] + } + ], + "source": [ + "colors = {'Red':1,'Green':2,'Blue':3}\n", + "if 'Yellow' in colors:\n", + " print (colors['Yellow'])\n", + "else:\n", + " print ('No such color') \n" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "69358712", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__class_getitem__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__ior__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__or__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__ror__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'clear',\n", + " 'copy',\n", + " 'fromkeys',\n", + " 'get',\n", + " 'items',\n", + " 'keys',\n", + " 'pop',\n", + " 'popitem',\n", + " 'setdefault',\n", + " 'update',\n", + " 'values']" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(colors)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc366635", + "metadata": {}, + "outputs": [], + "source": [ + "def myfunc():\n", + " a = 1\n", + " b = 0\n", + " c = a + b\n", + " return c\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "e9764c55", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "X before calling myfunc = 10\n", + "In the function, x = 11\n", + "X after calling myfunc = 10\n" + ] + } + ], + "source": [ + "x = 10\n", + "def myfunc(x):\n", + " # x = x + 1 #These two statements are identical. \n", + " x += 1\n", + " print (\"In the function, x = \",x)\n", + " \n", + "print (\"X before calling myfunc = \",x)\n", + "myfunc(x)\n", + "print (\"X after calling myfunc = \",x)\n" + ] + }, + { + "cell_type": "markdown", + "id": "1894c841", + "metadata": {}, + "source": [ + "What happens in Las Vegas, stays in Las Vegas. \n", + "Scoping \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4655084", + "metadata": {}, + "outputs": [], + "source": [ + "myfunc(a,b=0)" + ] + } + ], + "metadata": { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/.ipynb_checkpoints/Untitled1-checkpoint.ipynb b/examples/.ipynb_checkpoints/Untitled1-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/examples/.ipynb_checkpoints/Untitled1-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/Untitled1.ipynb b/examples/Untitled1.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/examples/Untitled1.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/europop1.py b/examples/europop1.py new file mode 100755 index 0000000..d23eabc --- /dev/null +++ b/examples/europop1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import sys + +try: + f = open ("Eur.pop.XL.zip") +except IOError: + print ("ERROR: Unable to open file!") + sys.exit() + +countrydata= {} + +# Skip the first three lines. They're header information that we don't need. + +for i in range (0,3): + try: + f.readline() + except IOError: + print "I got an IOError!" + f.close() + sys.exit() + + + +# Now start reading the data from the file. Build a list containing the tuple +# (countryname,population delta) + + +countrydata = {data[0],data[7] - data [1] for data in line.strip().split(',') if 'Sources' not in line} + +country_dictionary = {country_data[0]:country_data[7] -country_data[1] for country_data in .strip().split(",") in f if country_ +for record in f: + countrylist = record.strip().split(",") + if len(countrylist[0]) == 0 or "Sources:" in countrylist[0]: + continue + countrydata[countrylist[0]] = int(countrylist[7]) - int (countrylist[1]) + # countrydata.append((countrylist[0],int(countrylist[7]) - int(countrylist[1]))) + +usercountry = raw_input("Please enter the country: ") +print "For country ",usercountry," the population delta is: ",countrydata[usercountry] diff --git a/examples/example1.py b/examples/example1.py new file mode 100755 index 0000000..d404ae1 --- /dev/null +++ b/examples/example1.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 + +print ("This is a python program") diff --git a/examples/xml/food.xml b/examples/xml/food.xml new file mode 100644 index 0000000..628b3d6 --- /dev/null +++ b/examples/xml/food.xml @@ -0,0 +1,42 @@ + + + Belgian Waffles + $5.95 + + Two of our famous Belgian Waffles with plenty of real maple syrup + + 650 + + + Strawberry Belgian Waffles + $7.95 + + Light Belgian waffles covered with strawberries and whipped cream + + 900 + + + Berry-Berry Belgian Waffles + $8.95 + + Light Belgian waffles covered with an assortment of fresh berries and whipped cream + + 900 + + + French Toast + $4.50 + + Thick slices made from our homemade sourdough bread + + 600 + + + Homestyle Breakfast + $6.95 + + Two eggs, bacon or sausage, toast, and our ever-popular hash browns + + 950 + + diff --git a/labs/lab1/Lab1 Notes.odt b/labs/lab1/Lab1 Notes.odt index 73e001c..4e7b26a 100644 Binary files a/labs/lab1/Lab1 Notes.odt and b/labs/lab1/Lab1 Notes.odt differ diff --git a/labs/lab1/Lab1 Notes.pdf b/labs/lab1/Lab1 Notes.pdf new file mode 100644 index 0000000..2b0a235 Binary files /dev/null and b/labs/lab1/Lab1 Notes.pdf differ diff --git a/labs/lab2/Lab2 Notes.pdf b/labs/lab2/Lab2 Notes.pdf new file mode 100644 index 0000000..a27981f Binary files /dev/null and b/labs/lab2/Lab2 Notes.pdf differ diff --git a/labs/lab2/lab2.py.noscript b/labs/lab2/lab2.py.noscript new file mode 100755 index 0000000..fbc5cd2 --- /dev/null +++ b/labs/lab2/lab2.py.noscript @@ -0,0 +1,75 @@ +Sentence = ''' XVI + + +Book One: 1805 + + + +CHAPTER I + +WELL, PRINCE, so Genoa and Lucca are now +just family estates of the Buonapartes. But I +warn you, if you don't tell me that this means +war, if you still try to defend the infamies and +horrors perpetrated by that Antichrist I real- +ly believe he is Antichrist I will have nothing +more to do with you and you are no longer my +friend, no longer my 'faithful slave,' as you +call yourself! But how do you do? I see I have +frightened you sit down and tell me all the +news." + +It was in July, 1805, and the speaker was the +well-known Anna Pdvlovna Sch^rer, maid of +honor and favorite of the Empress Marya Fe- +dorovna. With these words she greeted Prince +Vasili Kurdgin, a man of high rank and impor- +tance, who was the first to arrive at her recep- +tion. Anna Pdvlovna had had a cough for some +days. She was, as she said, suffering from la +grippe; grippe being then a new word in St. +Petersburg, used only by the elite. + +All her invitations without exception, writ- +ten in French, and delivered by a scarlet-liver- +ied footman that morning, ran as follows: + + "If you have nothing better to do, Count [or + Prince], and if the prospect of spending an + evening with a poor invalid is not too terrible, + I shall be very charmed to see you tonight be- + tween 7 and 10 Annette Sch^rer." + +''' + + +# Solution for 1A + + +length = len(Sentence.split()) +print ("Total number of words = %d\n" % (length)) + +# Solution for 1B + +wordtocount = input ("Please enter the word you want to count: ") +counter = Sentence.count(wordtocount) +print (Sentence.count(wordtocount)) + +# Solution for 1C +insertstring = input ("Please enter the string you want to insert") + +position = int(input("Please enter the insertion point: ")) + +newSentence = Sentence[0:position] + insertstring+ Sentence[position::] +print (newSentence) + + +# Solution for 1D + +print (Sentence[::-1]) + +# Solution for 1E + +nth = int(input("Please specify nth position: ")) +foo = [word[1].upper() if word[0] % nth == 0 else word[1] for word in enumerate(Sentence.split())] +print (" ".join(foo)) diff --git a/labs/lab3/Lab3 Notes.pdf b/labs/lab3/Lab3 Notes.pdf new file mode 100644 index 0000000..bf106d2 Binary files /dev/null and b/labs/lab3/Lab3 Notes.pdf differ diff --git a/labs/lab3/lab3answers.py b/labs/lab3/lab3answers.py new file mode 100755 index 0000000..8165d63 --- /dev/null +++ b/labs/lab3/lab3answers.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import math +cartesian = [(1,3),(4,4),(2,6),(-5,2),(2,1),(1,3),(2,6)] +polar = [] + +def converttopolar(t): + + x = t[0] + y = t[1] + return (math.sqrt(x **2 + y **2),math.atan(y/x)) + +def calculateperfect(n): + + sum = 0 + for x in range(1,n): + if n % x == 0: + sum += x + if sum == x: + return x + else: + return -1 + + +# Calculate the polar coordinates from the given cartesian coordinate list. +# Remove duplicates from the cartesian list. + +cartesian = list(set(cartesian)) + + +for coord in cartesian: + polar.append(converttopolar(coord)) + +print (polar) + +# Create a list of the first 10,000 integers and calculate which of them are perfect numbers. +possibleperfects = [] +for i in range(10): + if (calculateperfect(i) == i): + possibleperfects.append(i) + +print (possibleperfects) + + + + + +n = int(input("Enter number of rows: ")) +a=[] +for i in range(n): + a.append([]) + a[i].append(1) + for j in range(1,i): + a[i].append(a[i-1][j-1]+a[i-1][j]) + if n != 0: + a[i].append(1) + +print (a) diff --git a/labs/lab4/Lab4 Notes.odt b/labs/lab4/Lab4 Notes.odt index 1ac1420..e82222d 100644 Binary files a/labs/lab4/Lab4 Notes.odt and b/labs/lab4/Lab4 Notes.odt differ diff --git a/labs/lab4/Lab4 Notes.pdf b/labs/lab4/Lab4 Notes.pdf new file mode 100644 index 0000000..264e10b Binary files /dev/null and b/labs/lab4/Lab4 Notes.pdf differ diff --git a/labs/lab4/data/test.dat b/labs/lab4/data/test.dat new file mode 100644 index 0000000..1383121 --- /dev/null +++ b/labs/lab4/data/test.dat @@ -0,0 +1,5 @@ +George Washington +John Adams +Thomas Jefferson +James Madison +James Monroe diff --git a/labs/lab4/data/test.py b/labs/lab4/data/test.py index a9398fc..7809bc7 100755 --- a/labs/lab4/data/test.py +++ b/labs/lab4/data/test.py @@ -1,6 +1,26 @@ #!/usr/bin/python3 -mylist = [1,2,3] -print (type(mylist)) -if not type(mylist) is list: - print ("Not a list") +import sys + +try: + f = open('test.dat') +except IOError: + print ("invalid file") + sys.exit(1) + +try: + for line in f: + try: + x = int(line) + except ValueError: + print ('Invalid number') + continue +except IOError: + print ('IOError found') + sys.exit(1) +finally: + f.close() + + + + diff --git a/labs/lab4/model_solution.py b/labs/lab4/model_solution.py new file mode 100644 index 0000000..8af8141 --- /dev/null +++ b/labs/lab4/model_solution.py @@ -0,0 +1,36 @@ +try: + f = open ('c:/users/bbrel/basicpython/labs/lab4/data/albb.salaries.2003.formatted.csv') +except FileNotFoundError: + print ("Invalid file!") + quit() + +team_dict = {} +try: + for line in f: + if len(line) == 4: + break + data = line.strip().split(',') + try: + if data[0] not in team_dict: + team_dict[data[0]] = int(data[3]) + else: + team_dict[data[0]] += int(data[3]) + except ValueError: + continue +except IOError: + print ("I/O Error raised!") + quit() +finally: + f.close() + +team_name = input("Please enter a team name: ") +#if team_name not in team_dict: +# print ("Invalid name! ", team_name) +#else: +# print (team_dict[team_name]) + +try: + print (team_dict[team_name]) +except KeyError: + print ("Invalid team name", team_name) + \ No newline at end of file diff --git a/labs/lab5/Lab 5 Notes.odt b/labs/lab5/Lab 5 Notes.odt index 4c95a67..102ed75 100644 Binary files a/labs/lab5/Lab 5 Notes.odt and b/labs/lab5/Lab 5 Notes.odt differ diff --git a/labs/lab5/Lab 5 Notes.pdf b/labs/lab5/Lab 5 Notes.pdf index 14cbb9d..3c03f1f 100644 Binary files a/labs/lab5/Lab 5 Notes.pdf and b/labs/lab5/Lab 5 Notes.pdf differ diff --git a/labs/lab5/data/Inventory.csv b/labs/lab5/data/Inventory.csv new file mode 100644 index 0000000..e1110ed --- /dev/null +++ b/labs/lab5/data/Inventory.csv @@ -0,0 +1,7 @@ +Item SKU,Item Name,Item cost,Quantity in stock +2A546,Batman outfit,$50.00,10 +3Z162,Dehydrated Boulders,$25.99,200 +5V261,Earthquake Pills,$15.99,1000 +9C512,Tornado Kit,$250.00,8 +6F612,Anvil,$124.99,2 +2D812,Giant Magnet,$495.99,3 diff --git a/labs/lab5/inventory.py b/labs/lab5/inventory.py new file mode 100644 index 0000000..2e2b675 --- /dev/null +++ b/labs/lab5/inventory.py @@ -0,0 +1,85 @@ + +import csv +import sys + +class InventoryFileException (Exception): + pass + +class InventoryNoKeyFoundException (Exception): + pass + +class Inventory: + def __init__(self,filepath): + self.file = filepath + self.inventory_dictionary = {} + self.readfile() + + + def readfile(self): + line_count = 0 + try: + self.fhandle = open(self.file) + reader = csv.reader(self.fhandle) + for row in reader: + if line_count == 0: + self.headers = row + line_count += 1 + continue + line_count += 1 + self.inventory_dictionary[row[0]] = row[1:] + return True + except FileNotFoundError: + raise InventoryFileException("Error: File not found") + finally: + self.fhandle.close() + return True + + def returnHeaders(self): + return self.headers + + def getCostOfInventoryData(self): + + return self.inventory_dictionary + + def set_itemname(self,SKU,new_name): + try: + self.inventory_dictionary[SKU] = new_name + return True + except KeyError: + raise InventoryNoKeyFoundException("Error: Invalid SKU") + + def set_itemunitcost(self,SKU,new_unit_cost): + try: + self.inventory_dictionary[SKU] = new_unit_cost + return True + except KeyError: + raise InventoryNoKeyFoundException("Error: Invalid SKU") + return False + +def cost_of_inventory_report(data): + + cost_of_inv_data = {} + total = 0 + count = 0 + print("SKU\t\tItem Name\t\tCost Of Inventory") + for (key,value) in data.items(): + count += 1 + cost_of_inventory = float(value[1].lstrip('$')) * int(value[2]) + total += cost_of_inventory + print (f"{key} {value[0]} ${cost_of_inventory}") + + print (f"Number of items processed: {count}") + print (f"Total Cost of Inventory: ${total}") + return + + + +def main(): + filepath = r"C:\Users\User\basicpython\labs\lab5\data\inventory.csv" + filepath = r"C:\Users\User\basicpython\labs\lab5\data\inventory.csv" + inventory = Inventory(filepath) + cost_of_inventory_report(inventory.getCostOfInventoryData()) + +main() + +