හොඳයි යාලුවනේ, Data Science ගමනේ තුන්වෙනි නැවතුමට ආවට පස්සේ, අපි හිතනවා ඔයාලට Python වල මූලික කරුණු ගැන දැනටමත් යම් අවබෝධයක් තියෙනවා කියලා. ඒ උනත්, අපි මේ පාඩම් මාලාවේදී එකම විදිහකට වැඩ කරගෙන යන්න, Code ලියන්න පාවිච්චි කරන Environment එක ගැන මුලින්ම කතා කරලා ඉමු. ඊට පස්සේ අපි Data Science වලට වැඩියෙන්ම අවශ්ය වෙන Core Python Concepts ටිකක් Refresh කරගමු. මේවා ඔයාට හුරුපුරුදු උනත්, Data Science පැත්තෙන් මේවා කොච්චර වැදගත්ද කියලා ආයෙත් බලන එක වටිනවා! 😉
Python Code ලියන්න අපිට ක්රම කීපයක් තියෙනවා. අපි ප්රධාන ක්රම දෙකක් ගැන කතා කරමු. මේ දෙකෙන් පළවෙනි ක්රමය තමයි අපි මේ පාඩම් මාලාවට මුලින්ම පාවිච්චි කරන්නේ, මොකද ඒක හරිම ලේසියි!
හරි, එහෙනම් අපි වැඩියෙන්ම Recommend කරපු Google Colab වලින්ම පටන් ගමු.
මේකේ ඔයාට කොටු (Cells) වගයක් පෙනෙයි. ප්රධාන Cells වර්ග 2ක් තියෙනවා:
හරි, දැන් හැමෝම Code ලියන්න ලෑස්ති ඇතිනේ Colab එකේ. එහෙනම් අපි යමු Data Science වලට අත්යවශ්ය Python Concepts ටික මතක් කරගන්න!
Python වල එන Built-in Data Structures කීපයක් Data එක්ක වැඩ කරද්දී අපිට නිතරම ඕන වෙනවා.
List එකක් කියන්නේ පිළිවෙලකට සකස් කරපු, වෙනස් කරන්න පුළුවන් (Mutable) දේවල් එකතුවක්.
my_list = [10, "python", 3.14, True]
my_list[0]
(පළවෙනි එක -> 10), my_list[-1]
my_list[1:3]
(දෙවෙනි එකේ ඉඳන් තුන්වෙනි එක දක්වා )my_list.append("data")
Python
# උදාහරණයක් - Colab එකේ මේ Cell එක Run කරලා බලන්න! student_marks = [65, 88, 72, 91, 56] student_marks.append(78) print(f"Updated Marks: {student_marks}") print(f"Second student's marks: {student_marks[1]}")
Lists හදන්න, Filter කරන්න තියෙන හරිම Powerful සහ Concise ක්රමයක්. Code එක Clean වෙනවා.
Python
# සාමාන්ය ක්රමය squares = [] for i in range(5): squares.append(i * i) print(f"Squares (normal): {squares}") # List Comprehension ක්රමය squares_comp = [i * i for i in range(5)] print(f"Squares (comprehension): {squares_comp}") # Condition එකක් එක්ක even_squares = [x**2 for x in range(10) if x % 2 == 0] print(f"Even Squares: {even_squares}")
Tuples කියන්නෙත් පිළිවෙලකට සකස් කරපු දේවල් එකතුවක්. හැබැයි ප්රධානම වෙනස තමයි මේවා හදලා වෙනස් කරන්න බෑ (Immutable).
my_tuple = (10, "python", 3.14)
(වටේට වරහන් නැතත් tuple එකක් හැදෙනවා my_tuple = 10, "python", 3.14
)my_tuple[0]
) සහ Slicing කරන්න පුළුවන්.(255, 0, 0)
, Coordinates (x, y)
). Dictionary Keys විදියට පාවිච්චි කරන්නත් පුළුවන් (Lists බෑ). Function එකකින් අගයන් කීපයක් return කරන්නත් tuples නිතරම යෙදෙනවා.Python
point = (100, 200) x_coord = point[0] print(f"X Coordinate: {x_coord}")
Dictionaries කියන්නේ පිළිවෙලක් නැති (Python 3.7+ වල පිළිවෙලක් තියෙනවා, ඒත් ඒක මත depend වෙන්න එපා) Key:Value යුගල එකතුවක්. හැම Key එකක්ම Unique වෙන්න ඕන.
student_info = {"name": "Kasun", "age": 24, "major": "Data Science", "courses": ["DS101", "PY201"]}
student_info["name"]
-> "Kasun"
(Key එක නැත්නම් Error එකක් එනවා). student_info.get("age")
-> 24
(Key එක නැත්නම් None
හරි Default අගයක් හරි දෙන්න පුළුවන්, Error එන්නේ නෑ).student_info["age"] = 25
(Update), student_info["city"] = "Colombo"
(Add)student_info.keys()
, student_info.values()
, student_info.items()
(key-value tuple list එකක්)Python
word_counts = {"apple": 5, "banana": 2, "orange": 8} word_counts["apple"] += 1 # Update count print(f"Word Counts: {word_counts}") print(f"Keys: {list(word_counts.keys())}") # List එකක් විදියට ගන්න # Dictionary එකක Key එකක් තියෙනවද බලමු if "grape" in word_counts: print("Grapes found!") else: print("Grapes not found!")
Sets කියන්නේ පිළිවෙලක් නැති, Unique (එකම item එක දෙපාරක් තියෙන්න බෑ) දේවල් එකතුවක්.
my_set = {1, 2, 3, "hello", 3, 2}
-> {1, 2, 3, 'hello'}
හැදෙයි. හිස් set එකක් හදන්න empty_set = set()
පාවිච්චි කරන්න ({}
වලින් හැදෙන්නේ හිස් dictionary එකක්).|
, intersection &
, difference -
) වගේ දේවල් වලට ප්රයෝජනවත්.Python
tags = ["python", "data", "analysis", "python", "ml", "data"] unique_tags = set(tags) print(f"All Tags: {tags}") print(f"Unique Tags: {unique_tags}") print(f"Is 'python' a tag? {'python' in unique_tags}") # Fast check! set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} print(f"Intersection (පොදු): {set1 & set2}") print(f"Union (සියල්ල): {set1 | set2}")
Data එක්ක වැඩ කරද්දී එක එක Conditions වලට අනුව දේවල් කරන්න, Data ගොඩක් හරහා එකින් එක යන්න (Iterate) Control Flow ඕන වෙනවා.
Conditions check කරලා ඒ අනුව විවිධ Code කොටස් Run කරන්න.
Python
score = 75 if score >= 80: grade = "A" elif score >= 60: grade = "B" elif score >= 40: grade = "C" else: grade = "Fail" print(f"The grade is: {grade}")
List, Tuple, Dictionary, Set වගේ Sequence එකක තියෙන හැම Item එකක් හරහාම යන්න (Iterate) for loop පාවිච්චි කරනවා.
Python
numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total += num print(f"Total: {total}") # Dictionary එකක් හරහා යෑම print("\nStudent Info:") # ටිකක් ලස්සනට output එක ගමු for key, value in student_info.items(): print(f" {key.title()}: {value}") # Index එකත් එක්ක යන්න enumerate print("\nMarks with Index:") for index, mark in enumerate(student_marks): print(f" Student {index + 1} Mark: {mark}")
Condition එකක් True වෙලා තියෙනකන් යම් Code එකක් නැවත නැවත Run කරන්න.
Python
count = 0 print("\nWhile Loop Example:") while count < 3: print(f" Count is: {count}") count += 1
ඔයා නිතරම කරන දෙයක් (උදා: Data Clean කරන step එකක්, Calculation එකක්) Function එකක් විදියට ලිව්වම, ඒක ඕනම තැනක ආයෙ ආයෙ පාවිච්චි කරන්න පුළුවන්. Code එක පිළිවෙල වෙනවා, Debug කරන්න ලේසියි.
Python
def calculate_average(numbers_list): """Calculates the average of a list of numbers. Returns 0 for empty list.""" # Docstring if not numbers_list: # List එක හිස්ද බලනවා print("Warning: List is empty!") return 0 return sum(numbers_list) / len(numbers_list) avg_marks = calculate_average(student_marks) print(f"\nAverage Marks: {avg_marks:.2f}") # දශමස්ථාන 2කට පෙන්වමු empty_list = [] print(f"Average of empty list: {calculate_average(empty_list)}")
numbers_list
).return
keyword එකෙන්).""" Docstring goes here """
). හරිම වැදගත් පුරුද්දක්!නමක් නැති, එක පේලියකින් ලියන පොඩි Functions. සාමාන්යයෙන් එක Expression එකක් විතරයි තියෙන්නේ.
Python
# සාමාන්ය Function එක def square(x): return x * x # Lambda Function එක square_lambda = lambda x: x * x print(f"\nSquare (normal): {square(5)}") print(f"Square (lambda): {square_lambda(5)}") # List එකක් sort කරන්න lambda පාවිච්චි කිරීම points = [(1, 5), (3, 2), (5, 8)] # දෙවෙනි අගය (y) අනුව sort කරමු points.sort(key=lambda p: p[1]) print(f"Sorted by y: {points}")
apply()
, map()
වගේ functions එක්ක, Lists sort කරනකොට වගේ තැන් වලදී ඉක්මනට පොඩි function එකක් ලියාගන්න ගොඩක් ප්රයෝජනවත්.ඔව්, ඔයා Python Basics දන්නවා ඇති. ඒත් මේ Concepts ටික Data Science Perspective එකෙන් ආයෙත් බැලුවේ, මේවා NumPy, Pandas වගේ Libraries පාවිච්චි කරද්දී කොච්චර වැදගත් වෙනවද කියලා තේරුම් කරන්න.
මේ Python Basics ටික Solid වෙන තරමට, අර Libraries වලින් වැඩ දාන එක, Errors තේරුම්ගන්න එක, ඔයාටම ඕන විදියට දේවල් හදාගන්න එක ගොඩක් පහසු වෙනවා.
හරි! දැන් අපි Python අත්තිවාරම තව ටිකක් ශක්තිමත් කරගත්ත නිසා, ඊළඟ පාඩමේ ඉඳන් අපි කෙලින්ම Data Science වල පළවෙනි ප්රධාන Library එක වෙන NumPy ගැන ඉගෙන ගන්න පටන් ගමු! 🚀
NumPy කියන්නේ Python වලින් සංඛ්යාත්මක දේවල් (Numerical Computing) කරන්න තියෙන අත්යවශ්යම Library එක. Lists වලට වඩා ගොඩක් වේගවත්ව, පහසුවෙන් Numbers ගොඩක් එක්ක වැඩ කරන්න NumPy අපිට උදව් කරනවා.
අද පාඩමෙන් අපි මුලින්ම Code ලියන්න Environment එකක් (Google Colab) හදාගන්න හැටි බැලුවා. ඊට පස්සේ Data Science වලට වැදගත්ම වෙන Python Data Structures (Lists, Tuples, Dictionaries, Sets), Control Flow (if
, for
, while
), සහ Functions (def
, lambda
) ගැන ගැඹුරින් මතක් කරගත්තා. මේවා කොහොමද Data Science වැඩ වලට සම්බන්ධ වෙන්නේ කියලත් බැලුවා.
Colab එකේ මේ Code කෑලි Run කරලා බලන්න, පොඩි පොඩි වෙනස්කම් කරලා බලන්න. Practice කරන එක තමයි වැදගත්ම!
❓ මොනවහරි ප්රශ්න තියෙනවා නම් අනිවාර්යයෙන් Comment කරන්න! ඊළඟ පාඩමෙන් NumPy එක්ක හමුවෙමු!
එතකන් හැමෝටම ජය! 🎉
Member since 2025-04-09 13:55:06
Comments
Please login to post a comment.
No comments yet. Be the first to comment!