Manogna has Published 66 Answers

Can we explicitly define datatype in a Python Function?

Manogna

Manogna

Updated on 13-Feb-2020 06:27:43

In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument.If we explicitly define datatype in a python function, ... Read More

How can we return a list from a Python function?

Manogna

Manogna

Updated on 13-Feb-2020 06:25:26

There are so many ways we can return a list from a python function. One such function is given below.Exampledef retList():     list = []     for i in range(0,10):         list.append(i)     return list a = retList() print aOutput[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

How can we return a tuple from a Python function?

Manogna

Manogna

Updated on 13-Feb-2020 06:24:23

We can return a tuple from a python function in many ways. For the given tuple, we define a  function as shown below.Exampledef foo():     str = "tutorialspoint"     x   = 30     return (str, x);             print foo()Output('tutorialspoint', 30)Read More

How to implement a custom Python Exception with custom message?

Manogna

Manogna

Updated on 13-Feb-2020 05:11:20

For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value ... Read More

Explain try, except and finally statements in Python.

Manogna

Manogna

Updated on 13-Feb-2020 05:03:24

In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.If an exception occurs, the rest of the try block is skipped and the except clause is executed.Exampletry: 'apple' + 6 except Exception: ... Read More

How to handle an exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 05:00:21

The simplest way to handle exceptions in python is using the "try-except" block.Exampletry: fob = open("test.txt", "r") fob.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find the file or read data" else: print "Write operation is performed successfully on the file" fob.close()OutputError: can't find the ... Read More

How to catch KeyError Exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:59:45

A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.Exampleimport sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())Output(<type 'exceptions.KeyError'>, KeyError('c', ), <traceback object at  0x0000000003203748>)Read More

What is the correct way to pass an object with a custom exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:56:50

In given code, a custom exception FooException has been created which is a subclass of the super class Exception. We will pass a string object to the custom exception as followsExample#foobar.py class FooException(Exception): def __init__(self, text, *args): super ( FooException, self ).__init__ ( text, *args ) self.text = text try: ... Read More

How to pass argument to an Exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:54:48

An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary from exception to exception. You capture an exception's argument by supplying a variable in the except clause as followsExampletry: b=float(56+78/0) except Exception, Argument: print 'This is the ... Read More

How to use the try-finally clause to handle exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:52:28

So far the try statement had always been paired with except clauses. But there is another way to use it as well. The try statement can be followed by a finally clause. Finally clauses are called clean-up or termination clauses, because they must be executed under all circumstances, i.e. a ... Read More

Advertisements