1-Introduction to programming
1-Introduction to programming
2
Algorithms
●Consider the problem of subtracting one three digit number from
another. You might follow the steps below:
1.Write down the two numbers, with the larger number above the
smaller number and their digits aligned in columns from the right.
2.Start with the rightmost column of digits and work your way left
through the rest of the columns.
3.Write down the difference between the two digits in the current
column of digits, borrowing a 1 from the top number’s next
column to the left if necessary.
4.If there is no next column to the left, stop. Otherwise, move to
the next column to the left, and go back to Step 3.
3
Algorithms
The above sequence of steps is an example of an
●
algorithm
●An algorithm is like a recipe. It provides a set of instructions
that tells us how to do something, such as make change,
bake bread, or put together a piece of furniture.
More precisely, an algorithm describes a process that ends
●
4
Features of an Algorithm
An algorithm consists of a finite number of instructions.
●
5
Features of an Algorithm
An algorithm describes a process that eventually halts after
●
6
Special- vs. General-Purpose Computers
Computers can be designed to run a small set of algorithms for
●
machines.
Unlike other tools, computers can be programmed, which is what
●
7
Information Processing
Information processing can be described with algorithms.
●
9
Computer Hardware
●The basic hardware components of a computer are
memory, a central processing unit (CPU), and a set of
input/output devices, as shown below.
10
Input and Output Devices
Human users primarily interact with the input and output devices.
●
communicate with the external world through various ports that connect them to
networks and to other devices such as smartphones and digital cameras.
The purpose of most input devices is to convert information that human beings deal
●
with, such as text, images, and sounds, into information for computational
processing.
●The purpose of most output devices is to convert the results of this processing back
to human-usable form.
11
Computer Memory
Computer memory is set up to represent and store information in
●
12
Computer Memory
Now, suppose there are 8 of these groups of 16 lights. We
●
13
Computer Memory
14
The CPU
The part of a computer that is responsible for processing
●
16
Computer Software
You have learned that a computer is a general-purpose
●
problem-solving machine.
To solve any computable problem, a computer must be
●
17
Computer Software
●Instead, we build some basic operations into the
hardware’s processor and require any algorithm to use
them.
●The algorithms are converted to binary form and then
loaded, with their data, into the computer’s memory.
●The processor can then execute the algorithms’
instructions by running the hardware’s more basic
operations.
18
Computer Software
Any programs that are stored in memory so that they can
●
20
The Operating System
Now that a loader exists, we can load and execute other
●
21
The Operating System
An operating system is responsible for managing and
●
22
The Operating Systen
● The operating system provides the following:
● A file system to allow users to organise their data.
A user interface to allow users to interact with the
●
simply apps.
An application is a program that is designed for a specific task,
●
24
Programming Languages
●As you have learned, computer hardware can execute only
instructions that are written in binary form—that is, in machine
language.
●Writing a machine language program, however, would be an
extremely tedious, error-prone task.
●To ease the process of writing computer programs, computer
scientists have developed high-level programming languages for
expressing algorithms.
●These languages resemble English and allow the author to
express algorithms in a form that other people can understand.
25
Programming
●A programmer typically starts by writing high-level language
statements in a text editor.
●The programmer then runs another program called a translator
to convert the high-level program code into executable code.
●Because it is possible for a programmer to make grammatical
mistakes even when writing high-level code, the translator
checks for syntax errors before it completes the translation
process.
●If it detects any of these errors, the translator alerts the
programmer via error messages.
26
Programming
The programmer then has to revise the program.
●
27
The Coding Process
28
A History of the Computer
29
A History of the Computer
30
Getting Started with Python
●Guido van Rossum invented the Python programming language
in the early 1990s.
●Python is a high-level, general-purpose programming language
for solving problems on modern computer systems.
The language and many supporting tools are free, and Python
●
31
The Python Shell
Python is an interpreted language, and you can run simple
●
33
The Python Shell
●When you enter an expression or statement, Python evaluates it
and displays its result, if there is one, followed by a new prompt.
●Note the colour coding in IDLE. This helps you to quickly identify
various program elements.
●The Python shell is useful for experimenting with short
expressions or statements to learn new features of the language,
test out ideas, as well as for consulting documentation on the
language.
●To quit the Python shell, you can either select the window’s close
box or press the ctrl+D key combination.
34
Input, Processing and Output
●Most useful programs accept inputs from some source, process
these inputs, and then finally output results to some destination.
In terminal-based interactive programs, the input source is the
●
35
The print function
The programmer can also force the output of a value by
●
36
The print function
●You can pass multiple expressions to the print function by separating them with a
comma.
–print(“hello”, name)
The print function will insert a space between the displayed values.
●
It you don’t want this behaviour, you can use the sep parameter to change it.
●
37
Receiving Input
As you create programs in Python, you’ll often want your
●
38
Receiving Input
● The input function does the following:
–Displays a prompt for the input. In this example, the prompt
is "Enter your name: " .
Receives a string of keystrokes, called characters, entered
–
argument.
●An argument is a piece of information that a function needs
to do its work.
39
Variables
The string returned by the function in our example is saved
●
40
Inputting Numbers
The input function always builds a string from the user’s
●
41
Inputting Numbers
●In the session below, a string is first returned by the input
function. Then the string is passed to the int function to be
converted to an integer.
42
Python Scripts
While it is easy to try out short Python expressions and
●
44
Creating and Running a Script
45
Steps in Interpreting a Python Program
46
Steps in Interpreting a Python Program
●The interpreter reads a Python expression or statement,
also called the source code, and verifies that it is well
formed. As soon as the interpreter encounters such a
syntax error, it halts translation with an error message.
●If a Python expression is well formed, the interpreter then
translates it to an equivalent form in a low-level language
called byte code.
●When the interpreter runs a script, it completely translates
it to byte code.
47
Steps in Interpreting a Python Program
This byte code is next sent to another software component,
●
an error message.
●This is a run-time error. Dividing by zero is an example of a run-
time error.
●Another type of error, that goes undetected and unreported, is
the logic error where your code does a computation other than
what is required to solve the problem at hand.
48
Exercise
●Write a script that asks the user for three things: their
name, the amount of money they have, and the price of the
item the wish to buy.
●The program then calculates and reports how much money
the user has left after they have bought the item.
49