Introduction to Computers and Programming · 2019. 7. 13. · 2. Outline •Introduction to...
Transcript of Introduction to Computers and Programming · 2019. 7. 13. · 2. Outline •Introduction to...
Introduction to Computer and Programming
Jittat Fakcharoenphol, Chaiporn Jaikaeo
Department of Computer EngineeringKasetsart University
Cliparts are taken from http://openclipart.org Revised 2018-01-09
01204111 Computer and Programming
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
3
The age of computing•Computers are everywhere.
By Philip Wilson (BY-ND) athttps://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/
4
What "computers" used to be
https://en.wikipedia.org/wiki/Human_computer
These people were "computers" in 1949
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/
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
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 10011001 11101000 10111100 10111110 00101001 01111100 10111110 00101001 01111100 1011
MOV AX,10SUB BX,AXMOV [DX],AXJMP 200MOV CX,5MOV AX,10MUL AX,CXCMP BX,AXJLE 500JMP 400
sum = 0for i in range(1,101):sum = sum+i
Machine instructions
Instructions in assembly language
Instructions in Python programming language
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.
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 of problems not just for going from your home to KU, but from any place to any other place.
10
A computer, inputs, and outputs• In a way, you are a computer.
Location of your home
Location of KU
Map
Route from home to KU
InputsOutputA computer
that solves the path problem
11
A program•Can you teach other people to solve the same problem?
A computer 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)
12
How computer works, abstractly
CentralProcessing
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 outside world through
various devices
13
The real computer components
Main Image by user HereToHelpfrom https://en.wikipedia.org/wiki/File:Personal_computer,_exploded_6.svg
Other devices are input or output devices
From http://www.intel.com/content/www/us/en/support/processors/000005576.html
From http://www.kingston.com/us/memory/server
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 RepairURL: http://flickr.com/photos/28004346@N08/3399597800
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 of 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
0 1
Two bits
0 1 0 1 1 1 0 0
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
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 10011001 11101000 10111100 10111110 00101001 01111100 10111110 00101001 01111100 1011
CentralProcessing
Unit(CPU)
From http://www.intel.com/content/www/us/en/support/processors/000005576.html
Memory
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 inhigh-levellanguage
sum = 0for i in range(1,101):
sum = sum + i
Machine instructions
0001 10011001 11101000 10111100 10111110 00101001 01111100 10111110 00101001 01111100 1011
Translation
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)
19
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)
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'Ecolepolytechnique at https://www.flickr.com/photos/117994717@N06/28066156056
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 fromhttps://www.jisc.ac.uk/rd/get-involved/supporting-technology-startup-projects (CC-BY-NC-ND)
22
Finally, it’s fun
From: https://pixabay.com/en/children-win-success-video-game-593313/ (CC0 license)
Definitely not this!
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.
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
30
Let’s try Python (2)•Guess what the following Python program does.
a = int(input())b = int(input())result = a + bprint(f"{a} + {b} = {result}")
112711 + 27 = 38
The output
The program reads two integers, and outputs their summation.
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())
32
Let’s try Python (3)•Guess what the following Python program does.
import math
r = float(input())print(math.pi*r*r)
10314.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.
http://www.azquotes.com/quote/580953
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*radiusprint("The area of the circle is",area)
Enter circle radius: 10The area of the circle is 314.15926538979
The output
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 rectanglelength = int(input("Enter length: "))width = int(input("Enter width: "))
# then compute and print out the areaarea = rectangle_area(length,width)print(f"Rectangle area is {area}")
Typical Python programs
Docstrings
Comments
Functiondefinition
Flow ofprogram
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 rectanglelength = int(input("Enter length: "))width = int(input("Enter width: "))
docstring
comment
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}")
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).
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
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.
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
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
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 ofprogram
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)
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_quare():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"
46
Make it more general•The draw_square subroutine can be generalized to draw
rectangles of any sizesimport 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
47
Get rid of repetitive code•Most programming languages provide special constructs
for repeated actionsimport 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)
48
Execute actions selectively• Some actions need to be executed only when certain
conditions are met
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)
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
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
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
51
Revision History• July 2016 – Jittat Fakcharoenphol ([email protected])
◦ Originally created for C#
• July 2017 – Chaiporn Jaikaeo ([email protected])◦ Revised for Python
◦ Added examples using Turtle graphics
• January 2018 – Chaiporn Jaikaeo ([email protected])◦ Added small exercise on Turtle graphics