Server Side Programming Articles - Page 1914 of 2650

Creating Instance Objects in Python

Mohd Mohtashim
Updated on 29-Aug-2023 03:34:56

37K+ Views

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts."This would create first object of Employee class" emp1 = Employee("Zara", 2000) "This would create second object of Employee class" emp2 = Employee("Manni", 5000)You access the object's attributes using the dot (.) operator with object. Class variable would be accessed using class name as follows −emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCountExampleNow, putting all the concepts together − Live Demo#!/usr/bin/python class Employee:    'Common base class for all employees'    empCount = 0    def __init__(self, name, salary):       ... Read More

Creating Classes in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:09:31

2K+ Views

The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows −class ClassName: 'Optional class documentation string' class_suiteThe class has a documentation string, which can be accessed via ClassName.__doc__.The class_suite consists of all the component statements defining class members, data attributes and functions.ExampleFollowing is the example of a simple Python class −class Employee:    'Common base class for all employees'    empCount = 0    def __init__(self, name, salary):       self.name = name       self.salary = salary       Employee.empCount += 1    def displayCount(self): ... Read More

OOP Terminology in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:08:18

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

String to Integer (atoi) in C++

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

735 Views

Suppose we have to design a module, that first discards as many whitespace characters as necessary until the first non-whitespace character is reached. After that, starting from this character, it takes an optional initial plus sign or minus sign followed by as many numerical digits, and interprets them as a numerical value.When the first sequence of non-whitespace characters in str is not a valid integral number, or when no such sequence exists because either str is empty or it contains only whitespaces, no conversion will be performed.So if the input is like “-45”, the output will be -45.To solve this, ... Read More

ZigZag Conversion in C++

Arnab Chakraborty
Updated on 27-Apr-2020 11:25:10

1K+ Views

Suppose the string is like " IWANTTOLEARNCODE". This string is written in a zigzag way on a given number of rows say n. So the pattern is looking like thisITAOWNOERCDALNEWhen we read the line like − "ITAOWNOERCDALNE"So we have to create one module that can perform this kind of operation by taking the string and the number of rows.To solve this, we will follow these stepswhen n = 1, then return screate an array of strings arr of size nrow := 0, and down := truefor i in range 0 to size of string – 1insert s[i] at the end ... Read More

Argument of an Exception in Python

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

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

Longest Palindromic Substring in Python

Arnab Chakraborty
Updated on 27-Apr-2020 11:19:01

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

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

SaiKrishna Tavva
Updated on 13-Nov-2024 14:25:57

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

Assertions in Python

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

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

Advertisements