Sunday 2 March 2014

(#3)Basic Syntax

Python Identifiers:

programming වලදී යම්කිසි object එකක් හැඳින්වීමට යොදාගන්නා නමක් පොදුවේ
Identifier එකක් ලෙස හඳුන්වයි.මෙය variable එකක් function එකක් class එකක් හෝ object එකක් විය හැකිය.මේවාට නම් ලබාදීමේදී අපට කැමති නම් ලබාදිය නොහැකි අතර ඒ සඳහා නීති මාලාවක් පවතියි.ඒවා පහත ලෙස වෙයි
  • identifier එකක් ආරම්භ කළ යුත්තේ ඉංග්‍රීසි capital (A-Z) අකුරකින් simple (a-z) අකුරකින් හෝ underscore(_) එක මගින් පමණි වෙනත් කිසිඳු character එකකින් ආරම්භ කළ නොහැක.
  • එක අකුරක් පමණක් වුවද identifier එකට ප්‍රමාණවත් වෙයි.තවත් අකුරු යොදන්නේ නම් ඒ සඳහා අකුරු underscore එක මෙන්ම 0 සිට 9 දක්වා වූ ඕනෑම ඉලක්කමක් හෝ ඉලක්කම් කිහිපයක් වුවද ඇතුලත් කල හැක.
  • පෙර කී අකුරු හා symbols හැරුණු විට වෙනත් කිසිම character එකක් identifiers හැදින්වීමට යොදාගත නොහැක.
identifiers case sensitive වෙයි . එනම් capital හා simple අකුරු වෙනස් වශයෙන් සලකයි.Man හා man යනු එකිනෙකට වෙනස් identifiers දෙකක් වෙයි.
 
identifiers නම් කිරීමේදී බලපාන තවත් නීති.
  • class name එකක පළමු අකුර capital විය යුතු අතර අනෙක් සියළුම අකුරු simple විය යුතුය.
  • identifier එකක් underscore එකකින් ආරම්භ කරයිනම් එය private identifier එකක් ලෙස සලකයි.
  • identifier එකක් underscore දෙකකින් ආරම්භ කරයිනම් එය strongly private identifier එකක් ලෙස හඳුන්වයි.
key words ලෙස වෙන්කර ඇති වචන කිහිපයක් ඇති අතර ඒවා identifiers ලෙස භාවිතා කල නොහැක.
පහත දැක්වෙන්නේ එම වචනයි.
andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

Lines and Indentation:

python වල code blocks වෙන් කිරීමට වරහන් හෝ කමා භාවිතා නොකරයි.එම සියල්ල වෙන් කිරීම indentation වලින් සිදුකරන අතර එම නිසා නිවැරදිව indentation තැබීම ඉතා වැදගත් වෙයි.

එකම කොටසට අයිතිවෙන හැම code line එකකටම සමාන indentation එකක් තිබිය යුතුය.
උදා;

if True:
    print "True"
else:
  print "False"
මෙය නිවැරදිය.

if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"
නමුත් ඉහත else එක යටතේ ඇති code lines දෙකෙහි indentation දෙක එකිනෙකට වෙනස් නිසා error එකක් පෙන්වයි.කෙසේ වෙතත් IDLE එක භාවිතයේදී එය නිවැරදි තැනට curser එක auto යොමු කරන නිසා ඒ ගැන වැඩි වශයෙන් බියවිය යුතු නැත.

Quotation in Python:

python වල වචන වාක්‍ය ආදී strings දැක්වීමේදී single quotation (') එක හෝ double quotation (") එක යන දෙකෙන් ඕනෑම එකක් භාවිතා කල හැකි අතර මේ දෙකෙන් එකක් හෝ නොදමයි නම් error එකක් පෙන්වයි.
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Comments in Python:

මෙහි ඇතුලත් වන්නේ single line comments පමණි. comment කල යුතු හැම වාක්‍යයක් ඉදිරියෙන්ම # ලකුණ යෙදිය යුතුය.
#!/usr/bin/python

# First comment
print "Hello, Python!";  # second comment
This will produce the following result:
Hello, Python!
A comment may be on the same line after a statement or expression:
name = "Madisetti" # This is again comment
You can comment multiple lines as follows:
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.







2 comments: