Sharable Java Python
Sharable Java Python
Functional/procedural programming:
Object-oriented programming
Source
Compiler Byte Code Interpreter Processor
Code
First Program
print("Hello, World!")
class Main {
public static void main(String args[]){
[Link]("Hello, World!");
}
}
Variables
a = 10
b = 10.4
c = "String"
d = 'single quoted string'
e = True
print("a, b, c, d, e", a, b, c, d, e)
class Main {
public static void main(String[] args) {
int a = 10;
float b = 10.4f;
String c = "String";
// Not possible
// String d = 'String';
boolean e = true;
[Link]("a, b, c, e " + a + " " + b + " " + c + " " + e);
}
}
Numbers in Python
a = 10
print(a, type(a))
a = float(a)
print(a, type(a))
a = complex(a)
print(a, type(a))
a = complex(a, a)
print(a, type(a))
Numbers in Java
class Main {
public static void main(String[] args) {
Integer a = 10;
Float b = 10.4f;
Double c = 10.4d;
[Link]("a, b, c " + a + " " + b + " " + c);
}
}
Number methods
class Main {
public static void main(String[] args) {
[Link]("[Link] " + [Link](81.5));
[Link]("[Link] " + [Link](81.5));
[Link]("[Link] " + [Link](81.5));
[Link]("[Link] " + [Link](3));
}
}
Number methods
class Main {
public static void main(String[] args) {
[Link]("[Link] " + [Link](-6));
[Link]("[Link] " + [Link](81));
[Link]("[Link] " + [Link](5, 6));
[Link]("[Link]" + [Link](5, 6));
}
}
String methods
str = "ethnus"
print([Link]("nus"))
print([Link]("eth"))
print([Link]())
print([Link]())
class Main {
public static void main(String[] args) {
String str = "ethnus tech";
Character c = '3';
[Link]([Link]("nus"));
[Link]([Link]("eth"));
[Link]([Link](c));
[Link]([Link](c));
}
}
String methods
str = "ethnus"
print([Link]())
print([Link]())
class Main {
public static void main(String[] args) {
String str = "ethnus tech";
[Link]([Link]());
[Link]([Link]());
}
}
Arithmetic Operators
print(a - b) [Link](a - b) 90
print(a // b) [Link](a / b) 10
print(a % b) [Link](a % b) 0
a = 100, b = 10
Comparison Operators
a = 100, b = 10
Bitwise Operators
print(a | b) [Link](a | b) 6
print(a ^ b) [Link](a ^ b) 6
a = 2, b = 4
Logical Operators
a = True, b = False
Ternary Operator
a = 100
b = 10
min = a < b and a or b
print(min)
class Main {
public static void main(String[] args) {
int a = 100;
int b = 10;
int min = a < b ? a : b;
[Link](min);
}
}
Branching
a = '' a = 0
if a: if a:
print("Value of a = ", a) print("Value of a = ", a)
else: else:
print("False value in a") print("False value in a")
a = -1 a = False
if a: if a:
print("Value of a = ", a) print("Value of a = ", a)
else: else:
print("False value in a") print("False value in a")
Branching
class Main {
public static void main(String[] args) {
boolean a = true;
if(a)
[Link]("A is true");
else
[Link]("A is false");
}
}
class Main {
public static void main(String[] args) {
int a = 5;
if(a)
[Link]("A is true");
else
[Link]("A is false");
}
}
Branching
a = 5
if a:
print("a is not False")
if a > 3:
print("a is greater than 3")
else:
print("a is lesser than or equal to 3")
class Main {
public static void main(String[] args) {
int a = 5;
if(a > 0) {
if(a > 3) {
[Link]("a is greater than 3");
} else {
[Link]("a is greater than 3");
}
}
}
}
Branching
public class Main {
public static void main(String[] args) {
char grade = 'B';
switch(grade) {
case 'A' :
[Link]("Excellent!");
break;
case 'B' :
case 'C' :
[Link]("Well done");
break;
case 'D' :
[Link]("You passed");
break;
default :
[Link]("Invalid grade");
}
[Link]("Your grade is " + grade);
}
}
Looping
count = 0
while (count < 9):
print(count)
count = count + 1
class Main {
public static void main(String[] args) {
int count = 0;
while (count < 9) {
[Link](count);
count = count + 1;
}
}
}
Looping
count = 0
while (count < 9):
if count == 4:
count = count + 1
continue
print(count)
count = count + 1
class Main {
public static void main(String[] args) {
int count = 0;
while (count < 9) {
if(count == 4) {
count = count + 1;
continue;
}
[Link](count);
count = count + 1;
}
}
}
Looping
count = 0 class Main {
for count in range(0,9): public static void main(String[] args) {
if count == 4: int count;
continue for(count = 0; count < 9; count++) {
print(count) if (count == 4)
continue;
[Link](count);
}
}
}
import [Link];
class Main {
public static void main(String[] args) {
int age;
Scanner scanner = new Scanner([Link]);
[Link]("Enter your age");
age = [Link]();
[Link]("Entered age is " + age);
}
}
Classes
class Player:
def __init__(self, name, role, rating):
[Link] = name
[Link] = role
[Link] = rating
class Player {
String name;
String role;
int rating;
Player(String name, String role, int rating) {
[Link] = name;
[Link] = role;
[Link] = rating;
}
}
Creating Objects
class Player:
def __init__(self, name, role):
[Link] = name
[Link] = role
def getRole(self):
return [Link]
print([Link]())
print([Link]())
Creating Objects
class Player {
String name;
String role;
Player(String name, String role) {
[Link] = name;
[Link] = role;
}
String getRole() {
return [Link];
}
}
class Main {
public static void main(String[] args) {
Player dhoni = new Player("Dhoni", "wicket keeper");
Player yadav = new Player("Yadav", "bowler");
[Link]([Link]());
[Link]([Link]());
}
}
Creating Objects
class Player:
def __init__(self, name = "", role = ""):
[Link] = name
[Link] = role
def getRole(self):
return [Link]
dhoni = Player()
yadav = Player("Yadav", 'bowler')
print([Link]())
print([Link]())
Creating Objects
class Player {
String name;
String role;
Player() {
}
Player(String name, String role) {
[Link] = name;
[Link] = role;
}
String getRole() {
return [Link];
}
}
class Main {
public static void main(String[] args) {
Player dhoni = new Player();
Player yadav = new Player("Yadav", "bowler");
[Link]([Link]());
[Link]([Link]());
}
}
Aggregation in Python
class Shoe:
def __init__(self, size):
[Link] = size
class Player:
def __init__(self, name, shoe):
[Link] = name
[Link] = shoe
shoe_eight = Shoe(8)
shoe_seven = Shoe(7)
dhoni = Player("Dhoni", shoe_eight)
jaddu = Player("Jadeja", shoe_seven)
print([Link])
print([Link])
Aggregation in Java
class Shoe { class Player {
int size; String name;
Shoe(int size) { Shoe shoe;
[Link] = size; Player(String name, Shoe shoe) {
} [Link] = name;
} [Link] = shoe;
}
}
class Main {
public static void main(String a[]) {
Shoe shoe_eight = new Shoe(8);
Shoe shoe_seven = new Shoe(7);
Player dhoni = new Player("Dhoni", shoe_eight);
Player jaddu = new Player("Jadeja", shoe_seven);
[Link]([Link]);
[Link]([Link]);
}
}
Googly
class Shoe: class Shoe:
def __init__(self, size): def __init__(self, size):
[Link] = size [Link] = size
shoe_eight = Shoe(8)
shoe_seven = Shoe(7)
dhoni = Player("Dhoni", shoe_eight)
jaddu = Player("Jadeja", shoe_seven)
[Link]()
[Link]()
Swapped?
class Shoe:
def __init__(self, size):
[Link] = size
class Player:
def __init__(self, name, shoe):
[Link] = name
[Link] = shoe
def swap(self, other):
other = self
shoe_eight = Shoe(8)
shoe_seven = Shoe(7)
dhoni = Player("Dhoni", shoe_eight)
jaddu = Player("Jadeja", shoe_seven)
[Link](jaddu)
print([Link])
print([Link])
Swapped?
class Shoe { class Player {
int size; String name;
Shoe(int size) { Shoe shoe;
[Link] = size; Player(String name, Shoe shoe) {
} [Link] = name;
} [Link] = shoe;
}
void swapShoe(Player other) {
[Link] = [Link];
}
}
class Main {
public static void main(String a[]) {
Shoe shoe_eight = new Shoe(8);
Shoe shoe_seven = new Shoe(7);
Player dhoni = new Player("Dhoni", shoe_eight);
Player jaddu = new Player("Jadeja", shoe_seven);
[Link](jaddu);
[Link]([Link]);
[Link]([Link]);
}
}
Association in Python
class Bat:
def __init__(self, company):
[Link] = company
class Player:
def __init__(self, name):
[Link] = name
def batting(self, bat):
print("I use " + [Link])
class Main {
public static void main(String a[]) {
Bat mrf = new Bat("MRF Bat");
Bat ss = new Bat("SS Bat");
Player dhoni = new Player("Dhoni", ss);
Player sachin = new Player("Sachin", mrf);
[Link](mrf);
[Link](ss);
}
}
Difference between association & aggregation
Instance Variable
Local Variable
Inheritance
class Employee:
def __init__(self, id):
[Link] = id
class FullTime(Employee):
def __init__(self, id, name):
super().__init__(id)
[Link] = name
def printDetails(self):
print([Link], [Link])
class Main {
public static void main(String a[]) {
FullTime aravind = new FullTime(1001, "Aravind");
FullTime raghavan = new FullTime(1002, "Raghavan");
[Link]();
[Link]();
}
}
Inheritance
class Employee:
def __init__(self, id, private):
[Link] = id
self.__private = private
class FullTime(Employee):
def __init__(self, id, name):
super().__init__(id, "Something")
[Link] = name
def printDetails(self):
print([Link], [Link])
class Main {
public static void main(String a[]) {
FullTime aravind = new FullTime(1001, "Aravind");
[Link]([Link]);
[Link]([Link]);
}
}
Access Specifiers in Java
public ✔ ✔ ✔ ✔
protected ✔ ✔ ✔ ✘
default ✔ ✔ ✘ ✘
private ✔ ✘ ✘ ✘
Calling inherited method
class Employee:
def __init__(self):
pass
def method(self):
print("I am here")
class FullTime(Employee):
def __init__(self):
[Link]()
aravind = FullTime()
Calling inherited method
class Employee {
void method() {
[Link]("I am here");
}
}
class FullTime extends Employee {
public FullTime() {
[Link]();
}
}
class Demo01 {
public static void main(String a[]) {
FullTime aravind = new FullTime();
}
}
Dynamic Polymorphism
class Employee {
void method() {
[Link]("I am here");
}
}
class FullTime extends Employee {
void method() {
[Link]("FullTime");
}
void myMethod() {
[Link]("myMethod");
}
}
class Demo01 {
public static void main(String a[]) {
Employee aravind = new FullTime();
[Link]();
}
}
Dynamic Polymorphism
class Employee {
void method() {
[Link]("I am here");
}
}
class FullTime extends Employee {
void method() {
[Link]("FullTime");
}
void myMethod() {
[Link]("myMethod");
}
}
class Demo01 {
public static void main(String a[]) {
Employee aravind = new FullTime();
[Link]();
}
}
Abstract class
aravind = FullTime()
[Link]()
Abstract class
abstract class Employee {
abstract void work();
}
class Main {
public static void main(String a[]) {
FullTime aravind = new FullTime();
[Link]();
}
}
Let’s ask
class Main {
public static void main(String args[]) {
int a = 5, b = 0;
try {
int c = a / b;
[Link]("No exception");
} catch(Exception e) {
[Link]("Exception");
}
}
}
Exceptions!
a = 5 class Main {
b = 0 public static void main(String args[]) {
try: int a = 5, b = 0;
c = a / b try {
print("No exception") int c = a / b;
except: [Link]("No exception");
print("Exception") } catch(Exception e) {
finally: [Link]("Exception");
print("Anyways") } finally {
[Link]("Anyways");
}
}
}
Our own exceptions
try:
someFun()
except:
print("Exception")
class Main {
public static void method() throws Exception {
throw new Exception();
}
public static void main(String args[]) {
try {
[Link]();
} catch(Exception e) {
[Link]("Exception");
}
}
}
About a static
class Animal:
def walk():
print("Static method")
dog = Animal()
[Link]()
[Link]()
class Animal {
public static void method() {
[Link]("Static method");
}
}
class Main {
public static void main(String[] args) {
Animal dog = new Animal();
[Link]();
[Link]();
}
}
About a final
class Animal {
final int age = 5;
}
class Main {
public static void main(String[] args) {
Animal dog = new Animal();
[Link] = 10;
}
}
About a final
class Animal {
int age = 5;
final void method() {
[Link]("A Method");
}
}
class Dog extends Animal {
void method() {
[Link]("D Method");
}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
}
}
About a final
import [Link];
import [Link];
import [Link];
import [Link];
class Demo01 {
public static void main(String[] args) throws Exception {
[Link]("[Link]");
String conn = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "";
Connection con = [Link](conn, user, pwd);
Statement stmt = [Link]();
}
}
Selecting data
import [Link]
mydb = [Link](
host="localhost", user="root", passwd="", database="test"
)
mycursor = [Link]()
[Link]("select * from demo")
myresult = [Link]()
for x in myresult:
print(x)
cipher = [Link]("AES/CBC/PKCS5PADDING");
[Link](Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original =
[Link]([Link]().decode(encryptedString));
[Link](new String(original));
Encrypting a string
key = Fernet.generate_key()
f = Fernet(key)
encrypted = [Link](message)
print(encrypted)
decrypted = [Link](encrypted)
print([Link]())
File Writing
fo = open("E:/[Link]", "a")
[Link]( "Text from python\n")
[Link]()
[Link]("Writing successful");
[Link]();
File Reading
fo = open("E:/[Link]", "r")
for x in fo:
print(x)
[Link]()
import threading
import time
fo = open("E:/[Link]", "a")
def jobA():
for x in range(1, 999):
[Link]("A\n")
if x % 100 == 0:
[Link](1)
print("Some random job A")
def jobB():
for x in range(1, 999):
[Link]("Booooooo\n")
if x % 100 == 0:
[Link](1)
print("Some random job B")
Threads in Python
t1 = [Link](target=jobA)
t2 = [Link](target=jobB)
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
Threads in Java
class Main {
public static void main(String[] args) throws Exception {
Multi t1=new Multi();
[Link]("A");
Multi t2 = new Multi();
[Link]("Booooo");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Done!");
}
}
Introduction
HTTP REQUEST
WEB SERVER
WEB CLIENT
HTTP RESPONSE
1
Process
Re
ce
iv
e
th
e
re
qu
es
t
2
Se
ar
c h
fo
rt
he
fil
e
3
Pr
o vi
de
th
e
re
sp
on
se
Web Server
Getting python CGI right
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
print("Content-type:text/html\r\n\r\n")
print('<html>')
print('<head>')
print('<title>Hello Word - First CGI Program</title>')
print('</head>')
print('<body>')
print('<h2>Hello Word! This is my first CGI program</h2>')
print('</body>')
print('</html>')
How about this form?
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
print("Content-type:text/html\r\n\r\n")
print('''<form action="/hello_get.cgi" method="get">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
''')
How about this form?
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
print("Content-type:text/html\r\n\r\n")
print('''<form action="/hello_get.cgi" method="get">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
''')
Get in server
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
print("Set-Cookie:UserID=XYZ;")
print("Set-Cookie:Password=XYZ123;")
print("Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT;")
print("Set-Cookie:Domain=localhost;")
print("Set-Cookie:Path=/perl;")
print("Content-type:text/html\r\n\r\n")
print('''I have set some cookie''')
Lets have some cookies
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
print("Set-Cookie:UserID=XYZ;")
print("Set-Cookie:Password=XYZ123;")
print("Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT;")
print("Set-Cookie:Domain=localhost;")
print("Set-Cookie:Path=/perl;")
print("Content-type:text/html\r\n\r\n")
print('''I have set some cookie''')
Reading the cookies
#!C:/Users/Aravind/AppData/Local/Programs/Python/Python36-32/[Link]
Thank You
[Link]