85. OCR A Level (H046-H446) SLR14 - 1.4 Arrays, records, lists & tuples

Craig'n'Dave
24 Oct 202213:50

Summary

TLDRThis video script explores fundamental data structures like arrays, records, lists, and tuples. Arrays are contiguous memory allocations for storing data like names, with zero-based indexing. Python arrays mimic list behavior despite being static. Two-dimensional arrays are like tables, and three-dimensional can be imagined as cubes. Higher dimensions are abstract but manageable. Records, not available in Python, are collections of related fields with varying data types, used in languages like VB. Lists in Python are mutable and can change size, unlike the immutable tuples, which cannot be altered post-creation.

Takeaways

  • 🗃️ Arrays are variables that can store multiple data items in a contiguous block of memory.
  • 🔢 Arrays are typically zero-indexed, meaning the first item is at index 0.
  • 💾 Lists in Python are dynamically sized and can be used like arrays, but they are not stored contiguously in memory.
  • 📊 Two-dimensional arrays can be visualized as tables with rows and columns, accessed via two indexes.
  • 🧊 Three-dimensional arrays can be thought of as cubes, with items accessed by three index values.
  • 🤔 Higher-dimensional arrays, such as four or five-dimensional, are abstract but can be conceptualized by extending the cube analogy.
  • 🏷️ Records are a data structure that groups related variables, each potentially of a different data type, under a single structure.
  • 📝 Records are defined with a specific structure and then variables are declared to use this structure.
  • 🔗 In Python, lists are mutable and can be changed after creation, unlike tuples which are immutable.
  • 🔄 Tuples in Python are similar to lists but cannot be altered once created, making them a fixed-size data structure.

Q & A

  • What is an array and how does it store data?

    -An array is a variable that can contain more than one data item, storing data in a contiguous part of memory. It allows easy access to its contents using an index relative to the start point.

  • How are arrays different from lists in Python?

    -Arrays are contiguous in memory, while Python lists are not. Python lists are dynamic and can grow or shrink, whereas arrays are static data structures with a fixed size.

  • What does it mean for an array to be zero-indexed?

    -Zero-indexing means that the index of the first element in an array is 0, not 1. So, the element in the third position of an array is at index 2.

  • Can you change the size of an array once it's created?

    -No, arrays are static data structures, meaning you cannot change their size once they have been set up.

  • How is a two-dimensional array visualized?

    -A two-dimensional array can be visualized as a table with two sets of indexes: one for the rows and another for the columns.

  • What is the difference between a record and an array?

    -A record is a collection of related fields, where each field can have a different data type. An array, on the other hand, fundamentally only supports a single data type.

  • How do you define a record structure in a programming language?

    -You define a record structure by specifying the fields it will contain, including the name and data type of each field, and then ending with an 'end structure' statement.

  • What is the main difference between lists and tuples in Python?

    -The main difference is that lists are mutable and can be changed after creation, while tuples are immutable and cannot be altered once created.

  • Can you add or remove elements from a tuple in Python?

    -No, you cannot add or remove elements from a tuple because it is an immutable data structure.

  • What is the significance of the dot syntax when accessing fields in a record?

    -The dot syntax is used to access the individual fields within a record structure, indicating that each record contains its own set of variables.

  • How can you represent a four-dimensional array conceptually?

    -A four-dimensional array can be conceptualized as a set of cubes, where you supply four indexes to access any element: the first to specify the cube, and the next three for width, depth, and height within that cube.

Outlines

00:00

📚 Arrays and Data Structures

This paragraph introduces the concept of arrays, explaining them as variables that can store multiple data items in a contiguous block of memory. It contrasts arrays with lists in Python, which are not contiguous but can be used interchangeably with arrays due to Python's methods. The paragraph also discusses the zero-indexing nature of arrays, using an example of a one-dimensional array of country names. It further explains how arrays can be expanded in Python, despite being static data structures, by moving the data to a new memory location. The concept of two-dimensional arrays is introduced as tables with row and column indexes, and three-dimensional arrays are visualized as cubes with height, length, and depth indexes.

05:00

