From 92352d25a456a0e1989edb8c9aaa0ec03602c35c Mon Sep 17 00:00:00 2001
From: bbrelin
Date: Wed, 3 Apr 2019 13:18:48 +0100
Subject: [PATCH 1/6] Added changes to notebook
---
.ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 +
Untitled.ipynb | 183 +
data/output_example.dat | 2 +-
.../Basic Python-checkpoint.ipynb | 3335 +++++++++++++++--
.../Untitled-checkpoint.ipynb | 6 +
docs/Basic Python.ipynb | 3335 +++++++++++++++--
docs/Untitled.ipynb | 105 +
docs/graphics | 1 +
.../Untitled1-checkpoint.ipynb | 6 +
examples/Untitled1.ipynb | 6 +
examples/europop1.py | 40 +
examples/example1.py | 3 +
examples/xml/food.xml | 42 +
labs/lab1/Lab1 Notes.odt | Bin 20920 -> 41422 bytes
labs/lab1/Lab1 Notes.pdf | Bin 0 -> 294960 bytes
labs/lab2/Lab2 Notes.pdf | Bin 0 -> 267459 bytes
labs/lab2/lab2.py.noscript | 75 +
labs/lab3/Lab3 Notes.pdf | Bin 0 -> 285655 bytes
labs/lab3/lab3answers.py | 58 +
labs/lab4/data/test.dat | 4 +
labs/lab4/data/test.py | 28 +-
21 files changed, 6784 insertions(+), 451 deletions(-)
create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb
create mode 100644 Untitled.ipynb
create mode 100644 docs/.ipynb_checkpoints/Untitled-checkpoint.ipynb
create mode 100644 docs/Untitled.ipynb
create mode 120000 docs/graphics
create mode 100644 examples/.ipynb_checkpoints/Untitled1-checkpoint.ipynb
create mode 100644 examples/Untitled1.ipynb
create mode 100755 examples/europop1.py
create mode 100755 examples/example1.py
create mode 100644 examples/xml/food.xml
create mode 100644 labs/lab1/Lab1 Notes.pdf
create mode 100644 labs/lab2/Lab2 Notes.pdf
create mode 100755 labs/lab2/lab2.py.noscript
create mode 100644 labs/lab3/Lab3 Notes.pdf
create mode 100755 labs/lab3/lab3answers.py
create mode 100644 labs/lab4/data/test.dat
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/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/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",
" | Operator | Operation | Example |
\n",
" | + | Add two numbers | a + b |
\n",
- " | - | Subtract two numbers | a - b |
\n",
+ " | - | Subtract two numbers | <\n",
+ " td> a - b
\n",
" | \\* | Multiply two numbers | a * b |
\n",
" | / | Floating point division | a / b |
\n",
" | // | Integer (Floor) division | a // b |
\n",
- "
| \\*\\* | Exponentiation | a ** 2 |
\n",
+ " | \\*\\*\\* | Exponentiation | a ** 2 |
\n",
" | % | Modulo (Remainder) | a % b |
\n",
"
\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/Basic Python.ipynb b/docs/Basic Python.ipynb
index 8fb5616..625438f 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",
" | Operator | Operation | Example | \n",
" | + | Add two numbers | a + b | \n",
- " | - | Subtract two numbers | a - b | \n",
+ " | - | Subtract two numbers | <\n",
+ " td> a - b \n",
" | \\* | Multiply two numbers | a * b | \n",
" | / | Floating point division | a / b | \n",
" | // | Integer (Floor) division | a // b | \n",
- " | \\*\\* | Exponentiation | a ** 2 | \n",
+ " | \\*\\*\\* | Exponentiation | a ** 2 | \n",
" | % | Modulo (Remainder) | a % b | \n",
" \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/Untitled.ipynb b/docs/Untitled.ipynb
new file mode 100644
index 0000000..04250d5
--- /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",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.6.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/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/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 73e001c0921a5506693e8464698689cabdcef82a..4e7b26a88826a3ef8952c994edff3d8f255a4efb 100644
GIT binary patch
delta 36142
zcmcG#bxa_^w=RggySu}{;O;&!xceZ3I}A?a?(Q(SySux)ySux*`MtY0dpDbG-alJO
zC#O!Is#JBpbbUvvIS+KQ4-82`1{?wd1Ox^IBmgD^}q#*vIjK}6C
z1U4MjnlU@Clpf#6N(L
z&?@9pjNa;Nz4bEtI{F|V5tF322(Ayzw+!f@BxSg?+jQ-1Qy7p+V;yB?+X}SCPas%*I&<&v;}}G100xT8
ztfi!}rkHKN;ZZ35EZhAvvr1-z%`1k^fNE=vpVnNeCumirCs^-hM3Kzv#B$>0C--qS
zw=jt1y8eezTpLxjTYA+!HE8wFG$K&})8AeJ(PjDy+2e2}SMP#S-=2)QY1qGGA2e5(
z)*g!!7V)^9NTK;zE!Euo`3H(2G|-o0+A3p+dP(p~oLyPzPZ>26ltMuDN1BY{2S?n_
z4_IDxo>Th_7DkyKu?ui2GfM?S?a`FWKQ0T}F^sM_9C2U-UC@=tQAe9-?e23WVz(bq
z%o{8)gjmQmW@NlJmnPS*{Nl>3%qhcE!*}AT!DLC4)iA@lEg(0#Vcd$kA%I1rrU}H_
zDQ(+OjN4_cL@|VupE)Y?C?z{mUkX0_cM>~{q)CQL3-9e4-L{XNFI8KaVc-{TTGxYdm#gOYTkjsenLIVT5=woBQhxq8%k20eP
z_AC=%s`iAI8AA+3meM<;lmT2}=T`WPZgN$vX!+ds>s0>Hh6AjIlwq!JGU|`7
zDN^;2QNZf(%bn!Ex|mpVT1Zu8FzE-uZb2q{J1E1jrO5b+%e9mJrE_nu1G!E!#8s^I
z0YM}0^sy?5)j}s0ndz|V2OS`q*foZaGB3{?U30XHzoell7-4oU+xXnJ@_cN|v+o3u
zGUE5_^*HDk5x*C#eFy%IK;hP`5J2i<>l{c11_^t8LH&p_hlquDSH?)wHR@W+wm?^4
zhzL$;!;n;RiBLL$k&r%aKRF{#4~ri_8%9eqXw$5{|9+JrT@p4V(Q0V22qqPQ&1wB`RDIktmq$Gft{B#cYTE&*T+M>10g)_U!kP!-9~
zyv|{)l(fpUdcw%4$h$jA+tn6^Q0PK+|F^kDDV=`&B+}t=F
z4Rr@@IcClPng;yRrGWVn)^oOTdLFaemXXt8H5g5w9-lw$jcFQv7$4~HAVE13nlcn}
z+Mi=i8T>7l`-Op3BDO7Te0G-5C*F{jbSVS_l4lOHE_0ujm&Gz4h>E%0|56y0d_D)4
zc6)WW9m{(yUlMft%Z*E!`@`wLi;1OKUH6GPLWQur{~Ex7_lm$c-zekp+z%A+ldwH~
zGxhQKetEKahZGwUmxDGE!F9XeRo6BC$t~{D{&1m%-%dSn!Vl_d3*$WA2KNv)hbd!?
z3li|VI^q+RzH;G8zSc00;T93a^@Guh1bL*VG5q2%ch*y+M!fy^%BnKQ;qrDDF{r+T
zBJFxc-X+k{bV@5!&Hh_tV!*Hgnn^;BMXr{SU!y*+I;B@r}W`A
zhd9Vi9I6NAz1fnJ?o0&FnpDctT_$O5SV%*xV*#*3)d;L*J|AR&z`&UJtDS9G)@1v$
z&n9-?=xi`Vaa4`V>Bs;gOgz_ZD2;$fn1L0(4?1*zKR{$raFknog(
z{`gK)WUxY0Y(F#ICQm9^YVFx#)}Es7EBy{e=Gi?}
zmjDUfy`9)i)oQ-fR*9-px_S0*mnw2b6g9AP|)e9kd~Dbd#ThAuMSD&8hd
ztG}M4V!B`2lVp;(xPjJSljW9X5yzuwmlS9*gCWZvR+{AIE|(RE8ZLlf4GmQ2sa1}#
zd2;|-%s+xGl(3~g{jjUVBVhSgtUp04Bl8gV`T>#9CulQvvhLBxEfRFm@RE%hCz#8Lf){3s)Ns+J*5#Ft9qh!wvEBmxxXy6H
zBI}Fi3meJ!uYCf9%%Es}XyMkzCUlE|7tGd=$UpCTj6?jZ(nY|GU^gwV8-7geDlJ
zOeN0j68$~FYnb{ThW>Xh*BBu7(XQ^1!?4S&q_~&pYlOw9Z&p-s3+Tu|(gi&{E3N{!Pvg~kdnip&y=#<~94keapgdQ;U@z7d|Y353z46t5*hA5$sm>YnAX55J$EWILZ8SgMfIKuX^-YD2I7KRm=pTDS
zJSAd!^O?fdpA-WY}Nm|b6>ALx*48^fb+d5Q()?I*G3>67f
zWc>Ror#;!SgL|LG$H-Gqot&u%>3SPh9k9Wr+?!3LzNP1@m2-2W-gCP)ylD=WuLgO-
zCkP9}yE{Zk6>v0Dui7i`bBS9~gD?*v
zeFF##tSm=bm)8%ppvB30Yx4~I2uq~NhFiVi_{Z`H&rPSp;Njs?!ZB^hppk^$s
zgmZewc-815FbZjk+C)C3s)L_TXb%Au5(4aNLEsh`>SNPE@m`E8m1Y|9%{xj~_)?R(
z2Id}ts6RLQ3Np|zD2-~Kk?MJa*ygmQL6TYZfz7{YvS0}1oP}0RUIjd+tVee
zSnJ}kBS_wHD#9~iPTo;dn?w)g977XLpoF1LA^83~;5(Rd7%CNRATnh04h@89&nvSp
zoA164OdfZiX4oH$17rwONSNg~x |