- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rajendra Dharmkar has Published 453 Articles
Rajendra Dharmkar
179 Views
% is a string formatting operator or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to using the sprintf() in the C language. For example, >>> lang = "Python" ... Read More
Rajendra Dharmkar
162 Views
To parse a string to int, you can use the following:try: print int('112') except ValueError: print 'Cannot parse'This will give you the output:112To parse a string to float, you can use the following:try: print float('112.15') except ValueError: print 'Cannot parse'This will give ... Read More
Rajendra Dharmkar
2K+ Views
The code can be written as follows to catch the exceptiona, b=5, 0 try: if b != 0: print a/b else: a/b raise ZeroDivisionError except Exception as e: print eWe get the following outputC:/Users/TutorialsPoint1/~.py integer division or modulo by zero
Rajendra Dharmkar
607 Views
OSError serves as the error class for the os module, and is raised when an error comes back from an os-specific function.We can re-write the given code as follows to handle the exception and know its type.#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except ... Read More
Rajendra Dharmkar
952 Views
The given code is rewritten to catch the exceptionimport sys import threading import time import Queue def thread(args1, stop_event, queue_obj): print "start thread" stop_event.wait(12) if not stop_event.is_set(): try: raise Exception("boom!") except Exception: queue_obj.put(sys.exc_info()) pass try: queue_obj = Queue.Queue() t_stop = threading.Event() t = threading.Thread(target=thread, args=(1, t_stop, queue_obj)) t.start() time.sleep(15) print ... Read More
Rajendra Dharmkar
144 Views
In Python 3.x, the code is subject to exception chaining and we get the output as followsC:/Users/TutorialsPoint1/~.py Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 2, in 1/0 ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line ... Read More
Rajendra Dharmkar
735 Views
We can write custom exception classes with error codes and error messages as follows:class ErrorCode(Exception): def __init__(self, code): self.code = code try: raise ErrorCode(401) except ErrorCode as e: print "Received error with code:", e.codeWe get outputC:/Users/TutorialsPoint1/~.py Received error ... Read More
Rajendra Dharmkar
216 Views
During the handling of one exception ‘A’, it is possible that another exception ‘B’ may occur. In Python 2.0 versions, if this happens, exception B is propagated outward and exception A is lost. It is useful to know about both exceptions in order to debug the problem.Sometimes it is useful ... Read More
Rajendra Dharmkar
120 Views
For the above module, we need to prepare following setup.py script −from distutils.core import setup, Extension setup(name='helloworld', version='1.0', \ ext_modules=[Extension('helloworld', ['hello.c'])])Now, we use the following command, $ python setup.py installOnce we install the extension, we would be able to import and call that extension in our Python script test.py and ... Read More
To Continue Learning Please Login
Login with Google