Searching through two-dimensional lists is best done with a FOR loop Each time, you can use an IF statement to check a particular part of the list item Sorting a 2D list is easy as long as the field to sort by is the first item (first name in the example below) Tasks Type in […]
Author: Daniel Norfolk
This may sound complicated, but lists within lists are the best way to save ‘records’ i.e. multiple bits of information about one thing, such as the name, address and telephone number of a user This type of list is known as a two-dimensional list Lists are sometimes referred to as ‘arrays’ Index 0 1 2 […]
A couple more tricks can be performed with lists to make them really useful Tasks Type in the code and make sure it works Create a new list with 10 numbers in it, and print them sorted into order Modify the program to accept user input to add more items to the list Save, print […]
The index number of the list item allows you to edit and delete particular items once located This example uses the in operator to quickly check the whole list If the search term exists, it then looks up the index, and uses this to directly edit the name “Sarah”, changing it to “Sara” Tasks Type […]
FOR and WHILE loops are essential to make full use of lists with the minimum amount of code The len() function can tell you how big your list is The in operator will be your best friend soon. It makes life very easy Tasks Type in the code and make sure that it works Modify […]
Lists are essential when using Python to store large amounts of information, especially if we don’t know how much in advance A list has a name, just like a variable but there may be many lists within it too (known as two-dimensional lists (that’s for later!) Each list has an index, starting at 0 The […]
The subroutines on the other page are simply procedures that run the code within then when called. The other type is a function that returns value(s) rather than printing to the screen Tasks Type in the code and make sure it works. Create a function that takes the radius of a circle, and returns the […]
Subroutines (sometimes known as procedures) allow you to split your program into reusable chunks that you can call from any other part of your program Subroutines always appear before the main program They can work on their own or you can pass them values Subroutines do not return values to the main program Tasks Type […]
Main Task Create a program that: Randomly generates a number between 1 -100 Asks the user to guess the number until they get it right Says whether the guess is too high or too low Says when they have guessed correctly, and quits Extension Task Modify the program so it records the number of guesses […]
Remember, indenting your code indicates it is inside a loop Always set your variable properly so the loop runs at least once Don’t forget the colon to start the conditional statement Tasks Type in the code and make sure that it works Change the code so it loops 20 times and prints out x each […]