Curriculum
Course: Ncert - Class 11- Computer Science
Login
Text lesson

Detailed Notes:1- Chapter 5-Getting Started with Python

INTRODUCTION TO PYTHON

  1. Programming Language Basics: It defines a programming language as an ordered set of instructions executed by a computer to perform a specific task. It distinguishes between high-level languages like Python and low-level/machine languages composed of 0s and 1s.

  2. Python Overview:

    • Python is introduced as a high-level, interpreted, free, and open-source programming language.
    • It’s known for its clear syntax, ease of understanding, and portability across different operating systems and hardware platforms.
    • Python’s interpreter translates source code into machine language, executing statements one by one.
    • Compared to compilers, interpreters like Python’s process code line-by-line.

Features of Python:

  1. High-level language:

    Python is a high-level language, offering abstractions that make it closer to human language.

  2. Interpreted language:

    Python programs are executed by an interpreter, which reads and executes the code line by line.

  3. Ease of understanding:

    Python programs are easy to understand due to their clearly defined syntax and relatively simple structure.

  4. Case sensitivity:

    Python is case-sensitive, treating variables and identifiers with different cases as distinct entities.

  5. Portability and platform independence:

    Python can run on various operating systems and hardware platforms without modification, enhancing its versatility.

  6. Rich library of predefined functions:

    Python comes with a vast standard library containing numerous modules and functions for various tasks.

  7. Useful in web development:

    Python is widely used in web development, with frameworks like Django and Flask facilitating the development of web applications.

  8. Indentation for blocks and nested blocks:

    Python uses indentation to define blocks of code, such as loops and conditionals, improving readability and enforcing code consistency.

Working with Python:

  1. Python Interpreter:

    • To write and execute Python programs, a Python interpreter must be installed on the computer.
    • The interpreter is also known as the Python shell.
  2. Execution Modes:

    • There are two primary ways to use the Python interpreter: Interactive mode and Script mode.

Interactive Mode (a):

  • Allows for the immediate execution of individual statements.
  • Users can type Python statements directly at the >>> prompt.
  • Upon pressing enter, the interpreter executes the statement and displays the result(s).
  • Ideal for testing single-line code for instant execution.
  • Drawback: Statements cannot be saved for future use, necessitating retyping for subsequent runs.

Script Mode (b):

  • Involves writing Python programs in files with a .py extension.
  • Scripts can be saved in the Python installation folder.
  • To execute a script: a) Type the file name along with the path at the prompt.
    • Example: If the file name is prog5-1.py, type prog5-1.py.
    • Alternatively, the program can be opened directly from IDLE. b) While working in script mode, after saving the file, execute it by clicking [Run] -> [Run Module] from the menu.
  • Enables writing and executing multiple instructions in a file.
  • Suitable for creating more complex programs and saving code for future use.

IDENTIFIERS

  1. Definition: Identifiers are names used to identify variables, functions, or other entities in a program.

  2. Rules for Naming Identifiers:

    • Must begin with an uppercase or lowercase alphabet or an underscore (_).
    • Can be followed by any combination of characters a–z, A–Z, 0–9, or underscore (_).
    • Cannot start with a digit.
    • Can be of any length, but it’s preferable to keep them short and meaningful.
    • Should not be a Python keyword or reserved word.
    • Special symbols like !, @, #, $, %, etc., cannot be used in identifiers.
  3. Examples:

    • Identifiers like marks1, marks2, marks3, and avg are preferable over single letters like a, b, c, or A, B, C.
    • For instance, to find the average of marks obtained by a student in three subjects, identifiers such as marks1, marks2, marks3, and avg can be used:
python

avg = (marks1 + marks2 + marks3) / 3

  • Similarly, when calculating the area of a rectangle, descriptive identifiers like area, length, and breadth are preferred over single alphabets for clarity and readability:
python

area = length * breadth

Following these rules and conventions enhances code readability and maintainability, making it easier for programmers to understand and work with Python code.

VARIABLES

 

  1. Definition:

    • A variable in a program is identified by a name, also known as an identifier.
    • In Python, a variable refers to an object stored in memory, which can hold various types of data such as strings, numbers, or combinations of alphanumeric characters.
  2. Assignment Statement:

    • Variables are created and assigned specific values using an assignment statement.
    • Syntax: variable_name = value
  3. Example Program (5-2):

    • Demonstrates the use of variables in Python to store different types of data such as strings and numbers.
    • Variables message, userNo, and price are assigned values of string, integer, and float types, respectively.
    • Implicit variable declaration: Variables are automatically declared and defined when they are assigned values for the first time.
    • Variables must be assigned values before they are used in expressions to avoid errors.
  4. Example Program (5-3):

    • Illustrates a Python program to find the area of a rectangle.
    • Variables length and breadth are assigned values of 10 and 20, respectively.
    • The area of the rectangle is calculated by multiplying length and breadth, and the result is stored in the variable area.
    • The calculated area is then printed using the print() function.

 

×

Cart