Practical 2: Common Python Data Structures and Functions

Introduction/Aims of this practical

Instructions

It is important that you understand what each of these examples is doing, it's not enough to simply reproduce the ouptut shown here on your own laptop screens! You should therefore test this by modifying some of these examples - you should be able to correctly predict the effects of these modifications, and if not then explain why not.

Note: In the examples below we will be using python interactively. In this case ">>>" represents the python prompt which tells you that python is ready for you to type something, not something that you should type.To do the excersises type in the text following the prompt and fit the return key.

Part 1: How data is represented in Python

Viewing data

Toggle line numbers

if we assign a value to a variable, then typing the variable name will cause the value of the variable to be printed:

Toggle line numbers

Finally, if we use the built in "print" function, this will also cause the value to be printed on the screen. For some data the two methods may result in a different represesentation of the same value:

Toggle line numbers

Question: What is the difference between the two ways in which Greet is represented?

Primitive Data Types

Boolean

Toggle line numbers

Question: Explain line 6 in the example above.

Numbers

Integers are whole, or counting, numbers and can be either positive or negative, they are represented as a sequence of digits which contains no other characters. Floating-point, or real, numbers contain a fractional component followng a decimal point. They can also be represented using the "e" notation, common to many other languages which represents "times 10 to the power of".

Toggle line numbers

The common mathematical operators (+,-,/,*) work as expected, note that x**y means xy. Numbers can also be compared using the common == (is equal), != (is not equal), < (is less than), <= (is less than or equal to), > (is greater than) and >= (is greater than or equal to). Some of these operators can also be used with other types of data.

Question:

Consider the expression:

Toggle line numbers

Explain (to yourself) the syntax of this. What type will the expression evaluate to? What value do you expect? Now try it out.

Strings

Toggle line numbers

The characters stored in the sequence are indexed by integers:

Toggle line numbers

Common operations involving strings include the .upper(), .startswith(), and endswith() methods:

Toggle line numbers

The type() function

The type() function returns the type of an object:

Toggle line numbers

The print() function can be used to print different objects within the same text. This can be done by separating each object with a comma.

Toggle line numbers

Lists (and tuples)

Toggle line numbers

Toggle line numbers

Toggle line numbers

Or:

Toggle line numbers

Toggle line numbers

Toggle line numbers

Nested lists:

Toggle line numbers

Note the syntax of line 8 above.

Question: Explain the syntax of line 8 - how many ways are there to specify the item with the value of 3 using this notation?

Tuples

Toggle line numbers

Exercise: Repeat the previous list examples with the tuple. What works and what doesnt? Explain.

Dictionaries

Toggle line numbers

Toggle line numbers

Part 2: Loops and conditionals

Repetition

For loops

Toggle line numbers

Toggle line numbers

Toggle line numbers

Toggle line numbers

Toggle line numbers

Part 3: Some more examples

Dictionaries can be iterated over in a similar way to how we can iterate through the items of a list.

Toggle line numbers

Toggle line numbers

Filtering strings

Sometimes we might want to select strings that have certain attributes. Recall the .startswith() function:

Toggle line numbers

This function, along with an if statement, can be used to specify which keys of a dictionary are printed.

Toggle line numbers

Toggle line numbers

None: Meetings/Nottingham2022/Prac2 (last edited 2024-09-10 14:18:21 by trunil)