You are on page 1of 46

Introduction to

Computers and
Programming
01204111 Computers and Programming

Pattara Leelaprute, Jittat Fakcharoenphol, Chaiporn


Jaikaeo
Department of Computer Engineering
Kasetsart University
Cliparts are taken from http://openclipart.org Revised 2018-01-09
Outline
• Introduction to computer programming.
◦ A bit of computer anatomy and a quick history of computing.
• A quick look at the Python programming language and
common constructs
◦ Sequential programs
◦ Subroutines
◦ Repetitive execution
◦ Selective execution

2
The age of Computers
• Computers are everywhere.

By Marc van der Chijs at https://www.


flickr.com/photos/chijs/21798665468

From: https://pixabay.com/en/network-iot-internet-of-
things-782707/ By Philip Wilson (BY-ND) at
https://www.flickr.com/photos/internetsense/9900738813/

3
What "computers" used to be
These people were "computers" in 1949

https://en.wikipedia.org/wiki/Human_computer 4
Computer programming
• Programming – an act of developing computer programs.
• What is a computer program?

Public domain Image from https://pixabay.com/en/work-typing-computer-notebook-731198/ 5


A computer program
• Margaret Hamilton with the
computer program that took
Apollo 11 to the moon.
• You can read the code at:
https://github.com/chrislgarry/Apollo-11

Public domain image from https://en.wikipedia.org/wiki/Margaret_Hamilton_(scientist)#/media/


File:Margaret_Hamilton.gif 6
A computer program
• A computer program is a sequence of instructions to be
executed by computers.
• Examples of computer programs in various forms:
0001 1001 MOV AX,10
1001 1110 SUB BX,AX sum = 0
1000 1011 MOV [DX],AX for i in range(1,101):
JMP 200 sum = sum+i
1100 1011
MOV CX,5
1110 0010 MOV AX,10 Instructions in Python programming language
1001 0111 MUL AX,CX which is one of “high-level programming language”
1100 1011 CMP BX,AX
1110 0010 JLE 500
1001 0111 JMP 400 More readable
1100 1011 Instructions in Assembly
language (“low-level
Machine instructions
programming language”)

7
From home to school
• To understand how computer
works, let’s try to make an
analogy with how people solve
some problem.
• Problem: It’s the first day of
school. You want to go to KU
from your home. What do you
have to do?
◦ Assume that your home is close to
KU, so you decide to walk to KU.

8
Walking from home to school
• If you know the way to KU, you can just
walk. But if you don’t, you may need to
look at the map and use it to plan your
route to KU.
• Note that if you can plan your walking
route with a map, you can solve this kind
of problems not just for going from your
home to KU, but from any place to any
other place.

9
A computer, inputs, and outputs
• In a way, you are a computer.

Location of
your home

Location
of KU Route from
home to KU

Map

A computer Output
Inputs that solves the route-
finding problem
10
A program
• Can you teach other people to solve the same problem?
“If you have a map, you
can find your way from
one place to another using
the following instructions.
First, locate ……”

A program
(or software)
A computer

11
How computer works, abstractly Performs calculation and
makes decision based on
instructions and data in the
memory

Central
Processing
Input Unit
& (CPU)
Output

Memory
Provides means for the
CPU to interact with the
outside world through Stores data and
various devices instructions

12
The real computer components
From http://www.intel.com/
content/www/us/en/support/
Power supply processors/000005576.html
CPU

From http://www.kingston.com/
Memory us/memory/server

Other devices are input or


output devices

Hard drive
(external memory)
Main Image by user HereToHelp
from https://en.wikipedia.org/wiki/File:Personal_computer,_exploded_6.svg 13
Yes, your smartphone is a computer too
• If you disassemble your
smartphone, you will find CPU(s),
memory units, and other I/O
devices as well.

CC-BY-SA Image by Tyler Love, from iPhone Repair


URL: http://flickr.com/photos/28004346@N08/3399597800 14
Inside the memory
• The smallest unit of information
that can be processed by digital 0 1
computers is a single binary digit Two bits
(a bit), which can be either 0 or 1.
• We usually group them in groups
of 8 bits, each called a byte. 0 1 0 1 1 1 0 0
One byte
• A lot of bytes can be stored in a
memory unit.
◦ 1 kB = 1,000 bytes IEEE-1541 recommendation
◦ 1 MB = 1,000,000 bytes • 1 kiB = 210 bytes = 1,024 bytes
◦ 1 GB = 1,000,000,000 bytes • 1 MiB = 220 bytes = 1,024 kiB
• 1 GiB = 230 bytes = 1,024 MiB

15
The instructions
Memory
0001 1001 Central • The memory, not only keeps
1001 1110 Processing the data to be processed
1000 1011
Unit with the CPU, but it also
1100 1011
1110 0010 (CPU) keeps the instructions.
1001 0111 • These instructions are in the
1100 1011
1110 0010 From http://www.intel.com/
format that the CPU can
1001 0111 content/www/us/en/support/
processors/000005576.html
easily understand, referred to
1100 1011 as “machine instructions.”
• When writing a program, we
rarely write in machine
instructions.

16
From programs to instructions
• Instead of working directly with machine instructions, people usually
develop software with higher-level programming languages.
• But the program must be translated into a form that the computer
can understand. This process is called program translation.
0001 1001
1001 1110
sum = 0 1000 1011
for i in range(1,101): 1100 1011
sum = sum + i 1110 0010

Translation 1001
1100
0111
1011
1110 0010

Machine 1001 0111

A program in
1100 1011

instructions
high-level
language

17
Why do you want to learn how to program?
• Computer programming
is not the easiest thing
to learn, but it will
definitely be useful to
you.

From: https://pixabay.com/en/computer-female-girl-isolated-15812/ (CC0 license)

18
For your career
• As an engineer or a scientist,
you will have to perform lots
of important computation
tasks.
• Knowing how to program
gives you advantages:
◦ you can write the programs to
do these tasks yourself, or
◦ if you let someone develop
programs for you, you might
have a better judgement on the
quality of the work.
(1) By User A1 from https://commons.wikimedia.org/wiki/File:Elmer-pump-heatequation.png (CC-BY-SA)
(2) By User Lazarus666 from https://commons.wikimedia.org/wiki/File:Osmosis_computer_simulation.jpg
(3) By Rocchini from https://commons.wikimedia.org/wiki/File:Self_avoiding_walk.svg (CC-BY-SA) 19
It is central to innovations
• Many exciting innovations
have components that
perform intelligent tasks.
• They usually rely on powerful
software running on the
devices.
• With recent cheap
prototyping hardware boards,
innovators can try new ideas
faster by writing codes on
existing hardware platforms.
(1) From: https://www.youtube.com/watch?v=H_xmR35Ws0w (2) By Multicherry From:
https://commons.wikimedia.org/wiki/File:Raspberry_Pi_2_Model_B_v1.1_front_angle_new.jpg (CC-BY-SA) (3) By L'Ecole
polytechnique at https://www.flickr.com/photos/117994717@N06/28066156056 20
Tech startups

• If you want to build a tech startup that changes people’s


life, it is very important that you know how to code so
that you can implement your ideas quickly and create
values.
By Jisc and Matt Lincoln from
21
https://www.jisc.ac.uk/rd/get-involved/supporting-technology-startup-projects (CC-BY-NC-ND)
Finally, it’s fun
Definitely not this!

PROGRAMMING

From: https://pixabay.com/en/children-win-success-video-game-593313/ (CC0 license) 22


Python programming language
• In this course, we will use Python programming
language to teach you computer programming.

• We will focus more on learning how to program and


how to write good codes, skills that can be applied with
any popular computer programming languages.

28
Let's try Python (1)
• Guess what the following Python program does.

The output
print("Hello, Python") Hello, Python

This may not look exactly like English, but it is not hard to
guess what the program will do

29
Let’s try Python (2)
• Guess what the following Python program does.

The output
a = int(input()) 11
b = int(input()) 27
result = a + b 11 + 27 = 38
print(f"{a} + {b} = {result}")

The program reads two integers, and outputs their summation.

30
That looks difficult…
a = int(input())

• If this is the first time you see computer programs, you


may feel that this line of code may look fairly difficult.
• But as you continue to see and write more programs, it will
be much easier to understand.

31
Let’s try Python (3)
• Guess what the following Python program does.

The output
import math 10
314.15926538979
r = float(input())
print(math.pi*r*r)

The program reads a number and outputs pi times that


number squared…. What is the goal of this program, again?

It actually computes the area of the circle with radius r.

32
http://www.azquotes.com/quote/580953
A better program
• The following fragment of the code
◦ Performs the exact same task
◦ Is easier to understand because it states its intention fairly clearly

import math

radius = float(input("Enter circle radius: "))


area = math.pi*radius*radius
print("The area of the circle is",area)

34
Typical Python programs
Flow of """Rectangle area calculator
program Ask rectangle's length and width from user, Docstrings
then compute rectangle area
"""
import math

def rectangle_area(length,width):
"""Compute area of rectangle of specified length and width"""
return length*width
Function
# ask user to input length and width of a rectangle definition
length = int(input("Enter length: "))
width = int(input("Enter width: "))

# then compute and print out the area Comments


area = rectangle_area(length,width)
print(f"Rectangle area is {area}")

35
Comments & Docstrings
• They are in the code to provide insights into what code is
doing, how code works, or give other notes
◦ They do not affect how program works
• Docstrings are displayed when calling for help
def rectangle_area(length,width):
"""Compute area of rectangle of specified length and width"""
return length*width
docstring
# ask user to input length and width of a rectangle
length = int(input("Enter length: ")) comment
width = int(input("Enter width: "))

