Found 10805 Articles for Python

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

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

594 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

263 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

Extending a list in Python (5 different ways)

Pradeep Elance
Updated on 07-Aug-2019 08:38:07

2K+ Views

Lists are the most frequently used data structures of python. When we want to add more elements to a list, the extension of list happens. This can be done in following 5 different ways.Using the Plus operatorHere we simply add the elements of a new list using the + operator. The behavior is similar to how we modify the values of a variable.Exampleslist = ['Mon', 'Tue', 'Wed'] list = list + ['Thu', 'Fri'] print(list)OutputRunning the above code gives us the following result −['Mon', 'Tue', 'Wed', 'Thu', 'Fri']Using SlicingWe can use slicing to add elements at the end of a list. ... 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

Mathematical Functions in Python - Special Functions and Constants

Pavitra
Updated on 03-Jul-2020 07:59:03

196 Views

In this article, we will learn about special functions and constants available in the math module in the Python Standard Library.Here we will discuss some constants like −pieinfNantauAnd some functions likeGammaIsinfIsnanisfinite()erf()Let's discuss constants and their respective values −pi3.141592…..e2.718281…...inf6.283185…...nantauNow let’s discuss some special functions and their implementation −Gamma − return the value of gamma(n)Isinf − checks whether the value of the function is infinity or not.Isnan − check whether the return value is a number or not.Isfinite − return True if the value is neither an infinity or a nan false otherwiseErf − returns the error function of x.Now let;’s take ... Read More

Different ways to clear a list in Python

Pradeep Elance
Updated on 07-Aug-2019 08:27:03

5K+ Views

Clearing up all the elements in a python list can be done in many ways. Below are the some of the methods which are implemented to achieve this.using clear()This function is a part of standard library and empties the python list completely.Syntax: list_name.clear() list_name is the name of the list supplied byExampleIn the below example we take a list and apply the clear (). The result is an empty list.list = ['Mon', 'Tue', 'Wed', 'Thu'] print("Existing list", list) #clear the list list.clear() print("After clearing the list") print(list)OutputRunning the above code gives us the following result −Existing list ['Mon', 'Tue', 'Wed', ... Read More

Logic Gates in Python

Pavitra
Updated on 07-Aug-2019 08:26:41

2K+ Views

In this article, we will learn about Logic Gates in Python. Let’s look at each of the logic gates in Python in detail with some easy examples.All of us are quite familiar while implementing logic gates in the processing of electrical signals and are widely used in the electrical and electronics industry. They are used in the diodes and transistors so that we can design by proper alignment of these electronic devices. In this article we will learn about the implementation of some basic gates ‘and‘, ‘or‘ ,’not‘ , ‘nand‘ ,’nor‘ in Python 3.x or earlier.These gates can be implemented ... Read More

List Methods in Python - in, not in, len(), min(), max()

Pavitra
Updated on 01-Jul-2020 07:19:09

17K+ Views

In this article, we will learn about various types of list methods available to us in Python 3.x. Or earlier. These operators allow us to perform the basic operations on the list content.In & Not in operators“in” operator  − This operator is used to check whether an element is present in the passed list or not. Returns true if the element is present in the list otherwise returns false.“not in” operator  − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the ... Read More

delattr() and del() in Python

Pradeep Elance
Updated on 07-Aug-2019 08:22:01

351 Views

These two functions are used to remove attributes from classes. The delattr() allows dynamoc deletion of attribute while the del() is more efficient explicit in deleting the attribute.Using delattr()Syntax: delattr(object_name, attribute_name) Where object name is the name of the object, instantiated form the class. Attribute_name is the name of the attribute to be deleted.ExampleIn the below example we consider a class named custclass. It has the id of the customers as its attributes. Next we instantiate the class as an object named customer and print its attriutes.class custclass:    custid1 = 0    custid2 = 1    custid3 = 2 ... Read More

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

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

122 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

Advertisements