0% found this document useful (0 votes)
45 views

Python - Part I

Python supports multiple data types including single values stored in variables and multiple values stored in data structures like tuples and lists. Tuples are used to store records of multiple items of different types, accessed using indices. Lists are similar to tuples but allow adding and modifying items. Both tuples and lists use indices to access single items or subsets of the data structure.

Uploaded by

Shehan Morawaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python - Part I

Python supports multiple data types including single values stored in variables and multiple values stored in data structures like tuples and lists. Tuples are used to store records of multiple items of different types, accessed using indices. Lists are similar to tuples but allow adding and modifying items. Both tuples and lists use indices to access single items or subsets of the data structure.

Uploaded by

Shehan Morawaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Grade 09 - Topic 02 – Programming and Development

NOTE - PART I

Data Structures in Python


❖ Python supports many data types including:
1. Single Values → Variables.
2. Multiple Values:
i. Records (known as Tuples in Python).
ii. Single-Dimensional Arrays (known as Lists in Python).
❖ Since we already know how to work with variables, let’s look at Tuples and Lists.

Tuples in Python
❖ A tuple in Python is the representation of a record data structure we discussed in Topic 01.
❖ We can store a collection of items as a tuples in Python.
❖ Those items can be of multiple types.
o Syntax:

o Examples:

❖ Following illustration shows how Python tuple items are saved in main memory.

Monday, October 17, 2022 Page 1 of 4


Grade 09 - Topic 02 – Programming and Development
NOTE - PART I

❖ We can use index numbers (indices) to access a value(s) of a tuple.


o To access a single item (place) of a tuple:

▪ Examples:

➢ However, if you try to access an index number that do not exist in the specified tuple,
following error will occur:

o To access a subset of a list:

▪ Examples:

Monday, October 17, 2022 Page 2 of 4


Grade 09 - Topic 02 – Programming and Development
NOTE - PART I

➢ However, if you try to access an index number that do not exist in the specified tuple, there
will not be an error:

Lists in Python
❖ A list in Python is the representation of array data structure we discussed in Topic 01.
❖ We can store a collection of items as a list in Python, just like in tuples.
❖ Those items can be of multiple types, just like in tuples.
o Syntax:

o Examples:

❖ Following illustration shows how Python tuple items are saved in main memory.

❖ We can use index numbers (indices) to access a value(s) of a list.


o To access a single item (place) of a list:

Monday, October 17, 2022 Page 3 of 4


Grade 09 - Topic 02 – Programming and Development
NOTE - PART I

▪ Examples:

➢ However, if you try to access an index number that do not exist in the specified list,
following error will occur:

o To access a subset of a list:

▪ Examples:

➢ However, if you try to access an index number that do not exist in the specified tuple, there
will not be an error:

Monday, October 17, 2022 Page 4 of 4

You might also like