Bruce's Notebook
In [4]:
print ('Bruce\'s notebook')
In [5]:
print '''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)
In [7]:
a, b = 0, 1
while b < 200:
print b
a, b = b, a+b
In [8]:
print(range(5))
In [20]:
%%ruby
puts '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'
In [ ]:
In [21]:
import random
In [22]:
random.randint(0,50)
Out[22]:
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)
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]:
In [13]:
print (mylist[:2], mylist[2:])
In [14]:
print (mylist[-2:])
In [15]:
name="bruce kingsbury"
print name
print name.title()
print name.upper()
In [16]:
shout="IT IS QUITE RUDE TO TYPE IN ALL CAPITAL LETTERS"
print shout.lower()
In [17]:
print ('this\nwill\tbe\nthree\tlines')
In [18]:
print(undefined)
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 [ ]: