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 ?

Worksheet/written sample programs for python practical workshop- for Advance level students

·         ගයිඩො වෙන් රොසම්(Guido van Rossum) - 1989 /1990 විසින් Python පරිගණක භාෂාව නිර්මාණය කලේය.
·         Download කර ස්ථාපනය කර ගැනීම සඳහා Downloads: http://www.python.org වෙබ් අඩවියට යන්න.
·         To begin and learn python, you need to code lot of simple programs. So here I give you some   simple examples to grab the basics of python programming language.
·         Used version python 3.1.2
·         python භාෂාව ප්‍රායෝගිකව ඉගෙන ගැනීම සඳහා ඉතා සරල අභ්‍යාස කිහිපයක් මෙම සටහන් මගින් ඉදිරිපත්

කෙරෙන අතර මෙමගින් python භාෂාවේ සියළු අංගයන් ආවරණය නොකෙරේ.
 පහත දක්වා ඇති විධානයන් python  interpreter >>>  හි ඇතුඵ කර ප්‍රතිදානයන් විමර්ශනය කරමින් අධ්‍යයනයේ යෙදෙන්න. වෙනස් කම් කරමින් නැවතත් ක්‍රියාත්මක කරන්න.
Execute the following python command in python interpreter and observe the results. Make minor changes to your codes and run them again.
උදා
>>> print()
>>> print(‘hello world’)
1)      යමක් ප්‍රතිදානය කිරීම සඳහා print () නැමැති  function එක භාවිතා කරන්න
Use print() function to output something.
a.       Print()
b.      print(‘hello, world’)
c.       print(“hello, world”)
d.       print(‘’’hello , world’’’)

2)      විචල්‍ය භාවිතය/using variables – variables are used to store values.
a.       X= 12
Y=56
Z = x +y
Print (“the total is ” , Z)
b.      X= “hello”
Y= “how are you”
Z = x +y
Print ( Z)
3)      යමක් ආදානය කිරීම(Input ) සඳහා input()නැමැති  function එක භාවිතා කරන්න
To input something use input()  function.
a.       x = input(“Enter your age”)
print (x)
b.        Number = input(“Enter first Number”)
  print (Number *2)
c.         Number = int(input(“Enter first Number”))
  print (Number *2)
c              N = input (“Enter phone number”)
                N [0:4]
4)      යමක් ආදානය සැකසීම සහ ප්‍රතිදානය කිරීම සිදු කරන අයුරු මෙමගින් ආදර්ශනය කර ඇත. The following programs will demonstrate you how to input , process and output in one program.
a.       Name = input(“Enter your name :-  “)
Print (“hello”,Name’ “how are are you”: )
b.      Num1 = input (“enter first number”)
Num2 = input (“enter second number”)
Total = Num1 + Num2
print (Total)
c.       Num1 = int (input (“enter first number”))
Num2 = int(input (“enter second number”))
Total = Num1 + Num2
print (Total)
5)      විවරණ /comments  යොදන අයුරු - using comments in python
# finding circumference of the circle:
Pi=22/7
radius =3
# Print the circumference of the circle:
print (2 * pi * radius)
6)      දත්ත වර්ගය පරීක්ෂා කිරීම (how to check the data types of python. Tada types are given for you. )
Python භාෂාවේ දත්ත වර්ග
·         Strings
·         Numbers
o   Integer numbers
o   Floating-point numbers
o   Complex numbers
·         Tuples
·         Lists
·         Sets
a.       type(15)
b.      x = 34.8
type(x)
c.       type(‘’saman”)
d.      type([4,5])

7)      string දත්ත වර්ගය හඳුනා ගැනීම සඳහා මෙම කේතයන් යොදාගන්න / Lets identify the data type - string
a.       x='abc'
x[0]
x[1:3]
b.      "hello"+"world"        "helloworld"      # concatenation
c.       "hello"*3                     "hellohellohello" # repetition
d.      "hello"[0]                    "h"                         # indexing
e.      "hello"[-1]                   "o"                         # (from end)
f.        "hello"[1:4]                 "ell"                       # slicing
g.       len("hello")                                5                              # size
h.      "hello" < "jello"         1                              # comparison
i.         "e" in "hello"                              1                              # search
j.        print ("Hello\tthere\nHow are you?")
k.       len(‘sarath’)                              # number of characters in a string
                                                                          (including spaces)
l.         str.lower(‘SARATH’)                              # lowercase version of a string
m.    str.upper(‘sarath’)

