Python Basics: Structure, Variables, Data Types, Operators and I/O – Grade 13 A/L ICT Notes 9.7

© 2026 schoolict.net (Ruwan Mangala Suraweera)

9.7 Python Basics: Structure, Variables, Data Types, Operators and I/OPython මූලික: ව්‍යුහය, විචල්‍ය, දත්ත වර්ග, කාරක සහ ආදාන/ප්‍රතිදාන

Grade 13 A/L ICT · Lesson 913 ශ්‍රේණිය A/L ICT · පාඩම 9

Prints all tabs in the language you are reading. Use “Save as PDF” in the print window to keep a copy.ඔබ කියවන භාෂාවෙන් සියලු tabs මුද්‍රණය වේ. පිටපතක් තියාගන්න print window එකේ “Save as PDF” තෝරන්න.
🎬Video coming soon in this language. Switch language to watch the other video.මේ භාෂාවෙන් වීඩියෝව ඉක්මනින් එනවා. අනෙක් වීඩියෝව බලන්න භාෂාව මාරු කරන්න.

In this videoමේ වීඩියෝවේ

  • Comments start with #.විවරණ # වලින් ආරම්භ වේ.
  • int, float, str and bool are primitive types.int, float, str, bool ප්‍රාථමික වර්ග ය.
  • input() returns a string – convert it.input() str දෙයි – හරවන්න.
  • ** before * / // % before + −.** පසු * / // %, පසු + −.
  • · is XOR; ** is power.· XOR; ** බලය.
CommentVariableConstantPrimitive data typeIntegerFloatBoolean

Notesසටහන්

Syllabus 9.7. Here we use an imperative language – Python – to encode algorithms. You meet the structure of a program, comments, constants and variables, the primitive data types, arithmetic, relational, logical and bitwise operators, operator precedence, keyboard input and screen output.විෂය නිර්දේශය 9.7. ඇල්ගොරිතම කේතනයට අනුඥාත්මක භාෂාවක් – Python – භාවිත කරමු. වැඩසටහනක ව්‍යුහය, විවරණ, නියත සහ විචල්‍ය, ප්‍රාථමික දත්ත වර්ග, අංක ගණිත, සම්බන්ධතා, තාර්කික සහ bitwise කාරක, කාරක ප්‍රමුඛතාව, යතුරුපුවරු ආදානය සහ තිර ප්‍රතිදානය හඳුනා ගනිමු.

1. Structure, comments, constants and variables1. ව්‍යුහය, විවරණ, නියත සහ විචල්‍ය

🐍 Constants, variables, types and inputනියත, විචල්‍ය, වර්ග සහ ආදානය

RATE = 0.08                      # constant (by convention UPPER CASE)
name = input("Name: ")           # str
qty = int(input("Quantity: "))   # int
price = 250.0                    # float
paid = True                      # bool
total = qty * price * (1 + RATE)
print(name, "pays Rs.", round(total, 2))
print(type(qty).__name__, type(price).__name__, type(paid).__name__)

▶ Outputප්‍රතිදානය (input: Nimal 4)(ආදානය: Nimal 4)

Name: Nimal
Quantity: 4
Nimal pays Rs. 1080.0
int float bool
Typeවර්ගයExampleඋදාහරණයMeaningඅර්ථය
intint44Whole numberපූර්ණ සංඛ්‍යාව
floatfloat250.0250.0Real (decimal) numberදශම සංඛ්‍යාව
strstr“Nimal”“Nimal”Textපෙළ
boolboolTrue / FalseTrue / FalseTruth valueසත්‍ය අගය

A comment starts with # and is ignored by the translator. A variable is a named memory location whose value can change; a constant (written in UPPER CASE by convention) should not change. input() always returns a str, so int() or float() converts it.විවරණය # වලින් ආරම්භ වී පරිවර්තකය නොසලකයි. විචල්‍යය අගය වෙනස් විය හැකි නම් කළ මතක ස්ථානයකි; නියතය (සම්ප්‍රදායට UPPER CASE) වෙනස් නොවිය යුතුය. input() සැමවිටම str ලබා දෙන නිසා int() හෝ float() මගින් හරවයි.

🧮 Worked exampleවිසඳූ උදාහරණය Nimal buys 4 items at Rs. 250.0 with 8% tax. What is printed?Nimal Rs. 250.0 බැගින් 4ක් 8% බදු සමඟ.
4 × 250.0 = 1000.0; × 1.08 = 1080.0, so it prints “Nimal pays Rs. 1080.0”. type() shows int, float, bool.4 × 250.0 = 1000.0; × 1.08 = 1080.0. type() int, float, bool පෙන්වයි.

2. Operators and precedence2. කාරක සහ ප්‍රමුඛතාව