🔍 Higher Dimensional Arrays and Records

The second paragraph delves into higher-dimensional arrays, explaining how computers can handle four-dimensional or higher arrays even though humans live in a three-dimensional world. It provides a method to visualize four-dimensional arrays as sets of cubes, requiring four indexes to access elements. The paragraph also introduces the concept of record data structures, which are collections of related fields with different data types. It explains the three steps to use record structures: defining the structure, declaring variables or arrays using the structure, and assigning and retrieving data from the record's variables. An example using Visual Basic is provided to illustrate the declaration and assignment process within a record structure.

10:03

📝 Lists, Tuples, and Their Differences

The final paragraph focuses on lists and tuples in Python, which are similar to arrays but with key differences. Lists are mutable, allowing items to be added or moved, whereas tuples are immutable and cannot be changed once created. The paragraph provides a detailed comparison by showing examples of both data structures in Python code. It demonstrates how lists can grow in size and have their contents changed, while tuples remain fixed. The summary also highlights the syntax and usage of lists and tuples, emphasizing the immutability of tuples as a critical distinction.

Mindmap

Keywords

💡Array

An array is a data structure that can hold a fixed-size sequential collection of elements of the same type. It is used to store multiple items in a single variable, often visualized as a contiguous block of memory. In the video, arrays are discussed as a way to store lists of data like names, with each element accessible via an index. The concept is fundamental to understanding data storage and retrieval in programming.

💡Contiguous Memory

Contiguous memory refers to a block of memory where data elements are stored next to each other in order. This concept is crucial for arrays, as it allows for efficient data access using indexes. The video explains that arrays in programming are stored in contiguous memory locations, which is why they can be accessed quickly using an index relative to the starting point of the array.

💡Zero-indexed

Zero-indexing is a method of numbering elements in an array, where the index of the first element is 0. This is a common practice in programming languages and is highlighted in the video as a key aspect of arrays. For example, if an array contains names, the name 'Jane' would be at index 2, not 3, even though it's the third name in the list.

💡Static Data Structure

A static data structure, like an array, has a fixed size and cannot be altered after its creation. This is mentioned in the video to contrast with dynamic data structures like Python lists, which can change size. The video explains that arrays are static, meaning you cannot add or remove elements once the array is defined.

💡Two-dimensional Array

A two-dimensional array can be visualized as a table with rows and columns, and it requires two indexes to access its elements. The video uses the analogy of a table to explain how data is organized and accessed in a two-dimensional array, such as storing a matrix of numbers.

💡Three-dimensional Array

A three-dimensional array extends the concept of two-dimensional arrays to include depth, visualized as a cube. The video explains that accessing elements in a three-dimensional array requires three index values, one for height, one for length, and one for depth.

💡Multi-dimensional Array

Multi-dimensional arrays are arrays with more than two dimensions, such as four-dimensional or higher. Although challenging to visualize in our three-dimensional world, computers can handle these without difficulty. The video discusses how to conceptualize and access elements in multi-dimensional arrays.

💡Record

A record is a data structure that groups together variables that are related, each potentially of a different data type. The video introduces the concept of records, explaining how they can contain multiple fields, each a variable that can store different types of data, such as a 'T car' record containing fields for registration plate, make, model, price, etc.

💡List

In Python, a list is a collection that is ordered and changeable, allowing elements to be added, removed, or changed. The video contrasts lists with arrays, noting that while arrays are static, lists are dynamic and can grow or shrink as needed. Lists are a core data structure in Python programming.

💡Tuple

A tuple is a collection of Python data that is ordered and unchangeable, once it is created. The video explains that tuples are similar to lists but are immutable, meaning their size and content cannot be altered after creation. This makes tuples a suitable choice for data that should not change.

Highlights

Arrays can be thought of as variables that can contain multiple data items.

Arrays store data contiguously in memory.

Python lists are different from arrays as they are not contiguous.

Arrays are typically zero-indexed.

Python arrays can behave like lists due to built-in methods.

Arrays are static data structures, meaning their size cannot be changed.

Two-dimensional arrays can be visualized as tables with rows and columns.

