Top Banner
Introduction to Introduction to Computers Computers and Programming and Programming 01204111 – Computer Programming
35
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Computers and Programming 01204111 – Computer Programming.

Introduction to Introduction to ComputersComputers

and Programmingand Programming01204111 – Computer

Programming

Page 2: Introduction to Computers and Programming 01204111 – Computer Programming.

2

What is a What is a computer?computer?

•A computer is a machine that Follows instructions (software) in memory

Reads input data Stores and processes data Produce outputs

Page 3: Introduction to Computers and Programming 01204111 – Computer Programming.

3

DevelopmentDevelopment•Abacus•Analytical Engine•1 Generation (1940-1956): Vacuum

Tubes•2 Generation (1956-1963):

Transisters•3 Generation (1964-1971):

Integrated Circuits•4 Generation (1971-Present):

Microprocessors, VLSI/ULSI

Page 4: Introduction to Computers and Programming 01204111 – Computer Programming.

4

Types of Types of computerscomputers

•Desktop computers•Notebook computers•Mobile computers

(Personal Digital Assistants, PDA)

•Mainframes•Super computers

MicrocomputersMicrocomputers

Page 5: Introduction to Computers and Programming 01204111 – Computer Programming.

5

HardwareHardware•Input devices•Output devices•Central Processing Unit (CPU)•Main memory•Storage

Page 6: Introduction to Computers and Programming 01204111 – Computer Programming.

6

Computer Computer ProgrammingProgramming

•To program a computer is to write instructions so that the computer can complete the required task.

•These instructions must be specific and unambiguous. To do so, you have to think carefully and describe your idea into instructions in the language that the machine can understand.

Page 7: Introduction to Computers and Programming 01204111 – Computer Programming.

7

Basic StepsBasic Steps

Problem analysis

Program design

Implementation

Testing

Page 8: Introduction to Computers and Programming 01204111 – Computer Programming.

8

Programming Programming languageslanguages

•Programming languages Low-level languages. e.g., machine languages, assembly language

High-level language. e.g., C, Pascal, Java, C#, Python

Page 9: Introduction to Computers and Programming 01204111 – Computer Programming.

9

Low-level Low-level languageslanguages

• Instructions depend on the specific architecture of the machines E.g., x86, ARM, AVR architectures

•Each instruction correspond to specific cycle of execution

•A program written for a specific architecture cannot be executed in another architecture.

Page 10: Introduction to Computers and Programming 01204111 – Computer Programming.

10

Low-level Low-level languageslanguages

• Machine languages Ready to be executed Usually written as

binary or hexadecimal numbers

• Assembly language Encoded machine

languages Each instruction

corresponds to one machine instruction.

SUBSUB R3, #2, R6R3, #2, R6SUBSUB R3, #2, R6R3, #2, R6

00011000 0110101100011000 0110101100011001 1111110000011001 1111110010011000 1110000010011000 11100000

00011000 0110101100011000 0110101100011001 1111110000011001 1111110010011000 1110000010011000 11100000

18 6B 19 FC 98 E018 6B 19 FC 98 E018 6B 19 FC 98 E018 6B 19 FC 98 E0

รหั�สฐานสอง

รหั�สฐานสบหัก

01 011 110 0000001001 011 110 0000001001 011 110 0000001001 011 110 00000010

Page 11: Introduction to Computers and Programming 01204111 – Computer Programming.

11

High-level High-level languageslanguages

•Read more naturally•Are usually independent of

the underlying architecture. -> More portable

•Each statement may correspond to many machine instructions

SUM := A * 2 + ALPHA/3;PRINTLN(SUM);

SUM := A * 2 + ALPHA/3;PRINTLN(SUM);

Page 12: Introduction to Computers and Programming 01204111 – Computer Programming.

12

Example of high-Example of high-level languageslevel languages

• Procedural Fortran Cobol Basic C Pascal

• Functional Lisp

• Object-oriented C++ Java C# VB

• Logic Prolog

Page 13: Introduction to Computers and Programming 01204111 – Computer Programming.

