Chapter 4
Chapter 4
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
• 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
• 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 Python language uses an interpreter, which is a program that both translates and
executes the instructions in a high-level language program.
• 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
• 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
• 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
lOOP CONDTION
High-Level
Languages
12 + 75
High-Level
Languages
• Using Python
• Installing Python
• The Python Interpreter
• Interactive Mode
• Script Mode
• The IDLE Programming Environment
Using Python
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
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
For example, suppose you want to write a Python program that displays the following
:three lines of text
Script Mode
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.
• 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
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
syntax: The set of legal structures and commands that can be used in a particular
programming language.
https://www.pearsonhighered.com/assets/samplechapter/0/3/2/1/0321537114.pdf
Click icon to add picture