Sunday 2 March 2014

(#4) Variables ,Data types ,Data type convertion

අප ලියන programme වලදී අපිට විවිධාකාරයේ data store කර තබාගැනීමට සිදුවෙයි.මෙවැනි data එකක් store කිරීම සඳහා memory space එකක් වෙන්කරගැනීම variable එකක් declare කිරීම ලෙස හැඳින්වෙයි.එලෙස වෙන් කරන ලද memory space එකක් තුළ data එකක් store කිරීම variable එකක් assign කිරීම ලෙස හැඳින්වෙයි.මෙහිදී විවිධාකාර data store කිරීම සඳහා විවිධාකාර වූ data types යොදා ගනියි.

Assigning Values to Variables:

අනෙකුත් languages වලමෙන් python වල variable එක මුලින් declare කර පසුව assign කිරීමක් සිදු කල නොහැකිය.variable එක declare කරන අවස්ථාවේම value එකද assign කල යුතුය.එමෙන්ම variable එකෙහි data type එක අපට define කල නොහැකි අතර අප එයට assign කරන value එක අනුව compiler එක මගින් data type එක හඳුනාගැනීම සිදු කරයි.

syntax:

variableName = value

උදා:

#!/usr/bin/python

counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string

print counter
print miles
print name
Output:

100
1000.0
John

Multiple Assignment:

එකවර variables කිහිපයකට වුවද එකම අගය assign කල හැක.
උදා:
a = b = c = 1
පහත උදාහරණයේදී 
a=1
b=2
c="john"
ලෙස අගයන් assign වීම සිදුවෙයි.
 a, b, c = 1, 2, "john"

Standard Data Types:

python වල ප්‍රධාන data types 5ක් පවතියි.


  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Python Numbers:

මෙම data type එක සංක්‍යාත්මක අගයන් store කිරීම සඳහා යොදා ගනියි.

var1 = 1
var2 = 10
ඔබට del   යන key word එක යොදාගෙන assign කරනලද variables delete කිරීමද සිදු කල හැකිය.
del var1[,var2[,var3[....,varN]]]]
ඔබට මෙම key word එක යොදාගනිමින් object එකකට වඩා වැඩි ගණනක් වුවද delete කල හැක.
del var
del var_a, var_b
මෙහිදී numeric datatypes වර්ග හතරකි.
  • int (signed integers)
  • long (long integers [can also be represented in octal and hexadecimal])
  • float (floating point real values)
  • complex (complex numbers)

Examples:

Here are some examples of numbers:
intlongfloatcomplex
1051924361L0.03.14j
100-0x19323L15.2045.j
-7860122L-21.99.322e-36j
0800xDEFABCECBDAECBFBAEl32.3+e18.876j
-0490535633629843L-90.-.6545+0J
-0x260-052318172735L-32.54e1003e+26J
0x69-4721885298529L70.2-E124.53e-7j

Python Strings:

string එකක් assign කිරීමේදී single quotation හෝ double quotation යොදාගත යුතුය. (+) ලකුණ මගින් string දෙකක් join කල හැකි අතර (*) ලකුණ මගින් එකම string එක කිහිප වාරයක් print කරගත හැකිය.
#!/usr/bin/python

str = 'Hello World!'

print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string
This will produce the following result:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

Python Lists:

එකම variable එකක් තුළ data කිහිපයක් store කරගැනීමට list එකක් භාවිතා කරයි.

syntax:
list = [item1,item2, item3]

#!/usr/bin/python

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']

print list          # Prints complete list
print list[0]       # Prints first element of the list
print list[1:3]     # Prints elements starting from 2nd till 3rd 
print list[2:]      # Prints elements starting from 3rd element
print tinylist * 2  # Prints list two times
print list + tinylist # Prints concatenated lists
This will produce the following result:
['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Python Tuples:

tuples , list වලටම සමාන data type එකක් වන අතර නමුත් tuple එකක් assign කල පසු එයට අලුතින් data add කිරීම හෝ තිබෙන data delete කිරීමක් සිදුකල නොහැක.නමුත් list එකක assign කරන data වලට අමතරව අලුතින් data add කිරීම මෙන්ම තිබෙන data delete කිරීමද සිදු කල හැකිය.

syntax:
tuple = (item1,item2, item3) මෙයට සාමාන්‍ය වරහන යොදාගනී.

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
tinytuple = (123, 'john')

print tuple           # Prints complete list
print tuple[0]        # Prints first element of the list
print tuple[1:3]      # Prints elements starting from 2nd till 3rd 
print tuple[2:]       # Prints elements starting from 3rd element
print tinytuple * 2   # Prints list two times
print tuple + tinytuple # Prints concatenated lists
This will produce the following result:
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists:
#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]
tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Python Dictionary:

dictionary එකක් යනු එකවර data විශාල ප්‍රමාණයක් store කරගත් හැකි data type එකකි.එය table එකකට සමාන වෙයි.
dictionary එක assign කිරීම සඳහා {} යොදාගන්නා අතර එයට data add කිරීම සඳහා [] යොදාගනියි.
උදා:

#!/usr/bin/python

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


print dict['one']       # Prints value for 'one' key
print dict[2]           # Prints value for 2 key
print tinydict          # Prints complete dictionary
print tinydict.keys()   # Prints all the keys
print tinydict.values() # Prints all the values
This will produce the following result:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order"; they are simply unordered.

Data Type Conversion:

එක් data type එකකින් assign කර ඇති data එකක් තවත් data type එකක් බවට පරිවර්තනය කල හැක.ඒ සඳහා python වල සකසන ලද විශේෂ functions කිහිපයක් පවතී. ඒවා පහත දැක්වෙයි.

FunctionDescription
int(x [,base])
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
float(x)
Converts x to a floating-point number.
complex(real [,imag])
Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
Converts s to a tuple.
list(s)
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
oct(x)
Converts an integer to an octal string.




2 comments: