Dictionaries

Rather than storing single objects like lists and sets do, dictionaries store pairs of elements: keys and values.

elements = {'hydrogen': 1, 'helium': 2, 'carbon': 6}
>>> print(element['carbon'])
6
>>> elements['lithium'] = 3
>>> print(elements['lithium'])
3
populations = {'Shanghai':17.8, 'Istanbul':13.3, 'Karachi':13.0, 'Mumbai':12.5}
if 'mithril' in elements:
	print("That's a real element!")
else:
	print("There's no such element")
>>> elements.get('dilithium')
>>> elements['dilithium']

Traceback (most recent call last):
  File "", line 1, in 
    elements['dilithium']
KeyError: 'dilithium'
>>> elements.get('kryptonite', 'There\'s no such element!')
"There's no such element!"
colors = set(['Pathalo Blue', 'Indian Yellow', 'Sap Green'])
for color in colors:
	print(color)
>>> elements = {'hydrogen': {'number':1, 'weight':1.00794, 'symbol':'H'},
	    'helium':{'number':2, 'weight':4.002602, 'symbol':'He'}}
>>> print(elements['helium'])
{'symbol': 'He', 'number': 2, 'weight': 4.002602}
>>> print(elements.get('unobtainium', 'There\'s no such element!'))
There's no such element!
>>> print(elements['helium']['weight'])
4.002602

Reorganizing code

Factoring: decomposing a complex problem into simpler parts.
Refactoring: restructuring existing code.

Reading the code, is it clear what each piece does? How could it be be easier?
If you needed to change some part of the functionality, would that be easy? Would you have to change the same thing in several places?
If you break down what the function does into steps, how many steps are there? It’s best to have each function doing only one thing.
Is there unnecessary repetition? Does every piece of code get used? Could anything be more succinct whilst still being readable? This is called the DRY (Don’t Repeat Yourself) principle.

def check_answers(my_answers, answers):
	results = [None, None, None, None, None]
	if my_answers[0] == answers[0]:
		results[0] = True
	elif my_answers[0] != answers[0]:
		result[0] = False
	if my_answers[1] == answers[1]:
		result[1] = True
	elif my_asnwers[1] != anwers[0]:
		results[1] = False
	if my_answers[2] == answers[2]:
        results[2] = True
    elif my_answers[2] != answers[2]:
        results[2] = False
    if my_answers[3] == answers[3]:
        results[3] = True
    elif my_answers[3] != answers[3]:
        results[3] = False
    if my_answers[4] == answers[4]:
        results[4] = True
    elif my_answers[4] != answers[4]:
        results[4] = False
    count_correct = 0
    count_incorrect = 0
    for result in results:
    	if result == True:
    		count_correct += 1
    	if result != True:
    		count_incorrect += 1
    if count_correct/5 > 0.7:
    	return "Congratulations, you passed the test! you scored " + str(count_correct) + " out of 5."
    elif count_incorrect/5 >= 0.3:
    	return "Unfortunately, you did not pass. You scored " + str(count_correct) + " out of 5."
>>> len(countries)
785
>>> countries[:5]
['Angola', 'Maldives', 'India', 'United States', 'India']
def remove_duplicates(source):
	target = []

	for element in source:
		if element not in target:
			target.append(element)

	return target
country_set = set(countries)
len(country_set)

country_set.add("Florin")

squares = set()

def nearest_square(limit):
answer = 0
while (answer+1)**2 < limit: answer += 1 return answer**2 n = 1 while n**2 < 2000: squares.add(n**2) n += 1 [/python]

while loop

card_deck = [4, 11, 8, 5, 13, 2, 8, 10]
hand = []

while sum(hand) <= 17:
	hand.append(card_deck.pop())

print(hand)
&#91;/python&#93;

&#91;python&#93;
manifest = &#91;&#91;"bananas", 15&#93;, &#91;"mattresses", 34&#93;, &#91;"dog kennels", 42&#93;,&#91;"machine that goes ping!", 120&#93;, &#91;"tea chests", 10&#93;, &#91;"cheeses", 0&#93;&#93;

cargo_weight = 0
cargo_hold = &#91;&#93;

for cargo in manifest:
	if cargo_weight >= 100:
		break
	else:
		cargo_hold.append(cargo[0])
		cargo_weight += cargo[1]
cargo_weight = 0
cargo_hold = []

for cargo in manifest:
	print("debug: the weight is currently: {}".format(cargo_weight))
	if cargo_weight >= 100:
		print("debug: breaking loop now!")
		break
	else:
		print("debug: adding item: {}".format(cargo[0]))
		print("debug: with weight: {}".format(cargo[1]))
		cargo_hold.append(cargo[0])
		cargo_weight += cargo[1]

For Loops

>>> names = ['charlotte hippopotamus turner', 'oliver st. john-mollusc', 'nigel incubator-jones', 'philip diplodocus mallory']
>>> for name in names:
	print(name.title())

	
Charlotte Hippopotamus Turner
Oliver St. John-Mollusc
Nigel Incubator-Jones
Philip Diplodocus Mallory
def list_sum(input_lists):
    sum = 0
    for input_list in input_lists:
        sum = sum + input_list
    return sum

test1 = list_sum([1, 2, 3])
print("expected result: 6, actual result: {}".format(test1))

test2 = list_sum([-1, 0, 1])
print("expected result: 0, actual result: {}".format(test2))
>>> names = ['charlotte hippopotamus turner', 'oliver st. john-mollusc', 'nigel incubator-jones', 'philip diplodocus mallory']
>>> capitalized_name = []
>>> for name in names:
	capitalized_name.append(name.title())

	
>>> print(capitalized_name)
['Charlotte Hippopotamus Turner', 'Oliver St. John-Mollusc', 'Nigel Incubator-Jones', 'Philip Diplodocus Mallory']

>>> for index in range(len(names)):
	names[index] = names[index].title()
>>> for i in range(3):
	print("Camelot!")

	
Camelot!
Camelot!
Camelot!
>>> print("It's only a model.")
It's only a model.
for _ in range(height-2):
	print("*" + " " * (width-2) + "*")
def starbox(width, height):
	print("*" * width)

	for _ in range(height-2):
		print("*" + " " * (width-2) + "*")

	print("*" * width)

Joining Lists

>>> nautical_directions = "\n".join(["fore","aft","starboard","port"])
>>> print(nautical_directions)
fore
aft
starboard
port

>>> names = ["Garcia", "O'Kelly", "Davis"]
>>> "-".join(names)
"Garcia-O'Kelly-Davis"

Note that join will trigger an error if we try to join anything other than strings.

>>> python_vareties.append('Blood Python')
>>> print(python_vareties)
['Burmese Python', 'African Rock Python', 'Ball Python', 'Reticulated Python', 'Angolan Python', 'Blood Python']
def median(numbers):
	numbers.sort()
	if len(numbers) % 2:
		middle_index = int(len(numbers)/2)
		return numbers[middle_index]
	else:
		right_of_middle = len(number)//2
		left_of_middle = right_of_middle -1
		return (numbers[right_of_middle] + numbers[left_of_middle])/2

Lists, Strings, and Mutability

>>> sample_list[3]
'Eric'
>>> print(sample_list)
['Graham', 'John', 'Terry', 'Eric', 'Terry', 'Michael']
>>> sample_string[8] = 'f'

Traceback (most recent call last):
  File "", line 1, in 
    sample_string[8] = 'f'
