[go: up one dir, main page]

0% found this document useful (0 votes)
25 views55 pages

Chapter 4

The document discusses how programs work at a high level. It explains that: 1) A program is a list of instructions that tells the CPU what operations to perform. The CPU executes these instructions through a fetch-decode-execute cycle. 2) Programming languages have evolved from machine language to make programming easier for humans. Assembly language uses mnemonics instead of binary, and high-level languages allow programming without understanding the CPU. 3) Compilers translate high-level language code into machine language programs, while interpreters translate and immediately execute the code.

Uploaded by

Roa Alturki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views55 pages

Chapter 4

The document discusses how programs work at a high level. It explains that: 1) A program is a list of instructions that tells the CPU what operations to perform. The CPU executes these instructions through a fetch-decode-execute cycle. 2) Programming languages have evolved from machine language to make programming easier for humans. Assembly language uses mnemonics instead of binary, and high-level languages allow programming without understanding the CPU. 3) Compilers translate high-level language code into machine language programs, while interpreters translate and immediately execute the code.

Uploaded by

Roa Alturki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Click icon to add picture

HOW A PROGRAM -4
WORKS

Basimah Aljhne
Click icon to add picture
1. How a Program Works
2. Getting start with python
3. PYTHON Basics
Click icon to add picture
How a Program Works
1. From Machine Language to Assembly
Language
2. High-Level Languages
3. Compilers and Interpreters
How a Program Works
• The CPU is an electronic device that is designed to do specific operations:
 Reading a piece of data from main memory
 Adding two numbers
 Subtracting one number from another number
 Multiplying two numbers
 Dividing one number by another number
 Moving a piece of data from one memory location to another
 Determining whether one value is equal to another value

• As you can see, the CPU performs simple operations on pieces of data.
• The CPU does nothing on its own, however it has to be told what to do, and that’s the
purpose of a program.
• What is a Program?
How a Program Works
• A program: is nothing more than a list of instructions that cause the CPU to perform
operations.
• Each instruction is a command that tells the CPU to perform a specific operation

• Here’s an example of an instruction that might appear in a program: 10110000

• To you and me, this is only a series of 0s and 1s


• To a CPU, however, this is an instruction to perform an operation
• This instruction is written in machine language
• As the CPU understand only 0s and 1s any program must be converted to machine
language
How a Program Works

• Programs are usually stored on a secondary storage device such as a disk drive
• This program must be copied into main memory, (i.e. RAM) each time the CPU
executes it.
• This happened when you double-click on the program’s icon causes
• Then, the computer’s CPU can executes this program
How a Program Works

• When a CPU executes the instructions in a program, it is engaged in a process that is


known as the fetch-decode-execute cycle.
• This cycle, is repeated for each instruction in the program.
How a Program Works

The steps of the fetch-decode-execute cycle:


• Fetch A program is a long sequence of machine language instructions. The first step
of the cycle is to fetch, or read, the next instruction from memory into the CPU.

• Decode the CPU decodes the machine language instruction that was just fetched
from memory, to determine which operation it should perform.

• Execute The last step in the cycle is to execute, or perform, the operation.
How a Program Works
From Machine Language to Assembly Language
• Computers can only execute programs that are written in machine language
• A program can have thousands or even millions of binary instructions
• Putting a 0 or a 1 in the wrong place will cause an error
• Writing a program is very difficult and time consuming

First solution:
Assembly language: Instead of using binary numbers for instructions, uses short words
that are known as mnemonics
Examples: the mnemonic
• add typically means to add numbers
• mov typically means to move a value to a location in memory.
From Machine Language to Assembly Language
• special program known as an assembler is used to translate an assembly language
program to a machine language program.

Mov eax, z

10100001
High-Level Languages

The assembly language is so close in nature to machine language. Therefore, it is


referred to as a low-level language.
Disadvantages of low-level language:
1. programmer must know details of CPU structure
2. write a large number of instructions for even the simplest program.
A high-level language which allows to create powerful and complex programs without:
3. knowing how the CPU structure
4. writing large numbers of low-level instructions
• For example, if a programmer were using COBOL:
DISPLAY "Hello world"
if a programmer were using Python: print ('Hello world’)

In the 1950s, the high-level languages began to appear.


Checkpoint
 A CPU understands instructions that are
written only in what language?
 A program has to be copied into what type
of memory each time the CPU executes it?

 When a CPU executes the instructions in


a program, it is engaged in what process?

 What is assembly language?

 What type of programming language


allows you to create powerful and complex
programs without knowing how the CPU
works?
Compilers and Interpreters

• The CPU understands only machine language instructions,


• Any high-level language program must be translated into machine language
• A compiler or an interpreter will make the translation
• A compiler is a program that translates a high-level language program into a
separate machine language program
• The machine language program can then be executed directly on CPU
Compilers and Interpreters
Compilers and Interpreters

• The Python language uses an interpreter, which is a program that both translates and
executes the instructions in a high-level language program.

• As the interpreter reads each individual instruction in the program, it converts it to


machine language instructions and then immediately executes them.

• This process repeats for every instruction in the program

• Because interpreters combine translation and execution, they typically do not


create separate machine language programs
Compilers and Interpreters
Compilers and Interpreters

• The statements that a programmer writes in a high-level language are called source
code, or simply code.

• The programmer types a program’s code into a text editor and then
saves the code in a file on the computer’s disk.

• Next, the programmer uses a compiler to translate the code into a machine language
program, or an interpreter to translate and execute the code.
Compilers and Interpreters
Compilers and Interpreters

• A syntax error is a mistake such as:


1. a misspelled key word
2. a missing punctuation character
3. incorrect use of an operator

• If the code contains a syntax error, it cannot be translated.

• When this happens the compiler or interpreter displays an error message indicating
that the program contains a syntax error.

• Normally, the programmer corrects the errors and attempts once again to compile
the program.
High-Level
Languages

Key Words Operators Syntax


Key Words

• Each high-level language has its own set of predefined words that the
programmer must use to write a program.

• The words that make up a high-level programming language are known as key
words or reserved words

• Each key word has a specific meaning, and cannot be used for any other
purpose.

Example:
In Python the key word “print” is used to print a message on the screen.
:Key Words

The following Table shows all of the Python key words:

lOOP CONDTION
High-Level
Languages

Key Words Operators Syntax

predefined words that the programmer


must use to write a program.
:Operators

• Programming languages have operators that perform various operations on data.


• For example, all programming languages have math operators that perform
arithmetic.
• In Python, as well as most other languages, the + sign is an operator that adds two
numbers.
• The following adds 12 and 75:

12 + 75
High-Level
Languages

Key Words Operators Syntax

predefined words that the programmer


must use to write a program.
perform various operations on data.
Syntax
• Syntax: is a set of rules that must be strictly followed when writing a program
• The syntax rules dictate how key words, operators, and various punctuation
characters must be used in a program.
• Each language also has its own
• The programmer must know the syntax rules for the language which he use
• The individual instructions that you use to write a program are called statements.
• A programming statement can consist of key words, operators, punctuation, and
other elements
• These elements are arranged in the proper sequence to perform an operation
Checkpoint
 Each language has a set of rules that must
be strictly followed when writing a
program. What is this set of rules called?
 What do you call a program that translates
a high-level language program into a
separate machine language program?
 What do you call a program that both
translates and executes the instructions in
a high-level language program?
 What type of mistake is usually caused by
a misspelled key word, a missing
punctuation character, or the incorrect use
of an operator?
Getting start with python Click icon to add picture

• Using Python
• Installing Python
• The Python Interpreter
• Interactive Mode
• Script Mode
• The IDLE Programming Environment
Using Python

The Python interpreter can run:


1. Python programs that are saved in files
2. interactively execute Python statements that are typed at the keyboard

Python comes with a program named IDLE that simplifies the process of writing,
executing, and testing programs.

Installing Python
The Python Interpreter
When you install Python on your computer, one of the items that are installed is the
.Python interpreter
The Python interpreter is a program that can read Python programming statements and
.execute them

:You can use the interpreter in two modes


1- interactive mode:
the interpreter waits for you to type Python statements on the keyboard. Once you type a
statement, the interpreter executes it and then waits for you to type another statement

2- script mode:
the interpreter reads the contents of a file that contains Python statements. Such a file is
known as a Python program or a Python script. The interpreter executes each statement
in the Python program as it reads it.
Interactive Mode

Once Python has been installed and set up on your system, you start the interpreter in
interactive mode by going to the operating system’s command line and typing the
:following command
python
Interactive Mode
If you are using Windows, you can alternatively click the Start button, then All Programs.

>>> _ : This is indicates the interpreter is waiting for you to type a Python statement.
Python is a print statement: causes a message to be displayed on the screen.
Example: print 'Python programming is fun!'
Script Mode

Interactive mode is:


 useful for testing code
 the statements that you enter are not saved. They are executed and results are
displayed on the screen.

The script mode allow to:


1. writing Python Programs (set of Python statements)
2. saving it in a file,
3. running Them in Script Mode

For example, suppose you want to write a Python program that displays the following
:three lines of text
Script Mode

To write the program you would use a -1


simple text editor like Notepad to create a
:file containing the following statements

To run the program you would -2


go to the directory in which the
file is saved and type the
following command at the
operating system command line:
python_test.py
The IDLE Programming Environment
The previous sections described how the Python interpreter can be started in
.interactive mode or script mode at the operating system command line
As an alternative, you can use an integrated development environment, which is a
single program that gives you all of the tools you need to write, execute, and test a
.program
IDLE also has a built-in text editor with features specifically designed to help you write
Python programs. For example, the IDLE editor “colorizes” code so that key words and
other parts of a program are displayed in their own distinct colors. This helps make
.programs easier to read
.In IDLE you can write programs, save them to disk, and execute them
.Although IDLE is installed with Python, there are several other Python IDEs available
The IDLE Programming Environment
).IDLE stands for Integrated Development Environment(

Use Python interactive shell


The IDLE Programming Environment

Use Python Script Mode


PYTHON Basics

1. PYTHON Features
2. Use The Three Rules
3. Programming basics
PYTHON Features
 Python is a high-level, interpreted, interactive and object-oriented scripting language.

• Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.

• Python is Object-Oriented:Python supports Object-Oriented style or technique of


programming that encapsulates code within objects.

• Python is designed to be highly readable.


• It uses English keywords frequently where as other languages use punctuation, and
it has fewer syntactical constructions than other languages.
PYTHON Features
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
• Easy-to-read: Python code is more clearly defined and visible to the eyes.

• Easy-to-maintain: Python's source code is fairly easy-to-maintain.

• A broad standard library: Python's bulk of the library is very portable and cross-
platform compatible on UNIX, Windows, and Macintosh.

• Interactive Mode: Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
PYTHON Features
Portable: Python can run on a wide variety of hardware platforms and has the same
.interface on all platforms

Extendable: You can add low-level modules to the Python interpreter. These
.modules enable programmers to add to or customize their tools to be more efficient

.Databases: Python provides interfaces to all major commercial databases

GUI Programming: Python supports GUI applications that can be created and ported
to many system calls, libraries, and windows systems, such as Windows MFC,
.Macintosh, and the X Window system of Unix

Scalable: Python provides a better structure and support for large programs than
.shell scripting
PYTHON Features
PYTHON Features

More information about Python in Python Essentials: Module 1part from PCAP
Use The Rules

Rule 1: Think before you program

Rule 2: A program is a human-readable essay


on problem solving that also happens to execute
on a computer.

Rule 3: The best way to improve your


programming and problem solving skills is to
practice.

RULE 4: Test your code, often and


thoroughly!
Programming basics

code or source code: The sequence of instructions in a program.

syntax: The set of legal structures and commands that can be used in a particular
programming language.

output: The messages printed to the user by a program.

console: The text box onto which output is printed.

Some source code editors pop up the console as


an external window, and others contain their own
console window.
Programming basics
Python Interfaces

IDLE helps you program in Python by:


• color-coding your program code
• debugging
• auto-indent
• interactive shell
Programming basics
Python Interfaces

The message (in red) shows:


traceback (which is the path that the code
traverses through different parts of the program -
you can ignore it for now, as it is empty in such a
simple code);

location of the error the effects of the error, not


necessarily the error itself;

content of the erroneous line

name of the error


READ MATERIALS
Welcome to PCAP | Programming Essentials in Python Module 0

https://www.pearsonhighered.com/assets/samplechapter/0/3/2/1/0321537114.pdf
Click icon to add picture

PCAP | Certified associate


in python programming
certification
Click icon to add picture

PCAP | Certified associate


in python programming
certification

You might also like