Multiple items of information are sometimes stored on each line in a file that your program might load up. They might be separated by commas or spaces and you can easily split the lines up and re-join them once you have finished Dates and product codes are frequently manipulated using these techniques Tasks Type in […]
Category: Uncategorized
Using loops and comparisons, you can do some pretty nifty things with strings of characters Performing comparisons is much more powerful when you use loops to inspect each individual character Note the in operator works on strings as well as lists Tasks Type in the code and test it works Modify the program to count […]
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 […]
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 […]