Third Generation Programming Languages AMS505 4'4 Winter 2001 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Third Generation Programming Languages AMS505 4'4 Winter 2001

Description:

countdown(n-1) Return from functions. can return from a function at any time ... countdown(n-1) Multiple parameters. functions can have multiple parameters ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 22
Provided by: GregPh4
Category:

less

Transcript and Presenter's Notes

Title: Third Generation Programming Languages AMS505 4'4 Winter 2001


1
Third Generation Programming LanguagesAMS505 4.4
Winter 2001
  • Major Greg Phillips
  • Royal Military College of Canada
  • Electrical and Computer Engineering
  • greg.phillips_at_rmc.ca
  • 1-613-541-6000 ext. 6190

2
Why programming languages?
  • machine language programming sucks
  • 0001001000000010 yeah, right!
  • assembly language programming sucks, too
  • MOVE.B D2,D1 ooh! what an improvement!
  • need a higher-level, more natural way to design
    and document computer programs
  • machine independent
  • should be able to move programs from platform to
    platform (hardware, OS) without significant
    rework
  • easily written and understood
  • provide generally useful data structures
    (integer, string, list) and control structures
    (selection, iteration)
  • allow us to program using concepts in the
    application domain (e.g., friendly unit, weapon
    system) rather than the machine domain (e.g.,
    byte, memory location, register)

3
Programming firsts
  • First computer program 1843 by Ada Byron,
  • Lady Lovelace, for Charles Babbages
  • Analytical Engine
  • First programming language 1945, Konrad
  • Zuses Plankalkül, designed for the Z-4 but
  • never implemented
  • First implemented programming language Short
    Code for the Binac and Univac by J.W. Mauchly,
    1949.
  • x y - z is coded as 00 V0 03 V1 01 V2
  • First true compiled language Autocode for
  • the Manchester Mark I by Alick E. Glennie,
  • 1952
  • First widely-used programming language
  • COBOL, largely developed by Grace Murray
  • Hopper, USN, 1959

4
Why Python?
  • because Python is an
  • interpreted,
  • easy to understand,
  • third generation language,
  • with object oriented features
  • because Python is freely available see
    http//www.python.org
  • because it's named after Monty Python
  • because I like Python, and it's my module

5
Compiled versus Interpreted
Pure Compiled Implementation
Pure Interpreted Implementation
Hybrid Implementation
Application (programming language)
Application (programming language)
Byte code Compiler
Native Compiler
Application (programming language)
Application (byte code compiled)
Application (compiled)
Interpreter
Byte code Interpreter
Operating System
Operating System
Operating System
Hardware
Hardware
Hardware
Fast Execution
Rapid Development and Portability
6
Values
  • a programming language needs a way of
    representing certain basic types of values
  • done using literal expressions
  • 2 10000 1234593427812
  • are literal expressions of type int
  • 2. 2.0 1.3 3.14592
  • are literal expressions of type float
  • 'Enjoying AMS505 so far?' 'x' 'Hello' '\t'
    '\n'
  • are literal expressions of type string
  • can check the type of any expression in Python
    using type(expression)

7
Operators
  • values can be acted on by operators
  • meaning of the operator determined by type of its
    operand(s)
  • arithmetic operators
  • - /
  • string operators
  • comparison operators
  • ! lt gt lt gt
  • actions of operators are ordered according to
    precedence rules and left to right
  • Parentheses, Exponentiation, Multiplication and
    Division, Addition and Subtraction (PEMDAS)

8
Variables
  • variables are just names with values assigned to
    them
  • in Python, assignment is done using in an
    assignment statement
  • spam 'Enjoying AMS505 so far?'
  • assigns the value of the literal expression
    'Enjoying AMS505 so far?' to the variable spam
  • in Python, a variable takes on the type of
    whatever value it is currently assigned
  • eggs 'Hello' eggs of type string
  • eggs 3 eggs of type int
  • spam eggs what is the type of spam?
  • as with values, can use type(variable)
  • operators can act on variables just as values

Python comments start with
9
Variable names and keywords
  • Python variable names start with a letter and may
    contain letters, numbers and underscores
  • this_is_123_aVariableName
  • upper and lower case are different
  • spam is a separate variable from Spam or sPam
  • Python also has 28 keywords, which may not be
    used as variable names
  • and continue else for import not raise
  • assert def except from in or return
  • break del exec global is pass try
  • class elif finally if lambda print while

10
Statements, composition sequence
  • a statement tells the interpreter to do something
  • assignments are statements (spam 3)
  • useful Python statement
  • print expression
  • can compose expressions and statements
  • print 'There are', 606024, 'seconds in a day'
  • statements are executed in top-to-bottom sequence
  • seconds 60
  • minutes 60
  • hours 24
  • secInDay seconds minutes hours
  • print 'There are', secInDay, 'seconds in a day'

11
Functions (procedures)
  • often need to use the same code over and over
    again put it in a function
  • Python library provides many useful functions
  • abs(-5)
  • raw_input('Enter a number')
  • can also define our own
  • def printTwice(message)
  • print message
  • print message
  • call just like any other function
  • printTwice('Spam')
  • functions can also have return values
  • def doubleString(message)
  • return message2

In Python, blocks of statements are defined by
indentation.
12
Conditionals
  • sometimes, only want to execute code if a certain
    condition is true
  • use if-elif-else blocks to control what happens
  • if x lt y
  • print x, "is less than", y
  • elif x gt y
  • print x, "is greater than", y
  • else
  • print x, "and", y, "are equal
  • can nest conditionals
  • if x y
  • print x, "and", y, "are equal"
  • else
  • if x lt y
  • print x, "is less than", y
  • else
  • print x, "is greater than", y

13
Recursion
  • functions can call functions including themselves
  • a function that calls itself is recursive
  • def countdown(n)
  • if n 0
  • print "Blastoff!"
  • else
  • print n
  • countdown(n-1)

14
Return from functions
  • can return from a function at any time
  • often used to detect error conditions
  • def countdown(n)
  • if n lt 0
  • print "No negative values please!"
  • return
  • elif n 0
  • print "Blastoff!"
  • else
  • print n
  • countdown(n-1)

15
Multiple parameters
  • functions can have multiple parameters
  • def printNTimes(message, number)
  • if number lt 0
  • return
  • else
  • print message
  • printNTimes(message, number-1)

16
Iteration
  • can iterate using recursion but frequently
    simpler or more natural to have a loop
  • Python provides iteration using while and for
  • while is used to iterate where we don't know in
    advance what values we need to iterate over
  • def countdown(n)
  • while n gt 0
  • print n
  • n n -1
  • print "Blastoff!"

17
Sequences
  • Python provides built-in sequence data types
  • the string type is a sequence type
  • spam 'Spanish Inquisition'
  • spam2 an element
  • spam811 a "slice"
  • spam
  • len(spam) the length of the sequence
  • the list type is another sequence type
  • eggs 1, 'a', 12, 13 , 'blork'
  • eggs0
  • eggs4
  • eggs32
  • can test if a value is in a sequence using in
  • 'blork' in eggs returns true (1)
  • 12 in eggs returns false (0)

18
More about sequences
  • strings are immutable
  • spam3 'X' error
  • spam3 'X' spam4 does what we want
  • lists are mutable
  • eggs2 'replacement' OK
  • eggs.append(654321) add to end of list
  • eggs.remove('a') remove first 'a' in list
  • tuples are exactly like lists, but immutable
  • 'this', 4.7, 'is', 'a tuple' seen this before?
  • ('this', 4.7, 'is', 'a tuple') normally
    parentheses

19
More iteration for loops
  • In Python, can use for to iterate across
    sequences
  • for c in spam
  • print c
  • Frequently want numeric sequences create using
    range function
  • range(5) 0, 1, 2, 3, 4
  • range(3, 7) 3, 4, 5, 6
  • range(7, 3, -1) 7, 6, 5, 4
  • for x in range(10)
  • print x, '\t', x2

20
Modules
  • the standard Python distribution includes many
    modules that provide useful functionality
  • a module is really just a Python file that
    defines variables, constants, functions, and
    other things
  • to access the contents of a module, we must
    import it using one of three forms
  • import math
  • print math.pi
  • from math import pi
  • print pi
  • from math import dangerous!
  • print pi
  • see the Python Library Reference

21
Join the RMC or Queens Jugglers!
  • RMC (New Gym)
  • Tuesday evenings, 2130-2300 hrs
  • Queens (Biosciences Atrium)
  • Tuesday evenings, 1900-2100 hrs
  • Saturday afternoons, 1400-1600 hrs
    http//www.ams.queensu.ca/juggling/
Write a Comment
User Comments (0)
About PowerShow.com