Brief Intro to Programming

Jiří Baum <[email protected]>
Newsgroups gmane.org.user-groups.slug.general
Message-ID <[email protected]>
Hi,

As promised at Friday's meeting, I'm sending a copy of the code from the
second part of the talk.

I'm sending it in two forms:

* SLUG-2014-01.ipynb is the IPython Notebook file, which is the
interactive exploratory programming environment I was using during my
presentation.

* SLUG-2014-01.py is an ordinary Python program, converted from the
above. This is the more common form of Python programs.


I've also uploaded my slides here:
http://www.baum.com.au/~jiri/slug/what-is-programming.pdf


Cheers

Jiri
-- 
Jiří Baum <[email protected]>
Sabik Software Solutions Pty Ltd
0413 183 117
http://www.baum.com.au/sabik

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
SLUG-2014-01.py (text/x-python, 841 B)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>

# <codecell>

print 'Hello World'
print 'How are you?'

# <codecell>

a = 1
print a
b = 2
print b
print a+b
b = 3*b
print b

# <codecell>

farmyard = ['dog', 'hen', 'horse', 'sheep', 'cow']
print farmyard

# <codecell>

print sorted(farmyard)

# <codecell>

def find_my(needle, haystack):
    """ Narrate a search for needle in haystack. """
    for animal in haystack:
        if animal==needle:
            print 'This is my %s.' % (animal)
            return
        else:
            print 'This is not my %s! It is a %s!' %(needle,animal)
    print 'There is no %s!' % (needle)

# <codecell>

find_my('cow', farmyard)

# <codecell>

find_my('shipwreck', ['coral', 'clownfish', 'anchor', 'mermaid', 'nemo'])

# <codecell>

find_my('ostrich', farmyard)

# <codecell>
SLUG-2014-01.ipynb (text/plain, 5.3 KB)
{
 "metadata": {
  "name": "SLUG-2014-01"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Hello World'\n",
      "print 'How are you?'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Hello World\n",
        "How are you?\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 1\n",
      "print a\n",
      "b = 2\n",
      "print b\n",
      "print a+b\n",
      "b = 3*b\n",
      "print b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n",
        "2\n",
        "3\n",
        "6\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "farmyard = ['dog', 'hen', 'horse', 'sheep', 'cow']\n",
      "print farmyard"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "['dog', 'hen', 'horse', 'sheep', 'cow']\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print sorted(farmyard)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "['cow', 'dog', 'hen', 'horse', 'sheep']\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def find_my(needle, haystack):\n",
      "    \"\"\" Narrate a search for needle in haystack. \"\"\"\n",
      "    for animal in haystack:\n",
      "        if animal==needle:\n",
      "            print 'This is my %s.' % (animal)\n",
      "            return\n",
      "        else:\n",
      "            print 'This is not my %s! It is a %s!' %(needle,animal)\n",
      "    print 'There is no %s!' % (needle)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "find_my('cow', farmyard)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is not my cow! It is a dog!\n",
        "This is not my cow! It is a hen!\n",
        "This is not my cow! It is a horse!\n",
        "This is not my cow! It is a sheep!\n",
        "This is my cow.\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "find_my('shipwreck', ['coral', 'clownfish', 'anchor', 'mermaid', 'nemo'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is not my shipwreck! It is a coral!\n",
        "This is not my shipwreck! It is a clownfish!\n",
        "This is not my shipwreck! It is a anchor!\n",
        "This is not my shipwreck! It is a mermaid!\n",
        "This is not my shipwreck! It is a nemo!\n",
        "There is no shipwreck!\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "find_my('ostrich', farmyard)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is not my ostrich! It is a dog!\n",
        "This is not my ostrich! It is a hen!\n",
        "This is not my ostrich! It is a horse!\n",
        "This is not my ostrich! It is a sheep!\n",
        "This is not my ostrich! It is a cow!\n",
        "There is no ostrich!\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "help(find_my)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Help on function find_my in module __main__:\n",
        "\n",
        "find_my(needle, haystack)\n",
        "    Narrate a search for needle in haystack.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "help(math.sqrt)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Help on built-in function sqrt in module math:\n",
        "\n",
        "sqrt(...)\n",
        "    sqrt(x)\n",
        "    \n",
        "    Return the square root of x.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    }
   ],
   "metadata": {}
  }
 ]
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.