Mohd Mohtashim has Published 251 Articles

Argument of an Exception in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:59:44

3K+ Views

An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary by exception. You capture an exception's argument by supplying a variable in the except clause as follows −try:    You do your operations here;    ...................... except ExceptionType, ... Read More

The try-finally Clause in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:58:31

2K+ Views

You can use a finally: block along with a try: block. The finally block is a place to put any code that must execute, whether the try-block raised an exception or not. The syntax of the try-finally statement is this −try:    You do your operations here;    ......................    Due ... Read More

Assertions in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:53:51

295 Views

An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program.The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more accurate, a raise-if-not statement). An expression is tested, ... Read More

Locating File Positions in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:48:32

877 Views

The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file.The seek(offset[, from]) method changes the current file position. The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from ... Read More

Reading and Writing Files in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:47:55

13K+ Views

The file object provides a set of access methods to make our lives easier. We would see how to use read() and write() methods to read and write files.The write() MethodThe write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.The write() method ... Read More

Opening and Closing Files in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:47:05

9K+ Views

Until now, you have been reading and writing to the standard input and output. Now, we will see how to use actual data files.Python provides basic functions and methods necessary to manipulate files by default. You can do most of the file manipulation using a file object.The open FunctionBefore you can read ... Read More

The import Statements in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:36:06

3K+ Views

You can use any Python source file as a module by executing an import statement in some other Python source file.SyntaxThe import has the following syntax −import module1[, module2[, ... moduleN]When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search ... Read More

Reading Keyboard Input in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:35:29

2K+ Views

Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are −raw_inputinputThe raw_input FunctionThe raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline).#!/usr/bin/python str = raw_input("Enter your input: ") print ... Read More

Printing to the Screen in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:34:53

368 Views

The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows −Example Live Demo#!/usr/bin/python print "Python is really a great language, ", "isn't ... Read More

The globals(), locals() and reload() Functions in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 30-Jan-2020 06:33:37

1K+ Views

The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called.If locals() is called from within a function, it will return all the names that can be accessed locally from that function.If globals() is called from within a ... Read More

Previous 1 ... 4 5 6 7 8 ... 26 Next
Advertisements