HW3, practice with python data types and syntax

This assignment reviews materials in lectures 4 and 5.

In all cases, programming should be done in python 3 and you should use any good programming practices that we have discussed so far (e.g. use of white space).

1. Data types

  1. Initialize a variable as a string which can be converted into a float, do so, and then print the number (with two decimal places) and object type that the variable points to.
In [ ]:

  1. Initialize variables pointing to the four types of collections discussed in class. Using comments, state two properties/features of each type of collection.
In [ ]:

  1. Create a list with the values 4, 9, 16, 25, 36, and 49 (in that order). Print 25, 36, 49, twice by using two different ways of indexing values. What happens when you index from a higher number to a lower number?
In [ ]:

2. Simple functions and logic

  1. Calculate a value from a formula of your choice that extends over two lines
In [ ]:

  1. Write a loop (whichever kind you wish) that creates a list containing the first 10 numbers in the Fibonacci sequence, where the first number is 0. Then, print the total of those 10 numbers and the final list.
In [ ]:

  1. Initialize a variable to point to a sentence of your choice. Check whether a word is contained within the string. If so, replace it with another word of your choice.
In [ ]:

  1. Predict the output from the following code block, then run it in python to check if you were correct:
var = None alt_var = None my_set = {7, 5, 2, 1, 7, 9} if len(my_set) == 5: alt_var = {2: "ten", 3: "fifteen"} else: var = [2, 5, 7] if var: print(var[2]) if alt_var: print(alt_var[2])