Found 10784 Articles for Python

The try-finally Clause in Python

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 to any exception, this may be skipped. finally:    This would always be executed.    ......................You cannot use else clause as well along with a finally clause.Example Live Demo#!/usr/bin/python try:    fh = open("testfile", "w")    fh.write("This is my test file for exception handling!!") finally:    print "Error: can\'t find file or read ... Read More

Longest Substring Without Repeating Characters in Python

Arnab Chakraborty
Updated on 27-Apr-2020 11:06:54

6K+ Views

Suppose we have a string. We have to find the longest substring without repeating the characters. So if the string is like “ABCABCBB”, then the result will be 3, as there is a substring that is repeating, of length 3. That is “ABC”.To solve this, we will follow these stepsset i := 0, j := 0, set one map to store informationans := 0while j < length of string sif s[j] is not present in map, or i > map[s[j]], thenans := max(ans, j – i + 1)map[s[j]] := jotherwisei := map[s[j]] + 1ans := max(ans, j – i + ... Read More

Assertions in Python

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

319 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, and if the result comes up false, an exception is raised.Assertions are carried out by the assert statement, the newest keyword to Python, introduced in version 1.5.Programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid ... Read More

Add Two Numbers in Python

Arnab Chakraborty
Updated on 27-Apr-2020 11:03:33

3K+ Views

Suppose we have given two non-empty linked lists. These two lists are representing two non-negative integer numbers. The digits are stored in reverse order. Each of their nodes contains only one digit. Add the two numbers and return the result as a linked list. We are taking the assumption that the two numbers do not contain any leading zeros, except the number 0 itself. So if the numbers are 120 + 230, then the linked lists will be [0 → 2 → 1] + [0 → 3 → 2] = [0 → 5 → 3] = 350.To solve this, we ... Read More

Locating File Positions in Python

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

921 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 where the bytes are to be moved.If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position as the reference position and if it is set to 2 then the end of the file would be taken as ... Read More

Reading and Writing Files in Python

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

14K+ 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 does not add a newline character ('') to the end of the string −SyntaxfileObject.write(string)Here, passed parameter is the content to be written into the opened file.Example#!/usr/bin/python # Open a file fo = open("foo.txt", "wb") fo.write( "Python is a great language.Yeah its great!!") # Close opend file fo.close()The above method would ... Read More

Opening and Closing Files in Python

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

10K+ 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 or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.Syntaxfile object = open(file_name [, access_mode][, buffering])Here are parameter details −file_name − The file_name argument is a string value ... Read More

Reading Keyboard Input in Python

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 "Received input is : ", strThis prompts you to enter any string and it would display same string on the screen. When I typed "Hello Python!", its output is like this −Enter your input: Hello Python Received input is : Hello PythonThe input FunctionThe input([prompt]) function is equivalent to raw_input, except that it assumes ... Read More

Printing to the Screen in Python

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

427 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 it?"OutputThis produces the following result on your standard screen −Python is really a great language, isn't it?

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

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 function, it will return all the names that can be accessed globally from that function.The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.When the module is imported into a script, the code in the top-level portion of a module is executed ... Read More

Advertisements