Found 10476 Articles for Python

Python Get a list as input from user

Pradeep Elance
Updated on 09-Jul-2020 13:46:54

10K+ Views

In this article we will see you how to ask the user to enter elements of a list and finally create the list with those entered values.With format and inputThe format function can be used to fill in the values in the place holders and the input function will capture the value entered by the user. Finally, we will append the elements into the list one by one.ExamplelistA = [] # Input number of elemetns n = int(input("Enter number of elements in the list : ")) # iterating till the range for i in range(0, n):    print("Enter element No-{}: ... Read More

Python Generate successive element difference list

Pradeep Elance
Updated on 09-Jul-2020 13:44:31

457 Views

In this as its elements article we will see how to find the difference between the two successive elements for each pair of elements in a given list. The list has only numbers as its elements.With IndexUsing the index of the elements along with the for loop, we can find the difference between the successive pair of elements.Example Live DemolistA = [12, 14, 78, 24, 24] # Given list print("Given list : ", listA) # Using Index positions res = [listA[i + 1] - listA[i] for i in range(len(listA) - 1)] # printing result print ("List with successive difference in elements ... Read More

Python Generate random string of given length

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

630 Views

In this article we will see how how to generate a random string with a given length. This will be useful in creating random passwords or other programs where randomness is required.With random.choicesThe choices function in random module can produce strings which can then be joined to create a string of given length.Example Live Demoimport string import random # Length of string needed N = 5 # With random.choices() res = ''.join(random.choices(string.ascii_letters+ string.digits, k=N)) # Result print("Random string : ", res)OutputRunning the above code gives us the following result −Random string : nw1r8With secretsThe secrets module also has choice method which ... Read More

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

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

471 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

210 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

3K+ 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

594 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

2K+ 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

Advertisements