Programming Articles - Page 2494 of 3363

Find size of a list in Python

Pradeep Elance
Updated on 07-Aug-2019 08:46:48

1K+ Views

A list is a collection data type in Python. The elements in a list are change able and there is no specific order associated with the elements. In this article we will see how to find the length of a list in Python. Which means we have to get the count of number of elements present in the list irrespective of whether they are duplicate or not.ExamplesIn the below example we take a list named as "days". We first find the length of the list using len() function. And then we add few more elements and check the length again ... Read More

Find length of a string in python (3 ways)

Pradeep Elance
Updated on 07-Aug-2019 08:44:46

2K+ Views

String is a python which is a series of Unicode characters. Once declared it is not changeable. In this article we'll see what are the different ways to find the length of a string.Using the len()This is the most straight forward way. Here we use a library function named len(). The string is passed as parameter to the function and we get a count of characters in the screen.Examplesstr ="Tutorials" print("Length of the String is:", len(str))OutputRunning the above code gives us the following result −Length of the String is: 9Using SlicingWe can use the string slicing approach to count the ... Read More

Find all the numbers in a string using regular expression in Python

Pradeep Elance
Updated on 07-Aug-2019 08:42:28

891 Views

Extracting only the numbers from a text is a very common requirement in python data analytics. It is done easily using the python regular expression library. This library helps us define the patterns for digits which can be extracted as substrings.ExamplesIn the below example we use the function findall() from the re module. The parameters to these function are the pattern which we want to extract and the string from which we want to extract. Please note with below example we get only the digits and not the decimal points or the negative signs.import re str=input("Enter a String with numbers: ... Read More

Finally keyword in Python

Pradeep Elance
Updated on 07-Aug-2019 08:40:44

391 Views

In any programming language we find a situation where exceptions are raised. Python has many inbuilt exception handling mechanisms. There are errors which are handled by this exception names. Python also has a block called finally which is executed irrespective of whether the exception is handled or not.Syntaxtry:    # main python Code.... except:    # It is optional block    # Code to handle exception finally:    # This Code that is always executedExampleIn the below code we see an exception called NameError. Here we create a code which refers to undeclared variables. Even though the exception is handled ... Read More

divmod() in Python and its application

Pradeep Elance
Updated on 07-Aug-2019 08:32:04

1K+ Views

The divmod() is part of python’s standard library which takes two numbers as parameters and gives the quotient and remainder of their division as a tuple. It is useful in many mathematical applications like checking for divisibility of numbers and establishing if a number is prime or not.SyntaxSyntax: divmod(a, b) a and b : b divides a a and b are integers or floatsExamplesIn the below example see the cases of both integers and floats. On the application of divmod() they give us a resulting tuple which is can also contain integers and float values.# with integers print("5 and 2 ... Read More

What is a Type-safe Enum in Java?

raja
Updated on 28-Aug-2025 14:47:43

2K+ Views

In Java, an enum is a special class that represents a "group of constants" that cannot be changed, just like a "final variables". Java provides an enum keyword to create an enum, and inside the class, the constants are written in uppercase letters and separated by commas (, ). Syntax to create an enum in Java: enum EnumName { CONST1, CONST2, CONST3 // ... } Here, enum: A predefined keyword used to define an enum. EnumName: The name ... Read More

List comprehension and ord() in Python to remove all characters other than alphabets

Pavitra
Updated on 07-Aug-2019 08:19:21

238 Views

In this article, we will learn about a program in which we can remove an all characters other than alphabets using the concept of list comprehension and ord() function in Python 3.x. Or earlier.Algorithm1.We Traverse the given string to check the charater. 2.Selection of characters is done which lie in the range of either [a-z] or [A-Z]. 3.Using the join function we print all the characters which pass the test together.Exampledef remchar(input): # checking uppercase and lowercase characters final = [ch for ch in input if (ord(ch) in range(ord('a'), ord('z')+1, 1)) or (ord(ch) in range(ord('A'), ord('Z')+1, 1))] return ... Read More

Count frequencies of all elements in array in Python using collections module

Pradeep Elance
Updated on 07-Aug-2019 08:18:37

386 Views

As python allows duplicate elements in a list we can have one element present multiple Times. The frequency of elements in a list indicates how many times an element occurs in a list. In this article we use the Counter function of the collections module to find out the frequency of each item in a list.SyntaxSyntax: Counter(list) Where list is an iterable in pythonExampleThe below code uses the Counter() to keep track of frequency and items() to iterate over each item in the result of counter function for printing in a formatted manner.from collections import Counter list = ['Mon', 'Tue', ... Read More

Count distinct elements in an array in Python

Pradeep Elance
Updated on 07-Aug-2019 08:15:52

653 Views

In a list in Python we may have duplicate elements. When we count the length of the list we get the total length including the duplicate elements. But in this article we will see how to get the total count of the distinct elements or unique elements in a list.ExampleIn the below example we use the counter() from the collections module. In this module a Counter is a dict subclass for counting hashable objects. Counter is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. So from the original list we ... Read More

Lambda and filter in Python Examples

Pavitra
Updated on 07-Aug-2019 08:15:51

3K+ Views

In this article, we will learn about lambda expressions and filter() functions in Python 3.x. Or earlier. These functions are present in the built-in Python standard library.What are lambda expressions?An inline function may be defined by the help of lambda expressions. A lambda expression consists of the lambda keyword followed by a comma seperated list of arguments and the expression to be evaluated using the list of arguments in the following format:Syntax Lambda arguments: expressionReturn value: The value computed by substituting arguments in the expressions.Lambda expressions are often abbreviated by the name of anonymous functions as they are not given any ... Read More

Advertisements