Tutorials Point, posted on 27th Sep, 2017

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

Tutorials Point, posted on 27th Sep, 2017

Functions

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

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

Tutorials Point, posted on 27th Sep, 2017

Range Function Program

for num in range(5):
   print num

Tutorials Point, posted on 27th Sep, 2017

FOR Loop

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

Tutorials Point, posted on 27th Sep, 2017

WHILE Loop

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

Tutorials Point, posted on 27th Sep, 2017

Jarray Class

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

Tutorials Point, posted on 27th Sep, 2017

Java Collection Types Program

import java.util.ArrayList as ArrayList
arr = ArrayList()
arr.add(10)
arr.add(20)
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

Tutorials Point, posted on 27th Sep, 2017

Date Program

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

1
Advertisements
Loading...