Script examples used in the video tutorials

Copying is disabled on this page.
Instead of copying-and-pasting the code, you should type it up yourself -- every bit of muscle memory helps!


# interactive shell session
# from Tutorial 2
>>> 5+5
10
>>> 'red'
'red'
>>>


# hello.py
# from Tutorial 2
print 'hello world!'


# shell output from hello.py
# from Tutorial 2
>>> ================================ RESTART ================================
>>> 
hello world!
>>> 


# lesson01.py
# from Tutorial 1 & 2
# mybringback Python Tutorial Video 01

print 'I really like mybringback'
print 'Press any key if you agree...'
raw_input()

print "The things that mybringback can't teach me aren't worth knowing"
print 'Press any key if you agree...'
raw_input()

print 'I am seriously considering getting a large mybringback tatoo on my neck'
print 'Press any key if you agree...'
raw_input()

print 'Without mybringback my food tastes like ashes'
print 'Press any key if you agree...'
raw_input()

print "I can't imagine a world without mybringback"
print 'Press any key if you agree...'
raw_input()


# ep_04.py
# from Tutorial 3
print 1 + 2
print 3 - 4
print 5 * 6
print 7 / 8
print 9 ^ 10
print 11 ** 12


# Shell output from the ep_04.py script above
# from Tutorial 3
>>>
3
-1
30
0
3
3138428376721
>>>


# An interactive shell session
# from Tutorial 3
>>> 3/4
0
>>> 4/3
1
>>> 5/3
1
>>> 5.0/3
1.6666666666666667
>>> 45.0/3
15.0
>>> 2/7.0
0.2857142857142857
>>> 2/7.
0.2857142857142857
>>> 5/3
1
>>> 5 % 3
2
>>> 3 ** 3
27
>>> 11 ** 2
121


# An interactive shell session
# from Tutorial 4
>>> x = 5
>>> x
5
>>> x + 5
10
>>> x +=1
>>> x
6
>>> x +=
SyntaxError: invalid syntax
>>> x += 6
>>> x
12
>>> red = 13
>>> _typ = 18
>>> red + x
25
>>> red = 'red'
>>> red
'red'
>>>


# Script for Tutorial 5
a = 'five'
b = "the romans"
c = "why my teeth are a-hurtin' for now?"
d = '!@#$%^&&**())_+'


# An interactive shell session
# from Tutorial 5
>>> ================================ RESTART ================================
>>> 
>>> a
'five'
>>> b
'the romans'
>>> c
"why my teeth are a-hurtin' for now?"
>>> d
'!@#$%^&&**())_+'
>>> c = "why my teeth are a-hurtin' for now?'
SyntaxError: EOL while scanning string literal
>>> a=b
>>> a
'the romans'
>>> b
'the romans'
>>> a + c
"the romanswhy my teeth are a-hurtin' for now?"
>>> b * 7
'the romansthe romansthe romansthe romansthe romansthe romansthe romans'
>>> b - c

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    b - c
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> b/c

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    b/c
TypeError: unsupported operand type(s) for /: 'str' and 'str'
>>> 


# Script from Tutorial6, Version 1
a = raw_input()
print (a+' ')*5


# Script from Tutorial6, Version 2
a = raw_input('please type a word: ')
print (a+' ')*5
raw_input()


# Script from Tutorial6, Version 3
a = raw_input('please type a word: ')
print (a+' ')*5
#raw_input()


# Tutorial 6
# Excution of the three scripts above
>>> ================================ RESTART ================================
>>> 
gee
gee gee gee gee gee 
>>> ================================ RESTART ================================
>>> 
please type a word: word up
word up word up word up word up word up 
>>>
>>> ================================ RESTART ================================
>>> 
please type a word: red
red red red red red 


# Script used in Tutorial 7
print 'a'

print 'this is a STRING'

print "this is a string 'too'"

print """" both '' and "" work"""


# Tutorial 7
# shell output after executing various versions of the script above
>>> ================================ RESTART ================================
>>> 
this is a STRING
>>> ================================ RESTART ================================
>>> 
this is a STRING
this is a string 'too'
>>> ================================ RESTART ================================
>>> 
a
this is a STRING
this is a string 'too'
>>> ================================ RESTART ================================
>>> 
a
this is a STRING
this is a string 'too'
" both '' and "" work
>>> ================================ RESTART ================================
>>> 
a
this is a STRING
this is a string 'too'
" both

'' and "

" work
>>> 


# Script used in Tutorial 8
# Different versions of myFears was used throughout
#myFears = ['clowns', 'dentists', 'endless, crippling loneliness', 'my mailman'] 

#myFears = ['1', 'dentists', 'endless, crippling loneliness', 'my mailman']

myFears = [1, 'dentists', 'endless, crippling loneliness', 'my mailman']


# From Tutorial 8
# Outputs from the script above, plus some follow-up commands in shell
>>> ================================ RESTART ================================
>>> 
>>> myFears
['clowns', 'dentists', 'endless, crippling loneliness', 'my mailman']
>>> myFears[1]
'dentists'
>>> myFears[0]
'clowns'
>>> myFears[4]

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    myFears[4]
IndexError: list index out of range
>>> len(myFears)
4
>>> ================================ RESTART ================================
>>> 
>>> myFears[0]
'1'
>>> ================================ RESTART ================================
>>> 
>>> myFears[0]
1
>>> 


# Script used in Tutorial 9
# Script was edited throughout the tutorial with varying output
myList = [1,1,2,3,5,8,13,21,34,55,89,144]

print myList

#print myList[0]

#print myList[5]

#print myList[-0]

#print myList[-5]

#print myList[2:]

#print myList[:5]

#print myList[2:5]

#print myList[:]


# From Tutorial 9
# Outputs from the script above, plus some follow-up commands in shell
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
144
21
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
1
21
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
1
21
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
1
21
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
[1, 1, 2, 3, 5]
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
1
21
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
[1, 1, 2, 3, 5]
[2, 3, 5]
>>> ================================ RESTART ================================
>>> 
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
1
8
1
21
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
[1, 1, 2, 3, 5]
[2, 3, 5]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
>>> 


# Script used in Tutorial 10
myGoals = ['defeat foes', 'eat veal', 'make ladies swoon']


# Interactive shell session in Tutorial 10
# Script above is executed, followed by a bunch of list methods
>>> ================================ RESTART ================================
>>> 
>>> myGoals
['defeat foes', 'eat veal', 'make ladies swoon']
>>> len(myGoals)
3
>>> myGoals.append('teach python')
>>> myGoals
['defeat foes', 'eat veal', 'make ladies swoon', 'teach python']
>>> myGoals.insert('brush teeth daily')

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    myGoals.insert('brush teeth daily')
TypeError: insert() takes exactly 2 arguments (1 given)
>>> myGoals.insert('brush teeth daily',3)

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    myGoals.insert('brush teeth daily',3)
TypeError: an integer is required
>>> myGoals.insert(3,'brush teeth daily')
>>> myGoals
['defeat foes', 'eat veal', 'make ladies swoon', 'brush teeth daily', 'teach python']
>>> myGoals.remove('eat veal')
>>> myGoals
['defeat foes', 'make ladies swoon', 'brush teeth daily', 'teach python']
>>> myGoals.remove('kill the moon')

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    myGoals.remove('kill the moon')
ValueError: list.remove(x): x not in list
>>> myGoals.remove(1)

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    myGoals.remove(1)
ValueError: list.remove(x): x not in list
>>> 


# Interactive shell session in Tutorial 11
# help(menInTub) output was truncated for space's sake
>>> ================================ RESTART ================================
>>> 
menInTub
['butcher', 'baker', 'candlestick maker']
>>> print menInTub
['butcher', 'baker', 'candlestick maker']
>>> len(menInTub)
3
>>> len(menInTub[2])
17
>>> help(len)
Help on built-in function len in module __builtin__:

len(...)
    len(object) -> integer
    
    Return the number of items of a sequence or mapping.

>>> help(menInTub)
Help on list object:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __delitem__(...)
 |      x.__delitem__(y) <==> del x[y]
 |  
 |  __delslice__(...)
 |      x.__delslice__(i, j) <==> del x[i:j]
 |      
 |      Use of negative indices is not supported.
 ## help(menInTub) result has been truncated. 

 


# Interactive shell session in Tutorial 12

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import time
>>> time.time()
1401725889.914
>>> time.localtime(time.time())
time.struct_time(tm_year=2014, tm_mon=5, tm_mday=27, tm_hour=12, 
tm_min=20, tm_sec=59, tm_wday=0, tm_yday=153, tm_isdst=1)
>>> time.asctime(time.localtime(time.time()))
'Tue May 27 12:22:08 2014'
>>> 


