Revision Summary: Getting Started with Python (NCERT Class 11)
1. Chapter at a Glance
- Python is a high-level, interpreted, case-sensitive, portable and platform-independent language that uses indentation for blocks and has a rich library of predefined functions.
- Python programs can be executed in interactive mode (individual statements at the
>>> prompt) or script mode (saved as .py files and run as a whole).
- Keywords are reserved words with fixed meanings; identifiers (names for variables, functions, etc.) must follow specific naming rules and cannot be keywords.
- Variables are created implicitly through assignment; every value in Python is treated as an object with a unique identity (obtained via
id()).
- Data types include Number (
int, float, complex, bool), Sequence (String, List, Tuple), Set, None and Mapping (Dictionary); they are classified as mutable or immutable.
- Operators (arithmetic, relational, assignment, logical, identity, membership) are used to form expressions; operator precedence and parentheses determine evaluation order.
- Programs interact with users via
input() (always returns string) and print(); explicit type conversion (int(), float(), str()) or implicit coercion may be required.
- Debugging involves removing syntax errors (interpreter stops), logical errors (wrong output, program runs) and runtime errors (abnormal termination during execution).
2. Key Terms and Definitions
- Program: An ordered set of instructions to be executed by a computer to carry out a specific task.
- Programming language: The language used to specify the set of instructions to the computer.
- Source code: A program written in a high-level language.
- Interpreter: Translates and executes Python statements one by one; stops on encountering an error.
- Keywords: Reserved words having specific meaning to the Python interpreter; must be written exactly as defined and are case-sensitive.
- Identifiers: Names used to identify a variable, function or other entities in a program.
- Variable: A named reference to an object stored in memory; created implicitly by assignment.
- Comments: Non-executable remarks added for human understanding; begin with
# and are ignored by the interpreter.
- Object: Every value or data item in Python; has a unique identity (ID) that remains constant during its lifetime.
- Data type: Identifies the type of values a variable can hold and the operations that can be performed.
- Mutable: Data types whose values can be changed after creation.
- Immutable: Data types whose values cannot be changed after creation; an attempt to modify creates a new object.
- Expression: A combination of constants, variables and operators that always evaluates to a value.
- Statement: A unit of code that the Python interpreter can execute.
- Syntax error: Violation of Python’s rules; interpreter displays error and stops.
- Logical error: Bug causing incorrect behaviour/output without abrupt termination.
- Runtime error: Error occurring during execution that causes abnormal termination.
3. Syntax and Constructs
```python
Interactive prompt example
message = "Keep Smiling"
print(message)
```
```python
Script mode (saved as .py)
length = 10
breadth = 20
area = length * breadth
print(area)
```
```python
Comment
Variable amount is the total spending on grocery
amount = 3400
```
```python
Variable assignment (implicit)
num1 = 20 # int
price = 987.9 # float
flag = True # bool
```
```python
Data type functions
type(num1) # returns
id(num1) # returns identity (memory address)
```
```python
input() and print()
fname = input("Enter your first name: ")
print("Hello", fname, end="!\n")
```
```python
Explicit type conversion
age = int(input("Enter age: "))
total = float(num1) + num2
str(totalPrice)
```
```python
Arithmetic operators
num1 + num2 # addition / concatenation
num1 // num2 # floor division
num1 ** num2 # exponentiation
```
```python
Relational, Logical, Identity, Membership
num1 == num2
num1 > num2 and num2 != 0
num1 is num2
2 in [1, 2, 3]
```
4. Algorithms and Worked Logic
Finding area of rectangle (step sequence)
1. Assign length and breadth values.
2. Compute area = length * breadth.
3. Display the result using print().
Evaluating an expression (dry-run steps)
1. Apply highest-precedence operator first (**, then * / // %, then + -).
2. For equal precedence, evaluate left to right.
3. Parentheses override precedence.
Example: 20 + 30 * 40 → 20 + (30 * 40) → 1220.
Type conversion decision
- Use
int(), float(), str() when the programmer explicitly needs a different type (e.g., input() always returns str).
- Implicit conversion occurs automatically when an
int and float are mixed (result becomes float).
Debugging procedure
- Check for syntax errors (interpreter halts).
- Verify output against expected result for logical errors.
- Handle runtime errors (division by zero, invalid type conversion) by validating inputs.
5. Common Errors and Exam Pitfalls
- Using a keyword or special character (
!, @, #) as an identifier or starting an identifier with a digit.
- Forgetting that
input() always returns a string; omitting int()/float() leads to wrong results (e.g., "2"*2 gives "22" instead of 4).
- Missing parentheses or using
+ between different types in print() (TypeError).
- Confusing
= (assignment) with == (equality); writing if num1 = num2.
- Ignoring operator precedence or forgetting left-to-right evaluation for same-precedence operators.
- Attempting to modify an immutable object (e.g., changing a character inside a string) without realising a new object is created.
- Not handling runtime errors such as division by zero or converting non-numeric string with
int().
- Writing comments without
# or placing code after # on the same line expecting it to execute.
- Using single-letter identifiers or names without meaning, losing readability marks.