and Programming 01204111 Computers and Programmin ing Jittat Fak - - PowerPoint PPT Presentation

and programming
SMART_READER_LITE
LIVE PREVIEW

and Programming 01204111 Computers and Programmin ing Jittat Fak - - PowerPoint PPT Presentation

Introduction to Computers and Programming 01204111 Computers and Programmin ing Jittat Fak Jit akcharo roenphol, , Cha haip iporn Jai Jaikaeo De Department of of Com omputer Eng ngineerin ing Kas asetsart rt Uni nivers rsity


slide-1
SLIDE 1

Introduction to Computers and Programming

Jit Jittat Fak akcharo roenphol, , Cha haip iporn Jai Jaikaeo De Department of

  • f Com
  • mputer Eng

ngineerin ing Kas asetsart rt Uni nivers rsity

Cliparts are taken from http://openclipart.org Revised 2018-01-09

01204111 Computers and Programmin ing

slide-2
SLIDE 2

2

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
slide-3
SLIDE 3

3

The age of computing

  • Computers are everywhere.

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

slide-4
SLIDE 4

4

What "computers" used to be

https://en.wikipedia.org/wiki/Human_computer These people were "computers" in 1949

slide-5
SLIDE 5

5

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/

slide-6
SLIDE 6

6

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

slide-7
SLIDE 7

7

More readable

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 1001 1110 1000 1011 1100 1011 1110 0010 1001 0111 1100 1011 1110 0010 1001 0111 1100 1011

MOV AX,10 SUB BX,AX MOV [DX],AX JMP 200 MOV CX,5 MOV AX,10 MUL AX,CX CMP BX,AX JLE 500 JMP 400 sum = 0 for i in range(1,101): sum = sum+i

Machine instructions Instructions in assembly language Instructions in Python programming language

slide-8
SLIDE 8

8

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.

slide-9
SLIDE 9

9

Walking from home to school

  • If you know the way to KU, you can just
  • walk. But if you don’t you may want 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

  • f problems not just for going from your

home to KU, but from any place to any

  • ther place.
slide-10
SLIDE 10

10

A computer, inputs, and outputs

  • In a way, you are a computer.

Location of your home Location

  • f KU

Map Route from home to KU

Inputs Output

A computer

that solves the path problem

slide-11
SLIDE 11

11

A program

  • Can you teach other people to solve the same problem?

A computer

“If you have a map, you can find you way from one place to another using the following instructions. First, locate ……”

A program (or software)

slide-12
SLIDE 12

12

How computer works, abstractly

Central Processing Unit (CPU) Memory Input & Output

Performs calculation and makes decision based on instructions and data in the memory Stores data and instructions Provides means for the CPU to interact with the

  • utside world through

various devices

slide-13
SLIDE 13

13

The real computer components

Main Image by user HereToHelp from https://en.wikipedia.org/wiki/File:Personal_computer,_exploded_6.svg

CPU Memory Hard drive (external memory) Other devices are input or

  • utput devices

Power supply

From http://www.intel.com/ content/www/us/en/support/ processors/000005576.html From http://www.kingston.com/ us/memory/server

slide-14
SLIDE 14

14

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

slide-15
SLIDE 15

15

Inside the memory

  • The smallest unit of information

that can be processed by digital computers is a single binary digit (a bit), which can be either 0 or 1.

  • We usually group them in groups
  • f 8 bits, each called a byte.
  • A lot of bytes can be stored in a

memory unit.

  • 1 kB = 1,000 bytes
  • 1 MB = 1,000,000 bytes
  • 1 GB = 1,000,000,000 bytes

1 1 1 1 1 Two bits One byte

IEEE-1541 recommendation

  • 1 kiB = 210 bytes = 1,024 bytes
  • 1 MiB = 220 bytes = 1,024 kiB
  • 1 GiB = 230 bytes = 1,024 MiB
slide-16
SLIDE 16

16

The instructions

  • The memory, not only keeps

the data to be processed with the CPU, but it also keeps the instructions.

  • These instructions are in the

format that the CPU can easily understand, referred to as “machine instructions.”

  • When writing a program, we

rarely write in machine instructions.

0001 1001 1001 1110 1000 1011 1100 1011 1110 0010 1001 0111 1100 1011 1110 0010 1001 0111 1100 1011

Central Processing Unit (CPU)

From http://www.intel.com/ content/www/us/en/support/ processors/000005576.html

Memory

slide-17
SLIDE 17

17

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. A program in high-level language

sum = 0 for i in range(1,101): sum = sum + i

Machine instructions

0001 1001 1001 1110 1000 1011 1100 1011 1110 0010 1001 0111 1100 1011 1110 0010 1001 0111 1100 1011

Translation

slide-18
SLIDE 18

18

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)

slide-19
SLIDE 19

19

For your career

  • As an engineer or a scientist,

you will have to perform lots

  • f 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)

slide-20
SLIDE 20

20

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

slide-21
SLIDE 21

21

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 https://www.jisc.ac.uk/rd/get-involved/supporting-technology-startup-projects (CC-BY-NC-ND)

slide-22
SLIDE 22

22

Finally, it’s fun

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

Definitely not this!

slide-23
SLIDE 23

28

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.

slide-24
SLIDE 24

29

Let's try Python (1)

  • Guess what the following Python program does.

print("Hello, Python") Hello, Python

The output

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

slide-25
SLIDE 25

30

Let’s try Python (2)

  • Guess what the following Python program does.

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

The output

The program reads two integers, and outputs their summation.

slide-26
SLIDE 26

31

That looks difficult…

  • 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. a = int(input())

slide-27
SLIDE 27

32

Let’s try Python (3)

  • Guess what the following Python program does.

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

The output

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.

slide-28
SLIDE 28

http://www.azquotes.com/quote/580953

slide-29
SLIDE 29

34

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)

slide-30
SLIDE 30

35

"""Rectangle area calculator Ask rectangle's length and width from user, then compute rectangle area """ import math def rectangle_area(length,width): """Compute area of rectangle of specified length and width""" return length*width # ask user to input length and width of a rectangle length = int(input("Enter length: ")) width = int(input("Enter width: ")) # then compute and print out the area area = rectangle_area(length,width) print(f"Rectangle area is {area}")

Typical Python programs

Docstrings Comments Function definition

Flow of program

slide-31
SLIDE 31

36

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 # ask user to input length and width of a rectangle length = int(input("Enter length: ")) width = int(input("Enter width: "))

docstring comment

slide-32
SLIDE 32

37

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}")

slide-33
SLIDE 33

38

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).

slide-34
SLIDE 34

39

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.

EDIT RUN TEST

slide-35
SLIDE 35

40

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.

slide-36
SLIDE 36

41

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

slide-37
SLIDE 37

42

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
slide-38
SLIDE 38

43

Tell Turtle to draw something

  • The code is executed sequentially from top to bottom
  • What is Turtle drawing?

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)

Flow of program

slide-39
SLIDE 39

44

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)

slide-40
SLIDE 40

45

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) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) draw_square() turtle.left(45) draw_square()

Define a subroutine "draw_square" Call the subroutine "draw_square"

slide-41
SLIDE 41

46

Make it more general

  • The draw_square subroutine can be generalized to draw

rectangles of any sizes

import turtle 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) turtle.left(45) draw_square(50)

"draw_square" now takes a parameter Draw a square of size 100 Draw a square of size 50

slide-42
SLIDE 42

47

Get rid of repetitive code

  • Most programming languages provide special constructs

for repeated actions

import turtle 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) turtle.left(45) draw_square(50) import turtle def draw_square(size): for _ in range(4): turtle.forward(size) turtle.left(90) draw_square(100) turtle.left(45) draw_square(50)

slide-43
SLIDE 43

48

Execute actions selectively

  • Some actions need to be executed only when certain

conditions are met

import turtle 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) turtle.left(90) draw_square(80) This code is executed only when size is greater than zero. This call gives no result

slide-44
SLIDE 44

49

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

slide-45
SLIDE 45

50

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
slide-46
SLIDE 46

51

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