Descriptive Strings

Edit it in Spyder; when you are done you should be able to run the whole file without errors/exceptions.

# -*- coding: utf-8 -*-

# 1. if-elif-else

# Fill missing pieces (____) of the following code

# such that the “print” statements make sense.

name = ‘John Doe’

if ____:

print(‘Name “{}” is more than 20 chars long’.format(name))

length_description = ‘long’

elif ____:

print(‘Name “{}” is more than 15 chars long’.format(name))

length_description = ‘semi long’

elif ____:

print(‘Name “{}” is more than 10 chars long’.format(name))

length_description = ‘semi long’

elif ____:

print(‘Name “{}” is 8, 9 or 10 chars long’.format(name))

length_description = ‘semi short’

else:

print(‘Name “{}” is a short name’.format(name))

length_description = ‘short’

# 2. for loops

# A. Fill the ____ parts in the code below.

words = [‘PYTHON’, ‘JOHN’, ‘chEEse’, ‘hAm’, ‘DOE’, ‘123’]

upper_case_words = []

for ____ in words:

if ____.isupper():

____.append(____)

# B. Calculate the sum of the values in magic_dict by taking only

# into account numeric values (hint: read the “isinstance” docs).

magic_dict = dict(val1=44, val2=’secret value’, val3=55.0, val4=1)

sum_of_values = 0

# Use a for loop here and update sum_of_values appropriately

# 3. Functions

# A. Fill inthe ____ blanks. Assume the argument is a list of integers.

____ count_even_numbers(numbers):

count = 0

for num in ____:

if ____ % 2 == ____:

count += ____

_____ _____

# B. Trasform a list of numbers to a list of descriptive strings

# using these rules:

# For odd multiples of five, the string should be ‘five odd’

# For even multiple of five, the string should be ‘five even’

# For odd and not a multiple of five, the string is ‘odd’

# For even and not a miultiple of five, the string is ‘even’

def describe_numbers(___):

# Your implementations here

# 4. Strings and lists

# A. Use str methods to convert ugly to wanted pretty.

ugly = ‘ tiTle of MY new Booknn’

# Your implementation: use str methods on ugly

pretty = ‘TODO’

# B. Number formatting — show pi to 4 decimal places: fill in the ___ blank

from math import pi

pi_to_four = ‘___’.format(pi)

# C. Lists amd sorting

# Take these three lists and create a single list, sorted descending

# Use list methods! Hint: help(sorted)

list1 = [6, 12, 5]

list2 = [6.2, 0, 14, 1]

list3 = [0.9]

my_sorted_list = ___

# 5. Dictionaries

# A. Create a dicitonary with keys that are the first 5 positive integers

# and the values are the squares of those numbers

square_integers = {___}

# B. Create a dict of the cubes of the next 5 integers, and then merge that

# with the first dictionsarys using the dict.update() method.

# Hint: reread the notebook for Lecture 4!

squares_and_cubes = {___}

# These are the answers

# The “assert” keyword will raise an AssertionError if the

# the statement that follows it is not True

assert length_description == ‘semi short’

assert upper_case_words == [‘PYTHON’, ‘JOHN’, ‘DOE’]

assert sum_of_values == 100

numbers = [1, 3, 4, 6, 81, 80, 100, 95]

my_list = describe_numbers(numbers)

assert my_list = [

‘odd’,

‘odd’,

‘even’,

‘even’,

‘odd’,

‘five even’,

‘five even’,

‘five odd’

]

assert pretty == ‘Title Of My New Book’

assert pi_to_four == ‘3.1416’

assert my_sorted_list == [14, 12, 6.2, 6, 5, 1, 0.9, 0]

# Remember that dictionairies don’t have a defined order. To be equal,

# they simply have to have the same key-value pairs.

assert square_integers == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

assert squares_and_cubes == {6: 216, 7: 343, 8: 512, 9: 729, 10: 1000,

1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Do you need help with this assignment or any other? We got you! Place your order and leave the rest to our experts.

Quality Guaranteed

Any Deadline

No Plagiarism