Professional Documents
Culture Documents
Information Technology 2
Meeting #4
Block 1 (Part 2)
Introduction to problem solving in Python
OU Materials, PPT prepared by Dr. Khaled Suwais
• Introduction
• Strategies for success
• Strategies for problem solving
• 2.1 Problem solving using decomposition
• 2.2 Iteration
• 2.3 Problems
• 2.4 Using lists for flexibility
• 2.5 Nested iteration
• Summary
2
Introduction
• Programming is about solving problems, so while it is
sometimes perceived as difficult, it is closely related to
many things we do every day.
• Python is a great language for beginner programmers as
small programs can be quickly and easily written.
• In this module, programming is viewed as a problem-
solving process that requires you to think about a
problem and decompose it, before writing any code.
3
Strategies for problem solving
4
Problem solving using decomposition
(Algorithms)
6
Problem solving using decomposition
(Make life simple: algorithms in simple English)
8
Problem solving using decomposition
Programming and robotic turtles
9
Problem solving using decomposition
Programming and robotic turtles
10
Problem solving using decomposition
Programming and robotic turtles
• Here is our first simple turtle program. (You can try these
commands in Python and see what happens.)
# Draw start of staircase
from turtle import * Comment “no execution“
forward(40)
left(90) the line is stating that we will be
forward(40) working with commands that
right(90) apply to a turtle
forward(40)
The operation that have been
available for us to use on turtle
• Our first line uses a ‘>’ symbol, which shows that the first line is a
heading: ‘> Draw start of staircase’, which tells us what we want
to do. It describes the problem we are solving.
• The next two lines are a decomposition of the heading line
above. These two lines achieve the task set out in the heading. 13
Problem solving using decomposition
Drawing some simple shapes through decomposition
14
Problem solving using decomposition
Iteration
Python
• The program will move the turtle forward by 100 units. It does this by
moving forward 10 units, and repeating this movement ten times.
• range(1, 11) means that the range of numbers starts at 1, counts
upwards by 1 and stops just before 11.
18
Problem solving using decomposition
(Programming for repetition)
Python
Alternative Solution
19
Problem solving using decomposition
(A more powerful approach to design)
• Let’s think about a slightly more complicated problem.
We will design and implement a program to draw two
squares, one below the other, with a gap in between.
Problem decomposition 20
Problem solving using decomposition
(A more powerful approach to design)
Python
# Produce graph for gloves sales
from turtle import *
# set up the variables
g1=10
g2=8
# produce the x axis
goto(40,0)
goto(0,0)
# produce the y axis
goto(0,100)
goto(0,0)
# Plot data
goto(20, g1*10)
goto(40, g2*10)
ht() # to hide the turtle prompt
25
Using lists for flexibility
(Drawing a graph of a fixed number of points)
Python
# Produce graph for gloves sales
from turtle import *
# set up the variables
clear()
pu()
setpos(0,0)
pd()
g1=10
g2=8
# produce the x axis
goto(40,0)
goto(0,0)
# produce the y axis
goto(0,120)
goto(0,0)
# Plot data
goto(20, g1*10)
dot( 5, "blue")
write("g1", False,"center","bold")
goto(40, g2*10)
dot( 5, "red")
26
write("g1", False,“left","bold")
ht() # to hide the turtle prompt
Using lists for flexibility
(Working with simple lists)
27
Using lists for flexibility
(Working with simple lists)
28
Using lists for flexibility
(Working with simple lists)
• To create a list we use square brackets to indicate the start and end of
the list, and separate the items by commas:
L = [1,2,3]
• You can use the print function to print the entire contents of a list:
print(L) #[1, 2, 3] will be printed
• The empty list is []. It is the list equivalent of 0 or empty string ''.
• If you have a long list to enter, you can split it across several lines, like below:
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40]
29
Using lists for flexibility
(Working with simple lists)
31
Nested iteration
(Independent nested loops)
• Sometimes we use loops within loops (embedded loops) to solve
more complex problems.
• For example, what if we had monthly sales data of gloves for several
years and wanted to calculate the total sales for each year? In this
case, we might have a list containing a list for each year.
• Breaking the problem down, we see the need to show all the
multiples of 1, all the multiples of 2, and so on. So we can use a loop.
The sub-problems, such as finding the multiples of 1, 2 and 3, can
also be done using a loop. 32
Nested iteration
(Independent nested loops)
Problem
Decomposition
Translated
Python Code 33
Nested iteration
(Independent nested loops)
34
Nested iteration
(Programming the turtle using nested loops)
Problem
Decomposition
pd()
Python Code
Nested iteration
(Dependencies between nested loops)
*
**
***
37
Nested iteration
(Dependencies between nested loops)
Python
# Print Right-angled triangle
size=3
for line in range(1,size+1):
for asterisk in range(1, line+1):
print('*', end='')
print()
38
Summary
After studying this part, you should be able to:
• decompose a simple problem to produce an algorithm,
using sequence, selection and iteration
• translate a simple design of an algorithm into Python
• make use of iteration to produce code that can solve
problems where we need to do things several times
• make use of lists to express the idea that a number of
data items are related
• solve problems involving drawing line-based images using
turtle graphics
• use algorithmic thinking to solve problems. 39