Lesson 2
Problem: Input scalar floating point and integers¶
- Create a variable called
scalar_float
containing $\pi$ to 4 digits. - Create a variable called
scalar_int
containing 31415. - Print each value using the
print
function.
In [ ]:
Problem: Create a string and an f-string¶
- Create a variable called
a_string
containingThis is a string
- Create a f-string the prints
The value of scalar_float is 3.1415
using the variable created in the previous step - Create two string,
first
containingString concatenation
and the second containingis like addition
, and join the two using+
to produceString concatenation is like addition
.
In [ ]:
Problem: Create a list¶
- Create a list containing
scalar_float
andscalar_int
- Add
a_string
to the list. - Select the second element from the list
- Select the the lst two elements of the list
In [ ]:
Problem: Create a list of lists¶
- Create a list containing the two lists
[1, 2, 3]
and[4, 5, 6]
- Select the element 5 from the nested list
In [ ]:
Problem: Create a tuple¶
- Create a tuple containing the values (1, 2.0, "c")
- Select the element "c" from the tuple
In [ ]:
Problem: Convert a list to a tuple and back¶
- Convert the list-of-lists created to a tuple-of-tuples
- Convert the tuple-of-tuples back to a list of lists
In [ ]:
Problem: Create a dictionary¶
- Create a dictionary containing the key-value pairs
"float"
and 3.1415,"int"
and 31415, and"string"
and"three-point-one-four-one-five"
.
In [ ]:
Problem: Lookup and Change a value¶
- Look up the value of
"float"
. - Change the value of
"float"
to22 / 7
.
In [ ]:
Problem: Add and remove a key¶
- Add the new key "better_float" with the value 3.141592.
- Remove the key "float" and its value.
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Exercise: Dictionary Manipulation¶
- Create a empty dictionary called
dct
- Add the pairs "apple" and 1, "banana" and 2.0, and "cherry" and "iii"
- Replace the value of "apple" with "I"
- Remove the entry for "banana"
- Add an entry "date" with the value 4
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Exercise: Directly create a Dictionary¶
Using the final verion of dct
from the previous exercise:
- Directly initialize a new dictionary called
other_dct
. - Use an f-string to print the values associated with each key.
Hint You must use both types of quotes. For example, to access a value in an f-string.
f"{other_dct['apple']}"
In [ ]:
In [ ]:
Exercise: Tuple Manipulation¶
- Create a tuple
tpl
containing 100 and 4 - Convert to a list, add the elements 101 and 5
- Convert back toa tuple
In [ ]:
In [ ]:
In [ ]: