0% found this document useful (0 votes)
96 views7 pages

A2 Worksheet - The Solar System

Uploaded by

xuanlwx0630
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views7 pages

A2 Worksheet - The Solar System

Uploaded by

xuanlwx0630
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operations on lists .

Source: docs.python.org/3/tutorial/datastructures.html#more-on-lists

Add and remove items

list.append(item) Add an item to the end of the list.

e.g. numbers.append(42)

list.insert(index, item) Insert an item at a given position.

e.g. cities.insert(2, "Oslo")

list.pop(index) Remove the item at the given


position in the list, and return it. If no
e.g. last = values.pop() index is specified, remove and
return the last item in the list.

list.remove(item) Remove the first item from the list


with a particular value. Raises a
e.g. countries.remove("Japan") ValueError if there is no such
item.

Find and count items

list.index(item) Search for the first occurrence of


an item in the list and return its
e.g. pos = planets.index("Mars") (zero-based) index. Raises a
ValueError if there is no such
item.

list.count(item) Return the number of times an item


appears in the list.
e.g. nb_the = words.count("the")
Modify list

list.reverse() Reverse the items of the list.

e.g. values.reverse()

list.sort() Sort the items of the list.

Use the reverse=True argument


e.g. names.sort()
to sort in descending order.
e.g. numbers.sort(reverse=True)

Worked example: Third rock from the sun


This program searches for the index of Earth in the list of planets.

1 planets = ["Mercury", "Venus",


2 "Earth", "Mars",
3 "Jupiter", "Saturn",
4 "Uranus", "Neptune"]
5 position = planets.index("Earth") + 1
6 print("Earth is planet number", position)

Here are the contents of the planets list, with an index next to each item:
0 1 2 3 4 5 6 7

"Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune"

The solar system

Task 1 Planets
Mercury, Venus, Mars, Jupiter, and Saturn are visible from Earth. These planets have
been known since antiquity.
By 1930, astronomers had added Uranus (1781), Neptune (1846), and Pluto (1930) to the
list of known planets.
A score of discoveries in the early 2000s led to the demotion of Pluto; since 2006 it is no
longer considered a planet and is classified instead as a dwarf planet.
In this task, you will create a program that recounts this short story about our view of
the planets in our solar system.

Step 1
Open this incomplete program (ncce.io/py-solar-1) in your development environment:

1 planets = ["Mercury", "Venus",


2 "Earth", "Mars",
3 "Jupiter", "Saturn"]
4 print("Planets in antiquity:")
5 print(planets)
6 # Add Uranus to the list
7 # Add Neptune to the list
8 # Add Pluto to the list

9 print("Planets by 1930:")
10 print(planets)
11 # Remove Pluto from the list

12 print("Planets after 2006:")


13 print(planets)

Challenge Steps 2 to 5

Step 2
Complete line 6, so that "Uranus" is added to the end of the list of planets.
Tip: Consult the ‘Operations on lists’ section at the beginning of this worksheet to find out
how to add an item to the end of a list.

Step 3
Run the program to make sure that Uranus is included in the list of planets “known by
1930”.

Example
Note: Use this example to check your program.

This should be part of the Planets by 1930:


program’s output: check that the ['Mercury', 'Venus', 'Earth', 'Mars',
items and their order are correct.
'Jupiter', 'Saturn', 'Uranus']

Step 4
Complete line 7 and 8, so that "Neptune" and "Pluto"are also added to the end of the
list of planets.

Step 5
Run the program, to make sure that Neptune and Pluto are included in the list of planets
“known by 1930”.

Example
Note: Use this example to check your program.

This should be part of the Planets by 1930:


program’s output: check that the ['Mercury', 'Venus', 'Earth', 'Mars',
items and their order are correct.
'Jupiter', 'Saturn', 'Uranus',
'Neptune', 'Pluto']
Challenge Steps 6 to 7

Step 6
Complete line 11, so that "Pluto" is removed from the list of planets.
Tip: Consult the ‘Operations on lists’ section at the beginning of this worksheet to find out
how to remove an item from a list.

Step 7
Run the program, to make sure that Pluto is not included in the list of planets “after
2006”.

Example
Note: Use this example to check your program.

This should be part of the Planets after 2006:


program’s output: check that the ['Mercury', 'Venus', 'Earth', 'Mars',
items and their order are correct.
'Jupiter', 'Saturn', 'Uranus',
'Neptune']

Task 2 Dwarf planets


In 2006, astronomers set out rules that would classify certain objects in the solar system
as dwarf planets. At present there are five such objects (but this list is subject to
change): Ceres, Pluto, Haumea, Eris, and Makemake, ordered according to their date of
discovery.
There was controversy around which team the discovery of Haumea should be
attributed to. The name originally proposed for it was Ataecina.
In this task, you will complete a program that displays the list of dwarf planets.

Step 1
Open this incomplete program (ncce.io/py-solar-2) in your development environment:

1 dwarves = # Create list of dwarves


2 # Change 2nd item to Haumea
3 # Add Pluto as 2nd item
4 print("Dwarf planets:")
5 print(dwarves)

Challenge Steps 2 and 3

Step 2
Complete line 1, so that the list of dwarf planets comprises Ceres, Ataecina, Eris, and
Makemake, in that order.
Tip: Check how the list of planets is created in the first task.

Step 3
Run the program to make sure that the list of dwarf planets is correct.

Example
Note: Use this example to check your program.

This should be part of the Dwarf planets:


program’s output: check that the ['Ceres', 'Ataecina', 'Eris',
items and their order are correct.
'Makemake']

Challenge Steps 4 and 5

Step 4
Complete line 2, so that the second item in the list is assigned the value of "Haumea".

Tip: You will need to assign the new value to the second item of the dwarves list.

Tip: Item numbering starts from 0, so the second item has an index of 1.

Step 5
Run the program to make sure that the second item in the list is Haumea, instead of
Ataecina.

Example
Note: Use this example to check your program.
This should be part of the Dwarf planets:
program’s output: check that the ['Ceres', 'Haumea', 'Eris', 'Makemake']
items and their order are correct.

Challenge Steps 6 to 7

Step 6
Complete line 3, so that "Pluto" is added to the list, as its second item.
Tip: Consult the ‘Operations on lists’ section at the beginning of this worksheet to find out
how to add an item to the end of a list.
Tip: Item numbering starts from 0, so the second item has an index of 1.

Step 7
Run the program to make sure that Pluto is now the second item in the list, preceded by
Ceres and followed by Haumea, Eris, and Makemake, in that order.

Example
Note: Use this example to check your program.

This should be part of the Dwarf planets:


program’s output: check that the ['Ceres', 'Pluto', 'Haumea', 'Eris',
items and their order are correct.
'Makemake']

Explorer task Add them up


Combine your programs for planets (Task 1) and dwarf planets (Task 2) into one.
At the end of your new program, add the following lines:

+ solar = planets + dwarves


+ solar.sort()
+ print("Planet-mass objects:")
+ print(solar)

Can you predict what the program will display, before actually running it?

You might also like