Program Program ExecutionExecution

•A computer does not "understand" high-level languages.

•To execute program in these languages, you need either: An interpreter A compiler

13

Page 14: Introduction to Computers and Programming 01204111 – Computer Programming.

14

InterpretersInterpreters•An interpreter reads

each high-level language instruction and executes that instruction, one by one.

อนเทอร�พร�เตอร�อนเทอร�พร�เตอร�

อนพ�ทอนพ�ท

เอาท�พ�ท

prog.bas

ซอร�สโค้�ดซอร�สโค้�ด

Page 15: Introduction to Computers and Programming 01204111 – Computer Programming.

15

CompilersCompilers•A compiler reads the

whole program and translate it into machine readable instructions.ค้อมไพเลอร�ค้อมไพเลอร�

อนพ�ทอนพ�ท

เอาท�พ�ท

prog.c

ซอร�สโค้�ดซอร�สโค้�ดprog.exe

รหั�สภาษาเค้ร��องรหั�สภาษาเค้ร��อง

Page 16: Introduction to Computers and Programming 01204111 – Computer Programming.

16

Languages used Languages used in this coursein this course

•Python An interpretative language Can be used on many computing systems (e.g., on MS Windows, Unix, and even some mobile phones).

•C# A compilation language More restrictive syntax Very good for developing GUI applications

Page 17: Introduction to Computers and Programming 01204111 – Computer Programming.

17

PythonPython•Is a multi-paradigm

programming language Procedural Functional Object-oriented

•Is an interpretative language•Easy to get started•Very rich standard library

Page 18: Introduction to Computers and Programming 01204111 – Computer Programming.

18

InstallationInstallation•Download Python 3.1 at

http://www.python.org/ftp/python/3.1.2/python-3.1.2.msi

•After the installation, you can try calling the Python Shell from the Start Menu.

Page 19: Introduction to Computers and Programming 01204111 – Computer Programming.

19

Thinking CornersThinking Corners•Try to guess what the

following program is doing.

print("Welcome")

g = input("Guess the number: ")

guess = int(g)

if guess == 5:

print("You win!")

else:

print("You lose!")

print("Game over!")

print("Welcome")

g = input("Guess the number: ")

guess = int(g)

if guess == 5:

print("You win!")

else:

print("You lose!")

print("Game over!")

1:

2:

3:

4:

5:

6:

7:

8:

1:

2:

3:

4:

5:

6:

7:

8:

Page 20: Introduction to Computers and Programming 01204111 – Computer Programming.

20

Natural languagesNatural languages•A program is not that

different from human natural language

Display "Welcome"

Let g be input("Guess the number: ")

Let guess be int(g)

if guess equals 5:

Display "You win!"

else:

Display "You lose!"

Display "Game over!"

Display "Welcome"

Let g be input("Guess the number: ")

Let guess be int(g)

if guess equals 5:

Display "You win!"

else:

Display "You lose!"

Display "Game over!"

1:

2:

3:

4:

5:

6:

7:

8:

1:

2:

3:

4:

5:

6:

7:

8:

Page 21: Introduction to Computers and Programming 01204111 – Computer Programming.

21

Interaction with Interaction with PythonPythonA

"prompt" that

shows that the

system is ready to

take command

s

Page 22: Introduction to Computers and Programming 01204111 – Computer Programming.

22

Try thisTry this•Try these commands (Don't

type >>>) and observe the output

>>> print(1)>>> print(3*8)>>> print("Hello")>>> input()>>> exit()

>>> print(1)>>> print(3*8)>>> print("Hello")>>> input()>>> exit()

Page 23: Introduction to Computers and Programming 01204111 – Computer Programming.

23

Combining Combining instructions into instructions into

programsprograms• We can combine many instructions into a single program for ease of usage later on. Each instruction are executed in

order from top to bottom. We call this sequence of

instructions "a program".• You can use any editing

software to create this program file, e.g., Notepad.

• Python programs's names end with .py ,e.g., first.py

Page 24: Introduction to Computers and Programming 01204111 – Computer Programming.

