Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a scripting language? What is Python?

Similar presentations


Presentation on theme: "What is a scripting language? What is Python?"— Presentation transcript:

1 What is a scripting language? What is Python?
Scripting with Python What is a scripting language? What is Python?

2 Scripting Languages Originally, a script was a file containing a sequence of commands that needed to be executed Control structures were added to make it possible to do more with scripts

3 Characteristics of Scripting Languages
Generally interpreted Dynamic typing - no declarations Make text processing easy Often provide pattern matching for strings Provide file and directory manipulation Make it easy to do things quickly

4 Basic Scripting Languages
Unix and Linux come with shell programs which are programmable sh bash ksh csh DOS had BAT files

5 Scripting in Other Environments
Even with a GUI operating system, it is still useful to be able to automate repetitive tasks Windows still has bat files Mac OS has AppleScript Some applications have a scripting language built into them Microsoft applications have Visual Basic for Applications (VBA)‏ Hypercard (Apple) had HyperTalk

6 Other Scripting Languages
Other scripting languages were developed to provide increased capability sed -- adapted from the UNIX ed editor in 1977 AWK -- created by Ajo, Wienberger and Kernighan in 1977 Tcl -- an extensible scripting language written by John Ousterhout in 1987 Perl -- created by Larry Wall in 1986 Python-- created in 1989 by Guido van Rossum Ruby -- created in 1993 by Yukihiro Matsumoto

7 Scripting and the Web More recently, a number of scripting languages have been developed for use with web browsers PHP JavaScript ASP (part of Microsoft .NET framework)‏

8 Scripting on onyx the shell languages sed awk perl python ruby tcl
javascript php

9 Python An object-oriented scripting language
Portable (written in ANSI C)‏ Powerful - see next slide Mixable - can be used in conjunction with other languages Easy to use Easy to learn

10 The Power of Python dynamic typing built-in objects built-in tools
built-in libraries garbage collection modular

11 Running Python code Run the python interpreter and type the commands in interactively Call the interpreter with the name of the script python progName Type the program name of an executable script file chmod +x script ./script The first line of the script should be #!/usr/bin/python

12 Hello World #!/usr/bin/python import sys
sys.stdout.write("Hello world!\n")‏

13 Python Program Structure
A python program is composed of modules A python module is composed of statements statements are terminated by a newline unless the last character is a \ or there is an unclosed (. [ or { Put two statements on a line with a ; between Statements create and process objects

14 Python Built-in Types Numbers (immutable)‏
Sequences - indexing, slicing, concatenation Strings (immutable)‏ Lists (mutable)‏ Tuples (immutable)‏ Mapping - indexing by key Dictionaries (mutable)‏ Files

15 Numbers Numeric types integers - a C long long integer - arbitrary number of digits floating point - C double complex Standard arithmetic, comparison, bitwise operators plus ** (exponentiation)‏ Built-in functions : pow, abs, … Modules : rand, math, cmath

16 Sequences Strings : 'abc', "def", """triple-qouted for multi-line strings""" Lists : [1, 2, 4] Tuples : (1, 2, 3)‏

17 Sequence Operations concatenation with + indexing with [ ]
slicing (substring) with [ : ] len( s) to get length % for formatting : fmtString % string * for repeating in for membership : 'a' in str

18 Strings Immutable, ordered sequences of characters
Supports all sequence operations (see strings.py)‏ string module provides other functions re module supports pattern matching

19 string module Names must be qualified with the module name (string. )‏
upper(str), lower(str) - returns a string find( str, substr) returns index of substr split( str, delimStr=" ") returns list of string join( strList, separator) returns a string

20 String class As objects, strings have may of the same methods that the string module contains Qualify the methods with the name of the string

21 Lists Ordered collection of arbitrary objects
arrays of object references mutable variable length nestable Representation : [0, 1, 2, 3] list methods modify the list (append, sort)‏ see lists.py

22 Tuples Ordered sequence of arbitrary objects Immutable Fixed length
Arrays of object references Representation : (1, 2, 3)‏ Like lists except no mutations allowed

23 Dictionary Unordered collection of arbitrary objects
Element access by key, not index (hash table)‏ Variable length, mutable nestable Representation : {'spam' : 2, 'eggs' : 3} see dict.py

24 Dictionary operations
element access : d[key] insert, update : d[key] = 'new' remove : del d [key] length: len(d)‏ methods has_key( str)‏ keys() returns list of keys values() returns list of values

25 Boolean Expressions Expression truth values
0, "", [], {}, 0.0, None are all false everything else is true Comparison operators: < <= == != >= > return True and False (1 and 0)‏ Python allows a < b < c Boolean operators: and or not return an object which is true or false

26 Comparison == checks for value equivalence
is checks for object equivalence < <= > >= do element by element comparison think of how strings are compared in Java

27 Python Statements Assignment Function calls print if/elif/else
for/else while/else Def, return import, from break, continue pass - no-op try-except-finally raise (exception)‏ class global del - delete objects exec

28 Assignment Variable names follow the same rules as in C
Names are created when they are first assigned Names must be assigned before they can be referenced Assignment creates an object reference Associativity is right to left

29 Assignment examples spam = 'spam' spam, ham = 'yum', 'YUM'
list = [1, 2, 3] tuple = ('a', 'b', 'c')‏ d = {1 : 'one', 2 : 'two', 3 : three}

30 Blocks Blocks in python are started by a compound statement
header : stmt1 laststmt Blocks terminate when the indentation level is reduced

31 if Syntax else and elif are optional Can have arbitrary number of elif
if test1: block1 elif test2: block2 else: elseblock else and elif are optional Can have arbitrary number of elif

32 while Syntax while test: loopbody else: nobreakaction
Optional else part is executed if loop exits without a break break and continue work as in Java pass is a null statement used for empty body

33 for for loop is a sequence iterator works on strings, lists, tuples
range is a function that generates a list of numbers for var in seq loopbody else: nobreakaction

34 Functions Define with def fun(p1, p2): return p1 + p2 Call with
Function without a return statement returns None Names are local unless declared global Variables are references assigning to a parameter does not change the caller changing a mutable object does change the caller

35 Scope Python has three scopes
local (in function)‏ global (in module)‏ built-in Nested functions do not have nested scope

36 os Module Provides a portable way to do OS- dependent operations
manipulating files and directories listdir( path) mkdir( path) chmod( path, mode) process management fork, exec, kill system( command) system information


Download ppt "What is a scripting language? What is Python?"

Similar presentations


Ads by Google