Introduction to Computers and Programming 01204111 Computer Programming

  • Slides: 35
Download presentation
Introduction to Computers and Programming 01204111 – Computer Programming

Introduction to Computers and Programming 01204111 – Computer Programming

What is a computer? • A computer is a machine that Follows instructions (software)

What is a computer? • A computer is a machine that Follows instructions (software) in memory Ø Reads input data Ø Stores and processes data Ø Produce outputs Ø 2

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

Development • 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 3

Types of computers • Desktop computers • Notebook computers • Mobile computers (Personal Digital

Types of computers • Desktop computers • Notebook computers • Mobile computers (Personal Digital Assistants, PDA) • Mainframes • Super computers Microcomputers 4

Hardware • Input devices • Output devices • Central Processing Unit (CPU) • Main

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

Computer Programming • To program a computer is to write instructions so that the

Computer Programming • 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. 6

Basic Steps Problem analysis Program design Implementation Testing 7

Basic Steps Problem analysis Program design Implementation Testing 7

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

Programming languages • Programming languages Low-level languages. e. g. , machine languages, assembly language Ø High-level language. e. g. , C, Pascal, Java, C#, Python Ø 8

Low-level languages • Instructions depend on the specific architecture of the machines Ø E.

Low-level languages • Instructions depend on the specific architecture of the machines Ø E. g. , x 86, ARM, AVR architectures • Each instruction correspond to specific cycle of execution • A program written for a specific architecture cannot be executed in another architecture. 9

Low-level languages • Machine languages Ready to be executed Ø Usually written as binary

Low-level languages • 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. 00011000 01101011 00011001 11111100 10011000 11100000 รหสฐานสอง 18 6 B 19 FC 98 E 0 รหสฐานสบหก Ø SUB R 3, #2, R 6 01 011 110 00000010 10

High-level languages • Read more naturally • Are usually independent of the underlying architecture.

High-level languages • 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); 11

Example of high-level languages • Procedural Fortran Ø Cobol Ø Basic Ø C Ø

Example of high-level languages • Procedural Fortran Ø Cobol Ø Basic Ø C Ø Pascal Ø • Functional Ø Lisp • Object-oriented C++ Ø Java Ø C# Ø VB Ø • Logic Ø Prolog 12

Program Execution • A computer does not "understand" high-level languages. • To execute program

Program Execution • A computer does not "understand" high-level languages. • To execute program in these languages, you need either: An interpreter Ø A compiler Ø 13

Interpreters • An interpreter reads each high-level language instruction and executes that instruction, one

Interpreters • An interpreter reads each high-level language instruction and executes that instruction, one by one. ������ prog. bas �������� 14

Compilers • A compiler reads the whole program and translate it into machine readable

Compilers • A compiler reads the whole program and translate it into machine readable instructions. ������ prog. c �����prog. exe ���������� 15

Languages used in this course • Python An interpretative language Ø Can be used

Languages used in 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 Ø 16

Python • Is a multi-paradigm programming language Procedural Ø Functional Ø Object-oriented Ø •

Python • Is a multi-paradigm programming language Procedural Ø Functional Ø Object-oriented Ø • Is an interpretative language • Easy to get started • Very rich standard library 17

Installation • Download Python 3. 1 at Ø http: //www. python. org/ftp/python/3. 1. 2/python

Installation • 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. 18

Thinking Corners • Try to guess what the following program is doing. 1: print("Welcome")

Thinking Corners • Try to guess what the following program is doing. 1: print("Welcome") 2: g = input("Guess the number: ") 3: guess = int(g) 4: if guess == 5: 5: print("You win!") 6: else: 7: print("You lose!") 8: print("Game over!") 19

Natural languages • A program is not that different from human natural language 1:

Natural languages • A program is not that different from human natural language 1: Display "Welcome" 2: Let g be input("Guess the number: ") 3: Let guess be int(g) 4: if guess equals 5: 5: Display "You win!" 6: else: 7: Display "You lose!" 8: Display "Game over!" 20

Interaction with Python A "prompt" that shows that the system is ready to take

Interaction with Python A "prompt" that shows that the system is ready to take commands 21

Try this • Try these commands (Don't type (<<< and observe the output >>>

Try this • Try these commands (Don't type (<<< and observe the output >>> >>> >>> print(1) print(3*8) print("Hello") input() exit() 22

Combining instructions into programs • We can combine many instructions into a single program

Combining instructions into programs • 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 23

Traditional program development cycle input Editor prog. py program The development process gets much

Traditional program development cycle input Editor prog. py program The development process gets much simplified with the use of IDE's. We shall talk about a particular IDE on the next few slides. Python output 24

How to run your program • You can double-click at the. py file. The

How to run your program • 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. 25

Wing IDE 101 • In this course, we will develop programs in an Integrated

Wing IDE 101 • In this course, we will develop programs in an Integrated Development Environment (IDE) It has a build-in editor Ø Can execute programs inside the system Ø It comes with a debugging tool. Ø 26

Turtle-style graphics • We shall learn basic ideas of programming by telling and teaching

Turtle-style graphics • 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 * 27

What can a turtle do? • Moves forward/backward • Turns left or right •

What can a turtle do? • Moves forward/backward • Turns left or right • Takes a pen with it 28

Basic turtle control – Create a turtle, call it t • t. forward(d) -

Basic turtle control – 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 • t = Turtle() 29

Tell the turtle to draw a square • In English: Walk Turn for 100

Tell the turtle to draw a square • In English: Walk Turn for 100 steps right 90 degree 30

Tell the turtle to draw a square • In Python: from turtle import *

Tell the turtle to draw a square • In Python: from turtle import * t = Turtle() t. forward(100) t. right(90) t. forward(100) 31

Teach the turtle • You can define new commands using def from turtle import

Teach the turtle • You can define new commands using def from turtle import * t = Turtle() Make sure the indentation is aligned. def square(): t. forward(100) t. right(90) square() 32

Repetitive instructions • The instructions are very repetitive. • High-level languages have ways to

Repetitive instructions • 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() 33

Thinking Corner • Write a program that tells the turtle to draw a equilateral

Thinking Corner • Write a program that tells the turtle to draw a equilateral triangle whose sides are 200 -step long, on the canvas. 34

Software required for the first half • Python 3. 1. 2 Ø http: //python.

Software required for 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 35