Found 10805 Articles for Python

classmethod() in Python

Pradeep Elance
Updated on 17-Oct-2019 12:03:20

352 Views

A class method receives the class itself as its first argument. This way we are able to call the method inside a class without first creating an instance from the class. We just use the decorator @classmethod before the declaration of the method contained in the class and then we can directly access the method. Below are the main features of classmethids.A classmethod is bound to a class and does not depend on the instantiation of a class to be used.A classmethod can modify a class which in turn propagates to all instances of the class.Using the classmethodIn the below ... Read More

Class & Instance Attributes in Python

Harshit Sachan
Updated on 13-Oct-2022 11:52:51

1K+ Views

In Python Programming Language, Class and Instance are two of object orientation's most crucial ideas. Instances are unique objects made in accordance with the class, whereas classes are templates. The procedure is the same for all objects, although the data may vary. In this tutorial, we will learn about classes in python, how to instantiate them, what attributes are, and the differences between class and instance attribute in Python. Let’s begin with definitions – What is a Class? Classes provide a means of bundling data and functionality together in python. Creating a new class creates a new type of ... Read More

Check if both halves of the string have same set of characters in Python

Pradeep Elance
Updated on 17-Oct-2019 11:56:42

200 Views

We can split a long string from the middle and check if the two halves are equal or not. The input string may have an odd or even number of characters. If it has an even number of characters, we divide the two halves by taking half of the length. But if the number of characters is odd then we ignore the middlemost character and then compare the remaining two halves.In the below program we create the two halves of the input string with above logic and thenExamplefrom collections import Counter def comparehalves(input_string):    str_len = len(input_string) # If number ... Read More

Check for balanced parentheses in Python

Pradeep Elance
Updated on 17-Oct-2019 11:53:22

741 Views

Many times we are required to find if an expression is balanced with respect to the brackets present in it. By balanced we mean for each left bracket there is a corresponding right bracket and the sequence of brackets is properly ordered. This has importance in writing a program or a mathematical expression where brackets are heavily used. In this topic, we will see how we can programmatically find out if an expression containing brackets is balanced or not.Through EliminationIn this method, we find out the innermost pair of brackets and replace them with null values. We keep doing this ... Read More

Unit Testing in Python Program using Unittest

Pavitra
Updated on 27-Sep-2019 11:30:13

110 Views

In this article, we will learn about the fundamentals of software testing by the help of unittest module available in Python 3.x. Or earlier. It allows automation, sharing of the setup and exit code for tests, and independent tests for every framework.In unit test, we use a wide variety of object oriented concepts. We will be discussing some majorly used concepts here.Testcase − It is response specific base class in accordance with a given set of inputs. We use base class of unittest i.e. “TestCase” to implement this operation.Testsuite − It is used to club test cases together and execute ... Read More

Twitter Sentiment Analysis using Python Program

Pavitra
Updated on 16-May-2022 12:23:58

323 Views

In this article, we will be learning about the twitter sentimental analysis. We will register for twitter oAuth API, install all the dependencies and finally write our sentimental analyzer script.An API(Application programming interface) is a gateway that allows you to access some servers(Twitter) internal functionality.The prerequisite is that we have a twitter account set up with verified phone number.After this, we visit the twitters website and tap on the create a new app icon. Now we fill all the credentials i.e. name and accept the developer agreement and finally click on create.Now our app is created , on the top ... Read More

Python program to print odd numbers in a list

Pavitra
Updated on 04-Jul-2020 13:00:29

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list iterable as input, we need to display odd numbers in the given iterable.Here we will be discussing three different approaches to solve this problem.Approach 1 − Using enhanced for loopExamplelist1 = [11, 23, 45, 23, 64, 22, 11, 24] # iteration for num in list1:    # check    if num % 2 != 0:       print(num, end = " ")Output11, 23, 45, 23, 11Approach 2 − Using lambda & filter functionsExample Live Demolist1 = [11, 23, 45, 23, ... Read More

Python Program to Print Numbers in an Interval

Pavitra
Updated on 27-Sep-2019 11:06:50

365 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven the starting and ending range of an interval. We need to print all the numbers in the interval given.A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.There are two for loops, first for loop is for getting the numbers in the interval and second loop for the checking whether the number is prime or not.Now let’s see the implementation.Example Live Demostart = 10 end = 29 for val in range(start, end + ... Read More

Python program to print negative numbers in a list

Pavitra
Updated on 04-Jul-2020 12:54:55

550 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list iterable, we need to print all the negative numbers in the list.Here we will be discussing three approaches for the given problem statement.Approach 1 − Using enhanced for loopExamplelist1 = [-11, 23, -45, 23, -64, -22, -11, 24] # iteration for num in list1:    # check    if num < 0:       print(num, end = " ")Output-11 -45 -64 -22 -11Approach 2 − Using filter & lambda functionExample Live Demolist1 = [-11, 23, -45, 23, -64, -22, -11, ... Read More

Python program to print even numbers in a list

Harshit Sachan
Updated on 13-Oct-2022 12:03:38

10K+ Views

Python Programming Language is one of the most efficient and user-friendly programming language and have endless uses and applications. Lists declared in Python are analogous to dynamically sized arrays in other programming languages (vector in C++ and ArrayList in Java). A list is simply a collection of items enclosed by [] and separated by commas. In this tutorial, we will learn about the solution and approach to find out all even numbers in a given list using Python. List is one of the most fundamental data structures in python. They are widely used and they store similar contiguous data. A ... Read More

Advertisements