Arrays fundamentally only support a single data type.

Three-dimensional arrays can be visualized as cubes.

Computers can handle arrays with four or more dimensions.

Record structures are collections of related fields with different data types.

Record structures are available in languages like VB but not in Python.

Tuples are immutable lists in Python.

Lists are mutable data structures that can grow and shrink.

Tuples cannot be changed once created, unlike lists.

The video provides a clear distinction between arrays, lists, and tuples.

Transcripts

play00:00

in this video we discuss arrays of up to

play00:03

three dimensions records lists and

play00:06

tuples

play00:07

[Music]

play00:13

so an array can be thought of like a

play00:15

variable which can contain more than one

play00:19

data item

play00:20

for example we could be storing a list

play00:23

of names

play00:26

and we can do that by allocating a

play00:29

continuous part of memory to storing

play00:32

that data

play00:33

so you can see here we have a number of

play00:36

memory locations in memory and starting

play00:39

at memory address 5 we've then started

play00:42

storing contiguously a list of names

play00:45

which will be part of our variable

play00:49

note that lists which are probably what

play00:51

you might be familiar with if you're

play00:53

programming in Python are different to

play00:55

arrays because they're not contiguous

play00:57

but for the perfect exam you can think

play01:00

of arrays as storing contiguous data

play01:02

items

play01:07

so our program will know where in memory

play01:10

our array starts in this case address 5

play01:13

and we can therefore use an index

play01:15

relative to the start point to allow us

play01:18

to easily access the arrays contents

play01:21

so notice arrays are typically zero

play01:24

indexed which means Jane is at index 2

play01:28

not index three it is in the third

play01:31

position in the array but because we

play01:33

start at zero it's at index two

play01:38

so we're going to use some code example

play01:40

here from python now just a quick note

play01:43

python does actually use arrays

play01:47

but it has methods that allow them to be

play01:50

used like lists by a programmer and

play01:53

that's why most people think python only

play01:55

supports lists you can effectively think

play01:58

of lists and arrays as the same things

play02:00

although that's not strictly true

play02:02

so here in this code example this line

play02:05

sets up a one-dimensional array of

play02:07

strings called countries

play02:10

it assigns it some initial values Angola

play02:13

at index 0 Austria index 1 and Belgium

play02:16

index 2. and again note how the indexes

play02:19

typically start at zero and not one

play02:24

to Output Austria from the array we

play02:27

would therefore need to use index one as

play02:30

index 1 represents the second item in

play02:33

our country's array

play02:37

arrays are a static data structure and

play02:40

that means you can't change the size of

play02:42

them once you've set them up now this is

play02:44

a good example of where python is

play02:46

allowing the array to behave like a list

play02:49

although you can't tell because this has

play02:52

been abstracted from you python is

play02:54

actually moving the entire data

play02:56

structure to a new part of the memory to

play02:58

ensure it's contiguous

play03:02

so with python we can insert an item

play03:05

into the new location at the end of the

play03:08

array using its index so here we've

play03:12

added Canada

play03:13

to the third index or the fourth

play03:16

position in the country's array

play03:20

now you can visualize a two-dimensional

play03:23

array as a table with two sets of

play03:25

indexes one index for the rows and

play03:28

another for the columns and we can see

play03:29

how that's implemented here in Python

play03:32

and how you might think about that in an

play03:34

abstract way below

play03:35

now note that although lists in Python

play03:39

can support more than one data type in

play03:41

other words heterogeneous for the exam

play03:43

you need to understand that arrays

play03:45

fundamentally only support a single data

play03:48

type so we couldn't actually have what

play03:50

we're seeing here which is strings and

play03:52

integers in a 2d array most other

play03:55

languages support Rays as unique data

play03:57

structure and they only allow for a

play04:00

single data type to be stored

play04:04

so to access Angola you'd be going to

play04:07

index 0 0 as shown here

play04:15

countries zero one contains the integer

play04:19

one two four six seven zero zero

play04:24

and countries one one contains the value

play04:28

eight three eight seven one

play04:33

so you already know that you can

