[go: up one dir, main page]

0% found this document useful (0 votes)
3 views62 pages

New 08- Programming Languages

The document covers the evolution of programming languages from machine language to high-level languages, detailing the translation process from source code to machine language using compilers and interpreters. It introduces various programming paradigms, including procedural, object-oriented, functional, and declarative programming, as well as a brief overview of Python as a high-level programming language. Additionally, it discusses programming basics, development environments, and provides examples of Python code.

Uploaded by

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

New 08- Programming Languages

The document covers the evolution of programming languages from machine language to high-level languages, detailing the translation process from source code to machine language using compilers and interpreters. It introduces various programming paradigms, including procedural, object-oriented, functional, and declarative programming, as well as a brief overview of Python as a high-level programming language. Additionally, it discusses programming basics, development environments, and provides examples of Python code.

Uploaded by

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

Programming

Languages
LECTURE 8
Objectives
❑ Describe the evolution of programming languages from
machine language to high-level languages.

❑ Understand how a program in a high-level language is


translated into machine language using an interpreter or a
compiler.

❑ Learn about computer language paradigms.

❑ Introducing Python programming and arithmetic


expressions
 Please refer to your text book
 Chapter 9 Programming Languages
Introduction
Introduction
 To write a program for a computer, we must use a computer
language.

 A computer language is a set of predefined words that are


combined into a program according to predefined rules
(syntax).

 Over the years, computer languages have evolved from


machine language to assembly languages to high-level
languages.
Programming
Languages
Programming Languages 7

 Programming language: A set of rules, words, symbols,


and codes used to write computer programs

 To write a program, you need appropriate software for the


programming language you will be using
Programming Languages
 Categories of programming languages
8

 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

 High-level languages: Closer to natural languages


 Machine independent
 Includes 3GLs (FORTRAN, BASIC, COBOL,C, etc.) and object-oriented
languages (Visual Basic, C#, Python, Java, etc.)
 Visual or graphical languages: Use graphical interface to create
programs
Programming Languages
 Categories of programming languages
9

 Low-level languages:

 High-level languages: Closer to natural 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

• high-level data types and operations • fast development

• object-oriented programming • code structuring and reuse,, like C++

• embedding and extending in C • mixed language systems


• classes, modules, exceptions • "programming-in-the-large" support

• dynamic loading of C modules • simplified extensions, smaller binaries

• dynamic reloading of C modules • programs can be modified without stopping


Python Features
• universal "first-class" object model • fewer restrictions and rules

• run-time program construction • handles unforeseen needs, end-user coding

• interactive, dynamic nature • incremental development and testing

• access to interpreter information • metaprogramming, introspective objects

• wide portability • cross-platform programming without ports

• compilation to portable byte-code • execution speed, protecting source code

• built-in interfaces to external services • system tools, GUIs, persistence, databases,


etc.
Grouping Indentation
0
Bingo!
---
---
In C (with indentation): ---
In Python: In C (No indentation): 3
---
---
for (i = 0; i < 20; i++) ---
for i in range(20): for (i = 0; i < 20; i++) 6
{ ---
if i%3 == 0: {
if (i%3 == 0) ---
if (i%3 == 0) { ---
print(i) { 9
printf("%d\n", i); ---
if i%5 == 0: printf("%d\n", i); ---
if (i%5 == 0) { ---
print("Bingo!") if (i%5 == 0) 12
printf("Bingo!\n"); }} ---
print("---") { ---
printf("---\n"); ---
printf("Bingo!\n"); 15
} Bingo!
} ---
---
} ---
printf("---\n");
18
---
} ---
Major Versions of Python
◼ “Python” or “CPython” is written in C/C++
- Version 2.7 came out in mid-2010
- Version 3.1.2 came out in early 2010
- Version 3.6 came out in 2018
- Version 3.7 came out in early 2019
- Version 3.8 came out in late 2019

◼ “Jython” is written in Java for the JVM


◼ “IronPython” is written in C# for the .Net environment
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
• 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.
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.
>>>

You can type things directly into a running Python session


>>> 2+3*4
14
>>> name = "Ahmed"
>>> name
'Ahmed'
>>> print ("Hello", name)
Hello Ahmed
>>>
Development Environments
Online Python Interpreter:
https://pynative.com/online-python-code-editor-to-execute-python-
code/

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

• PythonWin – a Windows only interface to Python

• Python Shell – running 'python' from the


Command Line opens this interactive shell
IDLE – Development Environment
IDLE helps you
program in Python by:
• color-coding your
program code
• debugging
• auto-indent
• interactive shell
Visual Studio Code
Visual Studio Code
Visual Studio Code
Example Python

• 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

• Python is an object oriented language


• Practically everything can be treated as an object
” hello world” is a string

• Strings, as objects, have methods that return the


result of a function on the string
Useful Links
Official site
http://www.python.org
Video Tutorial
http://www.youtube.com/tch?v=3cZsjOclmoM
Python Bookstore
https://opensource.com/article/18/9/python-
programming-book-list
Inputs and Outputs
Displaying String Output
• String output: A message appears onscreen that consists of a series of text
characters.
• Whatever is contained with the quotes (single or double) is what appears
onscreen.
–Don’t mix and match different types of quotation marks.
• Format:
print ("the message that you wish to appear")
OR
print ('the message that you wish to appear')
• Example:
print ("foo")
print ('bar')
print
◼ print: Produces text output on the console.
◼ Syntax:
print("Message")
print(Expression)
◼ Prints the given text message or expression value on the console, and moves
the cursor down to the next line.
print(Item1, Item2, ..., ItemN)
◼ Prints several messages and/or expressions on the same line.
◼ Examples:
print("Hello, world!")
age = 45
print("You have", 60 - age, "years until retirement")

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

name = input ("What is your name: ")


input
◼ input: Reads a number from user input.
◼ You can assign (store) the result of input into a variable.
◼ Example:
age = int(input("How old are you? "))
print("Your age is", age)
print("You have", 60 - age, "years until retirement“)

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

You might also like