0 results for
ICT
Computing
Responsive image

C6 - Data structures

Overview
Flight Path
Learning Objectives
0 results forGuest
GradeSkill_name
 3.6 I can select, apply, use and interpret LISTS to analyse and improve the effectiveness of code.
 3.6 I can select, apply, use and interpret single dimensional arrays to analyse and improve the effectiveness of code.
 3.6 I can select, apply, use and interpret multi-dimensional arrays to analyse and improve the effectiveness of code.
 3.6 I can select, apply, use and interpret Records to analyse and improve the effectiveness of code.
 3.6 I can select, apply, use and interpret Sets to analyse and improve the effectiveness of code.

Strength

Improvement

Targets

Progress this topic

0%


Overall Progress for this unit

NAN%


LO69 :- I can select, apply, use and interpret LISTS to analyse and improve the effectiveness of code.

Sequence

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.

Python Lists

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:

  • Accessing Values in Lists
  • Updating Lists
  • Deleting List Elements
  • Basic List Operations
  • Built-in List Functions & Methods:
  • Click here -> tutorialspoint

    Exercises

    Exercise 1

    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.

    Extension questions:

    1. Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list.
    2. Ask the user for a number and return a list that contains only elements from the original list 'a' that are smaller than that number given by the user.
    3. Write this in one line of Python. (Requires 'List comprehension')

    Click the button below for a sample solution that solves the exercise, including extras 1 and 2.

    ×

    Code Output

    a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    
    num = int(input("Choose a number: "))
    
    new_list = []
    
    for i in a:
    	if i < num:
    		new_list.append(i)
    
    print (new_list)
    

    Is this what you got?


    LO70 :- I can select, apply, use and interpret single dimensional arrays to analyse and improve the effectiveness of code.

    Arrays

    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:

    break

    Exercises

    Exercise 1

    Research the differences between an array and a list:

    Exercise 2

    Write a function that returns an array of all of the positive odd elements that are less than n, where n is the user input.

    So if a users enters the number: 10 your funtion will return :

     [1, 3, 5, 7, 9]

    Extension question:

    1. Write your function so it has only one line of Python. (Requires 'List comprehension')

    Click the button below for a solution.

    ×

    Code Output

    num = int(input("Enter a number: "))
    
    def odd_less(n):
      return [x for x in range(0, n) if x % 2 == 1 ] #single line list comprehension
    
    new_list = odd_less(num)
    
    print (new_list)
    

    Is this what you got?



    break




    LO71 :- I can select, apply, use and interpret multi-dimensional arrays to analyse and improve the effectiveness of code.

    Multi-Dimensional Arrays

    Watch the video below for a brief introduction on how to create multi-dimensional arrays

    Exercise

    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]]
    print(a[0])
    print(a[1])
    b = a[0]
    print(b)
    print(a[0][2])
    a[0][1] = 7
    print(a)
    print(b)
    b[2] = 9
    print(a[0])
    print(b)
    

    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]]
    

    Extension questions:

    1. Write a nested loop so that it prints out all of the elements of the two-dimensional array 'a', in a table like structure
    2. Find the index of the first occurrence of the maximum element in list 'b'. Print two numbers: the row number and the column number where the largest element in a two-dimensional array is.
      • If there is more than one element, print the one with smaller row number. If there are many maximums with the smallest row number, output the one which has the least column number.
    3. Chess board - Given two numbers n and m. Create a two-dimensional array of size n×m and populate it with the characters "." and "*" in a checkerboard pattern. Top left corner should have ".".

    Click the button below for a solution to task 1.

    ×

    Code Output

    for row in range(len(a)):
        for column in range(len(a[row])):
            print(a[row][column], end=' ')
        print()
    

    Is this what you got?


    LO72 :- I can select, apply, use and interpret Records to analyse and improve the effectiveness of code.

    Sets

    Sets are unordered collections of simple objects. These are used when the existence of an object in a collection is more important than the order or how many times it occurs.

    Using sets, you can test for membership, whether it is a subset of another set, find the intersection between two sets, and so on.

    break
    break
    break

    Exercise

    Exercise 1

    Given a list of integers e.g. 1 2 3 2 1.

    Determine how many distinct numbers there are. This task can be solved in one line of code.

    Extension questions:

    1. Given two lists of numbers. Find all the numbers that occur in both the first and the second list and print them in ascending order.

    LO73 :- I can select, apply, use and interpret Sets to analyse and improve the effectiveness of code.

    Dictionaries

    A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details).

    Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name.

    break
    break
    break
    break

    Exercise

    Given the following dictionary

    A = {'ab' : 'ba', 'aa' : 'aa', 'bb' : 'bb', 'ba' : 'ab'}

    Write a loop which iterates through the keys of all items in the dictionary:


    0 results forGuest
    Recent Comments

    Teacher Date: 2019-12-12


    Guest