Thursday, May 30, 2013

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 ක් අතුරින් විශාල සංඛ්‍යාව සොයන්න 
ඉතිරිය ඉදිරියට ................

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