text = "it was the best of times, it was the worst of times"
words = text.split()
I want to write some code that display this:
it
was
the
best
of
times,
it
was
the
worst
of
times
for item in words:
print(item)
it was the best of times, it was the worst of times
item = words[0]
print(item)
item = words[1]
print(item)
item = words[2]
print(item)
item = words[3]
print(item)
item = words[4]
print(item)
item = words[5]
print(item)
it was the best of times,
for item in words:
yelling = item.upper()
print(yelling)
IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES
shopping = []
shopping.append("apples")
shopping
['apples']
shopping.append("flour")
shopping.append("sugar")
shopping.append("eggs")
shopping
['apples', 'flour', 'sugar', 'eggs']
our task: new list that contains the number of letters in each ingredient on my shopping list.
[6, 5, 5, 4]
[len(item) for item in shopping]
[6, 5, 5, 4]
food_lengths = []
for item in shopping:
food_lengths.append(len(item))
food_lengths
[6, 5, 5, 4]
shopping.extend(['cinnamon', 'butter', 'beer'])
shopping
['apples', 'flour', 'sugar', 'eggs', 'cinnamon', 'butter', 'beer']
word = "bear"
print("before if statement...")
if len(word) == 8:
print("length is eight!")
print("I'm so glad that string had eight characters")
print("let's have a party! an eight-character party. 🎉")
print("...after if statement")
before if statement... ...after if statement
temperature = 14
print("before if statement...")
if temperature > 25:
print("wow really hot today huh?")
elif temperature > 15:
print("it's very pleasant today!")
elif temperature > 5:
print("kinda brisk!")
else:
print("it's quite cold")
print("...after if statement")
before if statement... kinda brisk! ...after if statement
temps = [12, 16, 24, 8, 42, 9, 23, 29, -2, 5, 8]
print("now beginning temperature broadcast!")
for item in temps:
if item > 25:
print("wow really hot today huh?")
elif item > 15:
print("it's very pleasant today!")
elif item > 5:
print("kinda brisk!")
else:
print("it's quite cold")
print("temperature broadcast complete.")
now beginning temperature broadcast! kinda brisk! it's very pleasant today! it's very pleasant today! kinda brisk! wow really hot today huh? kinda brisk! it's very pleasant today! wow really hot today huh? it's quite cold it's quite cold kinda brisk! temperature broadcast complete.
foo = "this is a test"
foo.find("test")
10
foo.find("blah")
-1
president_states = {"Obama": "Hawaii",
"Bush": "Texas",
"Clinton": "Arkansas",
"Trump": "New York"}
type(president_states)
dict
president_states["Obama"]
'Hawaii'
president_states["Clinton"]
'Arkansas'
a_president = "Clinton"
president_states[a_president]
'Arkansas'
president_states["Franklin"]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-67-4ba0a4441cac> in <module>() ----> 1 president_states["Franklin"] KeyError: 'Franklin'
shopping = {"apple": 2, "bag of flour": 1, "egg": 3, "bag of sugar": 1}
shopping["apple"]
2
"apple" in shopping
True
"banana" in shopping
False
president_states
{'Bush': 'Texas', 'Clinton': 'Arkansas', 'Obama': 'Hawaii', 'Trump': 'New York'}
"New York" in president_states
False
shopping.keys()
dict_keys(['apple', 'bag of flour', 'egg', 'bag of sugar'])
shopping.values()
dict_values([2, 1, 3, 1])
for item in shopping.keys():
print(item.upper())
APPLE BAG OF FLOUR EGG BAG OF SUGAR
shopping.items()
dict_items([('apple', 2), ('bag of flour', 1), ('egg', 3), ('bag of sugar', 1)])
for item, value in shopping.items():
print(item, value)
apple 2 bag of flour 1 egg 3 bag of sugar 1
shopping
{'apple': 2, 'bag of flour': 1, 'bag of sugar': 1, 'egg': 3}
shopping["stick of butter"] = 2
shopping
{'apple': 2, 'bag of flour': 1, 'bag of sugar': 1, 'egg': 3, 'stick of butter': 2}
shopping["cinnamon stick"] = 1
shopping
{'apple': 2, 'bag of flour': 1, 'bag of sugar': 1, 'cinnamon stick': 1, 'egg': 3, 'stick of butter': 2}
shopping["apple"] = 4
shopping
{'apple': 4, 'bag of flour': 1, 'bag of sugar': 1, 'cinnamon stick': 1, 'egg': 3, 'stick of butter': 2}
things = []
things.append("a")
things
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
user = {
"name": "Allison",
"city": "Brooklyn, NY, USA",
"favorite_color": "blue",
"pets": [
"Shumai",
"Althea"
]
}
user["name"]
'Allison'
user["city"]
'Brooklyn, NY, USA'
user["favorite_color"]
'blue'
type(user["pets"])
list
user["pets"]
['Shumai', 'Althea']
user["pets"][0]
'Shumai'
countries = set()
countries.add("United States")
countries.add("China")
countries.add("Argentina")
countries.add("France")
countries
{'Argentina', 'China', 'France', 'United States'}
"United States" in countries
True
"Jupiter" in countries
False
for item in countries:
print(item)
France China Argentina United States
countries.add("France")
countries
{'Argentina', 'China', 'France', 'United States'}
temps = [12, 12, 12, 13, 11, 12, 12, 13, 14, 14, 15, 14, 13, 11]
set(temps)
{11, 12, 13, 14, 15}
list(set(temps))
[11, 12, 13, 14, 15]
countries[1]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-165-57d5c736dde3> in <module>() ----> 1 countries[1] TypeError: 'set' object does not support indexing
list(range(100))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
for i in range(10):
print("hello")
hello hello hello hello hello hello hello hello hello hello
in
¶numbers_list = list(range(100000000))
numbers_set = set(numbers_list)
99999999999999 in numbers_list
True
99999999999999 in numbers_set
False
sonnet_words = set(open("sonnets.txt").read().split())
"love" in sonnet_words
True
"tyrannosaurus" in sonnet_words
False
t = ("a", "b", "c", "d", "e")
t
('a', 'b', 'c', 'd', 'e')
type(t)
tuple
t[3]
'd'
t[-1]
'e'
t[1:3]
('b', 'c')
t.append("f")
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-204-f3e9ba23d3d4> in <module>() ----> 1 t.append("f") AttributeError: 'tuple' object has no attribute 'append'
shopping
{'apple': 4, 'bag of flour': 1, 'bag of sugar': 1, 'cinnamon stick': 1, 'egg': 3, 'stick of butter': 2}
shopping.items()
dict_items([('apple', 4), ('bag of flour', 1), ('egg', 3), ('bag of sugar', 1), ('stick of butter', 2), ('cinnamon stick', 1)])
tuple([1, 2, 3, 4, 5])
(1, 2, 3, 4, 5)
list((1, 2, 3, 4, 5))
[1, 2, 3, 4, 5]
text = "this is a test"
words = text.split()
words
['this', 'is', 'a', 'test']
" and ".join(words)
'this and is and a and test'
temp_str = "14,15,16,14,12,11,16,17,18,20,11"
f_temp = [int(val) * (9 / 5) + 32 for val in temp_str.split(",")]
','.join([str(val) for val in f_temp])
'57.2,59.0,60.8,57.2,53.6,51.8,60.8,62.6,64.4,68.0,51.8'