Q.What Are The Uses of File Object: Except (Exception1, Exception2, Exceptionn) As E
Q.What Are The Uses of File Object: Except (Exception1, Exception2, Exceptionn) As E
ZeroDivisonErro Raised when division or modulo by zero takes place for all numeric types.
r
IOError Raised when an input/ output operation fails, such as the print statement or the
open() function when trying to open a file that does not exist. Also raised for
operating system-related errors.
RuntimeError Raised when a generated error does not fall into any category.
We can import the definitions inside a module to another module or the interactive interpreter in
Python.
We use the import keyword to do this. To import our previously defined module example, we
type the following in the Python prompt.
Example: import example
3marks
Q.Give a brief description of built in attributes related to file objects.
A file object has a lot of attributes. You can see a list of all methods and attributes of the file
object here: https://docs.python.org/2.4/lib/bltin-file-objects.html. Following are some of the
most used file object methods −
next() - When a file is used as an iterator, typically in a for loop (for example, for line in f:
print line), the next() method is called repeatedly. This method returns the next input
line, or raises StopIteration when EOF is hit.
seek(offset[, whence]) - Set the file's current position, like stdio's fseek(). The whence
argument is optional and defaults to 0 (absolute file positioning); other values are 1
(seek relative to the current position) and 2 (seek relative to the file's end).
name - If the file object was created using open(), the name of the file. Otherwise, some
string that indicates the source of the file object
Mutable types:
The data types or data structures which can be changed or manipulated is known as mutable
types
immutable :
The data types or data structures which can not be changed or manipulated is known as
mutable types
Context managers allow you to allocate and release resources precisely when you want
to. The most widely used example of context managers is the with statement. Suppose
you have two related operations which you’d like to execute as a pair, with a block of
code in between. Context managers allow you to do specifically that. For example:
The above code opens the file, writes some data to it and then closes it. If an error
occurs while writing the data to the file, it tries to close it. The above code is equivalent
to:
as we can notice that lot of code has been reduced which makes it less
complicated.
Q.Write syntax to create exceptions. Write 3 examples
In Python, users can define custom exceptions by creating a new class. This exception
class has to be derived, either directly or indirectly, from the built-in Exception class.
Most of the built-in exceptions are also derived from this class.
Example
# define Python user-defined exceptions
class Error(Exception):
"""Base class for other exceptions"""
pass
class ValueTooSmallError(Error):
"""Raised when the input value is too small"""
pass
class ValueTooLargeError(Error):
"""Raised when the input value is too large"""
pass
o/p
Enter a number: 12
This value is too large, try again!
Enter a number: 0
This value is too small, try again!
Enter a number: 8
This value is too small, try again!
Enter a number: 10
Congratulations! You guessed it correctly.
Although there are various unique namespaces defined, we may not be able to access all of
them from every part of the program. The concept of scope comes into play.
Scope is the portion of the program from where a namespace can be accessed directly without
any prefix.
Unit 3
Q.Explain about the following re patern (a)[dn]ot (b)[A-Za-z]\w*
[dn]ot - ”d” or ”n”, followed by an ”o” and, at most one ”t” after that; do, not, dot, not.
A-Za-z]\w* - a to z or A to z followed by a non word character atleast 0 or more times
Explain about match() and how can we match more than one string
The re.match() is a function of re module in python. These function very
efficient and fast for searching in strings. The function searches for some
substring in a string and returns a match object if found, else it returns none.
re.match() searches only from the beginning of the string and return match
object if found. But if a match of substring is found somewhere in the middle of
the string, it returns none.
compile() method is used if the Python code is in string form or is an AST object, and you want
to change it to a code object. The code object returned by compile() method can later be called
using methods like: exec() and eval() which will execute dynamically generated Python code.
The Python split() method divides a string into a list. Values in the resultant list
are separated based on a separator character. The separator is a whitespace by
default. ... The split() method allows you to break up a string into a list of
substrings, based on the separator you specify.