Homework 2
Contents
Homework 2#
Assignment Instruction#
Create a new Jupyter Notebook file to work on the following questions.
Run your codes to ensure the output is the same as the expected result of each question.
Rename your Jupyter Notebook (the
.ipynb
file) asHW2_<LASTNAME>.ipynb
, e.g.,HW2_CHEN.ipynb
.Submit this Jupyter Notebook no later than the deadline posted in the course schedule on Canvas.
1. Create a list of cities#
create a list variable named
cities
that contains top 20 cities in the world,print the variable.
ID |
Name |
Country |
Continent |
UN Estimation (2018) |
Urban Area |
---|---|---|---|---|---|
1 |
Tokyo |
Japan |
Asia |
37,400,068 |
4,751 |
2 |
Delhi |
India |
Asia |
28,514,000 |
14,272 |
3 |
Shanghai |
China |
Asia |
25,582,000 |
5,436 |
4 |
Sao Paulo |
Brazil |
South America |
21,650,000 |
6,949 |
5 |
Mexico City |
Mexico |
South America |
21,581,000 |
9,017 |
6 |
Cairo |
Egypt |
Africa |
20,076,000 |
9,844 |
7 |
Mumbai |
India |
Asia |
19,980,000 |
22,010 |
8 |
Beijing |
China |
Asia |
19,618,000 |
4,659 |
9 |
Dhaka |
Bangladesh |
Asia |
19,578,000 |
36,928 |
10 |
Osaka |
Japan |
Asia |
19,281,000 |
5,129 |
11 |
New York |
United States |
North America |
18,819,000 |
684 |
12 |
Karachi |
Pakistan |
Asia |
15,400,000 |
14,648 |
13 |
Buenos Aires |
Argentina |
South America |
14,967,000 |
5,033 |
14 |
Chongqing |
China |
Asia |
14,838,000 |
5,378 |
15 |
Istanbul |
Turkey |
Europe |
14,751,000 |
11,135 |
16 |
Kolkata |
India |
Asia |
14,681,000 |
13,830 |
17 |
Manila |
Philippines |
Asia |
13,482,000 |
12,798 |
18 |
Lagos |
Nigeria |
Africa |
13,463,000 |
7,877 |
19 |
Rio de Janeiro |
Brazil |
South America |
13,293,000 |
6,181 |
20 |
Tianjin |
China |
Asia |
13,215,000 |
3,886 |
# write your code below
Tokyo, Delhi, Shanghai, Sao Paulo, Mexico City, Cairo, Mumbai, Beijing, Dhaka, Osaka, New York, Karachi, Buenos Aires, Chongqing, Istanbul, Kolkata, Manila, Lagos, Rio de Janeiro, Tianjin
2. Calculate the ratio of Asian cities in the list#
create a new list called
asian_cities
.print the variable.
# write your code below
Tokyo, Delhi, Shanghai, Mumbai, Beijing, Dhaka, Osaka, Karachi, Chongqing, Kolkata, Manila, Tianjin
create two new integer variables to calculate the lengths of
cities
andasian_cities
pectively.print the two variables.
20
12
Define a new variable called
ratio
calculate the ratio by using the two lengths.
# write your code below
the ratio is: 0.6
3. Indexing and Selecting for list and string#
Select the 16th item in the
cities
and assign the value to a new variablecity
.create a new variable, you define its name, that calculates the number of characters in
city
.print both variables.
# write your code below
Kolkata
7
Convert the
city
variable to upper case and lower case and assign the results tocity_upper
andcity_lower
respectively.print the two new variables
# write your code below
KOLKATA
kolkata
obtain the second last (negative index) item of
cities
.assign the value you retrieved to a new variable
city2
.concatenate
city
andcity2
(separate with a whitespace) and print the result.
# write your code below
Kolkata Rio de Janeiro
Select and print out the middle part of the variable
city2
, i.e., characters between the two spaces.To do this, you must use proper start and end indices.
Your output should be the same as the following cell.
# write your code below
de
reverse the order of the
cities
and assign the value to a new variablere_cities
.print out the value of
re_cities
.
# write your code below
['Tianjin', 'Rio de Janeiro', 'Lagos', 'Manila', 'Kolkata', 'Istanbul', 'Chongqing', 'Buenos Aires', 'Karachi', 'New York', 'Osaka', 'Dhaka', 'Beijing', 'Mumbai', 'Cairo', 'Mexico City', 'Sao Paulo', 'Shanghai', 'Delhi', 'Tokyo']
4. Tuple#
convert
cities
to a tuple namedcities_tuple
.print the variable.
# write your code below
('Tokyo', 'Delhi', 'Shanghai', 'Sao Paulo', 'Mexico City', 'Cairo', 'Mumbai', 'Beijing', 'Dhaka', 'Osaka', 'New York', 'Karachi', 'Buenos Aires', 'Chongqing', 'Istanbul', 'Kolkata', 'Manila', 'Lagos', 'Rio de Janeiro', 'Tianjin')
obtain the top 3 cities from this tuple
use tuple unpacking to assign the items of the tuple to
city_1st
,city_2nd
, andcity_3rd
respectively.print them out.
Tokyo
Delhi
Shanghai
5. Dictionary, Boolean, and format()
function#
create a dictionary called
african_cities
, wherekey: the names of the two African cities in the
cities
value: a
list
contains two integers population and urban area
ID |
Name |
Country |
Continent |
UN Estimation (2018) |
Urban Area |
---|---|---|---|---|---|
6 |
Cairo |
Egypt |
Africa |
20,076,000 |
9,844 |
18 |
Lagos |
Nigeria |
Africa |
13,463,000 |
7,877 |
print the dictionary out
# write your code below
use
format()
function to print the following strings“The population of Cario is 20076000 and its urban area is 9844.”
“The population of Lagos is 13463000 and its urban area is 7877.”
you must substitute the numbers of population and urban area with the corresponding values and indexing in
african_cities
.
# write your code below
create two variables
cario_den
andlagos_den
calculate densities i.e., \(\frac{population}{urban\;area}\) for each city and stores them in the two variables respectively
compare the two variables using a comparison operator (print it out),
which city has a larger population (answer by print statement)?
# write your code below
True
Cario has a higher population density.