Chapter 3: Introduction to Python || Informatics Practices (IP) || Class 11th || NCERT CBSE || NOTES IN ENGLISH || 2024-25

 



Chapter 3: Introduction to Python

3.1 Overview of Python Programming Language

  • Python is a high-level, open-source programming language known for its simplicity and readability.

  • Created by Guido van Rossum in 1991, Python has become widely used in areas such as web development, data science, and artificial intelligence.


3.2 Execution Modes in Python

Python programs can be executed in two primary modes:

3.2.1 Interactive Mode

  • In this mode, users can execute Python commands line-by-line in the Python Shell.

  • It is beneficial for quick tests or small code snippets but does not save commands for later use.

3.2.2 Script Mode

  • Python programs are written and saved as scripts with a .py extension.

  • The entire script is executed at once, making it more suitable for larger programs.


3.3 Basic Elements of Python Syntax

3.3.1 Keywords

  • Python has a set of keywords reserved for specific functions, such as if, else, while, and return.

  • These keywords cannot be used as identifiers (variable or function names).

3.3.2 Identifiers

  • Identifiers are user-defined names for variables, functions, classes, etc., and must start with a letter (A-Z, a-z) or an underscore (_).

  • They are case-sensitive and should not match any Python keyword.

3.3.3 Variables

  • A variable stores data and can be updated or reused throughout the code.

  • Variables must be assigned a value before being used.


3.4 Data Types in Python

Python supports various data types for handling different types of data.

3.4.1 Numeric Types

  • Integer (int): Whole numbers, positive or negative (e.g., -5, 10).

  • Float: Numbers with decimal points (e.g., 4.5, -2.3).

  • Complex: Numbers with a real and imaginary part (e.g., 3+5j).

3.4.2 Sequence Types

  • Strings: A collection of characters (e.g., "Hello, World!").

  • Lists: Ordered and mutable collections, allowing various data types (e.g., [1, "text", 3.5]).

  • Tuples: Similar to lists but immutable, meaning they cannot be changed once created.

3.4.3 Mapping Type

  • Dictionaries: Store data in key-value pairs, where keys are unique and immutable (e.g., {'name': 'Alice', 'age': 25}).


3.5 Operators in Python

Operators perform various operations on variables and values.

3.5.1 Arithmetic Operators

  • Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Exponentiation (), and Floor Division (//).

3.5.2 Comparison Operators

  • Used to compare values, such as equal (==), not equal (!=), greater than (>), and less than (<).

3.5.3 Logical Operators

  • and: Returns True if both statements are true.

  • or: Returns True if at least one statement is true.

  • not: Reverses the logical state of the operand.


3.6 Expressions and Statements

  • Expressions combine values, variables, and operators to produce a result (e.g., 3 + 5 * 2).

  • Statements are instructions executed by the Python interpreter, such as assignments and function calls.


3.7 Input and Output in Python

3.7.1 Input Function

input(): Accepts user input as a string.
python
Copy code
name = input("Enter your name: ")


3.7.2 Output Function

print(): Displays output to the screen.
python
Copy code
print("Hello, World!")



3.8 Debugging

Debugging is the process of finding and fixing errors in code, which can be:

  • Syntax Errors: Issues with the structure or format of the code, like missing parentheses.

  • Logical Errors: Mistakes in logic that produce incorrect results without any error message.

  • Runtime Errors: Errors that occur during program execution, such as division by zero.


3.9 Control Flow: if...else Statements

Control flow statements decide the direction of program execution based on conditions.

3.9.1 if Statement

  • Executes code only if a specified condition is true.
    python
    Copy code
    if age >= 18:
        print("Eligible to vote")

3.9.2 if...else Statement

  • Provides an alternative action if the condition is false.
    python
    Copy code
    if age >= 18:
        print("Eligible")
    else:
        print("Not eligible")

3.9.3 if...elif...else Statement

  • Allows for multiple conditions to be checked in sequence.
    python
    Copy code
    if score > 90:
        print("Grade: A")
    elif score > 75:
        print("Grade: B")
    else:
        print("Grade: C")


3.10 Loops in Python

3.10.1 for Loop

  • Repeats a block of code a specified number of times, often used with sequences.
    python
    Copy code
    for i in range(5):
        print(i)

3.10.2 while Loop

  • Executes as long as a specified condition is true.
    python
    Copy code
    count = 0
    while count < 5:
        print(count)
        count += 1

3.10.3 Nested Loops

  • A loop inside another loop, often used for multi-dimensional data.
    python
    Copy code
    for i in range(3):
        for j in range(2):
            print(i, j)


3.11 Functions in Python

A function is a reusable block of code designed to perform a specific task.

  • Defining a Function:
    python
    Copy code
    def greet(name):
        print("Hello, " + name)

Calling a Function:
python
Copy code
greet("Alice")


  • Built-in Functions: Python offers several built-in functions, such as print(), input(), len(), and range().


Summary

This chapter introduces Python as a versatile programming language, covering fundamental concepts, such as syntax, data types, operators, control flow, loops, and functions. By understanding these basics, one can start developing simple programs and progress toward more complex applications.




0 comments: