Define Jython Functions Example

#defining function
def area(l,b):
   area = l*b
   print "area = ",area
   return area

#calling function
length = 10
breadth = 20
#calling function and obtaining its reurned value
result = area(length, breadth)
print "value returned by function : ", result

Defining Functions with two Arguments in Jython

#defining function with two arguments
def area(l,b):
   area = l*b
   print "area = ",area

#calling function
length = 10
breadth = 20
#with two arguments. This is OK
area(length, breadth)
#only one argument provided. This will throw TypeError

Range Function Program in Jython

for num in range(5):
   print num

For Loop Program in Jython

#each letter in string
for letter in 'Python':
   print 'Current Letter :', letter

WHILE Loop Program in Jython

count = 0
while count<10:
   count = count+1
   print "count = ",count
print "Good Bye!"

Jarray Class in Jython

my_seq = (1,2,3,4,5)
from jarray import array
arr1 = array(my_seq,'i')
print arr1
myStr = "Hello Jython"
arr2 = array(myStr,'c')
print arr2

Java Collection Types Program in Jython

import java.util.ArrayList as ArrayList
arr = ArrayList()
print "ArrayList:",arr
arr.remove(10) #remove 10 from arraylist
arr.add(0,5) #add 5 at 0th index
print "ArrayList:",arr
print "element at index 1:",arr.get(1) #retrieve item at index 1
arr.set(0,100) #set item at 0th index to 100
print "ArrayList:",arr

Date Program in Jython

from java.util import Date
d = Date()
print d


We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.