You are on page 1of 13

Week 3 – Translating Algorithm into a computer code

(CLO1)
Learning Outcomes
• List four of the mostly used high level programming languages
• Recognize four widely used Python packages or libraries
• Convert algorithms from human natural language to computer code
in Python
• Use loop to repeat a block of code

2
High Level Programming Languages
• Computer language also referred to as programming language is a
formal language used to communicate commands to a machine,
typically a computer.
• It is used to create a program to implement a specific algorithm.

3
Example of programming languages
• Java: Used to create programs for mobile phones, web sites, personal
computers, and many other platforms.
• C#, Visual Basic: Used to create programs that run on Microsoft
products.
• Swift: Used to create programs that run on Apple products.
• JavaScript: Used to write games and other programs that run on web
browsers.
• Python: One of the latest computer languages that has unique
capabilities and can run on many devices.

4
How to implement?
• In this course we will be using Python.
• We will use a website called https://repl.it to implement algorithms

5
Why Python Programming Language?
The ability to connect to a wide range of data sources,
integrate with many applications including machine learning,
artificial intelligence, motion graphics, etc.
Package for scientific computing in Python.

SciPy (pronounced “Sigh Pie”) mathematics, science, and engineering

High-performance, easy-to-use data structures and data analysis


tools.
6

6 Matplotlib is a Python 2D plotting library


Translating algorithm to Python Code
• The turtle library has built-in commands that make it easy to draw an object.
• To access a library in Python, you need to use the keyword import.
• For example, if you want to draw a line with length 100 points, you can simply use
the command forward(100).
import turtle as t

t.forward(100)

t.left(90)

7
t.forward(100)
7
Drawing a square in Python - turtle
• Size of each side = 100 dots
Algorithm: • Python code:
• Move pen forward by 100 dots
• Change the angle to the left by 90 degrees
• Move pen forward by 100 dots
• Change the angle to the left by 90 degrees
• Move pen forward by 100 dots
• Change the angle to the left by 90 degrees
• Move pen forward by 100 dots
• Change the angle to the left by 5 degrees

88
Some turtle commands
Method or Required
Command Parameters Description

forward amount Moves the turtle forward by the specified amount


backward amount Moves the turtle backward by the specified amount
right angle Turns the turtle clockwise
left angle Turns the turtle counter clockwise
penup None Picks up the turtle’s pen
pendown None Puts down the turtle’s pen to start drawing
color color name Changes the color of the turtle’s pen
Changes the color of the turtle  will use to fill a
fillcolor color name
polygon
position None Returns the current position
goto x,y Move the turtle to position x,y

shape shape name Should be ‘arrow’, ‘classic’, ‘turtle’, or ‘circle’


9

9 https://trinket.io/docs/colors
Drawing a circle
• Syntax: turtle.circle(radius, extent = None, steps = None)
• radius is the radius of the circle
• extent – an angle – determines which part of the circle is
drawn. If extent is not given, draw the entire circle. If extent
is not a full circle, one endpoint of the arc is the current pen
position.
• steps determines the number of steps to use
• Draw the arc in counterclockwise direction if radius is
positive, otherwise in clockwise direction.
10

10
Drawing a circle
• import turtle as t
• t.circle(50)  draw a circle
with a radius of 50 dots.
• t.circle(50, 180)  draw a ½
circle with a radius of 50 dots.
• t.circle(50, 180, 4)  draw a
½ circle with a radius of 50
dots, 4 steps.

11

11
fillcolor(colorName)
• Fills a closed shape with a color
• Should be used with begin_fill( ) and end_fill( )
• Example:
t.fillcolor("red")
t.begin_fill( )
t.circle(50)
t.end_fill( )

12

12
Exercise
• Draw the following shapes:

13

13

You might also like