# by default, only the result of the last expression in a cell is displayed after evaluation.
# the following forces display of *all* self-standing expressions in a cell.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
This list supports the mutable sequence operations in addition to the common sequence operations.
l = [1, 2, 1, 1, 2, 3, 3, 1] # tuple t = (1, 2, 1, 1, 2, 3, 3, 1)
l
[1, 2, 1, 1, 2, 3, 3, 1]
len(l)
8
l[5]
3
l[1:-1]
l
[2, 1, 1, 2, 3, 3]
[1, 2, 1, 1, 2, 3, 3, 1]
l + ['hello', 'world']
l
[1, 2, 1, 1, 2, 3, 3, 1, 'hello', 'world']
[1, 2, 1, 1, 2, 3, 3, 1]
l # `+` does *not* mutate the list!
l * 3
[1, 2, 1, 1, 2, 3, 3, 1, 1, 2, 1, 1, 2, 3, 3, 1, 1, 2, 1, 1, 2, 3, 3, 1]
sum = 0
for x in l: #[1, 2, 1, 1, 2, 3, 3, 1]
sum += x
sum
14
l = list('hell')
l
['h', 'e', 'l', 'l']
l.append('o')
l
['h', 'e', 'l', 'l', 'o']
l.append(' there')
l
['h', 'e', 'l', 'l', 'o', ' there']
del l[-1]
l
['h', 'e', 'l', 'l', 'o']
del l[0]
l
['e', 'l', 'l', 'o']
del l[1:3]
l
['e', 'o']
l = list('hello')
l
['h', 'e', 'l', 'l', 'o']
l.extend(' there')
l
['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
l.extend([1,2,3])
l
['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', 1, 2, 3]
l[2:7]
['l', 'l', 'o', ' ', 't']
del l[2:7]
l
['h', 'e', 'h', 'e', 'r', 'e', 1, 2, 3]
l=[]
l
[]
words = ['cat','dog','frog']
words
['cat', 'dog', 'frog']
words[1]
words[1][-1]
'dog'
'g'
words = ['cat','dog','frog']
for x in words:
if x=='dog':
del words[1]
print(x)
cat dog
words = ['cat','dog','frog']
for x in words[:]: # creates a new sequence a copy of words
if x=='dog':
del words[1]
print(x)
words
cat dog frog
['cat', 'frog']
words = ['cat','dog','frog']
for x in words:
if x=='frog':
words.append('goat')
print(x)
words
cat dog frog goat
['cat', 'dog', 'frog', 'goat']
[x for x in range(10)] # [WhatToPutInTheList loop/if statenments to generate those things ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l=[]
for x in range(10):
l.append(x)
l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2*x+1 for x in range(10)] # odd numbers
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
for radius in range(1,6):
for side in range(1,6):
if 3.14159*radius*radius > side*side:
print(radius,' ',side)
1 1 2 1 2 2 2 3 3 1 3 2 3 3 3 4 3 5 4 1 4 2 4 3 4 4 4 5 5 1 5 2 5 3 5 4 5 5
# What circles of radius 1 to 5 have more area than squares with sides 1 to 5?
[(radius, side) for radius in range(1,6) for side in range(1,6) if 3.14159*radius*radius > side*side]
[(1, 1), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
adjs = ('hot', 'blue', 'quick')
nouns = ('table', 'fox', 'sky')
phrases = []
for adj in adjs:
for noun in nouns:
phrases.append(adj + ' ' + noun)
phrases
['hot table', 'hot fox', 'hot sky', 'blue table', 'blue fox', 'blue sky', 'quick table', 'quick fox', 'quick sky']
[adj + ' ' + noun for adj in adjs for noun in nouns]
['hot table', 'hot fox', 'hot sky', 'blue table', 'blue fox', 'blue sky', 'quick table', 'quick fox', 'quick sky']
# givena list of random 0s and 1s, find a (run)sequence of an exact length '000'
import random
l=''
for i in range(10):
l=l+str(random.randint(0,1))
l
'1000011110'
l.index('000')
1
flips=''
for i in range(100):
flips=flips+str(random.randint(0,1))
runs=[]
prev=flips[0]
runCount=1
for c in flips[1:]:
if c==prev:
runCount+=1
else:
if prev=='0':
runs.append(runCount)
else:
runs.append(-1*runCount)
runCount=1
prev=c
runs
#'1000011110'
#-1 4 -4 1
[-5, 2, -2, 1, -2, 3, -1, 1, -5, 1, -1, 1, -1, 2, -1, 1, -2, 1, -1, 3, -1, 1, -1, 1, -2, 2, -3, 6, -3, 1, -3, 1, -1, 2, -1, 1, -2, 11, -3, 3, -2, 2, -1, 1, -3, 3, -1]
A set is a data structure that represents an unordered collection of unique objects (like the mathematical set).
s = {1, 2, 1, 1, 2, 3, 3, 1}
s
{1, 2, 3}
t = {2, 3, 4, 5}
s.union(t)
{1, 2, 3, 4, 5}
s.difference(t)
{1}
s.intersection(t)
{2, 3}
A dictionary is a data structure that contains a set of unique key → value mappings.
d = { # key:value mappings that can be referenced by d[key]
'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'Tony Stark'
}
d
{'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'Tony Stark'}
d['Ironman']
'Tony Stark'
d['Ironman'] = 'James Rhodes'
d
{'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'James Rhodes'}
e = { # key:value mappings that can be referenced by d[key]
'Ironman': 'matt' ,
'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'Tony Stark'
}
e
{'Ironman': 'Tony Stark',
'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker'}
d['pythonguy']='matt'
d
{'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'James Rhodes',
'pythonguy': 'matt'}
d['Batman']
d.get('Clark Kent') # index search also, if not found, return none
d['Clark Kent']
'Bruce Wayne'
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-72-14aa5464f31b> in <module> 1 d['Batman'] 2 d.get('Clark Kent') # index search also ----> 3 d['Clark Kent'] KeyError: 'Clark Kent'
# use of in operator to see if index is in a dictionary
'Batman' in d
True
d['Batman']= 'dddd'
d
{'Superman': 'Clark Kent',
'Batman': 'dddd',
'Spiderman': 'Peter Parker',
'Ironman': 'James Rhodes',
'pythonguy': 'matt'}
del d['Ironman']
d
{'Superman': 'Clark Kent',
'Batman': 'dddd',
'Spiderman': 'Peter Parker',
'pythonguy': 'matt'}
f ={ 1:'one', 2:'two', 3:'three'} # index can be anything that is immutable
f[3]
'three'
z = { [1,2]: 'list1', [3,4,5]: 'list2'}
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-77-cf6e90fff10a> in <module> ----> 1 z = { [1,2]: 'list1', [3,4,5]: 'list2'} TypeError: unhashable type: 'list'
z = { (1,2): 'list1', (3,4,5): 'list2'}
z[(1,2)]
'list1'
a=[1,2,3,4,5]
a[5]=6
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-70-e611dc6870dd> in <module> 1 a=[1,2,3,4,5] ----> 2 a[5]=6 IndexError: list assignment index out of range
{e:2**e for e in range(0,100,10)}
{x:y for x in range(3) for y in range(10)}
sentence = 'a man a plan a canal panama'
{w:w[::-1] for w in sentence.split()}