# Script used in Tutorial 13. It was edited throughout with some variation. 
print time()
print ''
print localtime(time())
print ''
print asctime(localtime(time()))


# Interactive shell session in Tutorial 13
# Slightly different versions of the script above are executed each time, 
# with differing results. 
>>> ================================ RESTART ================================
>>> 
1401809875.66

time.struct_time(tm_year=2014, tm_mon=6, tm_mday=3, tm_hour=11, 
tm_min=37, tm_sec=55, tm_wday=1, tm_yday=154, tm_isdst=1)

Tue Jun 03 11:37:55 2014
>>> ================================ RESTART ================================
>>> 
1401809944.64

Traceback (most recent call last):
  File "C:/Users/LMC user/Desktop/Python Tutorials/Episode 13 program.py", line 5, in <module>
    print t.localtime(time.time())
NameError: name 'time' is not defined
>>> ================================ RESTART ================================
>>> 
1401809991.34

time.struct_time(tm_year=2014, tm_mon=6, tm_mday=3, tm_hour=11, tm_min=39, 
tm_sec=51, tm_wday=1, tm_yday=154, tm_isdst=1)

Tue Jun 03 11:39:51 2014
>>> ================================ RESTART ================================
>>> 
1401810137.98

Traceback (most recent call last):
  File "C:/Users/LMC user/Desktop/Python Tutorials/Episode 13 program.py", line 5, in 
    print localtime(time())
NameError: name 'localtime' is not defined
>>> ================================ RESTART ================================
>>> 
1401810184.0

time.struct_time(tm_year=2014, tm_mon=6, tm_mday=3, tm_hour=11, tm_min=43, 
tm_sec=4, tm_wday=1, tm_yday=154, tm_isdst=1)

Tue Jun 03 11:43:04 2014
>>> 


# Interactive shell session in Tutorial 14
>>> book = open('C:/Users/mybringback/Desktop/pg16328.txt')
>>> book
<open file 'C:/Users/mybringback/Desktop/pg16328.txt', mode 'r' at 0x012CDF40>
>>> booktxt = book.readlines()
>>> len(booktxt)
7004
>>> help(booktxt)
Help on list object:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 ...........................  CONTENT CLIPPED ........................ 
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
 |      cmp(x, y) -> -1, 0, 1
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
 |  
 |  __new__ = 
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

>>> booktxt[0]
'\xef\xbb\xbfThe Project Gutenberg EBook of Beowulf \n'
>>> booktxt[1]
'\n'
>>> booktxt[2]
'This eBook is for the use of anyone anywhere at no cost and with\n'
>>> print booktxt[0:3]
['\xef\xbb\xbfThe Project Gutenberg EBook of Beowulf \n', '\n', 'This eBook is for the use of anyone anywhere at no cost and with\n']
>>> 


# Interactive shell session in Tutorial 15
>>> myText=['I','Love','my','bring','back']
>>> myText
['I', 'Love', 'my', 'bring', 'back']
>>> outfile = open('C:/Users/mybringback/Desktop/output.txt')

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    outfile = open('C:/Users/mybringback/Desktop/output.txt')
IOError: [Errno 2] No such file or directory: 'C:/Users/mybringback/Desktop/output.txt'
>>> outfile = open('C:/Users/mybringback/Desktop/output.txt', 'w')
>>> outfile.writelines(myText)
>>> outfile.close()
>>> 


# Interactive shell session in Tutorial 16
>>> theMotto = ['you', 'only', 'live', 'once', '(YOLO)']
>>> for item in theMotto:
	print item

	
you
only
live
once
(YOLO)
>>> for thing in theMotto:
	print thing
	print len(thing)

	
you
3
only
4
live
4
once
4
(YOLO)
6
>>> 


# Tutorial 17 Script, first version
# Shell output below. This script does not terminate
# due to faulty while loop.

myAge = 26
DEATH = 69

while myAge < DEATH:
    print 'when i am'
    print myAge
    print 'I will still love mybringback.com'

print 'But when I am'
print myAge
print 'I will die.'


