Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

computer memory£1.50

Embedded System Design and Software - Part 2£2.50

8-Bit analog to digital converter using successive approximation£1.49

Introduction to ICT and Computer Applications£5.00

RDM£0.50

eee fuses£1.50

Computer Languages £0.60

C++ notes for beginners£2.50

Uncontrolled Rectifier £1.49

computer methods in power systems£2.00

Robotics Lecture 4£5.00

e-banig simulation£6.25

Motor£6.88

Programmable Logic Controllers (PLCs)£12.50

Engineer's choice£1.50

Dos and Don'ts of Turbine operation£6.25

Arrays, Iterations and Invariants£2.00

Electro£0.75

Surge protection£0.75

Sun Tracker System£1.50

DC Theory£12.50

Combinational Logic£10.00

Practice Programs for python basics with answers£3.13

Total£88.08

Title: Extensive Python Notes - Lists and Tuples
Description: GIKI Electrical Engineering 1st year CS102L notes.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


2
...


Outcomes:




Students should be able to create, modify lists and access their elements
Students should be able to use slicing in lists
Student should be able to create and use tuples and tuples packing and unpacking

2
...
Python lists are made by putting a comma-separated sequence of objects in square brackets ([]), as
shown in the picture below
...

Lists can contain any arbitrary objects
...

Lists can be nested to arbitrary depth
...

Lists are dynamic
...


2
...
1 Lists Are Ordered
A list is not merely a collection of objects
...
The order in which you specify
the elements when you define a list is an innate characteristic of that list and is maintained for that list’s
lifetime
...
1
...
The elements of a list can all be the same type:

Or the elements can be of varying types:

Lists can even contain complex objects, like functions, classes, and modules, which you will learn about in
upcoming labs:

Prepared by: Zain Ul Abideen

41

A list can contain any number of objects, from zero to as many as your computer’s memory will allow:

(A list with a single object is sometimes referred to as a singleton list
...
A given object can appear in a list multiple time:

2
...
3 List Elements Can Be Accessed by Index
Individual elements in a list can be accessed using an index in square brackets
...
List indexing is zero-based as it is with strings
...
For example, a negative list index counts
from the end of the list:

2
...
4 List Slicing
Slicing also works
...
However, there is an important difference between how this operation works with
a list and how it works with a string
...
They are both special cases of a more general
object type called an iterable, which you will encounter in more detail in the upcoming labs on definite
iteration
...
But you can operate on a list literal as well:

For that matter, you can do likewise with a string literal:

2
...
5 Lists Can Be Nested
You have seen that an element in a list can be any sort of object
...
A list can contain
sub lists, which in turn can contain sub lists themselves, and so on to arbitrary depth
...

All the usual syntax regarding indices and slicing applies to sub lists as well:

Prepared by: Zain Ul Abideen

47

However, be aware that operators and functions apply to only the list at the level you specify and are not
recursive
...
The individual elements in the sublists don’t count
toward x’s length
...
It is only directly an element in the sublist x[1][1]
...


2
...
6 Lists Are Mutable
Most of the data types you have encountered so far have been atomic types
...
These types are immutable, meaning that they
Prepared by: Zain Ul Abideen

48

can’t be changed once they have been assigned
...
If you want a different integer, you just assign a different one
...
Strings are reducible to smaller parts—the component
characters
...
But you can’t
...

The list is the first mutable data type you have encountered
...
Python provides a wide range of ways to modify lists
...
1
...
1 Modifying a Single List Value
A single value in a list can be replaced by indexing and simple assignment:

You can’t do this with a string:

A list item can be deleted with the del command:

2
...
6
...
This assignment replaces the specified slice of a with
:

Prepared by: Zain Ul Abideen

49

The number of elements inserted need not be equal to the number replaced
...

You can insert multiple elements in place of a single element—just use a slice that denotes only one element:

Note that this is not the same as replacing the single element with a list:

You can also insert elements into a list without removing anything
...

You can also use the del statement with the same slice:

2
...
6
...
More precisely, a list
must be concatenated with an object that is iterable
...

Strings are iterable also
...
When a string is iterated through, the result is a list of its
component characters
...

If you really want to add just the single string 'corge' to the end of the list, you need to specify it as a singleton
list:

2
...
6
...
Information on these methods
is detailed below
...
append() Appends an object to a list
...
a ppend() appends object to the end of list a:

Prepared by: Zain Ul Abideen

51

Remember, list methods modify the target list in place
...
append() method does not work that way! If an iterable is appended to a list with
...
extend() Extends a list with the objects from an iterable
...
extend() also adds to the end of a list, but the argument is
expected to be an iterable
...
extend() behaves like the + operator
...
insert(, ) Inserts an object into a list
...
insert(, ) inserts object into list a at the specified
...
remove() Removes an object from a list
...
remove() removes object from list a
...
pop(index=-1) Removes an element from a list
...
remove() in two ways:



You specify the index of the item to remove, rather than the object itself
...


a
...
may be
negative, as with string and list indexing:

defaults to -1, so a
...
pop()
...
2 Python Tuples
Python provides another type that is an ordered collection of objects, called a tuple
...
Some pronounce it as though it were spelled “too-ple”
(rhyming with “Mott the Hoople”), and others as though it were spelled “tup-ple” (rhyming with “supple”)
...


2
...
1 Defining and Using Tuples
Tuples are identical to lists in all respects, except for the following properties:



Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ([])
...


Here is a short example showing a tuple definition, indexing, and slicing:

Never fear! Our favorite string and list reversal mechanism works for tuples as well:

Everything you’ve learned about lists—they are ordered, they can contain arbitrary objects, they can be indexed
and sliced, they can be nested—is true of tuples as well
...
(This is probably not
going to be noticeable when the list or tuple is small
...
If the values in the collection are meant to remain constant
for the life of the program, using a tuple instead of a list guards against accidental modification
...
A tuple can be used for this purpose, whereas a list
can’t be
...

There is one peculiarity regarding tuple definition that you should be aware of
...
Python knows you are defining a tuple:

But what happens when you try to define a tuple with one item:

Doh! Since parentheses are also used to define operator precedence in expressions, Python evaluates the
expression (2) as simply the integer 2 and creates an int object
...

When you display a singleton tuple, Python includes the comma, to remind you that it’s a tuple:
Prepared by: Zain Ul Abideen

55

2
...
2 Tuple Assignment, Packing, and Unpacking
As you have already seen above, a literal tuple containing several items can be assigned to a single object:

When this occurs, it is as though the items in the tuple have been “packed” into the object:

If that “packed” object is subsequently assigned to a new tuple, the individual items are “unpacked” into the
objects in the tuple:

Prepared by: Zain Ul Abideen

56

When unpacking, the number of variables on the left must match the number of values in the tuple:

Packing and unpacking can be combined into one statement to make a compound assignment:

Prepared by: Zain Ul Abideen

57

Again, the number of elements in the tuple on the left of the assignment must equal the number on the right:

Prepared by: Zain Ul Abideen

58


Title: Extensive Python Notes - Lists and Tuples
Description: GIKI Electrical Engineering 1st year CS102L notes.