New 08- Programming Languages
New 08- Programming Languages
Languages
LECTURE 8
Objectives
❑ Describe the evolution of programming languages from
machine language to high-level languages.
Low-level
languages: Difficult to code in; machine
dependent
Machine language: 1s and 0s
Assembly language: Includes some names and other
symbols to replace some of the 1s and 0s in machine
language
Low-level languages:
Fourth-generation
languages (4GLs): Even closer to natural
languages and easier to work with than high-level
Declarative rather than procedural
Includes structured query language (SQL) used with databases
Low-Level
Programming
Languages
Low-Level Programming Languages
11
Machine language
and
Assembly language
Code in Machine Language to Add Two Integers
Code in Assembly Language to Add Two Integers
High-Level
Programming
Languages
15
Code in High Level Languages (in C)
#include <stdio.h>
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Sum: %d",sum);
return 0;
}
Code in High Level Languages (in Python)
print("Enter two integers: ")
num1=int(input())
num2=int(input())
sum=num1+num2
print("Sum: ", sum)
Common Programming Languages
Python: Open-source, dynamic, object-oriented language that can be
used to develop a variety of applications
Gaming, scientific, database, and Web applications
Only recently gaining a following
18
Languages
Translators
Translation
• Programs today are normally written in one of the high-level
languages.
• To run the program on a computer, the program needs to be
translated into the machine language of the computer on which it will
run.
• The program in a high-level language is called the source program.
• The translated program in machine language is called the object
program.
The Process Of Creating A Computer
Program
Translation
A special computer program (translator)
translates the program written by the
programmer into the only form that the computer
can understand (machine language/binary)
Program Creation
A person (programmer) writes a computer
program (series of instructions).
The program is written and saved using a Execution
text editor. The machine language
The instructions in the programming instructions can now be directly
language are high level (look much like a executed by the computer.
human language).
Translators
Types of language translators:
Compilers: Language translator
that converts an entire program
into machine language before
executing it
Interpreters: Translates one line of code
at one time
Assemblers:
Convert assembly
language programs into machine
language
22
Source Code Translation Process
• Lexical analyzer reads the stream of characters making up the source program and groups
characters into meaningful sequence of lexemes (Tokens).
• Syntax analyzer uses the first components of the tokens (produced by the lexical analyzer) to
create a tree-like intermediate representation that depicts the grammatical structure of the
token stream.
• Semantic analyzer uses the uses syntax tree and symbol table to check whether the given
program is semantically consistent with language definition.
• Code generator takes as input an intermediate representation of the source program and
maps it into the target language.
Programming Paradigms
• Today computer languages are categorized according to the
approach they use to solve a problem.
• A paradigm, therefore, is a way in which a computer language looks
at the problem to be solved. In other words, it is a style of building the
structure and elements of computer programs.
• We divide computer languages into four paradigms:
• procedural (imperative)
• object-oriented
• functional
• declarative
Categories of Programming Languages
Approaches to Program Design and
Development
Procedural programming: An approach to program design in which a
program is separated into small modules that are called by the main
program or another module when needed
Uses procedures (modules, subprograms): Smaller sections of code that perform
specific tasks
Allows each procedure to be performed as many times as needed; multiple
copies of code not needed
Prior to procedural programming, programs were one large set of instructions
(used GOTO statements)
Structured programming: Goes even further, breaking the program into small
modules (Top-down design) that use basic structures
26
The Concept of the Procedural Paradigm
The Concept of a Procedural Program
Approaches to Program Design and
Development
Object-oriented programming (OOP): Programs
consist of a collection of objects that contain data
and methods to be used with that data
Class: Group of objects that share
some common properties
Instance: An individual object in a class
Attributes: Data about the state of an object
Methods: Perform actions on an object
Objects can perform nontraditional actions and be easily
used by more than one program
Inheritance between classes improve reuse
29
The Concept of an Object-Oriented Paradigm
The Components of a
Class
A Function in a Functional Language
Functional programming is a programming paradigm that treats
computation as the evaluation of mathematical functions and
avoids changing-state and mutable data.
It is a declarative type of programming style. Its main focus is on
“what to solve” in contrast to an imperative style where the main
focus is “how to solve” (i.e. functional programming focuses on the
result not the process).
It uses expressions instead of statements. An expression is evaluated
to produce a value whereas a statement is executed to assign
variables.
Solving quadratic equation
a=1 Quadratic
Root 1 = 3
b=3 Equation Solver
Root 2 = -6
c = -18
Other examples of functional programming are
• print and input functions in programming languages
• Built in functions provided by libraries of programming languages such as Math
functions (e.g. sqrt, ceil, power,…etc.)
Python
Programming
A BRIEF INTRODUCTION
Introduction to Python
◼ Most recent popular (scripting/extension) language
◼ although originally introduced around ~1991
◼ Heritage: teaching language (ABC)
◼ Tcl: shell
◼ perl: string (regex) processing
◼ Object-Oriented
◼ rather than add-on (OOTcl)
Introduction to Python
◼ Python is a high-level programming language
◼ Open source and community driven
◼ “Batteries Included”
◼ a standard distribution includes many modules
◼ Dynamic typed
◼ Source can be compiled or run just-in-time
◼ Similar to perl, tcl, ruby
Why Python?
◼ Unlike AML and Avenue, there is a considerable base of
developers already using the language
◼ “Tried and true” language that has been in development
since 1991
◼ Can interface with the Component Object Model (COM)
used by Windows
◼ Can interface with Open Source GIS toolsets
Python Philosophy
◼ Coherence
◼ not hard to read, write and maintain
◼ Power
◼ Scope
◼ rapid development + large systems
◼ Objects
◼ Integration
◼ hybrid systems
Python Features
• no compiling or linking • rapid development cycle
• no type declarations • simpler, shorter, more flexible
• automatic memory management • garbage collection
Some source code editors pop up the console as an external window, and others
contain their own console window.
Programming Basics
• code or source code: The sequence of instructions in a
program.
Some source code editors pop up the console as an external window, and others
contain their own console window.
Python Interactive Shell
% python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Python Editors
PIDA (Linux)(VIM Based)
NotePad++ (Windows)
BlueFish (Linux)
Python IDE
Visual Studio Code
PyDev extension on Eclipse
PyCharm
IDLE
IDLE – Development Environment
• IDLE – a cross-platform Python development
environment
• Hello World
• print(“hello world”)
• Prints hello world to
standard out
• Open IDLE and try it
out yourself
• Follow along using IDLE
More than just Printing
Output:
Hello, world!
You have 15 years until retirement
input
•The python instruction for getting string information from the user.
•Strings cannot be used for calculations ==> Need Conversions
•Format:
<variable name> = input()
OR
<variable name> = input("<Prompting message>")
•Example:
print ("What is your name: ")
name = input ()
OR
Output:
How old are you? 20
Your age is 20
You have 40 years until retirement
◼ Exercise: Write a Python program that prompts the user for his/her
amount of money, then reports how many PlayStaions (PS4) the
person can afford, and how much more money he/she will need to
afford an additional PS4.
Summary and Discussion