36
A lot of keywords
• The Python language uses many keywords to let you
describe your ideas precisely
• You will later learn the usage of some of the keywords,
but not all
import math

def rectangle_area(length,width):
return length*width

length = int(input("Enter length: "))


width = int(input("Enter width: "))
area = rectangle_area(length,width)
print(f"Rectangle area is {area}")

37
IDE – where you write programs
• We will write our Python programs in an application
software that provides editing facility and translation
services for Python
• This type of software is known as IDE
(integrated development environment).

38
Edit-Run-Test Loop
• Because everything in life doesn’t always work the first time you try
it, your program may not always do exactly like what you want.
• It may be correct in some case, but it might fail in some other.
Therefore, you need to test your program. If it is not correct, you
have to fix it (debug it), and try to test it again.
• Your process for writing a Python program would look like this.

RUN

EDIT
TEST

39
Debugging
• When programmers try to
fix mistakes in their codes,
they usually refer to the
activity as “debugging”
(killing the bugs).
• In the early days of
computing, there was
actually a bug in the
machine. Therefore,
problems with computers
are often referred to as
bugs.

40
Python Turtle Graphics
• Python comes with Turtle graphics module that allows
you to control movement of a robotic turtle on screen
• The turtle carries a pen and draws over the path he
moves on

Graphics illustrated by Jittat Fakcharoenphol

41
Basic Turtle Movements
• turtle.forward(d) – tell Turtle to walk forward d steps
• turtle.right(a) – tell Turtle to turn right for a degrees
• turtle.left(a) – tell Turtle to turn left for a degrees

42
Tell Turtle to draw something
• The code is executed sequentially from top to bottom
• What is Turtle drawing?
import turtle
Flow of
program turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)

43
Guess the result
• What does this code do? import turtle

turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.left(45)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)

44
Abstraction with subroutines
• A set of instructions can be defined into a subroutine
so that they can be called for execution later
import turtle

def draw_square():
turtle.forward(100)
Define a subroutine turtle.left(90)
"draw_square" turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)

Call the subroutine draw_square()


turtle.left(45)
"draw_square"
draw_square()

45
Make it more general
• The draw_square subroutine can be generalized to draw
rectangles of any sizes "draw_square"
import turtle now takes a parameter

def draw_square(size):
turtle.forward(size)
turtle.left(90)
turtle.forward(size)
turtle.left(90)
turtle.forward(size)
turtle.left(90)
turtle.forward(size)
turtle.left(90)

draw_square(100) Draw a square of size 100


turtle.left(45)
draw_square(50) Draw a square of size 50

46
Get rid of repetitive code
• Most programming languages provide special constructs
for repeated actions
import turtle import turtle

def draw_square(size): def draw_square(size):


turtle.forward(size) for _ in range(4):
turtle.left(90) turtle.forward(size)
turtle.forward(size) turtle.left(90)
turtle.left(90)
turtle.forward(size) draw_square(100)
turtle.left(90) turtle.left(45)
turtle.forward(size) draw_square(50)
turtle.left(90)

draw_square(100)
turtle.left(45)
draw_square(50)
47
Execute actions selectively
• Some actions need to be executed only when certain
conditions are met
This code is executed only when
import turtle size is greater than zero.

def draw_square(size):
if size > 0:
for _ in range(4):
turtle.forward(size)
turtle.left(90)

draw_square(100)
turtle.left(45)
draw_square(-50) This call gives no result
turtle.left(90)
draw_square(80)

48
Conclusions
• Computer programming skills (or coding skills) are very important
• A computer takes instructions in a machine readable form. We write
codes in a higher-level language which needs to be translated
• Computer programs consist of instructions. Complex ideas are
expressed in forms of
◦ Sequential instructions
◦ Subroutines
◦ Repetitive execution
◦ Selective (conditional) execution

• Finally, in this course you will learn to program using the Python
programming language

49
References
• You can look at computing history at
◦ https://en.wikipedia.org/wiki/History_of_computing
◦ http://www.computerhistory.org/timeline/computers/
• Learn how to program at code.org
◦ http://www.code.org
• There are a lot of additional online Python tutorials that
you can read and learn
◦ Python for non-programmers
◦ How to Think Like a Computer Scientist: Interactive Edition

50
Revision History
• July 2016 – Jittat Fakcharoenphol (jtf@ku.ac.th)
◦ Originally created for C#
• July 2017 – Chaiporn Jaikaeo (chaiporn.j@ku.ac.th)
◦ Revised for Python
◦ Added examples using Turtle graphics
• January 2018 – Chaiporn Jaikaeo (chaiporn.j@ku.ac.th)
◦ Added small exercise on Turtle graphics

51

You might also like