play04:35

visualize as an abstraction a

play04:38

one-dimensional way as a simple column

play04:41

and a two dimensional way as a table

play04:47

well it doesn't take much of a stretch

play04:49

then for you to realize that a

play04:51

three-dimensional way in your head could

play04:53

be visualized as a cube

play04:57

you could reference any item in a

play05:00

three-dimensional array by giving three

play05:03

index values

play05:04

one for the height one for the length

play05:07

and one for the depth so here we're

play05:10

accessing two two two we're going down

play05:13

two across two and two rows deep into

play05:16

our Cube

play05:18

now at that point people think well

play05:20

surely we can't go higher than a

play05:22

three-dimensional Ray

play05:23

how could we represent a

play05:25

four-dimensional array and although that

play05:28

can be a difficult concept for us to

play05:29

wrap our heads around because we live in

play05:32

a three-dimensional World a computer

play05:34

doesn't care about that it can easily

play05:37

have a fourth dimensional or fifth

play05:39

dimensional or higher dimensional array

play05:40

and actually it's not that hard for us

play05:44

to visualize even though we only live in

play05:46

a three-dimensional world

play05:50

so this is how you could potentially

play05:52

visualize a four-dimensional array we

play05:55

just take a set of Cubes we'd have to

play05:58

supply four indexes to this array to

play06:02

access any one of the elements the first

play06:04

index specifies which Cube we look at

play06:07

and then the next three specify the

play06:09

width the depth and the height

play06:13

in a similar manner we could carry on

play06:16

visualizing fifth sixth seventh and

play06:19

higher dimensional arrays although this

play06:21

does get a little confusing so here we

play06:23

have a fifth dimensional array with one

play06:25

suggestion of how you could visualize it

play06:27

you'd have to supply five index values

play06:30

to access any particular element in a

play06:33

five-dimensional array

play06:35

the first two here could be telling us

play06:37

the row and the column of the cube to

play06:41

access and the final three the row

play06:44

column and depth of that Cube now it's

play06:47

unlikely you're ever going to be dealing

play06:48

with five dimensional arrays even at a

play06:50

level but it just shows how computers

play06:52

are not really limited and we can often

play06:54

take Concepts to the next level quite

play06:56

easily once we understand some of the

play06:58

basics

play07:03

there's also a data structure available

play07:05

in many languages called a record data

play07:09

structure now if you're programming in

play07:11

Python this isn't something that's

play07:13

available to you but it's available in

play07:14

other languages such as VB so it's worth

play07:16

knowing about

play07:18

the record structure is simply a

play07:21

collection of related fields where a

play07:24

field is variable and each field in a

play07:27

record can have a different data type

play07:30

and we use it to collect together

play07:32

variables that are related to each other

play07:36

so in this example we have a record that

play07:39

we've called T car containing six

play07:41

related fields now you could call This

play07:44

Record whatever you want and don't worry

play07:45

too much about the capital T it's a bit

play07:48

of a standard convention in many

play07:49

languages to start the name for a record

play07:52

with a capital letter T the important

play07:54

thing here is to see that we've mixed

play07:56

and matched six different variables and

play07:59

different data types and collect them

play08:01

together under a generic record

play08:04

structure that we're calling T car

play08:09

there are three steps to be able to use

play08:11

the record data structure if your

play08:13

language supports it

play08:14

you define the record structure in other

play08:17

words you tell the program What fields

play08:19

are going to be in it

play08:20

you declare a variable or an array to

play08:23

use the record structure and then

play08:25

finally we can assign and retrieve data

play08:27

from the variables inside the record

play08:31

so let's look at each stage

play08:33

now note because python doesn't support

play08:35

the record structure the example code

play08:37

we're showing you here is from Visual

play08:39

Basic don't get too worried about the

play08:41

actual code we're just getting you to

play08:43

understand the record data structure

play08:45

that's available in many languages

play08:47

so you can see we've declared the record

play08:50

structure and given it a name t car and

play08:53

then we've simply listed all the

play08:55

variables that this record structure

play08:57

will contain

play08:58

we've listed the name of those variables

play09:00

and we've stated the data types for

play09:02

those variables and then we've written

play09:04

end structure to tell the program that's

play09:06

the end of our record definition

play09:11

now we've got our template for our

play09:14

record definition we can now use it to

play09:17

set up variables so here I declare a

play09:19

variable called car one and I tell the

play09:22

computer that car one's data type will

play09:26

be the record structure that we defined

play09:28

earlier by T car

play09:30

so I could have as many cars as I wanted

play09:32

here car one car two I could even have

play09:35

an array full of different cars each of

play09:38

them would have the template of t-car

play09:45

and now I can start to assign variables

play09:47

to my car record structure so here you

play09:51

can see car one dot regplate becomes

play09:54

equal to a string and car one dot price

play09:57

becomes equal to an integer and notice

play10:00

this dot syntax because of course I

play10:02

could have set up lots of copies of this

play10:05

car record structure car one car 12 car

play10:08

100 and each one is containing its own

play10:12

structure and set of variables reg play

play10:16

make model price engine size and petrol

play10:22

so on the screen there is just a summary

play10:24

of all the code from Visual Basic which

play10:27

you would use to define and then declare

play10:30

and then assign variables to a record

play10:33

data structure

play10:36

so as we've already mentioned lists are

play10:39

pythons versions of arrays python also

play10:42

ports another similar data structure

play10:43

known as tuples

play10:45

both lists and tuples share many similar

play10:48

concept of arrays with a few key

play10:50

differences

play10:54

the difference between arrays listen

play10:56

tuples can be quite confusing so we've

play10:59

provided a summary on the screen here

play11:01

tuples are essentially lists that can't

play11:04

be changed once created they're fixed in

play11:07

size set at the point of creation and

play11:10

therefore said to be an immutable data

play11:13

structure

play11:14

lists on the other hand can be changed

play11:16

after they've been created for example

play11:18

you can add or move items they're not a

play11:21

fixed size but rather they can grow and

play11:23

Shrink

play11:24

lists are said therefore to be a mutable

play11:28

data structure

play11:31

so let's actually look at the real

play11:33

difference between listen tuples in

play11:35

Python on the screen now

play11:37

so here we see an example of a list

play11:39

we've initially set it up with two data

play11:42

items in it cat and dog

play11:46

we can then go on to add a third item in

play11:50

this case mat and we're using that by

play11:52

and we're doing that sorry by doing

play11:54

animals that's the name of the list dot

play11:57

append and then supplying the item we

play11:59

want to append in Brackets

play12:03

we then realize we've made a mistake so

play12:06

we update the third data item replacing

play12:09

its contents so we say animal square

play12:12

bracket two square bracket so access the

play12:15

second element of the list and set its

play12:17

contents to bat

play12:19

note how the data structure has grown in

play12:22

size something we can't do with a static

play12:24

data structure like arrays or tuples

play12:26

furthermore note how we've changed the

play12:29

contents of the list again something we

play12:31

can't do a tuples is they're immutable

play12:36

so here is an example then of a tuple in

play12:39

Python we initially set it up again with

play12:42

two data items in it cat and dog now

play12:45

it's looking incredibly similar at this

play12:46

point though you'll notice the brackets

play12:49

have changed

play12:52

we then retrieve the second element of

play12:54

the tube pool so we do animals bracket

play12:57

one bracket and it retrieves and returns

play13:00

dog

play13:03

we then attempt to update the data item

play13:06

in the second element of the tube ball

play13:08

so we do animals square bracket one

play13:10

square bracket equals Mouse

play13:12

doing so however produces an error

play13:14

remember tuples are immutable

play13:17

so they can't be changed or altered once

play13:20

they've initially been created

play13:24

having watched this video you should be

play13:26

able to answer the following key

play13:27

question

play13:28

what are the differences between arrays

play13:30

lists and tuples

play13:36

[Music]

Rate This

5.0 / 5 (0 votes)

関連タグ
ArraysListsTuplesData StructuresPythonProgrammingMemoryCode ExamplesImmutabilityData Types
英語で要約が必要ですか?