
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

2K+ Views
Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.Data member − A class variable or instance variable that holds data associated with a class and its objects.Function overloading − The assignment of more than ... Read More

5K+ 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, Argument: You can print value of Argument here...If you write the code to handle a single exception, you can have a variable follow the name of the exception in the except statement. If you are trapping multiple exceptions, you can have a variable follow the tuple of the exception.This ... Read More

11K+ Views
Suppose we have a string S. We have to find the longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB”.To solve this, we will follow these stepsDefine one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of S + 1for i in range 0 ... Read More

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

7K+ Views
In Python, by using different methods we can find the longest substring without repeating characters, such as by using the Brute force approach which involves checking all the Substrings, and also by using the advanced Sliding window Two Pointer approach, which uses a Hashmap to store the indexes. Some Common Methods Some of the common approaches in Python to find the longest Substring Without Repeating Characters are as follows. Brute Force: Checks all possible substrings and verifies if they have all unique characters in the string. Sliding Window with Set: This approach tracks characters in the current substring ... Read More

329 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

3K+ Views
Adding two numbers in Python is one of the most basic tasks, where we need to combine their values to get a total. In this article, we will see different ways to add two numbers in Python. Using Arithmetic Operator To add two numerical values in Python, we can use the addition operation, represented by the "+" symbol. It is one of the arithmetic operators in Python that performs the addition operation. Example Here is the basic example that adds the two numbers using the addition operator (+). # Assign values to the variables a = 7 b = ... Read More

1K+ Views
Locating file positions in Python means identifying the current position of the file pointer within an open file. Python provides the tell() and seek() methods of the file object for this task. Locating the File Position The tell() method tells you the current position within the file. In other words, it specifies that the next read or write will occur at that many bytes from the beginning of the file. Example The following example shows how to get the current file position using the tell() method. Initially, when a file is opened, the pointer is placed at the beginning. After ... Read More

18K+ Views
Reading data from a file and writing to a file can be easily done in Python by using the file object. The file object in Python provides a set of methods for handling files. These methods make our lives easier for interacting with files on our computer. Below, we will see how to read and write files in Python using the read() and write() methods. The write() Method The write() method writes any string to an opened file. It is important to note that Python strings can have binary data and not just text. This method does not add a ... Read More

11K+ Views
Until now, you have been reading and writing to the standard input and output. Now, we will see how to interact with actual data files. Python provides built-in functions and methods necessary to manipulate files by default. To work with a file in Python, we first need to create a file object. Using this object you can do most of the file manipulation such as reading, writing, or closing a file. Opening a File in Python Before 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, ... Read More