Python Programming Exam Paper I-B.Sc CS
Python Programming Exam Paper I-B.Sc CS
Python uses the 'def' keyword to define functions, which includes specifying the function name and its parameters within parentheses. This keyword indicates the creation of a block of code that can be executed later by invoking the function name. Once defined, the function name is bound to the function body, allowing it to be referred to by that name throughout the program. This mechanism promotes code reusability and modularity, as the defined function can be reused wherever required by simply calling it with the appropriate arguments .
Recursive functions in Python are functions that call themselves during execution. They are used to solve problems that can be broken down into smaller, similar problems, known as divide and conquer strategy. A key characteristic of a recursive function is that it must have a base case to prevent infinite recursion. An example of a recursive function is the calculation of the factorial of a number: `def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)`. Here, `factorial(0)` provides the base case, returning 1 to stop further recursive calls .
Using Python's return statement with no arguments allows a function to exit without returning a value, implicitly returning `None`. This is particularly advantageous for functions that perform actions without needing to provide output, such as modifying global variables or input/output operations. Compared to explicitly returning a value, this approach emphasizes that the primary goal of the function is the side effect rather than the result. However, when output is needed from a function, returning specific values is essential for further processing .
In Python, the distinction between methods and functions lies in their association with objects. Methods are functions defined within a class and are associated with class instances, meaning they can operate on data contained within the object; they implicitly take the instance as their first parameter, typically named `self`. For example, in a class `Car`, `def drive(self)` is a method. Functions, on the other hand, are standalone blocks of code that perform a specific task and do not belong to any object context. This distinction is significant as methods provide a mechanism for defining behaviors and operations that are specific to the data structures they interact with, facilitating object-oriented programming .
To create a structured Python program for generating a student mark statement, the program can be divided into functions such as `basic()`, `marks()`, and `result()`. The `basic()` function can handle the input of student details. The `marks()` function can input or calculate marks for each subject, and the `result()` function can calculate the total and determine pass/fail status. This modular design enhances readability and maintainability. For instance: `def basic(): name = input('Enter student name: ') def marks(): return {'Math': 90, 'Science': 85} def result(marks): total = sum(marks.values()) return 'Pass' if total/len(marks) > 40 else 'Fail' .
Local variables are those defined within a function and have a scope limited to that function, meaning they cannot be accessed outside of it. For example, in `def foo(): x = 20`, `x` is a local variable. Global variables, on the other hand, are defined outside any function and can be accessed from any part of the program. For example, `y = 100` defined outside functions can be used anywhere. To modify a global variable inside a function, the `global` keyword is used .
String handling functions in Python provide a range of methods for string manipulation, making it easier to process and modify string data. Key functions include `len()`, which returns the length of a string; `str.upper()` and `str.lower()`, which convert strings to uppercase and lowercase respectively; `str.find()`, which locates a substring; and `str.replace()`, which replaces occurrences of a substring with another substring. For example, `"hello".upper()` transforms the string to "HELLO", and "hello world".replace("world", "Python") changes the string to "hello Python". These functions make handling strings straightforward and consistent .
Keyword arguments in Python allow you to specify argument values in a way that makes the function calls more readable by explicitly associating values with the parameter names, rather than relying on their positions. This improves code clarity and reduces errors, especially in functions with many parameters, as you don't need to worry about remembering the order of parameters. Also, keyword arguments enable functions to be called with default parameters more flexibly. This contrasts with positional arguments, where the order determined the argument association, often leading to mistakes in complex functions .
In Python, jumping statements are used to alter the flow of control in the program, and these include `break`, `continue`, and `pass`. The `break` statement terminates the loop execution and transfers control to the statement immediately following the loop. It is often used in loops to exit the loop when a certain condition is met. The `continue` statement skips the rest of the code inside the loop for the current iteration and jumps to the next iteration, useful for skipping specific loop cycles. The `pass` statement does nothing and is used as a placeholder for future code .
Looping statements in Python are constructs that enable repeated execution of a block of code as long as a specific condition is met. There are mainly two types of loops: `for` and `while`. The `for` loop iterates over a sequence (such as a list, string, or range) and executes the block of code a set number of times. For example, `for i in range(5): print(i)` will print numbers 0 to 4. The `while` loop continues to execute the block of code as long as the given condition is `True`, for example, `while x < 5: x += 1`. Proper use and understanding of these loops enable efficient problem-solving in programming .