tally: arcane, grim, esoteric, corpus, persnickety, menagerie
In [1]:
x = [5, 10, 15, 20, 25, 30]
In [2]:
x[3]
Out[2]:
In [3]:
[2, 4, 6, 8, 10][4]
Out[3]:
In [4]:
x[90]
In [5]:
type(x)
Out[5]:
In [6]:
type(x[0])
Out[6]:
In [7]:
len(x)
Out[7]:
In [8]:
len([10])
Out[8]:
In [9]:
type([10])
Out[9]:
In [10]:
len([])
Out[10]:
In [12]:
x
Out[12]:
In [13]:
max(x)
Out[13]:
In [14]:
min(x)
Out[14]:
In [15]:
sum(x)
Out[15]:
In [16]:
sorted(x)
#takes a list and returns it sorted in numerical or alphabetical order
Out[16]:
In [17]:
sorted([17, -4, 1004, 3, 15, 8.3])
Out[17]:
In [18]:
sorted(["badger", "crocodile", "aardvark", "zebra", "emu"])
Out[18]:
In [19]:
range(10)
Out[19]:
In [20]:
list(range(10))
Out[20]:
In [21]:
list("this is a test")
Out[21]:
In [25]:
x[3]
Out[25]:
In [27]:
n = 1 + 2
x[n]
Out[27]:
In [28]:
x[(4 + 2)//3]
#side note what is thaaaaat
Out[28]:
In [22]:
x[-1]
Out[22]:
In [23]:
x[-2]
Out[23]:
In [24]:
x[-3]
Out[24]:
In [29]:
x[-90]
In [30]:
x
Out[30]:
In [31]:
# _xprlist[__exprint_:__exprint__]
In [33]:
x[1:4]
Out[33]:
In [34]:
x[2:5]
Out[34]:
In [35]:
n = 2
x[n:n+3]
Out[35]:
In [36]:
x[-3:-1]
Out[36]:
In [37]:
x[3:9000]
#just givesyou what's included, no indicator you're way off base
Out[37]:
In [38]:
type(x[1:4])
Out[38]:
In [39]:
for item in x[2:5]:
print(item)
In [40]:
x[0:4]
Out[40]:
In [41]:
x[:4]
#at beginning, is zero
Out[41]:
In [42]:
x[4:]
#at end, gives you to the end
Out[42]:
In [43]:
x[-3:]
Out[43]:
In [44]:
x[-2:]
Out[44]:
In [45]:
x[:-1]
Out[45]:
In [46]:
x[1:]
Out[46]:
In [47]:
#very common to take a list, apply some transformation to it to get another list
#another thing that's very common is to take a list, filter it, and end up with another list
In [48]:
x
Out[48]:
In [53]:
source = [3, -1, 4, -2, 5, -3, 6]
dest = []
for item in source:
if item > 0:
dest.append(item * item)
dest
Out[53]:
In [54]:
[item * item for item in source if item > 0]
Out[54]:
In [56]:
ark = ["aardvark", "badger", "crocodile", "dingo", "emu", "flamingo"]
zoo = []
for item in ark:
if len(item) <= 6:
zoo.append(item)
zoo
Out[56]:
In [57]:
[item for item in ark if len(item) <= 6]
Out[57]:
In [58]:
#[_expr_ for _temp_ in _source_ if _expression_ ]
#if... is optional
In [59]:
x
Out[59]:
In [60]:
#what i want is: [2, 7, 12, 17, 22, 27]
stuff = []
for item in x:
stuff.append(item - 3)
stuff
Out[60]:
In [61]:
[item - 3 for item in x]
Out[61]:
In [62]:
source = [3, -1, 4, -2, 5, -3, 6]
In [63]:
# modulo operator: %
#gives you remainder, basically
#can be used to determine if a number is odd/even, just modulo 2 ya know
60 % 5
Out[63]:
In [64]:
60 % 7
Out[64]:
In [65]:
dest = []
for i in range(10):
if i % 2 == 0:
dest.append(i*i)
dest
Out[65]:
In [67]:
[i*i for i in range(10) if i % 2 == 0]
Out[67]:
In [68]:
#in python any number other than zero is considered to be True
In [69]:
rawdata = "2,3,5,7,11,13,17,19,23"
rawdata.split(",")
Out[69]:
In [70]:
values = rawdata.split(",")
In [71]:
sum(values)
In [72]:
#that doesn't work
In [73]:
int("17")
Out[73]:
In [74]:
srcstr = "1237"
type(srcstr)
Out[74]:
In [75]:
converted = int(srcstr)
type(converted)
Out[75]:
In [76]:
#int: str -> int() -> int
int("-17")
Out[76]:
In [77]:
values
Out[77]:
In [78]:
int(values)
In [81]:
#it does not work!
num_values = [int(i) for i in values]
#values is source list
#predicate expression is calling the function on the thing
In [82]:
sum(num_values)
Out[82]:
In [84]:
sum([int(i) for i in rawdata.split(",")])
Out[84]:
In [85]:
stuff = "hello there how are you"
In [86]:
stuff.split(" ")
#split divides things into a list based on what the divider is
Out[86]:
In [87]:
stuff.split("e")
Out[87]:
In [88]:
for i in stuff.split("e"):
print(i)
In [89]:
"foo" in "buffoon"
Out[89]:
In [90]:
#if the string on the left is a substring of the string on the right
"foo" in "reginald"
Out[90]:
In [91]:
ark
Out[91]:
In [92]:
[animal for animal in ark if 'a' in animal]
Out[92]:
In [93]:
for animal in ark:
if 'a' in animal:
print(animal)
In [94]:
check = "foodie"
In [95]:
check.startswith("foo")
Out[95]:
In [96]:
check.startswith("f")
Out[96]:
In [97]:
check.startswith("blah")
Out[97]:
In [98]:
check.startswith("Foo")
#case-sensitive, FYI
Out[98]:
In [99]:
check.isdigit()
#will say True if it is entirely numeric
Out[99]:
In [100]:
number_str = "12345"
In [101]:
number_str.isdigit()
Out[101]:
In [102]:
number_str = "12,345"
number_str.isdigit()
Out[102]:
In [103]:
number_str = "123.45"
number_str.isdigit()
Out[103]:
In [104]:
#.islower() .isupper()
check.islower()
Out[104]:
In [105]:
check.isupper()
Out[105]:
In [106]:
yelling = "I LIKE THINGS AND THYE ARE GOOD"
In [107]:
yelling.isupper()
Out[107]:
In [112]:
message = "bungalow"
message[0]
Out[112]:
In [113]:
message[-1]
Out[113]:
In [114]:
message[3:-2]
Out[114]:
In [108]:
src = "Now is the winter of our discontent"
In [109]:
src.find("win")
Out[109]:
In [110]:
src.find("lose")
Out[110]:
In [111]:
location = src.find("win")
src[location:]
Out[111]:
In [115]:
src[-1]
Out[115]:
In [116]:
location = src.find("z")
if location != -1:
print(src[location:location+2])
In [117]:
location = src.find("z")
print(location)
print(src[location:location+2])
In [118]:
src
Out[118]:
In [119]:
src.count("is")
#count takes a substring and counts how many it appears in the string
Out[119]:
In [122]:
src.count(" is ")
Out[122]:
In [124]:
my_is_patterns = ["is", " is ", "is ", " is", "Is"]
for item in my_is_patterns:
print(item, src.count(item))
In [121]:
for vowel in ['a', 'e', 'i', 'o', 'u']:
print(vowel, src.count(vowel))
In [125]:
src2 = "Someone tell me where the poetry is."
src3 = "is this really all the poetry you have?"
In [126]:
comment = "ARGUMENTATION! DISAGREEMENT! STRIFE!"
In [127]:
comment.lower()
Out[127]:
In [128]:
comment
Out[128]:
In [129]:
message = "e.e. cummings is not happy about this"
In [130]:
message.upper()
Out[130]:
In [131]:
str1 = "dog"
str2 = "Dog"
#so say i wanted to compare these but not case-sensitively
In [132]:
str1 == str2
Out[132]:
In [133]:
str1.lower() == str2.lower()
Out[133]:
In [134]:
movie = "dr. strangelove, or, how i learned to stop worrying and love the bomb"
In [135]:
movie.title()
Out[135]:
In [136]:
movie2 = "rosemary's baby"
#you would think, "Rosemary's Baby"
movie2.title()
#but no
Out[136]:
In [137]:
rawtext = " weird extra spaces before and after "
In [138]:
rawtext.strip()
Out[138]:
In [139]:
line = "hello there this is a line of text\n"
#\n is NEW LINE
In [140]:
print(line)
In [141]:
print(line.strip())
In [142]:
song = "I got rhythm, I got music, I got my man, who could ask for anything more"
In [148]:
step1 = song.replace("I got", "I used to have")
#turning it into a COUNTRY SONG
print(step1)
In [149]:
step2 = step1.replace("more", "less")
print(step2)
In [154]:
telegram = song.replace("I got", "someday I will have").replace("anything more", "a future more bright").replace(",", " STOP").upper() + " STOP"
In [155]:
print("original:", song)
print("much improved:", telegram)
In [144]:
song
#original stays the same eh
Out[144]:
In [145]:
rawdata = "Get data that<br>looks like this<br>because it was"
In [146]:
print(rawdata)
In [147]:
print(rawdata.replace("<br>", "\n"))
In [156]:
input_str = "Yes, y zip code is 12345. I heard that Gary's zip code is 23456. But 212 is not a zipc code."
In [158]:
print(input_str)
In [162]:
# results in: ["12345", "23456"]
current = ""
zips = []
for ch in input_str:
if ch.isdigit():
current += ch
else:
current = ""
if len(current) == 5:
zips.append(current)
current = ""
zips
Out[162]:
In [163]:
import re
zips = re.findall(r"\d{5}", input_str)
zips
#that means finds five digits in a row
Out[163]:
In [ ]: