1 / 17

SOFTWARE AND PROGRAMMING 1

SOFTWARE AND PROGRAMMING 1. Lecture 23.03.11 TODAY: Python and Java Instructor: Prof. Boris Mirkin web-site http:// www.dcs.bbk.ac.uk/~mirkin/sp109 Revision lecture 11 May: Java main concepts and past exam questions. In-class Test 2 results. Marks improved for those with 20 or greater

latika
Download Presentation

SOFTWARE AND PROGRAMMING 1

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. SOFTWARE AND PROGRAMMING 1 Lecture 23.03.11 TODAY: Python and Java Instructor: Prof. Boris Mirkin web-site http://www.dcs.bbk.ac.uk/~mirkin/sp109 Revision lecture 11 May: Java main concepts and past exam questions

  2. In-class Test 2 results • Marks improved for those with 20 or greater • Distribution • 34 or less 9 • 35-50 11 • 51-74 9 • 75 or more 7 _____________________________ • Total 36 PLS – DO COME to SP1 Exam 31 May, even if your expected mark is low: Head count is done over exams only, and our HEFCE funding depends on that

  3. Java/Python: Some history Java: SUN Microsystem’s creation supported by Netscape (currently, Mozilla) web browser – 1996, 2004 Python: one dedicated man’s development, Van Rossum (Netherlands, USA) – 1994, 2000, 2008

  4. Python and Java: Similarities - Object oriented • Typed variables • Static/non-static • Default constructors • User defined types • Assignments • Arithmetic operations • Expressions • If/elseif/else branching • Header-body method/function structure • Local variables • String/array entries indexed from 0,1,…

  5. Python and Java: Differences Java Python Compiler/Interpreter Interpreter Interactive regime Lower level, e.g. Higher level, e.g. System.out.println(); print() Class cannot change Structuring by using Structuring by using ; and {} ident / line

  6. Python and Java: Differences Java Python Loop for: formal Loop for: informal Type: explicit Type: implicit class – always only for user-defined types Work within main method No main method Array & List & Collections (list, set, map) Collections (tuple,set, dictionary)

  7. Python and Java: Differences Java HW.java class HW{ public static void main(String[] args){ System.out.println(“Hello world”);} } Python HW.py >>>print(“Hello world”) *Java’s System.out.print(“Hello world”);} is Python’s print(“Hello world”),

  8. Python and Java: Differences STRINGS: Python is much richer, e.g. >>>s=‘Hello, world’ 0 1 2 3456 7 891011 >>>s.find(‘l’) 2 >>>ns=s.replace(‘o’,’X’) >>>print ns ‘HellX, wXrld’ >>>s.split() [‘Hello,’, ‘world’] >>>s.split(‘l’) [‘He’, ‘o, wor’, ‘d’]

  9. Python and Java: Differences Input from keyboard Java: Scanner class called, Instance created and its method used to feed in Python: input anything (or raw input for all inputs treated as strings) print(“Please enter a number’) R=input() print(“You entered ”, R)

  10. Python and Java: Differences For loop Task: print numbers from 1 to 10 Java for( int count=1;count<11;count++) System.out.println(count); Python for count in range(1,11) print(count)

  11. Python and Java: Differences For loop in array/list Task: print contents of array/list demoar Java for( int count=0;count<demoar.length;count++) System.out.println(“Current item ”+demoar[count]); Python demoar=[‘life’, 5, 6, ‘and’, ‘other’, 333] for count in demoar print(“Current item %s” % count)

  12. Python and Java: Differences METHOD/Function Java: a special structure output_type o_name(typed parameters) { BODY }//, e.g. int square(int x){ return x*x;} Python: an object (flexibility!) def o_name(typed parameters): BODY #, e.g. def square(x): return x*x

  13. Python and Java: Differences Class and instances Java: class Pet{ BODY } Pet p1=new Pet(); Pet p2=new Pet(); //two instances Python: class Pet: BODY p1=Pet() p2=Pet()

  14. Python and Java: Differences Constructor Java: method class_name no_output_type class Pet{ int v1; String v2; Pet(int a, String B){ v1=a; v2=B;} } Pet p1=new Pet(5, “HaHa”); Pet p2=new Pet(3, “AhAh”);

  15. Python and Java: Differences Constructor Python: method __init__() class Pet: int v1; String v2; def __init(self, int a, String B){ self.v1=a self.v2=B # ‘self’ in Python, as ‘this’ in Java, refers to the # instance being created p1= Pet(5, “HaHa”); p2= Pet(3, “AhAh”);

  16. Python and Java: Differences Handling files in Python: Downloading a file import urllib urllib.urlretrieve(http://blah/blah/important.pdf, “nonimportant.pdf”) File input F=open(‘test.txt’, ‘r’) for line in F: print line[0] F.close

  17. Python and Java: Differences Handling files in Python: File output F=open(‘want.txt’, ‘w’) #or, F=open(‘want.txt’, ‘a’) for appending for line in F: F.write line F.close

More Related