Assignment, Variables, Constants & Sequences
Inputs, Outputs & Type Conversion
IF Statements & Boolean Operators
Nested Selection and Iteration
WHILE, REPEAT-UNTIL Loops
Validation and verification
Validation and authentication routines
High-level & Low-level languages
Assemblers, Compilers & Interpreters
The most basic data structure in Python is the sequence (lists, tuples and strings). Every element in a sequence is assigned a number, which is its position or index. The first index is always zero, the second is one, and so forth.
Indexes are numbered from 0 to n-1 where n is the number of items (or characters):
H e l l o , _ w o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership.
The list is versatile data structure that is available in Python, it is a mutable data structure, which can be written as a list of comma-separated values (items) between square brackets.
An Important thing to note about a list is that items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
Click on the link below to go through a python tutorial on lists. By the end you should have a basic understanding of:
Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5.
An Array is a 'sequence' type data structure, which is a series of memory locations each of which holds a single data type. All data in an array must be of the same data type.
You can think of an array like a row of cells, like the ones found in a table. Each cell represents an element:
Research the differences between an array and a list:
Write a program that first ouputs the length of a list,(you will need to create a list first, then asks the user to choose an element from that list and then prints out the value of that element
e.g. For the list:
[1, 2, 4, 7, 11]
'The list has 5 elements'
Your program will return the third value: 4
Write a program that creates an array of all of the positive odd elements that are less than n, where n is the user input.
e.g. So if a users enters the number: 10
your program will return :
[1, 3, 5, 7, 9]
Watch the video below for a brief introduction on how to create multi-dimensional arrays
Copy the following code into python and then run it to see if you can understand how it is manipulating a two dimensional array/list.
a = [[1, 2, 3], [4, 5, 6]]
b = a
a = 7
b = 9
To process a 2-dimensional array/list, you will most often use nested loops. The first loop iterates through the row number, the second loop runs through
the elements inside of a row, which you can think of like a column in a table.
a = [[1, 2, 3, 4], [5, 6]]
b = [[3, 7, 2, 7], [5, 4]]
Click the button below for a solution to task 1.