ASTU
CSEg 1101 Introduction to Computing
Lecture 1
2022/23
Computer Science & Engineering Program
The School of EE & Computing
Adama Science & Technology University
OUTLINE ASTU
Goals of the course
What is computation ?
Computational thinking
About Python
2D robot control
Reading assignment:
Chapter 1 of the textbook
Learning programming with robots
(You may download the PDF file on Hisnet site)
2
GOALS OF THE COURSE ASTU
Two-level goals
- Building up a basis on ICT (Information and
Communications Technology)
- Computational thinking and programming
(but not learning a programming language Python)
Think like a computer scientist for problem solving !
3
WHAT IS COMPUTATION ? ASTU
Problem solving with computer
- Finding the facts that a solution satisfies
- Designing an algorithm(recipe) to find the solution
- Mapping the algorithm to a program
- Understanding abilities and limitations
“Algorithm” is at the heart! -
- Efficient good method to solve the problem !
- 1+ 2+ 3+ 4 + 5+ 6 + 7 + 8 + 9 +10 = ?
Method 1: 1+ 2+ 3+ 4 + 5+ 6 + 7 + 8 + 9 + 10 = 55
Method 2: n(n+1)/2 = 10*(10+1)/2 = 10 * 11/2 = 110/2 =55
- It saves computing time..
4
9 times of computing versus 3 times of computing
ASTU
Knowledge
Declarative Imperative
Statement of facts Recipes for deducing information
“How to” knowledge
is y such that Start with guess G.
is x. Ifx, stop and return G.
Otherwise, G (G + x/G )/2.
Repeat.
Heron of Alexandria(10-70 AD)
Ancient Babylonians
5
ASTU
Problem is “finding y value”
-----> G**2 = x
19.99878
Guess G,
ex) x= 20, G=4
round G G**2 ≈ x status
1 4 4**2=16 20 fail
2 4.5 =(4+20/4)/2 4.5**2=20.25 20 fail
=(4.5+20/4.5)/
3 4.47222 4.472**2= 19.9988 20 TRUE 6
2
Fixed program computers ASTU
• Atanasoff and Berry(1941): a linear equation solver
- The Atanasoff–Berry computer (ABC) was the first
automatic electronic digital computer,
- an early electronic digital computing device
• Alan Turing: bombe machine
• Calculators
7
Fixed program computers ASTU
Atanasoff and Berry(1941): a linear equation solver
• The Atanasoff–
Berry computer
(ABC) was the
first automatic
electronic digital
computer,
• An early
electronic digital
computing device
8
Fixed program computers ASTU
Alan Turing: bombe machine
. The bombe was an electromechanical device used by British
cryptologists to help decipher German Enigma-machine-encrypted secret
messages during World War II
9
ASTU
Stored program computers
Memory
Data Program
Instruction 1
Instruction 2
………………
Instruction k
………………
Instruction N
Processor
Input Output
Control unit ALU
PC k
10
ASTU
Computation
Computation is solving a problem with a program.
A program is a realization of an algorithm(recipe)
on a computer.
An algorithm is a sequence of instructions to do a task.
imperative knowledge
(for humans)
An algorithm should be refined enough to be easily
translated into a programming language.
(for computers) 11
COMPUTATIONAL THINKING ASTU
How to design an algorithm : top-down design
How to convert it to a program: coding and debugging
What to do with computers ?
12
ASTU
Top-down design
Decomposing a problem into smaller sub-problems
Decompose each of the smaller sub-problems
recursively until every sub-problem is simple enough
to map to a few instructions in a program language
Multi-level abstraction
Divide and conquer
13
ASTU
Coding and debugging
Coding is “a process of fighting with bugs (errors).”
- Syntax error: Python cannot understand your program,
and refuses to execute it.
- Runtime error: At runtime, your program suddenly
terminates with an error message.
- Semantic error: Your program runs without error
messages, but does not do what it is supposed to do.
Why making such bugs (errors) ?
- Well, … , that is the difference between humans and
computers.
14
ASTU
What to do with computers?
According to Turing-Church Thesis (in 1936),
modern computers are essentially equivalent to
a stored program computer.
What kind of problems can we solve with a stored program
machine ?
Decidable problems
Tractable problems : good algorithms
Intractable problems: no good algorithms
e.g., travelling salesman’s problem
approximate algorithms
Undecidable problems: no algorithms ever found
e.g. halting problem 15
ABOUT PYTHON ASTU
Low level Lang. vs High level Lang.
General vs Targeted
Compiled vs Interpreted
Python is relatively young but one of the most popular
programming languages
Open software
16
ABOUT PYTHON ASTU
• Guido van Rossum, Python's principal author
• Employed by Google during 2005 ~ 2012,
where developing the Python language.
• 2001 Award for the Advancement of
Free Software
• Open Source
• multi-paradigm programming language:
- object-oriented programming and
- structured programming
• Since 2003, Python has consistently ranked
in the top 10 most popular programming languages
• As of September 2015, it is in the fifth position.
• It was ranked as Programming Language of the Year for the
year 2007 and 2010 17
ASTU
Why Python ?
A programming language easy to learn and very powerful
- Used in many universities for introductory courses
- Main language used for web programming at Google
- Widely used in scientific computation, e.g., at NASA
- Large portions of games written in Python (Civilization IV)
Once you learnt programming in one language,
it is relatively easy to learn another language, such as C++ or
Java. 18
ASTU
Characteristics of Python
Instruction set
Arithmetic and logical operations for defining
+, -, *, /, and ** expressions
and, or, not
Assignment
Conditionals
Iterations
Input/output
No pointers
No declarations
19
ASTU
Why programming ?
Every scientist and engineer must know some programming.
It is part of basic education, like calculus, linear algebra, introductory
physics and chemistry, or English.
Alan Perlis 1961
After half a century later, we should change it as follows:
Every student in a university should learn some programming.
It is part of basic education, like calculus, linear algebra, introductory
physics and chemistry, or English.
20
2D ROBOT CONTROL ASTU
A small grid-like 2D world
Basic actions
move (): moving one grid forward
turn_left (): turning left by 90
pick_beeper(): pick ing up beepers
drop_beeper(): putting down beepers
Ourowninstructions:functions
Comments
Interactive mode
Python programs (scripts)
21
14
ASTU
Integrative mode
>>>from cs1robots import *
>>>create_world()
>>>hubo = Robot()
>>>hubo.move()
>>>hubo.turn_left()
22
ASTU
Script mode
from cs1robots import *
create_world()
hubo = Robot()
hubo.move()
hubo.turn_left()
23
ASTU
Functions
A function definition specifies the name of a function
and the sequence of statements that are executed when
the function is called.
def print_message():
print("CSE1061 is fantastic!")
print("Programming is fun!")
Key word
You can call a function inside another function:
def repeat_message():
print_message()
colon
print_message()
Indentation
24
ASTU
Flow of execution
def print_message():
print("CSE1061 is fantastic!")
print("Programming is so much fun")
function definitions
def repeat_message():
print_message()
print message()
repeat_message() function calls
print(‘Done’)
Execution begins at the first statement. Statements are executed
one by one, top to bottom.
Function definitions do not change the flow of execution
but only define a function.
Function calls are like detours in the flow of execution.
25
ASTU
Comments
# create a robot with one beeper
hubo = Robot(beepers = 1)
# move one step forward
hubo.move()
dot notation
# turn left 90 degrees
hubo.turn_left()
26
ASTU
Turning right
Define a
function!
def turn_right():
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()
27
ASTU
Newspaper delivery
Hubo should climb the stairs to the front door, drop
a newspaper there, and return to his starting point.
Algorithm(pseudo code): Python version:
Climb up four stairs climb_up_four_stairs()
Drop the newspaper hubo.drop_beeper()
Turn around turn_around()
Climb down four stairs climb_down_four_stairs() 28
ASTU
Climbing up stairs
def climb_up_four_stairs():
climb_up_one_stair()
climb_up_one_stair()
climb_up_one_stair()
climb_up_one_stair()
def climb_up_one_stair():
hubo.turn_left()
hubo.move()
turn_right()
hubo.move()
hubo.move()
def turn_around():
hubo.turn_left() 29
hubo.turn_left()
ASTU
Iteration: for-loops
We should avoid writing the same code repeatedly.
A for-loop allows us to write it more elegantly:
def climb_up_four_stairs():
climb_up_one_stair()
climb_up_one_stair()
climb_up_one_stair()
climb_up_one_stair()
def climb_up_four_stairs():
for i in range(4):
climb_up_one_stair()
30
ASTU
To repeat the same instruction 4
times:
for i in range(4): for-
loop
print("CSE1061 is fantastic!")
Don’t forget the indentation!
What is the difference between the following
two programs?
for i in range(4):
print(" CSE1061 is great!")
print("I love programming!")
for i in range(4):
print(" CSE1061 is great!")
print("I love programming!")
31