Class 11 Computer Science Python Exam Guide
Class 11 Computer Science Python Exam Guide
The split() function in Python divides a string into a list of substrings based on a specified delimiter (default is space). It's commonly used in data processing for parsing text data into manageable parts, such as splitting lines of text from a file into words for analysis or extraction of specific fields from formatted strings .
Shallow copying should be used when you want a new list whose elements are references to the objects in the original list, but with the ability to modify the structure independently. Deep copying is necessary when you need a completely independent copy of the original list, with all inner objects recursively copied. A pitfall of shallow copying is unintentional mutable state sharing between copies, leading to side effects when inner objects are modified .
Python is considered a dynamically typed language because the type of a variable is determined at runtime, not in advance. This allows for greater flexibility and simplicity in code writing, as variables can change type and are not bound to a strict type declaration . However, it can lead to runtime errors if type mismatches are not correctly managed, reducing some compile-time error checking .
A compiler translates the entire program at once into machine code before execution, which can be executed multiple times without recompilation. In contrast, an interpreter translates and executes the program line by line, needing to reprocess the source code each time .
A Python list is mutable, allowing modifications such as adding or removing elements, making it useful for collections that may change in size. A tuple, however, is immutable, providing a fixed-size, unordered collection ideal for data that should not change, meant for faster access and better memory optimization .
The immutability of strings in Python means they cannot be changed after creation, allowing for optimized memory usage since unchanged strings can be shared across different parts of a program. This immutability also enhances performance in terms of speed by leveraging string interning and caching, avoids unintended side effects from modifying strings, and supports efficient hashing operations critical for usage as dictionary keys .
The flow of execution in a Python program is generally sequential, executing statements from top to bottom unless directed otherwise by control statements like loops and conditionals or modified by function calls. Understanding this flow is crucial for debugging because it allows programmers to predict which parts of code execute and in what order, helping in identifying where issues may arise if the expected behavior isn't met .
The append() method adds a single element at the end of a list, while extend() adds each element from an iterable to the list. Append should be used when adding individual items, such as numbers or strings, whereas extend is more suitable when concatenating another collection, like adding all items from another list . Example: appending a single integer, extend for merging lists.
Mutable data types, like lists and dictionaries, can be changed after their creation (e.g., elements can be added or modified). Immutable data types, like strings and tuples, cannot be altered after they are set, meaning that any 'change' creates a new object . Mutable example: a list can have elements appended, whereas an immutable tuple cannot be changed once initialized .
Functions in Python create modular code blocks that help in reusability and enhance readability by reducing redundancy. Reusing functions across different parts of a program or in different projects increases efficiency and consistency, making code easier to maintain and understand. This leads to higher quality code by ensuring single-point control over functionality changes .