Python Articles - Page 912 of 1048

floor() and ceil() function Python

Pradeep Elance
Updated on 08-Aug-2019 06:53:20

2K+ Views

These two methods are part of python math module which helps in getting the nearest integer values of a fractional number.floor()It accepts a number with decimal as parameter and returns the integer which is smaller than the number itself.SyntaxSyntax: floor(x) Where x is a numeric valueExample of floor()In the below example we take different types of numeric values like, integer, positive decimal and negative decimal and apply the floor function to them. We get the nearest integer which is less than the numeric value supplied.import math x, y, z = 21 , -23.6 , 14.2 print("The value of ", x, ... Read More

float() in Python

Pradeep Elance
Updated on 07-Aug-2019 08:55:11

232 Views

Float method is part of python standard library which converts a number or a string containing numbers to a float data type. There are following rules when a string is considered to be valid for converting it to a float.The string must have only numbers in it.Mathematical operators between the numbers can also be used.The string can represent NaN or infThe white spaces at the beginning and end are always ignored.ExampleThe below program indicates how different values are returned when float function is applied.n = 89 print(type(n)) f = float(n) print(type(f)) print("input", 7, " with float function becomes ", float(7)) ... Read More

Finding Mean, Median, Mode in Python without Libraries

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

11K+ Views

Mean, Median and Mode are very frequently used statistical functions in data analysis. Though there are some python libraries.Finding MeanMean of a list of numbers is also called average of the numbers. It is found by taking the sum of all the numbers and dividing it with the count of numbers. In the below example we apply the sum() function to get the sum of the numbers and th elen() function to get the count of numbers.Examplenum_list = [21, 11, 19, 3, 11, 5] # FInd sum of the numbers num_sum = sum(num_list) #divide the sum with length of the ... Read More

Find the k most frequent words from data set in Python

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

1K+ Views

If there is a need to find 10 most frequent words in a data set, python can help us find it using the collections module. The collections module has a counter class which gives the count of the words after we supply a list of words to it. We also use the most_common method to find out the number of such words as needed by the program input.ExamplesIn the below example we take a paragraph, and then first create a list of words applying split(). We will then apply the counter() to find the count of all the words. Finally ... Read More

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

856 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

373 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

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

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

223 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