# Tutorial 17. 
# Shell output from the script above.
# Due to the logical glitch in the script, the while loop does not terminate
# Output is interrupted by CTRL + C
>>> 
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
I will still love mybringback.com
when i am
26
...
Traceback (most recent call last):
  File "C:/Users/Owner/Desktop/Python Tutorials/Tutorial 17 program.py", line 5, in <module>
    print 'when i am'
  File "C:\Program Files\Python\lib\idlelib\PyShell.py", line 1351, in write
    return self.shell.write(s, self.tags)
KeyboardInterrupt


# Tutorial 17
# Working version of script. myAge is properly incremented this time. 
# Shell output below.

myAge = 26
DEATH = 69

while myAge < DEATH:
    print 'when i am'
    print myAge
    print 'I will still love mybringback.com'
    myAge += 1

print 'But when I am'
print myAge
print 'I will die.'


# Tutorial 17. Output from script above.
>>> 
when i am
26
I will still love mybringback.com
when i am
27
I will still love mybringback.com
when i am
28
I will still love mybringback.com
when i am
29
I will still love mybringback.com
when i am
30
I will still love mybringback.com
when i am
31
I will still love mybringback.com
when i am
32
# ...... THE MIDDLE PORTION WAS CLIPPED. IT WAS TAKING LONG BECAUSE ED STARTED TOO YOUNG.
when i am
66
I will still love mybringback.com
when i am
67
I will still love mybringback.com
when i am
68
I will still love mybringback.com
But when I am
69
I will die.
>>> 


# Tutorial 18, shell session 
>>> True
True
>>> False
False
>>> x = Trues

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    x = Trues
NameError: name 'Trues' is not defined
>>> x = True
>>> True
True
>>> False
False
>>> True and True
True
>>> True and False
False
>>> 7 > 3 and 2 == 5
False
>>> True or False
True
>>> False or True
True
>>> False or False
False
>>> not True
False
>>> not False
True
>>> 


# Tutorial 19, initial script. 4 versions executed to produce shell output below. 
# 1st execution: script is run as-is
# 2nd execution: 7 on top is changed to 700 (nothing prints)
# 3rd execution: number is set back to 7, the bottom block is uncommented (Alt+4) 
#                and the top if conditional block is commented out (Alt+3)
# 4th execution: number is set to 700

number = 7

if number < 100:
    print 'it is less than 100'

##if number*7 == 49
##    print 'it is 7'
##else:
##    print 'it wasnt 7; it was ' +str(number)


# Tutorial 19, shell outputs
# 4 different versions of script above were executed
>>> ================================ RESTART ================================
>>> 
it is less than 100
>>> ================================ RESTART ================================
>>> 
>>> ================================ RESTART ================================
>>> 
it is 7
>>> ================================ RESTART ================================
>>> 
it wasnt 7; it was 700
>>>


# Tutorial 20, script version 1

driving_age = 16
smoking_age = 18
lotto_age = 19
drinking_age = 21

age = int(raw_input('how old are you? '))

if age > drinking_age:
    print 'You can drive, smoke, gamble, and drink! Woohoo!'
elif age > lotto_age:
    print 'You can drive, smoke, and gamble! Yipee!'
elif age > smoking_age:
    print 'You can drive and smoke! Nice!'
elif age > driving_age:
    print 'You can drive! Vroom Vroom!'
else:
    print "You can't even drive! :("


# Tutorial 20, script version 2
# > is replaced by >= 

driving_age = 16
smoking_age = 18
lotto_age = 19
drinking_age = 21

age = int(raw_input('how old are you? '))

if age >= drinking_age:
    print 'You can drive, smoke, gamble, and drink! Woohoo!'
elif age >= lotto_age:
    print 'You can drive, smoke, and gamble! Yipee!'
elif age >= smoking_age:
    print 'You can drive and smoke! Nice!'
elif age >= driving_age:
    print 'You can drive! Vroom Vroom!'
else:
    print "You can't even drive! :("


# Tutorial 20, shell output
# 1st, 2nd, 3rd execution: script v1 (uses >)
# 3th execution: script v2 (uses >=)
>>> ================================ RESTART ================================
>>> 
how old are you? 30
You can drive, smoke, gamble, and drink! Woohoo!
>>> ================================ RESTART ================================
>>> 
how old are you? 5
You can't even drive! :(
>>> ================================ RESTART ================================
>>> 
how old are you? 18
You can drive! Vroom Vroom!
>>> ================================ RESTART ================================
>>> 
how old are you? 18
You can drive and smoke! Nice!
>>> 


# Tutorial 21, script 1st version 
# (Same as 2nd script in previous tutorial)
# uses raw_input() to get the age from user
# Ran twice to produce 1st and 2nd shell output 
driving_age = 16
smoking_age = 18
lotto_age = 19
drinking_age = 21

age = int(raw_input('how old are you? '))

if age >= drinking_age:
    print 'You can drive, smoke, gamble, and drink! Woohoo!'
elif age >= lotto_age:
    print 'You can drive, smoke, and gamble! Yipee!'
elif age >= smoking_age:
    print 'You can drive and smoke! Nice!'
elif age >= driving_age:
    print 'You can drive! Vroom Vroom!'
else:
    print "You can't even drive! :("


# Tutorial 21, script 2nd version
# uses a user-defined function this time
# 3rd shell execution followed by interactive function calls  
driving_age = 16
smoking_age = 18
lotto_age = 19
drinking_age = 21

#age = int(raw_input('how old are you? '))
def get_legal(age):
    if age >= drinking_age:
        print 'You can drive, smoke, gamble, and drink! Woohoo!'
    elif age >= lotto_age:
        print 'You can drive, smoke, and gamble! Yipee!'
    elif age >= smoking_age:
        print 'You can drive and smoke! Nice!'
    elif age >= driving_age:
        print 'You can drive! Vroom Vroom!'
    else:
        print "You can't even drive! :("


# From Tutorial 21
# 1st and 2nd execution: script version 1
# 3rd execution: script version 2  
>>> ================================ RESTART ================================
>>> 
how old are you? 15
You can't even drive! :(
>>> ================================ RESTART ================================
>>> 
how old are you? 28
You can drive, smoke, gamble, and drink! Woohoo!
>>> len('red')
3
>>> ================================ RESTART ================================
>>> 
>>> get_legal(99)
You can drive, smoke, gamble, and drink! Woohoo!
>>> get_legal(13)
You can't even drive! :(
>>> age

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    age
NameError: name 'age' is not defined
>>> 


# Tutorial 22 script, initial version
#create a dictionary of myBringback's favorite colors
bringBackDict = {'Trav':'Red',
                 'Jake':'Blue',
                 'Joel':'Purple',
                 'Ed':'Yellow'}


# Tutorial 22 script, after 1st edit
#create a dictionary of myBringback's favorite colors
bringBackDict = {'Trav':'Red',
                 'Jake':'Blue',
                 'Joel':'Purple',
                 'Ed':'Yellow',
                 'Trav':'Orange'}


# Tutorial 22 script, after 2nd edit
#create a dictionary of myBringback's favorite colors
bringBackDict = {'Trav':'Red',
                 'Jake':'Blue',
                 'Joel':'Orange',
                 'Ed':'Orange',
                 'Trav':'Orange'}


# Tutorial 22, shell output
# Script was edited twice. All three versions shown above. 
>>> ================================ RESTART ================================
>>> 
>>> bringBackDict
{'Ed': 'Yellow', 'Joel': 'Purple', 'Trav': 'Red', 'Jake': 'Blue'}
>>> bringBackDict[3]

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    bringBackDict[3]
KeyError: 3
>>> bringBackDict['Joel']
'Purple'
>>> ================================ RESTART ================================
>>> 
>>> bringBackDict
{'Ed': 'Yellow', 'Joel': 'Purple', 'Trav': 'Orange', 'Jake': 'Blue'}
>>> bringBackDict['Trav']
'Orange'
>>> ================================ RESTART ================================
>>> 
>>> 


# ep34.py
# The module script in Tutorial 23
# It is imported in the second script below

#test module

def test_print():
    print 'you imorted it!'

def test_add(x,y):
    print x+y


# ep34test.py 
# Second script from Tutorial 23
# Imports ep34.py as a module, uses the function in it

import ep34

ep34.test_add(5,4)


# Interactive IDLE shell session from Tutorial 23
# First "RESTART" is the result of running ep34.py followed by interactive commands
# Second "RESTART" is the result of running ep34test.py 
>>> ================================ RESTART ================================
>>> 
>>> ep34.test_print
<function test_print at 0x0251DEB0>
>>> ep34.test_print()
you imorted it!
>>> ================================ RESTART ================================
>>> 
9
>>>