This notebook was prepared by [Thunder Shiviah](https://github.com/ThunderShiviah). Source and license info is on [GitHub](https://github.com/ThunderShiviah/code_guild).
My contact info
Where does course info live?
Did you know: Python is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles.
Remember: when you're stuck, google it!
Install:
Deliverables:
Hello!
print('hello world')
In [ ]:
!ipython # you can exit by using Ctrl-c twice.
ipython notebook
git --version
You should see
git version 2.1.4
or some version greater than 2.1.4.git clone git@github.com:ThunderShiviah/code_guild.git
This should download some class materials to a directory named code_guild. Navigate to wk0/notebooks and run
ipython notebook wk0.0_course_overview
Try running and editing some of the cells.
Python was conceived in the late 1980s[27] and its implementation was started in December 1989[28] by Guido van Rossum at CWI in the Netherlands as a successor to the ABC language (itself inspired by SETL)[29] capable of exception handling and interfacing with the Amoeba operating system.[6] Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, benevolent dictator for life (BDFL).
About the origin of Python, Van Rossum wrote in 1996:[30]
Over six years ago, in December 1989, I was looking for a "hobby" programming project that would keep me occupied during the week around Christmas. My office ... would be closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus).
In [6]:
from IPython.display import Image
Image(url='https://upload.wikimedia.org/wikipedia/commons/6/66/Guido_van_Rossum_OSCON_2006.jpg')
Out[6]:
In [7]:
import this
See Python 3.5 official tutorial for help.
numbers
using variables
strings
lists
Reminder: You can get help on any command by using help(command) (ex. help(str) for help on strings.)
Open up the REPL and write the following code. Note: read https://docs.python.org/3.5/tutorial/introduction.html as you do the exercises. It's very useful!
What happens if you call an index that's greater than the length of the string? # Ex. str1[100]
Type in the following code (hit enter after each line):
str1 == 'hello'
str2 == 'mars'
What do these return? How is = different from ==?
a, b = 4, 5
What does a equal? What about b?
Now type (but don't press enter)
b, a = a, a + b
Before you press enter, take a guess at what a and b will equal. Were you right?What would the output for the following statements be? Guess the output and then type up the following code to verify your results. Note: Make sure you use four spaces for your indents. Yay muscle memory practice!
for i in range(5):
print(i*i)
for d in [3,1,4,1,5]:
print(d, end=" ")
for i in range(4):
print("Hello")
for i in range(5):
print(i, 2**i)
for i in range(0):
print("Hello")
for i in range(20):
if i%2 == 0:
print("i equals", i)
for i in range(20):
for j in range(i,20):
print(i, j)
for i in range(20):
for j in range(i,20):
if i != 0 and i == j - 3:
print(i, j)
i, j = 10, 20
while i < j:
print("not yet...", i, j)
i += 1 # What does the += symbol do?
i, j = 10, 20
while i < j:
print("not yet...", i, j)
j -= 1 # What does the -= symbol do?
i, j = 1, 20
while i < j:
print("not yet...", i, j)
j /= 2 # What does the /= symbol do?
i, j = 2, 200000
while i < j:
print("not yet...", i, j)
i **= 2
for i in range(10):
if i % 3 == 0:
print(i, "foo")
if i % 6 == 0: # What if I indented this line?
print(i, "bar")
else:
print(i, "baz")
for i in range(10):
if i % 3 == 0:
print(i, "foo")
elif i % 6 == 0: # elif is short for else if
print(i, "bar")
else:
print(i, "baz")
for i in range(10):
if i % 3 == 0:
print(i, "foo")
elif i % 2 == 0: # elif is short for else if
print(i, "bar")
else:
print(i, "baz")
lst = ['a', 'b', 'c', 'd', 'e']
for elem in lst:
print(elem)
words = 'How many of these do I have to make!!?!?!'
for word in words:
print(word)
words = 'How many of these do I have to make!!?!?!'
for thingy in words:
print(thingy)
some = 'abvdfh'
stuff = 'mnbdjhe'
for foo in some:
for bar in stuff:
if foo == bar:
print(foo) # What would happen if I instead printed bar?
some = 'abvdfh'
stuff = 'mnbdjhe'
for foo in some:
for bar in stuff:
if foo != bar: # What does != do?
print(foo) # What would happen if I instead printed bar?
some = 'abvdfh'
stuff = 'mnbdjhe'
for foo in some:
if foo == bar:
for bar in stuff:
print(foo)
Figure out what' wrong with each of the below examples and then fix the code.
# Don't run this!
i, j = 10, 20
while i < j:
print("not yet...", i, j)
# How would you fix this
for i in range(20):
for j in range(10):
print(j/i)
for i in range(20):
for j in range(10):
print(j%i)
for i in range(20):
for j in range(10):
print(i, j)
for i in range(20):
if j < i:
print(i, j) # This will probably work on your machine but that makes it even worse! What's going on?
In [30]:
In [ ]: