Python Lab Viva Questions and Answers
Python Lab Viva Questions and Answers
Python uses open() function along with read(), write(), and append() methods to handle file operations. To read from a file, the mode 'r' is used, e.g., file = open('file.txt', 'r'). For writing, 'w' or 'a' can be used based on whether you wish to overwrite or append, e.g., file.write('new content'). It's crucial to always close the file using file.close() or use the 'with open() as' context manager for automatic management .
Regular expressions in Python are powerful tools for pattern matching and text processing. They are widely used in file processing tasks to search, replace, and manipulate strings based on specific patterns. Python's 're' module provides support for regular expressions enabling tasks like extracting emails from text files or formatting data correctly with minimal code .
Lists in Python are ordered collections of elements accessed by integer indices, e.g., list = [1, 2, 3]. They are highly versatile and can store mixed data types. Dictionaries, however, store key-value pairs with unique keys for fast lookups, e.g., dict = {'key1': 'value1'}. Dictionaries are optimal for associative arrays where retrieval by key is frequent, while lists are more suitable for ordered sequences .
String concatenation in Python refers to joining one string with another, which can be done using the '+' operator, e.g., 'Hello' + ' World' results in 'Hello World'. String replication, on the other hand, involves repeating a string multiple times using the '*' operator, such as 'Hi' * 3 which yields 'HiHiHi' .
The 'with open() as' syntax in Python provides a more efficient way to handle files. It ensures that files are properly closed after their suite finishes, even if an exception is raised, thereby reducing the risk of file corruption and ensuring resources are released cleanly. However, the automatic file closure can obscure exactly when the file closes, sometimes making it unsuitable in complex workflows requiring precise resource management .
Python provides a variety of string manipulation functions. Examples include str.upper() to convert all characters to uppercase, str.lower() for converting to lowercase, str.strip() to remove whitespace, and str.replace() for replacing specified parts of a string with another string. These functions are widely used for formatting and cleaning string data .
In Python, data types define the kind of data a variable can hold. Common data types include: integers, which hold numeric values like 5; floats, which hold decimal numbers such as 10.5; strings, which contain sequences of characters, for example 'hello'; and booleans, which represent True or False values .
In Python, user input validation is typically performed using conditional statements combined with input functions to ensure the input meets certain criteria before processing it. For example, a loop can repeatedly prompt for input until a valid value is entered. Also, try/except blocks can catch exceptions to handle invalid inputs without crashing the program .
Exception handling in Python, implemented using try, except, finally blocks, increases program reliability by allowing programs to continue execution or gracefully terminate in the face of errors. Common practices include catching specific exceptions instead of general ones, using finally blocks for cleanup, and providing informative descriptions of the error for debugging purposes. This helps prevent abrupt crashes and aids in debugging .
A recursive function in Python is one that calls itself in order to divide a problem into smaller, more manageable sub-problems. An example is the computation of factorials: a function factorial(n) calls itself as factorial(n-1) until it reaches the base case, factorial(0), which is 1. The recursion stops when the base case is reached, preventing infinite loops .