Class 11 Computer and Communicatio Notes Chapter 3 (Chapter 3) – CCT Part-I Book

CCT Part-I
Detailed Notes with MCQs of Chapter 3, "Getting Started with Python". This is a foundational chapter, crucial not just for your Class 11 understanding but also potentially very useful for various government exams that include basic computer literacy or programming concepts. Pay close attention.


Chapter 3: Getting Started with Python - Detailed Notes

1. Introduction to Python

  • What is Python?

    • Python is a high-level, interpreted, general-purpose programming language.
    • High-level: Closer to human language, abstracts away low-level computer details (like memory management).
    • Interpreted: Code is executed line by line by an interpreter, without needing to be compiled first into machine code (like C++ or Java). This makes development faster.
    • General-purpose: Can be used for a wide variety of tasks – web development, data science, scripting, automation, AI, etc.
    • Created by Guido van Rossum and first released in 1991.
    • Emphasizes code readability and simplicity.
  • Key Features of Python:

    • Simple and Easy to Learn: Clean syntax, resembles plain English.
    • Free and Open Source: Available for free, source code is public.
    • High-level Language: Less worry about low-level details.
    • Portable: Python code written on one OS (like Windows) can run on others (like macOS, Linux) without significant changes.
    • Interpreted: Facilitates rapid development and debugging.
    • Extensive Standard Library: Comes with many pre-built modules and functions for various tasks (like math operations, file handling, networking).
    • Expressive Language: Can achieve complex tasks with fewer lines of code compared to some other languages.
    • Object-Oriented: Supports object-oriented programming principles.
    • Dynamically Typed: You don't need to declare the type of a variable explicitly; the interpreter infers it at runtime.

2. Working with Python

  • Python Interpreter: The software that reads and executes Python code.

  • Modes of Working:

    • Interactive Mode (REPL - Read-Eval-Print Loop):
      • You type commands directly into the Python interpreter (often accessed via a terminal or the IDLE shell, indicated by the >>> prompt).
      • The interpreter executes the command immediately and shows the result.
      • Useful for testing small snippets of code, exploring features, and quick calculations.
      • Code is not saved permanently.
    • Script Mode:
      • You write your Python code in a file (usually with a .py extension, e.g., myprogram.py).
      • You then run the entire file using the Python interpreter (e.g., python myprogram.py in the terminal).
      • Suitable for writing longer programs.
      • Code is saved for later use and modification.
  • Python Keywords:

    • Reserved words that have special meaning to the interpreter.
    • Cannot be used as variable names or identifiers.
    • Examples: if, else, for, while, def, class, import, return, True, False, None, and, or, not, try, except, etc.
  • Identifiers:

    • Names given to entities like variables, functions, classes, etc.
    • Rules for Naming Identifiers:
      • Can contain letters (a-z, A-Z), digits (0-9), and the underscore (_).
      • Cannot start with a digit.
      • Cannot be a keyword.
      • Case-sensitive (e.g., myVar is different from myvar).
      • Convention: Use descriptive names (e.g., student_name instead of sn).
  • Variables:

    • Named locations in memory used to store data values.
    • Created when you first assign a value to them (e.g., age = 25).
    • Python is dynamically typed: the type of the variable is determined by the value assigned to it, and it can change if reassigned.
      • x = 10 (x is an integer)
      • x = "Hello" (Now x is a string)
  • Comments:

    • Text in the code ignored by the interpreter. Used for explaining the code.
    • Single-line comments: Start with #. Everything after # on that line is a comment.
      # This is a single-line comment
      age = 30 # Assigning age
      
    • Multi-line comments: Often done using triple quotes (""" """ or ''' '''). Technically, these are multi-line strings, but commonly used for comments or docstrings.
      """
      This is a multi-line
      comment explaining a function
      or a block of code.
      """
      

3. Data Types

  • Data type specifies the type of value a variable can hold and the operations that can be performed on it.

  • Fundamental Types:

    • Numbers:
      • int: Integers (e.g., 10, -5, 0).
      • float: Floating-point numbers (decimal numbers, e.g., 3.14, -0.5).
      • complex: Complex numbers (e.g., 3+4j).
    • Boolean (bool): Represents truth values: True or False. Note the capitalization.
    • String (str): Sequence of characters enclosed in single (' '), double (" "), or triple (""" """ or ''' ''') quotes. Strings are immutable.
    • None (NoneType): Represents the absence of a value. It's a special constant.
  • Sequence Types (Ordered Collections):

    • List (list): Ordered, mutable (changeable) sequence of items. Enclosed in square brackets []. Items can be of different types. (e.g., [1, "hello", 3.5, True])
    • Tuple (tuple): Ordered, immutable (unchangeable) sequence of items. Enclosed in parentheses (). (e.g., (10, 20, "world"))
  • Mapping Type (Unordered Collection):

    • Dictionary (dict): Unordered collection of key-value pairs. Enclosed in curly braces {}. Keys must be unique and immutable (like strings, numbers, tuples). Values can be anything. (e.g., {"name": "Alice", "age": 25})
  • Set Types (Unordered Collections):

    • Set (set): Unordered collection of unique, immutable items. Enclosed in curly braces {}. Duplicate elements are automatically removed. (e.g., {1, 2, 3, "apple"})
    • Frozenset (frozenset): An immutable version of a set.
  • type() function: Used to determine the data type of a variable or value.

    x = 10
    print(type(x)) # Output: <class 'int'>
    y = "Hello"
    print(type(y)) # Output: <class 'str'>
    
  • Mutability:

    • Mutable: Objects whose value can be changed after creation (e.g., lists, dictionaries, sets).
    • Immutable: Objects whose value cannot be changed after creation (e.g., integers, floats, booleans, strings, tuples, frozensets).

4. Operators

  • Symbols that perform operations on values (operands).
  • Arithmetic Operators: + (add), - (subtract), * (multiply), / (float division), % (modulus/remainder), ** (exponentiation), // (floor division - divides and rounds down to the nearest whole number).
  • Relational/Comparison Operators: Compare two values and return True or False. == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Assignment Operators: Assign values to variables. = (assign), +=, -=, *=, /=, %=, **=, //= (shorthand operators, e.g., x += 5 is same as x = x + 5).
  • Logical Operators: Combine boolean expressions. and (True if both operands are True), or (True if at least one operand is True), not (reverses the boolean value).
  • Membership Operators: Check if a value is present in a sequence. in (True if value is in sequence), not in (True if value is not in sequence).
  • Identity Operators: Compare the memory locations of two objects. is (True if both operands refer to the same object), is not (True if they refer to different objects).
  • Operator Precedence: The order in which operators are evaluated in an expression (e.g., * and / before + and -). Parentheses () can be used to override precedence.

5. Input and Output

  • Output (print() function): Displays values/variables to the console.
    name = "Ravi"
    age = 20
    print("Hello there!")
    print("Name:", name, "Age:", age)
    print("Value is", 100, sep='---', end='***\n') # sep changes separator, end changes ending character
    
  • Input (input() function): Reads input from the user via the keyboard.
    • It always returns the input as a string.
    • You often need to convert the input to the desired type (e.g., int, float).
    user_name = input("Enter your name: ")
    age_str = input("Enter your age: ")
    age_int = int(age_str) # Type casting/conversion
    print("Hello,", user_name)
    print("Next year you will be", age_int + 1)
    
  • Type Casting/Conversion: Explicitly converting a value from one data type to another using functions like int(), float(), str(), list(), tuple(), etc.

6. Expressions and Statements

  • Expression: A combination of values, variables, and operators that evaluates to a single value (e.g., 5 + 10, x * 2, age > 18).
  • Statement: A complete unit of execution or instruction that the Python interpreter can execute (e.g., assignment statement x = 5, print() statement, if statement).

7. Indentation

  • Crucial in Python: Unlike many other languages that use braces {} to define blocks of code (like loops, function definitions, conditional statements), Python uses indentation (whitespace at the beginning of a line).
  • Typically, 4 spaces are used per indentation level (consistent indentation is mandatory).
  • Incorrect indentation will lead to IndentationError.
    # Correct Indentation
    if age > 18:
        print("Eligible to vote.") # Indented block
        print("Please register.")   # Part of the same block
    else:
        print("Not eligible yet.") # Another indented block
    
    # Incorrect Indentation (will cause error)
    # if age > 18:
    # print("Eligible") # Missing indentation
    

Multiple Choice Questions (MCQs)

Here are 10 MCQs based on the chapter content:

  1. Which of the following describes Python accurately?
    a) Compiled, Statically Typed
    b) Interpreted, Statically Typed
    c) Compiled, Dynamically Typed
    d) Interpreted, Dynamically Typed

  2. What is the output of print(type(15.0))?
    a) <class 'int'>
    b) <class 'float'>
    c) <class 'str'>
    d) <class 'double'>

  3. Which of the following is NOT a valid variable name in Python?
    a) _myVar
    b) my_var_1
    c) MyVar
    d) 1_my_var

  4. What does the input() function in Python always return?
    a) Integer
    b) Float
    c) String
    d) Boolean

  5. What is the primary purpose of using # in Python code?
    a) To indicate a multi-line string
    b) To start a block of code
    c) To write a single-line comment
    d) To perform integer division

  6. Which operator is used for floor division in Python?
    a) /
    b) %
    c) //
    d) **

  7. Consider x = ["apple", "banana"]. What does print("cherry" not in x) output?
    a) True
    b) False
    c) Error
    d) None

  8. How are blocks of code defined in Python (e.g., inside an if statement or for loop)?
    a) Using curly braces {}
    b) Using parentheses ()
    c) Using indentation
    d) Using keywords begin and end

  9. What will be the output of print(100 / 25 * 4)?
    a) 1.0
    b) 16.0
    c) 1
    d) 16

  10. Which data type in Python is an ordered, immutable sequence of items?
    a) List
    b) Dictionary
    c) Set
    d) Tuple


Answers to MCQs:

  1. d) Interpreted, Dynamically Typed
  2. b) <class 'float'> (Because 15.0 has a decimal point)
  3. d) 1_my_var (Identifiers cannot start with a digit)
  4. c) String
  5. c) To write a single-line comment
  6. c) //
  7. a) True ("cherry" is indeed not in the list x)
  8. c) Using indentation
  9. b) 16.0 (Division / results in a float. Evaluation is left-to-right for * and /: (100 / 25) * 4 -> 4.0 * 4 -> 16.0)
  10. d) Tuple

Study these notes carefully. Understanding these basics is essential before moving on to more complex programming concepts. Let me know if any part needs further clarification.

Read more