Download Python and Java.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
• 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
2
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
3
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,…
4
Python and Java: Differences
Java
Compiler/Interpreter
Python
Interpreter
Interactive regime
Lower level, e.g.
Higher level, e.g.
System.out.println();
print()
Class cannot change
Structuring by using
; and {}
Structuring by using
ident / line
5
Python and Java: Differences
Java
Loop
for: formal
Python
Loop
for: informal
Type: explicit
Type: implicit
class – always
only for user-defined
types
Work within main method
No
Array &
List &
Collections (list, set, map)
Collections (tuple,set,
dictionary)
main method
6
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
Python’s
System.out.print(“Hello world”);} is
7
print(“Hello world”),
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’]
8
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)
9
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)
10
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)
11
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
12
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()
13
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”);
14
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”);
15
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
16
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
17