Reading Python Code #04 Arrays
CHG
Key Ideas
[] brackets means it is a list.
Items are separated by commas.
For loops can help you print them all out.
.append() can add things to a list
.remove() can also add things
There are also a lot of other neat features!
Part 1
Basic Python Code
Try to see what the code does or write it in Spyder and run it.
You could also just explain what it does as well…
See what Happens
Yes…Mr Chuang made it easier to start with.
Q1
What gets printed out into the console?
Q2
What gets printed out into the console?
Q3
What gets printed out into the console?
Q4
What gets printed out into the console?
Q5
What gets printed out into the console?
Q6
What gets printed out into the console?
Part 2
Back to normal.
Try to figure out the problem and put your code in.
Animals #1
We’re collecting data on neighbourhood pets.
Ask the user to enter 5 animals/pets, and then print the list out.
YOUR ANSWER HERE
Hint. Delete to show.
Animals #2
After the user has entered 5 animals, find out how many cats are in the list.
YOUR ANSWER HERE
Hint. Delete to show.
Turned in Homework.
Here is a list of names:
Write a program to ask whether that person is in the list and have turned in their homework.
YOUR ANSWER HERE
Hint. Delete to show.
Detention (Names are an EXAMPLE)
Mr Chuang has a list of students for detention. Write a program to ask the user for a name and see if they’re free to go.
YOUR ANSWER HERE
Hint. Delete to show.
Adding up all numbers
Here is a list of numbers. Use a loop to find the total and print it out at the end of the loop.
YOUR ANSWER HERE
Hint. Delete to show.
Average(Mean) Credits of 12CHG
Here is a list of student’s and their credits in Mr Chuang’s Y12 class has.
Write a program to calculate the average number of credits his students have. What happens if we add 1 more person to the list?
YOUR ANSWER HERE
Hint. Delete to show.
Academic Tracking
Mr Chuang has recorded the his class’ credits. Write a program to find out how many students have passed and the pass rate as a percentage for this class. Assume 80 credits to pass.
YOUR ANSWER HERE
Hint. Delete to show.
Starting with A
Ask the user for a word and see if it starts with an A.
YOUR ANSWER HERE
Hint. Delete to show.
Part 3
Travel Log
Or just jump straight to the practice tasks.
Travel Log
Ask the user to enter a location and how much they have spent at a location. This can be done in 2 separate inputs
Store the information by using arrays.
You may initialise some locations beforehand to not need to re-enter it everytime it runs. Here is the sample data to store in an array:
Locations: Katikati, Waihi, Tauranga, Auckland
Money spent:20,100,300,400
If they input "end" print out a summary of their travel log.
The summary is all the locations they've been and how much they have spent in each location.
Then print the total locations they have travelled to and the total amount they have spent.
Extension 1: Print out the maximum, minimum amount spend and the location.
Extension 2: Print the average amount that was spent.
Extension 3: Print out the mode that was spent.
Challenge1: Do the extension above WITHOUT the math/statistics library
Challenge2: Losing the data everytime is a pain. Investigate how to save the data between execution of the program.
Challenge3: Obtain input from 1 input rather than 2. How would you handle unexpected inputs now?
Code - in notes.
All the code is in comments below.