Found 10783 Articles for Python

Python Generate random numbers within a given range and store in a list

Pradeep Elance
Updated on 09-Jul-2020 13:39:55

375 Views

In this article we will see how to generate Random numbers between a pair of numbers and finally store those values into list.We use a function called randint. First let's have a look at its syntax.Syntaxrandint(start, end) Both start and end should be integers. Start should be less than end.In this example we use the range function in a for lowith help of append we generate and add these random numbers to the new empty list.Example Live Demoimport random def randinrange(start, end, n):    res = []    for j in range(n):       res.append(random.randint(start, end))    return res # ... Read More

Python fsum() function

Pradeep Elance
Updated on 09-Jul-2020 13:37:18

200 Views

fsum() finds the sum between a given range or an iterable. It needs the import of the math library. Its widely used in mathematical calculations.SyntaxBelow is the syntax of the function.maths.fsum( iterable ) The iterable can be a range, array , list. Return Type : It returns a floating point number.Below are the example on how fsum function on a single number or on group of elements in a list.Example Live Demoimport math # Using range print(math.fsum(range(12))) # List of Integers listA = [5, 12, 11] print(math.fsum(listA)) # List of Floating point numbers listB = [9.35, 6.7, 3] print(math.fsum(listB))OutputRunning the above ... Read More

Python frexp() Function

Pradeep Elance
Updated on 09-Jul-2020 13:35:21

156 Views

This function is used to find the mantissa and exponent of a number. It is heavily used in mathematical calculations. In this article we will see the various ways it can be used in python programs.SyntaxBelow is the syntax and its description for using this function.math.frexp( x ) Parameters: Any valid number (positive or negative). Returns: Returns mantissa and exponent as a pair (m, e) value of a given number x. Exception: If x is not a number, function will return TypeErrorSimple ExpressionsBelow is an example program where the function is applied directly on given numbers to give us mantissa ... Read More

Python Categorize the given list by string size

Pradeep Elance
Updated on 09-Jul-2020 13:30:19

136 Views

Let's consider a list containing many strings of different lengths. In this article we will see how to club those elements into groups where the strings are of equal length in each group.With for loopWe design a for loop which will iterate through every element of the list and happened it only to the list where its length matches with the length of existing element.Example Live DemolistA = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Given list print("Given list : ", listA) # Categorize by string size len_comp = lambda x, y: len(x) == len(y) res = [] for sub_list in listA: ... Read More

Correlation and Regression in Python

Pradeep Elance
Updated on 09-Jul-2020 13:21:47

2K+ Views

Correlation refers to some statistical relationships involving dependence between two data sets. While linear regression is a linear approach to establish the relationship between a dependent variable and one or more independent variables. A single independent variable is called linear regression whereas multiple independent variables is called multiple regression.CorrelationSimple examples of dependent phenomena include the correlation between the physical appearance of parents and their offspring, and the correlation between the price for a product and its supplied quantity.We take example of the iris data set available in seaborn python library. In it we try to establish the correlation between the ... Read More

Converting an image to ASCII image in Python

Pradeep Elance
Updated on 09-Jul-2020 13:10:56

474 Views

In this article we we want to convert a given images into a text bases image also called ASCII image.Below is the Python program which will take an input imagee and various functions to convert them into grayscale picture and then apply the ASCII characters to create different patterns insert the image. Finally the emails comes text based image this is a series of plane ASCII characters.Examplefrom PIL import Image import os ASCII_CHARS = [ '#', '?', '%', '.', 'S', '+', '.', '*', ':', ', ', '@'] def resize_image(image, new_width=25):    (input__width, input__height) = image.size    aspect_ratio = input__height/float(input__width)   ... Read More

Boolean list initialization in Python

Pradeep Elance
Updated on 09-Jul-2020 13:04:18

1K+ Views

There are scenarios when we need to get a list containing only the Boolean values like true and false. In this article how to create list containing only Boolean values.With rangeWe use range function giving it which is the number of values we want. Using a for loop we assign true or false today list as required.Example Live Demores = [True for i in range(6)] # Result print("The list with binary elements is : " ,res)OutputRunning the above code gives us the following result −The list with binary elements is : [True, True, True, True, True, True]With * operatorThe * operator ... Read More

Binary element list grouping in Python

Pradeep Elance
Updated on 09-Jul-2020 12:46:49

170 Views

Suppose we have a list of lists in which each sublist has two elements. One element of each sublist is common across many other subjects of the list. We need to create a final list which will show sublists grouped by common elements.With set and mapIn the given list the first element is a string and the second element is a number. So we create a temp list which will hold the second element of each sublist. Then we compare is sublist with each of the element in the temp list and designer follow to group them.Example Live DemolistA = [['Mon', ... Read More

Alternate element summation in list (Python)

Pradeep Elance
Updated on 09-Jul-2020 12:44:44

625 Views

Given a list of numbers in this article we are going to calculate the sum of alternate elements in that list.With list slicing and rangeEvery second number and also use the range function along with length function to get the number of elements to be summed.Example Live DemolistA = [13, 65, 78, 13, 12, 13, 65] # printing original list print("Given list : " , str(listA)) # With list slicing res = [sum(listA[i:: 2]) for i in range(len(listA) // (len(listA) // 2))]    # print result    print("Sum of alternate elements in the list : ", res)OutputRunning the above code gives ... Read More

Add the occurrence of each number as sublists in Python

Pradeep Elance
Updated on 09-Jul-2020 12:42:17

126 Views

We have a list whose elements are numeric. Many elements are present multiple times. We want to create sub list so the frequency of each element along with the elements itself.With for and appendIn this approach we will compare each element in the list with every other elements after it. If there is a match then count will be incremented and both the element and the count will be made into a subsist. List will be made which should contain subsists showing every element and its frequency.Example Live Demodef occurrences(list_in):    for i in range(0, len(listA)):       a = ... Read More

Advertisements