Professional Documents
Culture Documents
TM112: Introduction To Computing And: Information Technology 2
TM112: Introduction To Computing And: Information Technology 2
Information Technology 2
Meeting #6
Block 2 (Part 2)
Patterns, Algorithms and Programs 2
Block 2 (Part 4)
Organizing Your Python Code and Data
OU Materials, PPT prepared by Dr. Khaled Suwais
• Introduction
• Generate a List- Append
• Reduce
• Search
• Combine
• The Final Problem
• Summary
2
Introduction
• This part continues to apply the problem-solving approach from Block 1
Part 4, in which we go from problems to programs via patterns and
algorithms.
• Lists are a very useful and flexible way of storing multiple data items.
Most Python programs use lists. In this part, I will show some common
problem types on lists and the corresponding solution patterns.
3
• Generate a List
4
Append
• Normally the generated sequence has to be stored for
further processing, and lists are ideally suited for that.
Storing the generated sequence in a list requires just two
changes to Pattern 2.1:
start with the empty list.
instead of (or in addition to) printing the value,
append it to the list, i.e. add it to the end of the list.
5
Append
• In Python, adding value to the end of a list , say sequence, is
intuitively written as:
or
sequence.append(value)
6
Filter
7
Python editor: hot days program
• Given a list of daily temperatures in degrees Celsius in a certain place,
construct a list of the temperatures above 30. Assume temperatures are
given as whole numbers.
O/P: The hot days had temperatures [33, 32, 42, 36] 8
Transform
• Another way to construct a new list from an existing
one is to transform each item of the input list into
one item of the output list.
11
Count
• In its more general form, the counting problem asks for how
many items in a list satisfy some condition. Here’s an
example.
12
Program 2.5 Negative temperatures
• Pattern 2.5 Counting:
1. initialize the input list.
2. set a counter to zero.
3. for each item in list:
Algorithm a. if the item satisfies the condition:
i. increment the counter, i.e.
add 1 to it.
4. print the counter.
15
Program 2.7 list sum
• Pattern 2.6 Aggregate:
17
Find a Value
• The pattern is similar to the previous reduction patterns: a
variable stores the item found so far, if any, which will be the final
result when the iteration over the list ends.
• The issue is, again, how to initialize the result variable.
• The trick is, again, to think what should happen if the list is empty.
In that case, the loop is not executed, so the final value of the
result variable will be the value it was initialized with.
• If the list is empty, there is no item to be found, and the output
should somehow represent ‘no item’.
• Some programming languages have a ‘null value’ for that. Hence
the result variable has to be initialized with the ‘null value’.
18
Program 2.8 Find Negative Temperature
• Pattern 2.7 Find value:
• For this problem type, I will assume that the input list is not empty.
This guarantees that there will be a best value.
22
Block 2 (Part 4)
Organizing Your Python Code and Data
23
Functions in Python
• You may recall being told that you can find the size of a list named
gloves by using len(gloves). This involved a Python function,
len().
24
Function names and arguments
• When we talk about a function, we use its name – in this case, len – followed
by a pair of parentheses: len(). This way, you can see at once that we are
talking about a function rather than, for instance, a variable.
def printHello():
print("Hello! ")
26
Functions without return
• Some functions perform simple procedural tasks (specified in their bodies)
but do not return any information when they are called.
• Example: Write a function that will display a welcome message to a student with
his/her name. Then, use the function in your program.
def welcome(aName):
print("Hello " , aName)
print("Welcome to AOU")
welcome("Ahmad") #Function call
• When the function is called, an actual value for the argument must be used.
• For example, when the function call welcome('Ahmad') is executed, the actual
string 'Ahmad' replaces the argument aName resulting in the following output:
-----output-----
Hello Ahmad
Welcome to AOU
27
Functions with return
28
Functions with return
• Example : Write a function that takes the height and width as arguments,
calculates the area of a rectangle, and returns it. Then display the area in the
output window. Use the function in your program.
4 6
def recArea(aHeight,
4
aWidth) :
6
area = aHeight * aWidth
return area
-----output-----
Enter the height: 4
Enter the width: 6
Area = 24
29
Functions with Multiple return values
So what if you want to return two variables from a function instead of one?
There are a couple of approaches which new programmers take.
Let’s take a look at a simple example:
30
Hiding Complexity: Interfaces and
Implementations
• When a function is called, the resulting value is referred to as the return
value of the function.
• There is a name for the items you need to know: the interface of the
function. To use a function, knowing the interface is enough. There’s no
need to know what goes on under the bonnet-its implementation.
• The Python interpreter deals with a program one line after the
other, starting with the very first line. A normal program line is
executed when the interpreter gets to it. However, functions do
receive special treatment, which we examine in this section.
32
The Python Interpreter and Functions
33
Using Functions- The Benefits
• Replacing duplicate code with a function can make a program
shorter and more readable.
34
Reusing Code
• There is a more elegant way to reuse functions. Instead of
copying a function into a new program, you can also put all
your functions – say, for drawing figures – into a separate file
(with the .py extension). Let’s call it
figure_drawing_functions.py. At the beginning of
your new program, you then simply add from
figure_drawing_functions import *. This has the
same effect as placing the function definitions themselves at
the beginning of the program. This way, you can create your
own library of figure drawing functions.
35
Summary
• In this part, you again practiced the techniques to solve
computational problems by:
1. looking in the problem statement for the input(s) and the
output(s)
2. thinking which inputs are not allowed, e.g. negative
numbers or empty lists
3. writing tests (pairs of admissible inputs and their
expected outputs)
4. recognizing the type of the problem or the types of the
sub-problems
5. instantiating the patterns for those problem types
6. combining the algorithms for the sub-problems to solve
the whole problem.
7. organizing your code using functions.