+ indep. WoS citations

Python and Networks - Class notes - 2016-02-09

1.  Grading

  • Suggested: absent at most 3 times
  • 40% - Assignments: Python code, output, Visualization code (e.g., gnuplot) and output
  • 50% - Project
  • 10% - Discussion

2.  Code

Code should be

  • easy to write
  • easy to maintain

Thus, code should

  • be 50% comments, the comments should explain in a compact way what the code is doing
  • use the entire width of your screen

Use Google search:

  • python <your question>
  • docs are at docs.python.org
  • sourceforge has many recipes

Comment:

  • we are learning Python 2 now

3.  Installation, Terminal

  • Open a terminal window
  • Try the following commands, and check their results
    ls -l, cd, mkdir, pwd, chmod, echo $PATH (linux and mac, similar on windows)
  • Find python:
    which python

3.1  On Windows: Cygwin

  • Google search cygwin
  • Download setup-64bit (assuming that your Windows is 64-bit)
  • Install all
  • Run overnight
  • In case of trouble: ask me, I will try to help

4.  Hello World

4.1  code (hello.py)

print "Hello, World!"

4.2  usage

python hello.py

4.3  output

Hello, World!

5.  iPython notebook

Try the "Hello, World!" program at https://tmpnb.org. Open a New "python 2" file (top right corner), enter the above code (hello.py), and run it (click on the forward triangle).

6.  Scalars, operators, basic for loop (hello.py)

Check the output and learn from the error messages (they are your friends).

print "Hello, World!\n",
print "Hello again!"

a = 3
b = 5
print a + b
c = "3"
print b + int(c)
pi = 3.14
print a + pi
print a ** b
print (1.0 * b) / a

if (a < b):
        print "a < b"
        print "%d < b" % a
        print "%d < %d" % (a, b)

if (b < a):
        pass
else:
        print "%d > %d" % (b, a)

# for loop 
for y in (3,5):
        print "y = %d" % y

7.  Mean and standard deviation of N random numbers (poohbear.py)

import random

print "#n\taverage\tsigma"

# Take each N value: N = 10, 100, 1000, 10000
for n in (10, 100, 1000, 10000):
        sum = 0
        sumsqr = 0

        for i in range(n):
                r = random.random()
                sum += r
                sumsqr += r * r
        avg = sum / n
        sigma = (sumsqr / n - avg * avg) ** 0.5

        print "%d\t%g\t%g" % (n, avg, sigma)