Python operator precedence (high → low)( ) brackets** power (right to left)* / // %+ −< <= > >= == !=notandor3 + 4 * 2 = 11(3 + 4) * 2 = 142 ** 3 ** 2 = 51210 − 4 − 3 = 3
Operator precedenceකාරක ප්‍රමුඛතාව
Categoryකාණ්ඩයOperatorsකාරකExample → resultඋදාහරණය → ප්‍රතිඵලය
Arithmeticඅංක ගණිත+ – * / // % **+ – * / // % **7 // 2 → 3, 7 % 2 → 1, 7 / 2 → 3.57 // 2 → 3, 7 % 2 → 1, 7 / 2 → 3.5
Relationalසම්බන්ධතා== != < <= > >=== != < <= > >=5 > 3 → True5 > 3 → True
Logicalතාර්කිකand or notand or not5 > 3 and 2 > 8 → False5 > 3 and 2 > 8 → False
BitwiseBitwise& | · ~ << >>& | · ~ << >>5 & 3 → 1, 5 | 3 → 7, 5 · 3 → 6, 5 << 1 → 105 & 3 → 1, 5 | 3 → 7, 5 · 3 → 6, 5 << 1 → 10

🐍 Operators and precedenceකාරක සහ ප්‍රමුඛතාව

print(7 // 2, 7 % 2, 7 / 2)     # arithmetic
print(3 + 4 * 2, (3 + 4) * 2)  # precedence
print(2 ** 3 ** 2)             # ** is right to left
print(10 - 4 - 3)              # - is left to right
print(5 > 3 and 2 > 8)         # relational + logical
print(not True or True)
print(5 & 3, 5 | 3, 5 · 3, 5 << 1)   # bitwise

▶ Outputප්‍රතිදානය

3 1 3.5
11 14
512
3
False
True
1 7 6 10
🧮 Worked exampleවිසඳූ උදාහරණය Explain 2 3 2 and 5 · 3.2 3 2 සහ 5 · 3 පැහැදිලි කරන්න.
groups right to left: 3 2 = 9, then 2 9 = 512. 5 = 101 and 3 = 011 in binary; XOR gives 110 = 6**. දකුණේ සිට වමට: 3 2 = 9, 2 9 = 512. 101 XOR 011 = 110 = 6**.
💡 In Python · is bitwise XOR, not power – use ** for powers.Python හි · යනු XOR ය; බලයට ** භාවිත කරන්න.
🚀 Beyond the syllabus (2026 update)🚀 විෂය නිර්දේශයෙන් ඔබ්බට (2026 යාවත්කාලීනය) Not required for the exam – read for interest.විභාගයට අවශ්‍ය නැත – දැනුම සඳහා කියවන්න. Python 3.12+ in 2026 gives clearer error messages and optional type hints (qty: int) that IDEs check before running.2026 Python 3.12+ පැහැදිලි දෝෂ පණිවිඩ සහ IDE පරීක්ෂා කරන type hints (qty: int) ලබා දෙයි.

Key Wordsප්‍රධාන පද

Commentවිවරණය
Note in code starting with # that is not executed.ධාවනය නොවන # වලින් ආරම්භ වන සටහන.
Variableවිචල්‍යය
Named storage location whose value can change.අගය වෙනස් විය හැකි නම් කළ ගබඩා ස්ථානය.
Constantනියතය
Named value that should not change while running.ධාවනයේදී වෙනස් නොවිය යුතු නම් කළ අගය.
Primitive data typeප්‍රාථමික දත්ත වර්ගය
Basic built-in type such as int, float, str or bool.int, float, str, bool වැනි මූලික වර්ගය.
Integerපූර්ණ සංඛ්‍යාව
Whole number type int.int පූර්ණ සංඛ්‍යා වර්ගය.
FloatFloat
Number type with a decimal part.දශම කොටසක් ඇති සංඛ්‍යා වර්ගය.
BooleanBoolean
Type holding True or False.True හෝ False රඳවන වර්ගය.
Arithmetic operatorඅංක ගණිත කාරකය
Operator such as + or // that calculates numbers.සංඛ්‍යා ගණනය කරන + හෝ // වැනි කාරකය.
Relational operatorසම්බන්ධතා කාරකය
Operator that compares values and gives True or False.අගයන් සසඳා True/False දෙන කාරකය.
Logical operatorතාර්කික කාරකය
and, or, not combining conditions.කොන්දේසි එක් කරන and, or, not.
Bitwise operatorBitwise කාරකය
Operator working on individual bits such as & or ·.& හෝ · වැනි bits මත ක්‍රියා කරන කාරකය.
Operator precedenceකාරක ප්‍රමුඛතාව
Order in which operators are evaluated.කාරක ගණනය කරන පිළිවෙළ.

Sample Q&Aආදර්ශ ප්‍රශ්න සහ පිළිතුරු

1What does input() return?input() ලබා දෙන්නේ?

Answerපිළිතුර: A string (str).str.

27 // 2 and 7 % 2?7 // 2 සහ 7 % 2?

Answerපිළිතුර: 3 and 1.3 සහ 1.

33 + 4 * 2?3 + 4 * 2?

Answerපිළිතුර: 11, because * is done before +.11.

4Value of 10 – 4 – 3?10 – 4 – 3?

Answerපිළිතුර: 3 (left to right).3.

5Four primitive types?ප්‍රාථමික වර්ග හතර?

Answerපිළිතුර: int, float, str, bool.int, float, str, bool.

6Constant vs variable?නියතය සහ විචල්‍යය?

Answerපිළිතුර: A constant should not change; a variable can.නියතය වෙනස් නොවේ.

MCQබහුවරණ ප්‍රශ්න

Choose the correct answer for each question.සෑම ප්‍රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.

Scoreලකුණු: 0 / 20

17 // 2 in Python gives7 // 2

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Floor division.floor බෙදීම.

27 % 2 in Python gives7 % 2

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Remainder.ශේෂය.

33 + 4 * 2 gives3 + 4 * 2

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

* first.* මුලින්.

42 3 2 gives2 3 2

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Right to left.දකුණේ සිට.

55 · 3 in Python gives5 · 3

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

XOR.XOR.

6not True or True givesnot True or True

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

not first.not මුලින්.

7The type of the value returned by input() isinput()

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Always str.සැමවිටම str.

8Nimal’s total with 8% tax on 4 × 250.0 isඑකතුව

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

1000 × 1.08.1000 × 1.08.

9Which term means: “Note in code starting with # that is not executed”?“ධාවනය නොවන # වලින් ආරම්භ වන සටහන” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Comment – Note in code starting with # that is not executed.විවරණය – ධාවනය නොවන # වලින් ආරම්භ වන සටහන.

10Which term means: “Named storage location whose value can change”?“අගය වෙනස් විය හැකි නම් කළ ගබඩා ස්ථානය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Variable – Named storage location whose value can change.විචල්‍යය – අගය වෙනස් විය හැකි නම් කළ ගබඩා ස්ථානය.

11Which term means: “Named value that should not change while running”?“ධාවනයේදී වෙනස් නොවිය යුතු නම් කළ අගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Constant – Named value that should not change while running.නියතය – ධාවනයේදී වෙනස් නොවිය යුතු නම් කළ අගය.

12Which term means: “Basic built-in type such as int, float, str or bool”?“int, float, str, bool වැනි මූලික වර්ගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Primitive data type – Basic built-in type such as int, float, str or bool.ප්‍රාථමික දත්ත වර්ගය – int, float, str, bool වැනි මූලික වර්ගය.

13Which term means: “Whole number type int”?“int පූර්ණ සංඛ්‍යා වර්ගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Integer – Whole number type int.පූර්ණ සංඛ්‍යාව – int පූර්ණ සංඛ්‍යා වර්ගය.

14Which term means: “Number type with a decimal part”?“දශම කොටසක් ඇති සංඛ්‍යා වර්ගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Float – Number type with a decimal part.Float – දශම කොටසක් ඇති සංඛ්‍යා වර්ගය.

15Which term means: “Type holding True or False”?“True හෝ False රඳවන වර්ගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Boolean – Type holding True or False.Boolean – True හෝ False රඳවන වර්ගය.

16Which term means: “Operator such as + or // that calculates numbers”?“සංඛ්‍යා ගණනය කරන + හෝ // වැනි කාරකය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Arithmetic operator – Operator such as + or // that calculates numbers.අංක ගණිත කාරකය – සංඛ්‍යා ගණනය කරන + හෝ // වැනි කාරකය.

17Which term means: “Operator that compares values and gives True or False”?“අගයන් සසඳා True/False දෙන කාරකය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Relational operator – Operator that compares values and gives True or False.සම්බන්ධතා කාරකය – අගයන් සසඳා True/False දෙන කාරකය.

18Which term means: “and, or, not combining conditions”?“කොන්දේසි එක් කරන and, or, not” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Logical operator – and, or, not combining conditions.තාර්කික කාරකය – කොන්දේසි එක් කරන and, or, not.

19Which term means: “Operator working on individual bits such as & or ·”?“& හෝ · වැනි bits මත ක්‍රියා කරන කාරකය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Bitwise operator – Operator working on individual bits such as & or ·.Bitwise කාරකය – & හෝ · වැනි bits මත ක්‍රියා කරන කාරකය.

20Which term means: “Order in which operators are evaluated”?“කාරක ගණනය කරන පිළිවෙළ” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Operator precedence – Order in which operators are evaluated.කාරක ප්‍රමුඛතාව – කාරක ගණනය කරන පිළිවෙළ.

MCQ answer keyMCQ පිළිතුරු

  1. D – Floor division.floor බෙදීම.
  2. A – Remainder.ශේෂය.
  3. B – * first.* මුලින්.
  4. C – Right to left.දකුණේ සිට.
  5. A – XOR.XOR.
  6. D – not first.not මුලින්.
  7. E – Always str.සැමවිටම str.
  8. C – 1000 × 1.08.1000 × 1.08.
  9. B – Comment – Note in code starting with # that is not executed.විවරණය – ධාවනය නොවන # වලින් ආරම්භ වන සටහන.
  10. C – Variable – Named storage location whose value can change.විචල්‍යය – අගය වෙනස් විය හැකි නම් කළ ගබඩා ස්ථානය.
  11. D – Constant – Named value that should not change while running.නියතය – ධාවනයේදී වෙනස් නොවිය යුතු නම් කළ අගය.
  12. E – Primitive data type – Basic built-in type such as int, float, str or bool.ප්‍රාථමික දත්ත වර්ගය – int, float, str, bool වැනි මූලික වර්ගය.
  13. B – Integer – Whole number type int.පූර්ණ සංඛ්‍යාව – int පූර්ණ සංඛ්‍යා වර්ගය.
  14. E – Float – Number type with a decimal part.Float – දශම කොටසක් ඇති සංඛ්‍යා වර්ගය.
  15. B – Boolean – Type holding True or False.Boolean – True හෝ False රඳවන වර්ගය.
  16. A – Arithmetic operator – Operator such as + or // that calculates numbers.අංක ගණිත කාරකය – සංඛ්‍යා ගණනය කරන + හෝ // වැනි කාරකය.
  17. D – Relational operator – Operator that compares values and gives True or False.සම්බන්ධතා කාරකය – අගයන් සසඳා True/False දෙන කාරකය.
  18. A – Logical operator – and, or, not combining conditions.තාර්කික කාරකය – කොන්දේසි එක් කරන and, or, not.
  19. C – Bitwise operator – Operator working on individual bits such as & or ·.Bitwise කාරකය – & හෝ · වැනි bits මත ක්‍රියා කරන කාරකය.
  20. E – Operator precedence – Order in which operators are evaluated.කාරක ප්‍රමුඛතාව – කාරක ගණනය කරන පිළිවෙළ.

Structured Questionsව්‍යුහගත ප්‍රශ්න

1Name four primitive data types with examples.ප්‍රාථමික වර්ග. (4 marksලකුණු 4)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
int 4; float 250.0; str “Nimal”; bool True.int; float; str; bool.

2Explain variable, constant and comment with code.විචල්‍ය, නියත, විවරණ. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
qty = 4 variable; RATE = 0.08 constant; # comment ignored.qty; RATE; #.

3Evaluate 3 + 4 * 2, (3 + 4) * 2 and 2 3 2 and explain.ගණනය කරන්න. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
11 (* first); 14 (brackets); 512 (** right to left).11; 14; 512.

4Give one example each of relational, logical and bitwise operators with results.කාරක තුන. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5 > 3 True; 5 > 3 and 2 > 8 False; 5 & 3 = 1.True; False; 1.

5Write a program that inputs quantity and prints the total with 8% tax.8% බදු වැඩසටහන. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
int(input()); total = qty * price * 1.08; print round.input; ගණනය; print.

🎮 Game🎮 ක්‍රීඩා

Pick a game. Each one uses words from this lesson.ක්‍රීඩාවක් තෝරන්න. හැම එකක්ම මේ පාඩමේ වචන පාවිච්චි කරනවා.

Summary & Downloadsසාරාංශය සහ බාගත කිරීම්

Key pointsප්‍රධාන කරුණු

  • Comments start with #.විවරණ # වලින් ආරම්භ වේ.
  • int, float, str and bool are primitive types.int, float, str, bool ප්‍රාථමික වර්ග ය.
  • input() returns a string – convert it.input() str දෙයි – හරවන්න.
  • ** before * / // % before + −.** පසු * / // %, පසු + −.
  • · is XOR; ** is power.· XOR; ** බලය.

Downloadsබාගත කිරීම්

Full Lesson 9 PDFසම්පූර්ණ පාඩම 9 PDF: English · සිංහල

Next noteඊළඟ සටහන 9.8 →

© 2026 schoolict.net (Ruwan Mangala Suraweera). All rights reserved.සියලු හිමිකම් ඇවිරිණි.

Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool