Bruce's Notebook


In [4]:
print ('Bruce\'s notebook')


Bruce's notebook

In [5]:
print '''This is a multi-line
print statement, also it's possibly going to
handle apostrophies without a backslash?'''


This is a multi-line
print statement, also it's possibly going to
handle apostrophies without a backslash?

In [6]:
h = 'hello'
w = 'world'
print (h + ' ' + w)


hello world

In [7]:
a, b = 0, 1
while b < 200:
     print b
     a, b = b, a+b


1
1
2
3
5
8
13
21
34
55
89
144

In [8]:
print(range(5))


[0, 1, 2, 3, 4]

In [20]:
%%ruby
puts 'hello'


hello

In [9]:
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print n, 'equals', x, '*', n/x
            break
    else:
        # loop fell through without finding a factor
        print n, 'is a prime number'


2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

In [ ]:


In [21]:
import random

In [22]:
random.randint(0,50)


Out[22]:
38

In [ ]:
random.division

In [10]:
def prime(l):
    for n in range(2, l):
        for x in range(2, n):
            if n % x == 0:
                break
        else:
            # loop fell through without finding a factor
            print n,

In [11]:
prime(200)


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

note from william: you are not making an array here - it's a list. fixed :)


In [12]:
mylist = ['spam','eggs','ham',100,102]
mylist


Out[12]:
['spam', 'eggs', 'ham', 100, 102]

In [13]:
print (mylist[:2], mylist[2:])


(['spam', 'eggs'], ['ham', 100, 102])

In [14]:
print (mylist[-2:])


[100, 102]

In [15]:
name="bruce kingsbury"
print name
print name.title()
print name.upper()


bruce kingsbury
Bruce Kingsbury
BRUCE KINGSBURY

In [16]:
shout="IT IS QUITE RUDE TO TYPE IN ALL CAPITAL LETTERS"
print shout.lower()


it is quite rude to type in all capital letters

In [17]:
print ('this\nwill\tbe\nthree\tlines')


this
will	be
three	lines

In [18]:
print(undefined)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-0b271fec6335> in <module>()
----> 1 print(undefined)

NameError: name 'undefined' is not defined

In [ ]:
print

Some great work Bruce. I would explore more keywords and modules. You may be interested in some sysadmin python modules and such


In [ ]:
import sys

In [ ]:
sys.hexversion

In [ ]:
sys.version

In [ ]:
sys.copyright

In [ ]:
import os

In [ ]:
os.system('nmap')

In [ ]:
f = os.popen('date')
now = f.read()
print "Today is ", now

In [ ]:
import subprocess
subprocess.call(["ls", "-l", "/home"])

In [ ]:
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], 
stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output

In [ ]:
p = subprocess.Popen(["ping", "-c", "10", "http://artcontrol.com"], stdout=subprocess.PIPE)
output, err = p.communicate()
print  output

In [ ]:
cmdping = "ping -c4 www.artcontrol.me"
p = subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

In [ ]: