In [ ]:
#Python notebook highlighting some topics discussed in http://www.slideshare.net/MattHarrison4/learn-90
In [1]:
welcome_message = "hello, hubpy!"
dir(welcome_message)
Out[1]:
In [2]:
import requests
help(requests)
In [3]:
print "hello, hubpy!"
print("hello, hubpy!") #In python 3, print is a function - not a statement
In [4]:
#watch the whitespace!
for i in range(10):
print(i)
In [5]:
#everything in python is an object
x = 10
id(x)
Out[5]:
In [6]:
x
Out[6]:
In [7]:
#mutable
people = []
print(id(people))
people.append('brian')
people.append('andrew')
print(id(people))
print(people)
In [8]:
#immutable
value = 10
print(id(value))
value = value + 12
print(id(value))
In [10]:
x = 4
y = 13.985
x = "I'm a string"
message = """ I'm a big ole
multiline string. Really useful for
small templates or blocks of text"""
#See PEP8 for all things naming and spacing related. When in doubt, do it like PEP8 -> http://www.python.org/dev/peps/pep-0008/
In [11]:
message
Out[11]:
In [12]:
y
Out[12]:
In [13]:
y = "change up"
In [14]:
y
Out[14]:
In [15]:
#integer division heads up
3/4
Out[15]:
In [17]:
3/int(4.0)
Out[17]:
In [18]:
#Long integers (see the 'L' on the end...)
10**100
Out[18]:
In [19]:
single_quotes_str = 'welcome to hubpy'
double_quotes_str = "welcome to hubpy"
multi_line_str = """
welcome
to
hubpy"""
In [20]:
"{0}, {1}!".format(single_quotes_str, 'Brian')
Out[20]:
In [21]:
'%s, %s!' % (single_quotes_str, 'Brian')
Out[21]:
In [22]:
#if you need to assign 'no value' to something - like null in other languages
value = None
In [23]:
if value:
print(value)
In [24]:
value = 1
In [25]:
if value:
print(value)
In [26]:
if value is not None:
print('awesome')
In [27]:
a = True
b = False
In [28]:
a
Out[28]:
In [29]:
if a:
print('a is True!')
In [30]:
my_list = [1, 3, 'bob', 34.6] #mutable
a_tuple = (1, 3, 'bob', 34.6) #immutable
In [31]:
a_tuple
Out[31]:
In [32]:
my_list
Out[32]:
In [33]:
'bob' in my_list
Out[33]:
In [34]:
'bob' in a_tuple
Out[34]:
In [35]:
some_numbers = [3, 2, 4, 6, 8, 9]
In [36]:
some_numbers
Out[36]:
In [37]:
some_numbers.sort() #in place sort
In [38]:
some_numbers
Out[38]:
In [39]:
some_numbers.insert(0, 1)
In [40]:
some_numbers
Out[40]:
In [41]:
some_numbers.append(10)
In [42]:
some_numbers
Out[42]:
In [43]:
help(some_numbers.insert)
In [44]:
more_numbers = [11, 15, 19, 20]
some_numbers + more_numbers
Out[44]:
In [45]:
dir(some_numbers)
Out[45]:
In [46]:
#dictionary keys must be immutable
scores = {'brian': 0, 'bob': 90, 'jane': 100}
In [47]:
scores
Out[47]:
In [48]:
scores['bob']
Out[48]:
In [49]:
scores.values()
Out[49]:
In [50]:
scores.keys()
Out[50]:
In [51]:
scores['andy'] = 85
In [52]:
scores
Out[52]:
In [53]:
'brian' in scores
Out[53]:
In [54]:
scores.get('brian', 50)
Out[54]:
In [55]:
scores.get('billy', 50)
Out[55]:
In [56]:
del scores['brian']
In [57]:
scores
Out[57]:
In [58]:
try:
scores['brian']
except:
print("BOOM")
else:
print('yeah')
In [62]:
def basic_math(number):
"""a simple function to do very basic math"""
return number + 36
In [61]:
basic_math(10)
Out[61]:
In [63]:
help(basic_math)
In [64]:
#functions are objects too
def function_fun(number, func):
"""apply function 'func' to the specified number"""
return func(number)
In [65]:
function_fun(10, basic_math)
Out[65]:
In [66]:
if scores['bob'] > 90:
print('A')
elif scores['bob'] > 80:
print('B')
elif scores['bob'] > 70:
print('C')
else:
print('D')
In [67]:
if 85 < scores['bob'] < 95:
print('yes!')
In [68]:
for name in scores.keys():
print(name)
In [69]:
for index, name in enumerate(scores.keys()):
print('index: {0}, name: {1}'.format(index, name))
In [70]:
for name, score in scores.items():
print('name: {0}, score: {1}'.format(name, score))
In [71]:
scores.keys()[1]
Out[71]:
In [72]:
scores.keys()
Out[72]:
In [73]:
scores.keys()[0:2]
Out[73]:
In [74]:
scores.keys()[-1]
Out[74]:
In [75]:
scores.keys()[len(scores.keys())-1]
Out[75]:
In [ ]: