Download presentation
Presentation is loading. Please wait.
Published byScott Wilcox Modified over 8 years ago
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 http://www.dcs.bbk.ac.uk/~mirkin/sp109 Revision lecture 11 May: Java main concepts andpast exam questions
2
2 In-class Test 2 results Marks improved for those with 20 or greater Distribution –34 or less9 –35-5011 –51-749 –75 or more7 _____________________________ –Total36 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
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
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
5 Python and Java: Differences JavaPython Compiler/InterpreterInterpreter Interactive regime Lower level, e.g.Higher level, e.g. System.out.println(); print() Class cannot changeStructuring by using ; and {}ident / line
6
6 Python and Java: Differences JavaPython Loop for : formalLoop for : informal Type: explicitType: implicit class – alwaysonly for user-defined types Work within main methodNo main method Array & List & Collections (list, set, map) Collections (tuple,set, dictionary)
7
7 Python and Java: Differences JavaHW.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
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
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
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
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
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
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
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
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
16 Python and Java: Differences Handling files in Python: Downloading a file import urllib urllib.urlretrieve(http://blah/blah/important.pdf,http://blah/blah/important.pdf “nonimportant.pdf”) File input F=open(‘test.txt’, ‘r’) for line in F: print line[0] F.close
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.