Informatics Practices: Class 11 — Chapter 3: Brief Overview of Python

3.1 Introduction to Python & Execution Modes

An ordered set of instructions or commands to be executed by a computer is called a program . The language used to specify those set of instructions to the computer is called a programming language (e.g., Python, C, C++, Java, etc.).

Python is a highly popular, easy-to-learn, high-level, interpreter-based programming language created by Guido van Rossum in 1991 . It is widely used in software development, web development, scientific computing, big data, and Artificial Intelligence.

3.1.1 Working with Python

To write and run Python programs, a Python interpreter must be installed, or an online interpreter can be used. The interpreter is also referred to as the Python shell . The symbol >>> is the Python prompt , indicating that the interpreter is ready to receive commands.

Technical Profile: IDLE (Integrated Development and Learning Environment)

  • Architecture Layer / Tier: Built-in Python Integrated Development Environment (IDE) packaged with standard Python installations.
  • Operational Mechanics: Provides a Python Shell (Interactive Mode) with the >>> prompt, and a built-in \text editor (Script Mode) for writing, saving ( .py files), and executing code via the F5 shortcut or Run Module command.
  • System Impact & Execution Outputs: Facilitates rapid prototyping, syntax highlighting, basic debugging, and isolated script execution within a clean, lightweight graphical interface.
3.1.2 Execution Modes

Python supports two primary execution modes:

  • Interactive Mode: Commands are typed directly at the >>> prompt. Pressing Enter executes the statement immediately and displays the result. This mode is ideal for testing single-line code snippets but does not allow saving statements for future use.
  • Script Mode: Programs are written in a \text file, saved with a .py extension, and executed using the interpreter. This mode is preferred for multi-line programs that need to be saved and run repeatedly.

Interactive Mode Example:

  
    >>> 1 + 2
    3
    >>> 4 - 2
    2
    >>> "Hello" + "World"
    'HelloWorld'
  
3.2 Python Keywords & Identifiers
3.2.1 Python Keywords

Keywords are reserved words with specific meanings for the Python interpreter. Because Python is case-sensitive, keywords must be written exactly as defined.

Keyword Keyword Keyword Keyword Keyword
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
3.2.2 Identifiers

Identifiers are user-defined names used to identify variables, functions, classes, or other entities in a program.

Rules for naming identifiers in Python:

  1. Must begin with an uppercase or lowercase letter (A-Z, a-z) or an underscore ( _ ).
  2. Can be followed by any combination of letters, digits (0-9), or underscores. It cannot start with a digit.
  3. Can be of any length, though short and meaningful names are recommended.
  4. Must not be a reserved keyword.
  5. Cannot contain special symbols such as ! , @ , # , $ , % , etc.
3.3 Variables & Comments

A variable is an identifier whose value can change during program execution. Variables must be assigned a value before they are used, otherwise a runtime error occurs.

  
    gender = 'M'
    message = "Keep Smiling"
    price = 987.9
  

Comments are non-executable notes added to the source code to make it easier for humans to understand. In Python, a single-line comment starts with the hash sign ( # ).

  
    # This is a comment
    # Program to find the sum of two numbers
    num1 = 10
    num2 = 20
    result = num1 + num2
    print(result) # Output: 30
  
3.4 Data Types

Every value in Python belongs to a specific data type, which determines the operations that can be performed on it.

3.4.1 Numbers

Stores numerical values. It is classified into three types:

  • int (Integer): Whole numbers (e.g., -12, 0, 123). bool (Boolean) is a subtype of integer containing only True (non-zero) and False (zero).
  • float (Floating Point): Numbers with fractional parts (e.g., -2.04, 4.0, 14.23).
  • complex (Complex Numbers): Numbers with real and imaginary parts (e.g., 3 + 4j).
Type/Class Description Examples
int integer numbers -12, -3, 0, 123, 2
float floating point numbers -2.04, 4.0, 14.23
complex complex numbers 3 + 4j, 2 - 2j
3.4.2 Sequences

An ordered collection of items where each item is indexed by an integer:

  • String: A group of characters enclosed in single or double quotes (e.g., 'Hello' , "452" ).
  • List: A comma-separated sequence of items enclosed in square brackets [] . Items can be of different data types and are mutable (e.g., [5, 3.4, "New Delhi"] ).
  • Tuple: A comma-separated sequence of items enclosed in parentheses () . Tuples are immutable (e.g., (10, 20, "Apple") ).
3.4.3 Mappings (Dictionary)

An unordered data type that stores data in key-value pairs enclosed in curly brackets {} . Keys must be unique and are typically strings, while values can be of any data type.

  
    >>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
    >>> print(dict1['Price(kg)'])
    120
  
3.5 Operators & Expressions
3.5.1 Arithmetic Operators

Used to perform mathematical calculations.

Operator Operation Description Example
+ Addition Adds values on either side of the operator. Also concatenates strings. 5 + 6 = 11
- Subtraction Subtracts right operand from left operand. 5 - 6 = -1
* Multiplication Multiplies values. Also repeats strings if one operand is an integer. 5 * 6 = 30
/ Division Divides left operand by right operand; returns a float. 5 / 2 = 2.5
% Modulus Divides left operand by right operand; returns the remainder. 13 % 5 = 3
// Floor Division Divides and returns the quotient as an integer (removes decimal part). 5 // 2 = 2
** Exponent Raises the base to the power of the exponent. 3 ** 4 = 81
3.5.2 Relational Operators

Compares values and returns either True or False .

Operator Operation Description Example (num1=10, num2=0)
== Equals to True if values are equal. num1 == num2 (False)
!= Not equal to True if values are not equal. num1 != num2 (True)
> Greater than True if left operand is greater than right. num1 > num2 (True)
< Less than True if left operand is less than right. num1 < num2 (False)
>= Greater than or equal to True if left operand is greater than or equal to right. num1 >= num2 (True)
<= Less than or equal to True if left operand is less than or equal to right. num1 <= num2 (False)
3.5.3 Assignment, Logical, and Membership Operators

Assignment Operators: Used to assign values to variables (e.g., = , += , -= , *= , /= ).

Logical Operators: Used to combine conditional statements. Written in lowercase: and , or , not .

Membership Operators: Used to test if a value is present in a sequence (string, list, tuple): in , not in .

3.5.4 Operator Precedence

Determines the order of evaluation in expressions containing multiple operators. Parentheses () have the highest precedence and can override default evaluation orders.

  
    # Precedence of * is higher than +
    20 + 30 * 40  # Evaluates to 20 + 1200 = 1220
    # Parentheses force addition first
    (20 + 30) * 40  # Evaluates to 50 * 40 = 2000
  
3.6 Input, Output & Debugging
3.6.1 Input and Output Functions

input(): Prompts the user for input and returns it as a string. Type conversion functions like int() or float() must be used to convert the input to numeric types.

  
  age = int(input("Enter your age: "))

print(): Evaluates and displays expressions on the screen.

  
    print("Hello")
    print(10 * 2.5) # Output: 25.0
  
3.6.2 Debugging and Error Types

The process of identifying and removing errors from a program is called debugging . Errors are categorized into three types:

  1. Syntax Errors: Violations of the grammatical rules of the programming language. The interpreter stops execution immediately when a syntax error is encountered (e.g., missing parenthesis).
  2. Logical Errors: The program runs successfully but produces incorrect or unexpected output. These are also called semantic errors (e.g., writing 10 + 12 / 2 instead of (10 + 12) / 2 ).
  3. Runtime Errors: Occur during execution, causing abnormal termination of the program even though the syntax is correct (e.g., division by zero).
3.7 Functions & Control Flow
3.7.1 Functions

A function is a named, reusable block of statements that performs a specific task. Python provides built-in functions (e.g., print() , input() , type() , len() ) and allows importing functions from modules using the import statement.

3.7.2 Conditional Statements (if..else)

Used to execute different blocks of code based on conditional outcomes:

  • if statement: Executes code only if the condition is true.
  • if..else statement: Executes one block if the condition is true, and another if it is false.
  • if..elif..else statement: Checks multiple conditions sequentially.
  
    # Example of if..elif..else
    number = int(input("Enter a number: "))
    if number > 0:
    print("Positive")
    elif number < 0:
    print("Negative")
    else:
    print("Zero")
  
3.7.3 Loops (for & nested loops)

for Loop: Used to iterate over a sequence (range, list, tuple, string).

  
    # Print even numbers
    numbers = [1, 2, 3, 4, 5]
    for num in numbers:
    if num % 2 == 0:
    print(num, "is even")
  

range() Function: Generates a sequence of numbers. Syntax: range([start], stop[, step]) .

  
    >>> list(range(2, 10, 2))
    [2, 4, 6, 8]
  

Nested Loops: A loop inside another loop.

  
    for i in range(3):
    for j in range(2):
    print(i, j)
  
3.8 Case Study: Student Management Information System

"Schools use 'Student Management Information System' (SMIS) to manage student related data. This system provides facilities for: Recording and maintaining personal details of students, maintaining marks scored in assessments and computing results of students, keeping track of student attendance, and managing many other student-related data in the school."


Socio-Technical Analysis & Core Systemic Impact: The automation of student records via an SMIS represents a critical transition from manual ledger-keeping to digital database management. Systemic considerations include:

  1. Data Privacy & Security: Storing sensitive personal details (contact numbers, addresses, parent details) requires strict access control protocols to prevent unauthorized data exposure.
  2. Data Integrity: Automated computation of assessment marks and attendance reduces human calculation errors but introduces vulnerabilities to software bugs or incorrect formula implementations.
  3. System Redundancy: Regular database backups are essential to mitigate the risk of data loss from hardware failures or system crashes.