TypeError: 'str' object does not support item assignment
>>> 
>>> name = "Old Woman"
>>> person = name
>>> name = "Dennis"
>>> print(name)
Dennis
>>> print(person)
Old Woman
>>> dish = ["Spam", "Spam", "Spam", "Spam", "Spam", "Spam", "baked beans", "Spam", "Spam", "Spam", "Spam"]
>>> mr_buns_order = dish
>>> print(dish)
['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'baked beans', 'Spam', 'Spam', 'Spam', 'Spam']
>>> print(mr_buns_order)
['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'baked beans', 'Spam', 'Spam', 'Spam', 'Spam']
>>> dish[6] = "Spam"
>>> print(mr_buns_order)
['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam']
>>> print(dish)
['Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam', 'Spam']

Working with Lists
len(some_list)
max(some_list)

>>> batch_sizes = [15, 6, 89, 34, 65, 35]
>>> max(batch_sizes)
89
>>> python_vareties = ['Burmese Python', 'African Rock Python', 'Ball Python', 'Reticulated Python', 'Angolan Python']
>>> max(python_varieties)

Traceback (most recent call last):
  File "", line 1, in 
    max(python_varieties)
NameError: name 'python_varieties' is not defined
>>> max(python_vareties)
'Reticulated Python'

sorted

>>> sorted(batch_sizes)
[6, 15, 34, 35, 65, 89]
>>> sorted(batch_sizes, reverse=True)
[89, 65, 35, 34, 15, 6]
>>> print(batch_sizes)
[15, 6, 89, 34, 65, 35]

List

>>> python_versions = [1.0, 1.5, 1.6, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6]
>>> python_versions[0]
1.0
>>> python_versions[1]
1.5
>>> python_versions[7]
2.4
>>> python_versions[-1]
3.6

Error

>>> my_list = ['a','b','c','d','e']
>>> my_list[4]
'e'
>>> my_list[5]

Traceback (most recent call last):
  File "", line 1, in 
    my_list[5]
IndexError: list index out of range
def how_many_days(month_number):
    days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
    return days_in_month[month_number-1]
    
print(how_many_days(8))

slice

>>> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> q3 = months[6:9]
>>> print(q3)
['July', 'August', 'September']
>>> print(months)
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003',
                 'March 29, 2006', 'August 1, 2008', 'July 22, 2009',
                 'July 11, 2010', 'November 13, 2012', 'March 20, 2015',
                 'March 9, 2016']
                 
last_eclipse= eclipse_dates[-3:]                 
print(last_eclipse)
>>> sample_string = "And Now For Something Completely Different"
>>> sample_list = ['Graham', 'John', 'Terry', 'Eric', 'Terry', 'Michael']
>>> sample_string[4]
'N'
>>> sample_list[4]
'Terry'
>>> sample_string[12:21]
'Something'
>>> sample_list[2:4]
['Terry', 'Eric']
>>> len(sample_string)
42
>>> len(sample_list)
6
>>> 'thing' in sample_string
True
>>> 'Rowan' in sample_list
False

Branches -2

def punctuate(sentence, phrase_type):
	if phrase_type == "exclamation":
		sentence_punct = sentence + "!"
	elif phrase_type == "question":
		sentence_punct = sentence + "?"
	elif phrase_type == "aside":
		sentence_punct = "(" + sentence + ".)"
	else:
		sentence_punct = sentence + "."
	return sentence_punct
def cylinder_surface_area(radius, height, has_top_and_bottom):
	side_area = height * 6.28 * radius
	if has_top_and_bottom:
		top_area = 3.14 * radius ** 2
		return top_area + side_area
	else:
		return side_area
errors = 3
if errors:
	print("There are " + errors + " mistakes. Please correct.")
else:
	print("No mistakes here!")
def convert_to_numeric(score):
    """
    Convert the score to a numerical type.
    """
    converted_score = float(score)
    return converted_score

def sum_of_middle_three(score1,score2,score3,score4,score5):
    """
    Find the sum of the middle three numbers out of the five given.
    """
    max_score = max(score1,score2,score3,score4,score5)
    min_score = min(score1,score2,score3,score4,score5)
    sum = score1 + score2 + score3 + score4 + score5 - max_score - min_score
    return sum

def score_to_rating_string(score):
    if score < 1:
    	return "Terrible"
    elif score < 2:
    	return "Bad"
    elif score < 3:
    	return "OK"
    elif score < 4:
    	return "Good"
    elif score <= 5:
    	return "Excellent"
    else:
    	return "nothing"

def scores_to_rating(score1,score2,score3,score4,score5):

    score1 = convert_to_numeric(score1)
    score2 = convert_to_numeric(score2)
    score3 = convert_to_numeric(score3)
    score4 = convert_to_numeric(score4)
    score5 = convert_to_numeric(score5)

    #STEP 2 and STEP 3 find the average of the middle three scores
    average_score =     
        sum_of_middle_three(score1,score2,score3,score4,score5)/3

    #STEP 4 turn average score into a rating
    rating = score_to_rating_string(average_score)

    return rating

Code with Branches

point = 189
prize = "No prize"

if point <= 50:
    prize = "wooden rabbit"
elif 51 <= point <= 150:
    prize = "No prize"
elif 151 <= point <= 180:
    prize = "wafer-thin mint"
elif 181 <= point <= 200:
    prize = "penguin"
    
if prize == "No prize":
    print("oh dear, no prize this time")
else:
    print("Congratulations! You have won a " + prize + "!" ) 

keywords like and, or and not

if is_raining and is_sunny:
	print("Is there a rainbow?")

if (not do_not_emai) and (location == "USA" or location == "CAN"):
	send_email()

branches

if 18.5 <= weight_in_kg / (height_in_m)**2 < 25:
	print("BMI is considered 'normal'.")
&#91;/python&#93;

&#91;python&#93;
if numer % 2 == 0:
	print("The number " + str(number) + " is even.")
else:
	print("The number " + str(number) + " is odd.")
&#91;/python&#93;

&#91;python&#93;
def garden_calendar(season):
	if season == "spring":
		print("time to plant the garden!")
	elif season == "summer":
		print("time to water the garden!")
	elif season == "autumn" or season == "fall":
		print("time to harvest the garden!")
	elif season == "winter":
		print("time to stay indoors and drink tea!")
	elif:
		print("I don't recognize that season")
&#91;/python&#93;

&#91;python&#93;
phone_balance = 7.62
bank_balance = 104.39

if phone_balance < 10:
	phone_balance += 10
	bank_balance -= 10
print(phone_balance)
print(bank_balance)

number = 145346334
if number % 2 == 0:
	print("The number " + str(number) + " is even.")
else:
	print("The number " + str(number) + " is odd.")

age = 35

free_up_to_age = 4
child_up_to_age = 18
senior_from_age = 65

concession_ticket = 1.25
adult_ticket = 2.50

if age <= free_up_to_age:
	ticket_price = 0
elif age <= child_up_to_age:
	ticket_price = concession_ticket
elif age >= senior_from_age:
	ticket_price = concession_ticket
else:
	ticket_price = adult_ticket

message = "somebody who is {} years old will pay ${} to ride the bus.".format(age,ticket_price)
print(message)