Below are the code sample which shows how to carry out basic tasks in Jython.The Files should be shared as .py extension if your are using eclipse.To know abount configuring jython in eclipse follow the
Link
Printing a Message in Console
print 1 + 1
print "Hello" + " World "
print "Hello Jython "
print "Jython ", "Scripting"
Simple Jython Function that Takes Argument and Print Values
def displayName(x):
print x
displayName("Mugil")
Jython Function which Calculates the Factorial of a Number
def factorial(x):
if x<=1 :
return 1
return x * factorial(x-1)
print factorial(3)
Function which compares Two Strings in If Condition
def compareName(FirstName, LastName):
if FirstName == LastName:
print "They are Equal"
else:
print "They are Not Equal"
compareName("Jython", "Jython")
Conditional Operators
Function which Perform AND, OR and Not Ops
def BoolOps(pOne, pTwo):
if pOne and pTwo:
print "They are Equal"
else:
print "They are Not Equal"
BoolOps(1,0)
Jython Supports Three Numeric Values as Below
- Integers
- Long
- Floating Point
Array from Delimited String and Looping through It
Names = "A, B, C, D, E"
arrNames = Names.split(",")
for x in range(len(arrNames)):
print arrNames[x]
Array Function
Names = "A,B,C,D,E,F,G,H,I,J,K,L,M"
arrNames = Names.split(",")
#B
print arrNames[1]
#['B']
print arrNames[1:2]
#['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
print arrNames[1:]
#['A', 'B', 'C', 'D', 'E', 'F']
print arrNames[:6]
#['B', 'C', 'D', 'E']
print arrNames[1:5]
#['H', 'I', 'J']
print arrNames[-6:-3]
#['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
print arrNames[:]
#['A', 'C', 'E', 'G', 'I', 'K', 'M']
print arrNames[::2]
Concatenating Two Arrays together
Names1 = "A,B,C"
Names2 = "D, E, F"
arrNames1 = Names1.split(",")
arrNames2 = Names2.split(",")
arrNames = arrNames1 + arrNames2
for i in range(len(arrNames)):
print arrNames[i]