Assignment 2

Instructions:

Assignment 2 will cover Python’s Built-in Data Types and Built-in Data Structures. The overall goal of this assignment is to ensure that you are comfortable with the syntax and commonly-used objects and structures within Python. There are 2 parts to this assignment. For the first part, you will be answering questions and solving problems within this notebook, the second part requires you to modify a .py file that will be run within this notebook for simplicity. Follow the steps below to ensure that you receive a passing grade:

- Complete the following tasks within this notebook
- Create and modify the Assignment.py file to work as the assignment dictates
- When finished, convert this notebook to an HTML/PDF file
- Place the following items into a zip folder:
    - Assignment.HTML or Assignment.PDF
    - Assignment.ipynb
    - Assignment.py
- Name this folder using the naming convention: FIRSTNAME_LASTNAME_ASSIGNMENT2.zip
    - For example, If your name was Jane Doe, then the zipped folder should be called JANE_DOE_ASSIGNMENT2.zip

Part 1

The following chunk of code has an error. Correct the mistake and explain why using comments or markdown.

Myint = 23

print(myint)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-54455d7de866> in <module>
      1 Myint = 23
      2 
----> 3 print(myint)

NameError: name 'myint' is not defined

Your explanation goes here

The following arithmetic statement returns 24.0. Use only parentheses to group these operations such that it returns 32.0

20 + 24 / 4 - 2
24.0
# your code goes here
# 20 + 24 / 4 - 2

Cast output as a floating point value

x = 30
y = 29
output = x + y

# your code goes here

print("x = ",output)
print("type = ",type(output))
x =  59
type =  <class 'int'>

Calculate the square root of 23 and divide by 2 using floor division and print it. Also print what data type the number is.

# your code goes here

Create a print statement that returns the exact phrase This 'is' a "sample" sentence.

# your code goes here

The following sentence has the word brown repeated. Fix this typo using string slicing and concatenation. Then print the new string.

myString = "The quick brown brown fox jumps over the lazy dog"

# your code goes here

Use what you’ve learned about lists to remove all the foods from this list, then print it

myList = ["car", "apple", "mountain", "banana", "planet"]

# your code goes here

Why does this code raise an error?

myList = ["racecar", "sayonara"]

mySet = {"carousel", myList, "home"}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-bd5a332d1baa> in <module>
      1 myList = ["racecar", "sayonara"]
      2 
----> 3 mySet = {"carousel", myList, "home"}

TypeError: unhashable type: 'list'

Your Answer Goes Here

Count how many unique characters are in the following string and print it.

myString = """\
Lorem ipsum dolor sit amet, consectetur adipiscing elit, \
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi \
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit \
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \
Excepteur sint occaecat cupidatat non proident, \
sunt in culpa qui officia deserunt mollit anim id est laborum. \
"""


# your code goes here

Which of the following built-in data structures are immutable?

A. Lists
B. Tuples
C. Sets
D. Dictionaries

Your Answer Goes Here

In your own words (i.e. don’t copy from the lecture notebook), describe the following concepts in detail * Classes: Your Answer Goes Here * Objects: Your Answer Goes Here * Methods: Your Answer Goes Here * Attributes: Your Answer Goes Here

Your Answer Goes Here

Part 2

This part will require you to modify Assignment.py file. Perform all tasks in Part 2 in the .py file and run the code within this notebook.

A.

Create code that asks the user for:

- Their Name
- The Current Year
- Their Hair Color
- The Year They Were Born

Put these results into a dictionary and print the dictionary

Then calculate how old the user will be this year and add it to a new entry into the dictionary using a key named age

Print the dictionary again

print the following using only the dictionary and its keys:

-"Hello {The input name}, you have {the input hair color}."
-"The current year is {The input current year}."
-"You were born in {the input birth year}"
-"This means you will be {calculated age} years old this year"

B.

The area of a triangle can be computed using the following formula

\[ a = \frac{1}{2} b \times h\]

where \(b\) is the length of the base of the triangle, and \(h\) is its height. Write code that asks the user to enter values for \(b\) and \(h\). The code should then compute and display the area of a triangle with base length \(b\) and height \(h\). The information displayed to the console should look like this:

-"Input Height: "
-"Input Base: "
-"The area of the triangle is: {calculated area} "

C.

Create code that asks the user for two numbers, separated by a space. Split this input on a space character. Using the type() function, confirm that this split string object is a list. With this information, extract the values from that list, print their sum, and use a comparison operator to print whether the sum of these two numbers is greater than 10. The information displayed to the console should look like this:

-"Input two numbers, separated by a space: "
-"The sum of these two numbers is: {sum of numbers}"
-"Is the sum greater than 10? : {True/False}"
Run the cell below when you have finished modifying the python file
run Assignment.py