24

Traditional program Traditional program development cycledevelopment cycle

PythonPython

inputinput

output

prog.py

programprogramEditorEditor

The development process gets much

simplified with the use of IDE's.

We shall talk about a particular IDE on the next few slides.

The development process gets much

simplified with the use of IDE's.

We shall talk about a particular IDE on the next few slides.

Page 25: Introduction to Computers and Programming 01204111 – Computer Programming.

25

How to run your How to run your programprogram

•You can double-click at the .py file. The Python interpreter will start to execute your program.

•Notes: After the program finishes, the window will be closed immediately. By adding a command "input()", you can keep the window opened.

Page 26: Introduction to Computers and Programming 01204111 – Computer Programming.

26

Wing IDE 101Wing IDE 101•In this course, we will

develop programs in an Integrated Development Environment (IDE) It has a build-in editor Can execute programs insidethe system

It comes with a debuggingtool.

Page 27: Introduction to Computers and Programming 01204111 – Computer Programming.

27

Turtle-style Turtle-style graphicsgraphics

•We shall learn basic ideas of programming by telling and teaching turtles to draw.

•From the Python Shell we can start using the Turtle graphics system by typing from turtle import *

Page 28: Introduction to Computers and Programming 01204111 – Computer Programming.

28

What can a turtle What can a turtle do?do?

•Moves forward/backward•Turns left or right•Takes a pen with it

Page 29: Introduction to Computers and Programming 01204111 – Computer Programming.

29

Basic turtle Basic turtle controlcontrol

• t = Turtle() – Create a turtle, call it t• t.forward(d) - tell t to walk d steps• t.backward(d) - tell t to walk backwards d for

steps• t.right(a) - tell t to turn right for a

degree• t.left(a) - tell t to turn left for a degree• t.penup() – tell t to lift the pen up from the

canvas• t.pendown() - tell t to put the pen back to the

canvas

Page 30: Introduction to Computers and Programming 01204111 – Computer Programming.

Tell the turtle to Tell the turtle to draw a squaredraw a square

•In English:

30

Walk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degree

Walk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degreeWalk for 100 stepsTurn right 90 degree

Page 31: Introduction to Computers and Programming 01204111 – Computer Programming.

Tell the turtle to Tell the turtle to draw a squaredraw a square

•In Python:

31

from turtle import *

t = Turtle()t.forward(100)t.right(90)t.forward(100)t.right(90)t.forward(100)t.right(90)t.forward(100)

from turtle import *

t = Turtle()t.forward(100)t.right(90)t.forward(100)t.right(90)t.forward(100)t.right(90)t.forward(100)

Page 32: Introduction to Computers and Programming 01204111 – Computer Programming.

32

Teach the turtleTeach the turtle•You can define new

commands using deffrom turtle import *

t = Turtle()

def square(): t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90)

square()

from turtle import *

t = Turtle()

def square(): t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90)

square()

Make sure the

indentation is aligned

.

Page 33: Introduction to Computers and Programming 01204111 – Computer Programming.

33

Repetitive Repetitive instructionsinstructions

•The instructions are very repetitive.

•High-level languages have ways to reduce this instruction repetition and duplication To reduce program complexity and increase readability.

from turtle import *t = Turtle()def square(): for x in range(4): t.forward(100) t.right(90)square()

from turtle import *t = Turtle()def square(): for x in range(4): t.forward(100) t.right(90)square()

Page 34: Introduction to Computers and Programming 01204111 – Computer Programming.

34

Thinking CornerThinking Corner•Write a program that tells the

turtle to draw a equilateral triangle whose sides are 200-step long, on the canvas.

Page 35: Introduction to Computers and Programming 01204111 – Computer Programming.

35

Software required Software required for the first halffor the first half

•Python 3.1.2 http://python.org/ftp/python/3.1.2 -/python 3.1.2.msi

•Wing IDE 101 An integrated development environment for Python

http://wingware.com/pub/wingide-101/3.2.5/wingide-101-3.2.5-1.exe