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

5AAVC210 Introduction to Programming WEEK 2

Recap: Variables
A variable holds a value. That value can vary, depending on what calculations or operations we perform in the program.

Types of values variables can have are:
int (whole numbers, e.g. 3 or -786)
float (numbers with decimal places, e.g. 4.05 or -32.1679)
str (a string of text inside quotation marks, e.g. ‘hello’ or “hello”)
bool (a value of either True or False)

Operators

Comparisons

Functions
A function is a block of code which only runs when it is called.
You give a function data, known as parameters.
A function can return data as a result.
Python has built-in functions and we can also write our own

print(“Inside these brackets are the parameters”)
We are calling the print() function and we are giving it a string as a parameter

Conditional statements: if
Sometimes we want to our code to do something if certain conditions are met.

if
else
elif

Any questions so far?

While loops
With a while loop we can execute a set of statements as long as a condition is true.

The syntax of a while loop in Python programming language is
while expression:
statement(s)

Variable called i, an int,
initially has a value of 1
While the value of i
is less than 6,
call the print() function
to print out the value of i
Once the value of i
has been printed out,
add 1 to the value of i
RESULT
Go back around the loop
until the value of i reaches 6

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.

What can lists hold?
numeric_list = [12, 35, -5.68]
print(numeric_list)

shop_list = [‘milk’, ‘coffee’, ‘chocolate’]
print(shopping_list)

mixed_list = [ ‘milk’, 12, ‘chocolate’, -5.68 ]
print(mixed_list)

final_list=[numeric_list, shop_list]
print(final_list)

Querying/slicing lists

List membership

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.

Questions?