A program that cannot remember anything or talk to its user is not much use. Variables give it memory, input() lets it listen, and print() lets it answer. These three ideas open Chapter 2 of the book and run through every Python program you will write this year.
Chapter 2 of the PECTAA Computer Science 11 book is Python Programming. Sections 2.1 and 2.2 introduce programming, the Python development environment, basic syntax, variables, data types and input/output. This note is for FSc Part 1 (1st year, Class 11) students in Lahore, Faisalabad, Sargodha, Rawalpindi, Multan, Gujranwala and the other Punjab boards. Gujrat district comes under the Gujranwala board. PECTAA writes the books. Your own board sets your paper.
What to know
Why Python (section 2.1). Python is versatile and used in web development, data analysis and other fields. Its simple syntax and clear structure let beginners focus on programming ideas rather than complex rules.
Programming basics (section 2.1.1). Computer programming is creating a set of instructions that tell a computer how to do a task, written in a language the computer can understand and run. The basic steps:
- Write code in a programming language.
- Compile or interpret it into a form the computer understands.
- Execute it to perform the task.
- Output the results or carry out actions.
Development environment (section 2.1.1.2). Setting up means preparing a computer to write, run and debug Python: installing and configuring Python and any tools and libraries needed. An IDE makes coding easier, and online services can also write and run Python programs.
Basic syntax (section 2.2). The book's first program is a single line:
print("This is my first page")
The print function displays the text inside the quotation marks.
Comments. Comments are lines the interpreter does not execute; they explain the code. A single-line comment starts with #. For longer notes, the book places text between triple quotes at the start and end.
Variables (section 2.2.1.1). A variable is a storage container in memory for storing, retrieving and changing data. Its value can change during the program. In the book's example, age = 71 is printed in a sentence about Ahmad, then age = 60 replaces it for a sentence about Iqbal.
Naming rules (section 2.2.1.2).
- Begin with a letter (a–z, A–Z) or an underscore (_).
- Later characters may be letters, digits (0–9) or underscores.
- Names are case-sensitive: age and Age are different variables.
- Reserved keywords such as for, while and if cannot be names.
The book's tips: use meaningful names (age, not a), lowercase letters, and underscores between words (student_name).
Data types (section 2.2.1.3).
| Type | Stores | Book example |
|---|---|---|
| int | whole numbers | age = 17 |
| float | decimal numbers | a value such as 19.99 |
| str | text | name = "Ali" |
| bool | True or False | is_student = True |
Input and output (section 2.2.1.4).
input()shows a message, waits for the user to type and press Enter, and returns the text, which is stored in a variable:name = input("Enter your name:").print()takes one or more arguments and displays them:print("Hello, " + name + "!").
Number inputs (section 2.2.1.5). input() gives text, so wrap it in int() or float():
user_age = int(input("Enter your age:"))user_height = float(input("Enter your height in meters:"))
Key points
- A variable gets its value with =, and a new assignment replaces the old value.
- Python decides the type from the value: 17 is int, 17.0 is float, "17" is str.
- input() always returns a string.
- Comma-separated print arguments are shown with spaces between them; + joins strings with no extra space.
Worked examples
Example 1 (book variable example).
age = 71
print("Ahmad lived for", age, "years")
age = 60
print("Iqbal lived for", age, "years")
Output: Ahmad lived for 71 years, then Iqbal lived for 60 years. The same variable holds two values at different times.
Example 2 (valid or invalid names).
student_name: valid._total: valid; it starts with an underscore.2nd_mark: invalid; it starts with a digit.while: invalid; it is a keyword.Ageandage: both valid, and different.
Example 3 (number input). The user types 17 at user_age = int(input("Enter your age:")). Without int(), the stored value would be the text "17". With int(), it is the whole number 17, so user_age + 1 gives 18.
Common mistakes in the exam
- Starting a name with a digit.
1st_nameis not allowed. - Forgetting conversion.
input()returns a string; adding 1 to it raises an error. - Joining a number with +.
"Age: " + 17fails; use a comma or str(17). - Writing True as "True". With quotes it is a string, not a Boolean.
- Assuming case does not matter. Name and name are different variables.
Exam technique
- When asked for output, trace the program line by line and write each printed line separately.
- For naming questions, quote the rule that each invalid name breaks.
- Write code in small, clearly indented blocks and keep quotation marks matched.
- Check your board's notice for the sections in your paper.
Practice questions
- Name the data type of 3.5, "Lahore", 42 and False. Answer: float, str, int, bool.
- Is
my-scorea valid variable name? Answer: No. A hyphen is not allowed; usemy_score. - What does
print("Sum:", 4 + 5)display? Answer:Sum: 9. - Write a line that reads a decimal height from the user.
Answer:
height = float(input("Enter height:")). - Which symbol starts a single-line comment? Answer: The # symbol.
Ready to practise this? Continue in 1st Year Computer Science.