1 / 13

Current state

Current state. C and Assembly – Efficient code with access to OS services Shell Scripts – connecting Unix utilities by I/O redirection and pipes. Glue language. In practice, programs output needs pre-processing Usually an interpreted scripting language Useful for writing and maintaining:

ravi
Télécharger la présentation

Current state

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Current state • C and Assembly – Efficient code with access to OS services • Shell Scripts – connecting Unix utilities by I/O redirection and pipes

  2. Glue language • In practice, programs output needs pre-processing • Usually an interpreted scripting language • Useful for writing and maintaining: • custom commands for a command shell • wrapper programs for executables • C/Java VS. Python

  3. Example import java.io.IOException;import java.io.FileReader;import java.io.BufferedReader;class numlines { public static void main(String[] args) throws IOException {BufferedReaderinp = new BufferedReader(new FileReader(args[0])); String line; for(inti=1;;++i) { line = inp.readLine(); if(line==null) break;System.out.printf("%3d: %s\n", i, line); }inp.close(); }} ------------------------------------------------------------------------------------------- import sysi = 1for line in file(sys.argv[1]): print "%3d: %s" % (i, line),i+= 1

  4. Python • An open source interpreted programming language • High-level language • Object-Oriented • Functional Programming • Suitable for scripting • Practical ( VS. Efficiency ) • Dynamic language

  5. Variables • No need to declare • Not typed greeting = "hello world" greeting = 12**2 print greeting • Need to assign (initialize) • use of uninitialized variable raises exception

  6. Strings • "hello"+"world" "helloworld" # concatenation • "hello"*3 "hellohellohello" # repetition • "hello"[0] "h" # indexing • "hello"[-1] "o" # (from end) • "hello"[1:4] "ell" # slicing • len("hello") 5 # size • "hello" < "jello" 1 # comparison • "e" in "hello" 1 # search

  7. Lists [<elements>] >>> my_list = ['Hello', 1, 2.2, 'world'] >>> len(my_list) 4 >>> my_list[0] ‘Hello’ >>>my_list.append(‘espl’) >>> my_list>>> ['Hello', 1, 2.2, 'world‘,’espl’] >>> delmy_list[0] [1, 2.2, 'world‘,’espl’] >>> my_list.index(‘espl’) >>> 4 >>> ‘espl141’ inmy_list >>> False

  8. Dictonary • Hash tables, "associative arrays" • d = {"duck": "eend", "water": "water"} • Lookup: • d["duck"] -> "eend" • d["back"] # raises KeyErrorexception • Keys, values, items: • d.keys() -> ["duck", "back"] • d.values() -> ["duik", "rug"] • d.items() -> [("duck","duik"), ("back","rug")] • Presence check: • d.has_key("duck") -> 1; d.has_key("spam") -> 0

  9. Tuples • key = (lastname, firstname) • point = (x, y, z) # parentheses optional • a,b,c= point # unpack • lastname = key[0] • sort of an immutable list • key[1] = ‘Abed’ #TypeError: 'tuple' object does not support item assignment

  10. Functions def<function_name>(<arguments>): <body> def factorial(n):fact = 1 foriin range(1, n): fact = fact * i returnfact

  11. Classses class student: def__init__(self, id): self._id = id self._grades = {} defupdate_grade(self, subject, grade): self._grades[subject] = grade #Comment, Main: s = student('233445598') s.update_grade('heshbon', 80) s.update_grade('handasa', 49)

  12. Modules >>> from math import * >>> sin(30*3.14/180) >>> from math import sin >>> sin(30*3.14/180) >>> from math import sin as sinus >>>sinus(30*3.14/180)

  13. And More … • Control structures • If statement • While • For • ….

More Related