程序代写代做代考 python data structure 5AAVC210 Introduction to Programming WEEK 2

5AAVC210 Introduction to Programming WEEK 2

More data types
Strings
Numbers
Lists
Tuples
Dictionaries

Data structures: lists
A list is a collection which is ordered and changeable.
A list contains items separated by commas and enclosed within square brackets [ ]
Lists can hold different data types, e.g. strings and numbers
An index refers to a position within an ordered list.

You access the list items by referring to the index number:

NOTE: the index number [1] refers to the second item in the list!
Lists in Python are zero-indexed, so the first element is at index 0, the second is at index 1, and so on.

Querying/slicing lists

Modifying lists
To change the value of a specific item, refer to the index number:

Tuples
A tuple is another sequence data type, similar to the list
Main differences between lists and tuples are:
Lists are enclosed in brackets [ ], their elements and size can be changed,
Tuples are enclosed in parentheses ( ) and are immutable, i.e. cannot be updated.

Tuples can be thought of as read-only lists.

Access tuple items by referring to the index number, inside square brackets:

Create the tuple using round brackets:

For loops
A for loop is used for iterating over a sequence (such as a list or a tuple).
The for loop that is used to iterate over elements of a sequence, it is often used when you have a piece of code which you want to repeat for a given number of times.
It works like this: ” for all elements in a list, do this ”

The variable x is the loop variable. We could have chosen any other variable name instead.
Here is a list called fruits.
It has three items in it.
This is the loop body.
The loop body is always indented
On each iteration or pass of the loop, first a check is done to see if there are still more items to be processed. If there are none left (this is called the terminating condition of the loop), the loop has finished.

If there are items still to be processed, the loop variable is updated to refer to the next item in the list. This means, in this case, that the loop body is executed here 3 times, and each time x will refer to a different fruit.

At the end of each execution of the body of the loop, Python returns to the for statement, to see if there are more items to be handled, and to assign the next one to x.

https://www.lynda.com/Python-tutorials/Loops/661773/707226-4.html

For loops
(from 1m30 – 6m10)

Questions?

Dictionaries
A dictionary is another data structure like a list and a tuple. Dictionaries are unordered, changeable and indexed.

Dictionaries are written with curly brackets { }, and they have keys and values.

It is best to think of a dictionary as an unordered set of key:value pairs, with the requirement that the keys are unique (within one dictionary).

Dictionary name
key
value of the key
Note: dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

You can access the items of a dictionary by referring to its key name, inside square brackets:
print(mydict) # Prints complete dictionary
print(mydict.keys()) # Prints all the keys
print(mydict.values()) # Prints all the values

Questions?

Errors
Python has lots of different types of error which we say are raised when something unexpected happens in our code.

When an error is raised, we call it an Exception. Python will normally stop and generate an error message.

Common Python 3 Error Messages (and How to Eliminate Them!) 
https://www2.cs.arizona.edu/people/mccann/errors-python#Four

KeyError
Python raises a KeyError whenever a dict() object is requested (using the format a = adict[key]) and the key is not in the dictionary.

IndexError
Python raises an IndexError if your list indices are out of range –that is, you are trying to refer to some index that doesn’t even exist.

Try/Except
To handle errors, Python gives us a special syntax that we can use to try and do something, and if it fails, catch an exception of a given type and do something else.

Error handling in Python is done through the use of exceptions that are caught in try blocks and handled in except blocks.

If an error is encountered, a try block code execution is stopped and transferred down to the except block.

In addition to using an except block after the try block, you can also use the finally block.

The code in the finally block will be executed regardless of whether an exception occurs.

https://www.w3schools.com/python/python_try_except.asp

Questions?