Thursday, May 30, 2013

Worksheet 2 for python practical workshop- for Advance level students -Python ප්‍රායෝගික වැඩමුඵව-

1)      පුනර්කරණය -The While loop සඳහා අදාල ගැටඵ - problems for Iteration
a.        
X=5
While x <10:
                x=x+1
                print x
Print (“end of the while”)

b.       
number =1
value= int (input("Enter the last value : - "))
while (number <=value):
    print (number)
    number = number+ 1
print("done")
b.          
i = 4
while i < 9:
print(i)
i = i+2
13.    Break සහ continue යන විධානයන් හදුනා ගැනීම සදහා - Identify Break and continue commands

a.        
while 1:
    print 'Spam'
    answer = input('Press y to end this loop')
    if answer == 'y':
        print ('Fries with that?')
        break
print ('Have a ')
print ('nice day!')

b.       
while True:
        s = input(‚Enter something : ‚)
        if s == ‚quit‚:
                        break
        if len(s) < 3:
                        print(‚Too small‚)
                        continue
        print(‚Input is of sufficient length‚)
        # Do other kinds of processing here…


14.   range()  වලින් කරන්නේ කුමක්ද what is meant by range()  function in python.
a.        
range(1,10)
list(range(1,10))
list (range(3,67,5))


15.   For ලූපය හදුනා ගැනීම සඳහා අදාල ගැටඵ - this program is used to identify for loop
a.        
for someChar in “Hello World”:
   print someChar

b.       
for i in range(1, 5):
            print(i)
            else:
               print(‚The for loop is over‚)

c.        
words =['The', 'cat', 'sat', ' on', 'the', 'mat.']
for word in words:
        print (word)

d.       
for c in range (1,6):
    if c == 3:
        continue
    print c

e.       
li= ['s','r','t']
for i in li:
    print (i)

16.    දශමය සංඛ්‍යාවක් ද්විමය සංඛ්‍යාවකට හැරවීම- How to convert decimal to binary

a.        
x= int (input("Enter and integer"))
while x!=0:
    bn=""
    while x>1:
        q= int (x/2)
        r = x%2
        bn =str(r) + bn
        x= q
    bn=str(x) +bn
    print ("binarynumber", bn)
    x= int (input("Enter and integer"))


17.   Function භාවිතය - practice how to use Functions in python
a.        
def cube( y ):
    return y * y * y
for x in range(1,6):
    print (cube(x))

b.       
def printmax(a,b):
    if (a>b):
        print(a,'is maximum')
    elif (a== b):
        print(a,'is equal to',b)
    else:
        print(b ,"is maximum")


c.        
def add_ten(number):
    return number +10
def add_five(number):
    return number +5

#------------------------
a = int (input ( "Enter anumber"))
               
b=add_ten(a)
c= add_five(add_ten(a))
print ("a=",a ,"b=" ,b ,"c=" ,c)


18.    List භාවිතය - using lists
a.        
primes =[2,3,5,7,11,13,17]

print (type(primes))

print (primes[3])

print (len(primes))

primes[0]= "sdf"

print (primes[0])

del primes[3]

print( primes[3])

primes.append(19)

print (primes [6])

print(primes)

primes.reverse()

print(primes)

del primes [6]

primes.insert(2,29)
primes.sort()
print(primes)
primes.remove(11)
print(primes)
help(primes)
print(sorted(primes))
primes = primes+ [31,37,41]

print(primes)

newlist= list('hello')
print(newlist)
print(list('hello'))

print('h' in newlist)

b.       
>>> x = [1,'hello', (3 + 2j)]
>>> x
[1, 'hello', (3+2j)]
>>> x[2]
(3+2j)
>>> x[0:2]
[1, 'hello']

           
19.   Tuples  භාවිතය -using tuples
a.       >>> x = (1,2,3)
>>> x[1:]
b.      tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
tu[1]
c.       >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> t[2] = 3.14        #IMMUTABLE
d.       
e.       
20.    Dictionaries භඳුනා ගැනීම - Indentify what are dictionaries in python
a.        
>>> d = {1 : 'hello', 'two' : 42, 'blah' : [1,2,3]}
>>> d
>>> d['two'] = 99
>>> d
>>> d[7] = 'new entry'
>>> d
>>> d 1={1: 'hello', 2: 'there', 10: 'world'}
>>> del(d[2])

>>> d

need more ?

No comments:

Post a Comment

Memory Locatıons and Addresses (Week 3) - ppt download

Memory Locatıons and Addresses (Week 3) - ppt download : Memory Locations and addresses We will first consider how the memory of a computer...

Total Pageviews

Followers