Computer Science Notes 1st Year

11th Class Computer Science Chapter 2: Python Programming

Chapter 2 of 11th Class Computer Science is the first full Python unit. It is not a tour of every library. It is the path from installing Python, through variables and operators, into decisions, loops, functions, lists, classes, files, and the habit of testing what you wrote. If you only memorise the print examples, the exercise on precedence, range(), and classes will feel disconnected. They are the same idea: a name holds a value, an operator produces a result, and a control structure decides whether that result runs again.

This guide follows the chapter text. The solved short, long, and additional questions are on the printable paper. The 15 MCQs are the online quiz. Prepared by Mahmood Khan.

Key concepts

  • A program is written, translated, run, then shown. You write code, Python interprets it, the computer executes it, and print or a file shows the result. During installation, check “Add Python to PATH” or the command line will not find Python.
  • A variable is a named container whose value can change. age = 17 is an integer, price = 18.99 is a float, text is a string, and is_student = True is a boolean. Names start with a letter or underscore, are case-sensitive, and cannot be keywords such as if or for. input() returns text, so int() or float() is required before arithmetic.
  • Operators and precedence decide the result before any if statement sees it. + − * / // % ** are the arithmetic set. Comparisons return True or False. = stores a value; == tests equality. and, or, and not combine conditions. Parentheses run first, then **, then * / %, then + −.
  • Decisions, loops, functions, and lists are how the unit organises work. if, if-else, the one-line short-hand, and if-elif-else choose a path. while repeats while a condition stays true. for walks a sequence, often built with range(). def creates a function; a default parameter is used when the caller omits an argument. A list is ordered and changeable (append, remove, sort). A tuple is ordered and immutable. A class is the template; an object is one instance, with self referring to that instance.

Common exam traps

  • Calling 1variable or variable-name a valid identifier. A name cannot start with a digit, and a hyphen or a space is not allowed. variable1 is valid. age and Age are two different names.
  • Using = inside a condition. weather = "sunny" stores a string. weather == "sunny" asks a question. The if statement needs the question.
  • Forgetting that input() is a string, that // is not /, and that a tuple cannot append(). 10 / 3 is a decimal. 10 // 3 is the whole-number quotient. 10 % 3 is the remainder. append() and item assignment belong to lists. A while loop that never changes its tested variable never stops.

Worked example: even or odd, then a hot day

The class activity asks for both a normal if-else and the short-hand form. With n = 8:

if n % 2 == 0:
    print("Even")
else:
    print("Odd")
label = "Even" if n % 2 == 0 else "Odd"

% is the remainder operator from the arithmetic section. Both forms print Even. The short-hand is one expression; the block form is clearer when each branch has several lines. The same pattern classifies temperature: if temperature > 30 print a hot-day message, else print that it is not. if-elif-else extends this when there are three outcomes — positive, negative, or zero — which is a long question in the exercise.

When you can write that without looking, open the Chapter 2 quiz (15 MCQs) and the printable short, long, and additional questions. If you still need an earlier Python syntax drill, use 10th Class Unit 3: Introduction to Python.

Prepared by Mahmood Khan, MS Cybersecurity — Study E-Hub, Sahiwal. The notes follow Chapter 2 of the 11th Class Computer Science book. Attempt the exercise before you open the answer paper.

Take Chapter 2 Quiz

Comments

0 comments

No comments yet. Be the first to share your thoughts!