hello world, variable declarations, data types හා තවත් syntax අමු ද්රව්ය python වලින් ලියන හැටි පොඩ්ඩක් හොයලා බලමු. මීට කලින් programming කරලා තියෙන අයට තකට තක ගලපන්න පුලුවන් විදියට ලියවුනු python කතාවක් තමයි මේ.
මුලින්ම කියන්න ඕන හරිය
කඩ්ඩෙන් ලියවුනු
මේ tutorial එකත් සලකා බලන්න -- Python in 10 minutes. පහල තියෙන සියලු script, python 2.6+ හා python 3k syntax බව කරුණාවෙන් සලකන්න. පටාන් ගන්න කලියෙන් python interactive shell එක open කරගෙන ඉන්න. python ගැන දන්නෙවත් නැත්නම්
පරණ ලිපිය කියවලා හිටියොත් හොඳා.
අපේ විදියට Hello world ලියමු
Python interactive shell එකේ තමයි අපිට ගොඩක් සෙල්ලම් දාන්න පුලුවන්. ඒක නිසා තමන්ගේ මැෂිමේ හැටියට python shell එක on කරගන්නවා හොඳයි. බොහොමයක් වෙලාවට ඕනම මැෂින් එකක shell එකේ python කියලා type කරාම වැඩේ ගොඩ. මේක '
>>>' ඉස්සරහා ඕන දෙයක් කොටලා බලන්න තියෙන්නේ.
ඔන්න බලන්න මම Fedora මැෂිමක ලියලා වෙච්චි දෙයක්.
[user@fedoramachine ~]$ python
Python 2.7.1 (r271:86832, Apr 12 2011, 16:16:18)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help", "copyright", ....
>>> print('ayubowan')
ayubowan
>>> 1+1
2
>>> 9*9
81
උඩ තියෙන print statement එක පැහැදිලි නැති හින්දා ඔන්න මේක බලාගන්න. C පවුලේ language දන්න අය බලාගන්න ඕන දෙයක්; statement එක අගට තිත්කොමාවක් නෑ - සෙල්ලම් නෑ :) හිතන්න එපා මේ interactive shell එකේ විතරයි තිත්කොමා නැත්තේ කියලා -- python script වලත් එහෙමම තමා.
>>> print('ayubowan')
'ayubowan' කියලා තියෙන්නේ string එකක් කියල ඔය ඇත්තො දන්නවා ඇතිනේ. C පවුලේ භාෂා වල නම් string එක wrap කරන්නේ double quotation mark වලින් වුනාට, python strings මේ පහල තියෙන ඕන විදියකට ලියන්න පුලුවන්. Method call එකේ නම් වෙනසක් නෑ.
>>> print("ayubowan")
ayubowan
>>> # Triple double quotes
>>> print("""Hello crazy world""")
Hello crazy world
ඔය උඩ # (
# Triple double quotes) එකක් එක්ක ලියල තියෙන්නේ comment එකක්. අන්න එහෙම තමයි python comments.
Triple double quotes අතරේ තියෙන strings වල newlines එක්කම print කරන්න පුලුවන්. Try එකක් දීලාම බලන්න.
>>> print("""Hello
crazy world""")
Hello
crazy world
variable declare කරන හැටි
කලින් ලිපියේ කිව්ව මතක ඇතිනේ python -- dynamically typed කියලා. ඒ කිව්වේ variable declaration එකේදී type එක define කරන්න ඕන නෑ.
>>> abc = 'abc'
>>> abc = '123'
>>> # type() method returns the type of variable
>>> type(abc)
<class 'str'>
>>> # a different type for same old abc
>>> abc = 456
>>> type(abc)
<class 'int'>
තවත් data types ටිකක් -
>>> mbool = True
>>> type(mbool)
<class 'bool'>
>>> mfloat = 100.001
>>> type(mfloat)
<class 'float'>
>>> mnull = None
>>> type(mnull)
<class 'NoneType'>
- boolean True එකේ T capital -- False නුත් ඒවගේමයි
>>> 1 == 2
False
- null / undefined වෙනුවට None
Variable එක්ක පොඩි පොඩි සෙල්ලම්
++ / -- operator එක වෙනුවට += / -= operator එකම තමයි පාවිච්චි කරන්නේ
>>> j = 99
>>> j += 1
>>> j
100
+= operator එකේ අනිත් language එක්ක වැඩි වෙනසක් නෑ
>>> i = 20
>>> i += 30
>>> i
50
>>> abc = "Hello"
>>> abc += " wewa"
>>> abc
'Hello wewa'
වෙනස් වූ data type දෙකක් එකතු / operate කරද්දී
>>> num = 20
>>> abc = '40'
>>> type(abc)
<class 'str'>
>>> num + abc
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
num + abc
TypeError:
unsupported operand type(s) for +: 'int' and 'abc'
>>> # cast abc to int
>>> int(abc) + num
60
ඔය (උඩ) තියෙන විදියට තමයි type cast කරන්නේ python වල
int(abc)
Cast වෙන්නෝන ( / ප්රතිඵලය වෙන) data type එකේ class constructor එකට cast කරන්න ඕන variable type එක පාස් කරනවා.
Java වල (මට මතක විදියට) මේ code කෑල්ලේ අවුලක් නැති වුනත් -
System.out.println("Hello " + 123);
Python කියන්නේ Java නෙමේ නේ ඒ නිසා -
මෙහෙම කියලා python අතාරින්න එපා පාවිච්චියේදී තේරෙයි අගය
>>> print("Hello "+ 123)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
print("Hello "+ 123)
TypeError:
Can't convert 'int'' object to str implicitly
>>> print("Hello " + str(123))
Hello 123
Lists සහ Dictionaries
අපි සාමාන්යයෙන් arrays කියන ඒවාට Python වල නම් යොදා තිබෙන්නේ Lists කියලා.
Dictionaries නම් Java වල hash tables වගේ - [ key -- value pairs ]
>>> # the backslash helps to break 1 line to 2
>>> # it's not anything with list syntax
>>> justalist = ['a string', 123 \
, 'with numbs' , ['and other', 'objects']]
>>> type(justalist)
<class 'list'>
>>> dir(justalist)
අන්තිමට ලියලා තියෙන
dir(justalist) කියන එක ඔය ගොල්ලන්ගේ shell එකෙත් අනිවාර්යෙන්ම කොටලා බලන්න.
ඕක කරන්නේ පාස් කරන object එකේ ඇතුලේ තියන methods list එකක් shell එකට dump කරනවා. මේනිසා හැමතිස්සෙම source code සහා documentation වල පිටු පෙරලපෙරල බලන්න ඕන නෑ. අපි වගේ අයට ඕක ගොඩක් වටිනවා.
ඒවගේම තවත් වටින දෙයක් තමයි මේ පහල තියෙන්නේ
>>> justadict = {'name' : 'value can be anything', 'key-is-a-string' : 123}
>>> type(justadict)
<class 'dict'>
>>> help(dict)
help( [class name උදා: dict] ) දැම්මම තනිකර explanation එකක් එනවා ඒ class එක ගැන. අනිවා මේකත් පොඩ්ඩක් shell එකේ කොටලා බලන්න.
ඉගෙන ගන්නා අපිට ගොඩක් උදවු වෙන මේ dir method එකයි help method එකයි linux shell එකේ ls command එකයි man command එකයි වගේ.
මේ help method එක එහෙම දුවන්නේ
කලින් ලිපියේ කියපු auto documentation වලට උදවු වෙන docstring නිසා බව කියමින්, මේ ලිපියෙන් යම්තම් සන්තම් හෝ දෙයක් ඔබ දැන ගත්තායැයි පතමින්, අදට අහවර කරන්නයි මේ ලෑස්තිය.
methods, control statements සහ loops ගැන කතාකිරීමට පසුවට තබමින් help සහ dir methods දෙක පාවිච්චියෙන් python ගවේෂණය ඔබට භාර කරමින් අදට නවතිමි.