8)      python  interpreter  ගණක යන්ත්‍රයක් ලෙස භාවිතය මගින් සියඵ අංක ගණිතමය කාරක හැසිරෙන්නේ කෙසේදැයි හොඳින් පුහුණු විය යුතුය. අමතර ගැටඵ නිර්මාණය කර ගන්න
practice all the mathematical operators by using the python interpreter so the python interpreter  works as a calculator.
a.       2*(6+9)
b.      2//4
c.       999%1000
d.      999/1000
e.      1000/999
f.        r=11; y=2.5; c=4 නම්
 r%3*c+10/y හි අගය සොයන්න

17/2*3+2 නම්  Print (x) ?
අංක ගණිතමය කාරක / mathematical operators in python





  

9)      සැසඳුම් කාරක ( comparison operators)
a.       A=45
B= 25
A>B
Operator
Examples
<
5 < 3 gives False and 3 < 5 gives True.
Comparisons can be chained arbitrarily: 3 < 5 < 7
gives True.
>
5 > 3 returns True. If both operands are numbers,
they are first converted to a common type.
Otherwise, it always returns False.
<=
x = 3; y = 6; x <= y returns True.
>=
x = 4; y = 3; x >= 3 returns True.
==
x = 2; y = 2; x == y returns True.
x = 'str'; y = 'stR'; x == y returns False.
x = 'str'; y = 'str'; x == y returns True.
!=
x = 2; y = 3; x != y returns True.a


10)   Logical Operators (තාර්කික කාරක)
a.       a=False
b.      b=True
c.       print (a)
d.      print (not(a))
e.      print (a or  b)
f.        print (a and b)
Operator
Examples
NOT
x = True; not x returns False.
AND
x = False; y = True; x and y returns False
since x is False. In this case, Python will not evaluate
y since it knows that the left hand side of the 'and'
expression is False which implies that the whole
expression will be False irrespective of the other
values. This is called short-circuit evaluation.
OR
x = True; y = False; x or y returns True.
Short-circuit evaluation applies here as well.


Python IDLE à FileàNew window මගින් නව කවුඵවක් ලබා ගෙන එහි මින් මතු සඳහන් ක්‍රමලේඛ type කරන්න  සෑම ක්‍රමලේඛයක්ම .py යන ගොනු දිගුව සහිතව save කරන්න. යම් ක්‍රමලේඛයක් Run කිරීම සඳහා F5 යතුර තද කරන්න. The following programs should be type in a new windows and each program should be save with  the .py extension. To Run the program press F5.
11)    
a.                   වරණය (if/else) සඳහා -  Practice how the selection works in python
මෙම ගැටඵවේදී පරිගණකයට ආදානය කළ සංඛ්‍යාවක් ඔත්තේද ඉරට්ටේද යන්න පරීක්ෂා කිරීම සිදු කෙරේ. It will test whether a  given number is odd or even. Indentation is a must.
number = int(input("Enter number"))
if number %2 ==0 :
    print("it is a even number")
else:
    print("It is a Odd number")
print ("is'nt it fun ")

b.      If-elif-else වගන්තිය සඳහා (දුරකථන ජාලයක මුල් සංඛ්‍යා 3 ලබා දුන් විට ජාලයේ නම මුද්‍රණය කිරීම ට ක්‍රමලේඛයක් ලියන්න)
Write a simple program  to Practice  the statement If-elif-else
code= input("Enter code of the phone :-")
if code == "071":
    provider= "mobitel"
elif code == "072":
    provider= "eticelat"
elif code == "075":
    provider= "AirTel"
elif code == "077":
    provider= "Dialog"
elif code == "074":
    provider = "hutch"
else :
    provider = "I do not know "
print("The provider is %s" % provider)

ඉහත ගැටඵවේ අදාල දුරකථන code එක වෙනුවට සම්පූර්ණ දුරකථන අංකයම ඇතුඵ කල විට එමගින් code එක සොයා ගැනීමට හැකි වනසේ කේතය වැඩි දියුණු කරන්න

c.       දෙනු ලැබු සංඛ්‍යාවක් පරීක්ෂාකර එය ධන සංඛ්‍යාවක් ද ඍන සංඛ්‍යාවක් ද නො‍එසේ නම් ශුන්‍යය ද යන්න සොයන්න.
d.       සංඛ්‍යා 3 ක් අතුරින් විශාල සංඛ්‍යාව සොයන්න 
ඉතිරිය ඉදිරියට ................

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