Introduction to Computers and Programming 01204111 Computers and

  • Slides: 46
Download presentation
Introduction to Computers and Programming 01204111 Computers and Programming Jittat Fakcharoenphol, Chaiporn Jaikaeo Department

Introduction to Computers and Programming 01204111 Computers and Programming 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

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 computing • Computers are everywhere. By Marc van der Chijs at

The age of computing • Computers are everywhere. By Marc van der Chijs at https: //www. flickr. com/photos/chijs/21798665468 From: https: //pixabay. com/en/network-iot-internet-ofthings-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.

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

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

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

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

From home to school • To understand how computer works, let’s try to make

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

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

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 Inputs A computer Output that solves the path problem 10

A program • Can you teach other people to solve the same problem? “If

A program • Can you teach other people to solve the same problem? “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) A computer 11

How computer works, abstractly Performs calculation and makes decision based on instructions and data

How computer works, abstractly Performs calculation and makes decision based on instructions and data in the memory Input & Output Central Processing Unit (CPU) Memory Provides means for the CPU to interact with the outside world through various devices Stores data and instructions 12

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

The real computer components Power supply From http: //www. intel. com/ content/www/us/en/support/ processors/000005576. html CPU Memory From http: //www. kingston. com/ us/memory/server Other devices are input or output devices Hard drive (external memory) Main Image by user Here. To. Help 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

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 i. Phone Repair URL: http: //flickr. com/photos/28004346@N 08/3399597800 14

Inside the memory • The smallest unit of information that can be processed by

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 of 8 bits, each called a byte. • A lot of bytes can be stored in a memory unit. ◦ 1 k. B = 1, 000 bytes ◦ 1 MB = 1, 000 bytes ◦ 1 GB = 1, 000, 000 bytes 0 1 Two bits 0 1 1 1 0 0 One byte IEEE-1541 recommendation • 1 ki. B = 210 bytes = 1, 024 bytes • 1 Mi. B = 220 bytes = 1, 024 ki. B • 1 Gi. B = 230 bytes = 1, 024 Mi. B 15

The instructions Memory 0001 1001 1110 1000 1011 1110 0010 1001 0111 1100 1011

The instructions Memory 0001 1001 1110 1000 1011 1110 0010 1001 0111 1100 1011 Central Processing Unit (CPU) From http: //www. intel. com/ content/www/us/en/support/ processors/000005576. html • 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 the CPU can easily understand, referred to 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

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. sum = 0 for i in range(1, 101): sum = sum + i A program in high-level language Translation Machine instructions 0001 1000 1110 1001 1100 1001 1110 1011 0010 0111 1011 17

Why do you want to learn how to program? • Computer programming is not

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/ (CC 0 license) 18

For your career • As an engineer or a scientist, you will have to

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 A 1 from https: //commons. wikimedia. org/wiki/File: Elmer-pump-heatequation. png (CC-BY-SA) (2) By User Lazarus 666 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

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_xm. R 35 Ws 0 w (2) By Multicherry From: https: //commons. wikimedia. org/wiki/File: Raspberry_Pi_2_Model_B_v 1. 1_front_angle_new. jpg (CC-BY-SA) (3) By L'Ecole polytechnique at https: //www. flickr. com/photos/117994717@N 06/28066156056 20

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

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

Finally, it’s fun Definitely not this! PROGRAMMING From: https: //pixabay. com/en/children-win-success-video-game-593313/ (CC 0 license)

Finally, it’s fun Definitely not this! PROGRAMMING From: https: //pixabay. com/en/children-win-success-video-game-593313/ (CC 0 license) 22

Python programming language • In this course, we will use Python programming language to

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

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

Let’s try Python (2) • Guess what the following Python program does. The output a = int(input()) b = int(input()) result = a + b print(f"{a} + {b} = {result}") 11 27 11 + 27 = 38 The program reads two integers, and outputs their summation. 30

That looks difficult… a = int(input()) • If this is the first time you

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

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

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

A better program • The following fragment of the code ◦ Performs the exact

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 print("The area of the circle is", area) 34

Typical Python programs Flow of program """Rectangle area calculator Docstrings Ask rectangle's length and

Typical Python programs Flow of program """Rectangle area calculator Docstrings 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 = rectangle_area(length, width) print(f"Rectangle area is {area}") Function definition Comments 35

Comments & Docstrings • They are in the code to provide insights into what

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

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

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

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

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

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

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

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

Guess the result • What does this code do? import turtle. forward(100) turtle. left(90)

Guess the result • What does this code do? import turtle. forward(100) turtle. left(90) turtle. left(45) turtle. forward(100) turtle. left(90) 44

Abstraction with subroutines • A set of instructions can be defined into a subroutine

Abstraction with subroutines • A set of instructions can be defined into a subroutine so that they can be called for execution later import turtle Define a subroutine "draw_square" Call the subroutine "draw_square" def draw_square(): turtle. forward(100) turtle. left(90) draw_square() turtle. left(45) draw_square() 45

Make it more general • The draw_square subroutine can be generalized to draw rectangles

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) draw_square(100) turtle. left(45) draw_square(50) Draw a square of size 100 Draw a square of size 50 46

Get rid of repetitive code • Most programming languages provide special constructs for repeated

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) def draw_square(size): for _ in range(4): turtle. forward(size) 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

Execute actions selectively • Some actions need to be executed only when certain conditions are met import turtle This code is executed only when 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) turtle. left(90) draw_square(80) This call gives no result 48

Conclusions • Computer programming skills (or coding skills) are very important • A computer

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

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

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