# for loop review
names = ['Joey', 'Leslie', 'Antonio']
# here 'names' is an iterable
# here 'name' is the iteration variable
for name in names:
print(name)
# solution 1: using the end argument to print elements separated by comma
for name in names[:-1]:
print(name, end=', ')
print(names[-1])
# solution 2: using string concatenation to print elements separated by comma
result = "" # placeholder, an empty string
for name in names[:-1]:
result += name + ", "
result += names[-1]
print(result)
# here we have a list of words
book_title = ['great', 'expectations','the', 'adventures', 'of', 'sherlock','holmes','the','great','gasby','hamlet','adventures','of','huckleberry','fin']
word_count = {}
for word in book_title:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
print(word_count)
# here we have a list of words
book_title = ['great', 'expectations','the', 'adventures', 'of', 'sherlock','holmes','the','great','gasby','hamlet','adventures','of','huckleberry','fin']
word_count = {}
for word in book_title:
word_count[word] = word_count.get(word, 0) + 1
print(word_count)
item_prices = [4, 11, 8, 5, 13, 2, 8, 10]
items = []
while sum(items) < 20:
print("Before adding: ", sum(items))
items.append(item_prices.pop())
print("After adding: ", sum(items))
print(items)
names = ['Joey', 'Leslie', 'Antonio']
for name in names:
if name == 'Leslie':
continue
print(name)
names = ['Joey', 'Leslie', 'Antonio']
while len(names) > 0:
name = names.pop()
if name == 'Leslie':
break
print(name)
# we have a list of tuples
# containing items and their weight in KGs
cargo_items = [('bananas', 15), ('beds', 24), ('rice', 42), ('machine', 120), ('milk', 5)]
# The maximum weight that your truck can take is 100 KG
weight = 0
items = []
# iteration variable here is tuple unpacking
for item_item, item_weight in cargo_items:
if weight > 100:
print("Truck weight is above 100 kg")
break
elif weight + item_weight > 100:
print("Skipping {}".format(item_item))
continue
else:
print("Adding {} into the truck.".format(item_item))
weight += item_weight
items.append(item_item)
print("Final weight: ", weight)
print("Final items: ", items)
%run -i students.py
# Exercise: find the nearest square number of an integer
# A square number is the product of an integer multiplied by itself
# for example 36 is a square number because it equals 6*6.
# For example, if limit is 40, your code should set the nearest_square to 36.
limit = 5
# TODO: find the largest square number less than the limit
# Use a while loop
integer = 1
while (integer + 1)**2 < limit:
largest_square = (integer + 1)**2
integer += 1
print(largest_square)
# Exercise 2: Create a tag counter
# You are given a list of tokens.
tokens = ['<greeting>', 'Hello World!', '</greeting>', 'test', '<tst>']
# TASK: count how many of them are XML tags.
# XML is a data language similar to HTML.
# You can tell if a string is an XML tag if it begins with
# a left angle bracket "<" and ends with a right angle bracket ">".
count = 0
for token in tokens:
if token[0] == "<" and token[-1] == ">":
count += 1
print(count)