CSC108H1 Lecture Notes - Lecture 18: Memory Address, Letter Case

90 views5 pages
Verified Note
Some notes about lists:
General syntax of a list:
<list_variable_name> = [<element1>, <element2>, <element3>, ]
or simply
[<element1>, <element2>, <element3>, ]
(an unnamed list)
Where an element can be of any data type
Lists are non-homogenous
items = [ 12, ‘abc’, [‘a’, ‘e’, ‘i’], 35.765 ]
↑ ↑ ↑ ↑
int str list of str float
Lists are structured by indices.
Like in many other programming languages, indices in python begin from 0
example_list = [1, 2, 3, 4, 5]
x = example_list[0] # element 1
y = example_list[1] # element 2
Empty lists are defined like this:
>>> Example_list = []
Calling on an element in this list will produce an index error:
>>> Example_list[0]
Traceback (most recent call last):
Python Shell, prompt 7, line 1
builtins.IndexError: list index out of range
In python, unlike in many other languages, lists elements can be called from the end
>>> example_list[-1]
5
>>> # This called the 1st element from the end
This allows us to access the last element of a list without knowing the length of the list
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Lists are mutable.
Changes can be made to a list element’s value.
>>> L = [1, 2, 3, 5, 5]
Suppose we want to change the 4th element L[3] to 4 instead
>>> L[3] = 4
>>> L
[1, 2, 3, 4, 5]
However, doing this will not overwrite the value stored in the memory location where 0 was
stored. It will simply make L[3] point to a different memory location (ie. store a different
memory address for where 4 is stored).
Lists can be ‘sliced’ to produce a sublist from a particular list
Syntax:
<list_variable>[ <starting_index> : <stopping_index>]
Original list int int
A slice of the original list from the starting upto the stopping index is created, not inclusive of
the stopping index
>>> L[1:4] # L’s elements at index 1, 2, 3
[2, 3, 4]
Lists can be called by multiple names. This is called aliasing.
>>> L_alias = L
Both L and L_alias hold the same memory address and refer to the same list. Any changes
made to one list will reflect in the other.
>>> L[0] = 10
>>> L_alias
[10, 2, 3, 4, 5]
When used in function definition parameters, lists are referred in the type contracts as
list.
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Where an element can be of any data type. Lists are non-homogenous items = [ 12, abc", [ a", e", i"], 35. 765 ] int str list of str float. Like in many other programming languages, indices in python begin from 0 example_list = [1, 2, 3, 4, 5] x = example_list[0] y = example_list[1] Calling on an element in this list will produce an index error: Python shell, prompt 7, line 1 builtins. indexerror: list index out of range. In python, unlike in many other languages, lists elements can be called from the end. This allows us to access the last element of a list without knowing the length of the list. Changes can be made to a list element"s value. Suppose we want to change the 4th element l[3] . Point to a different memory location (ie. store a different. However, doing this will not overwrite the value stored in the memory location where 0 stored.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents