Found 10805 Articles for Python

How to clear screen in python?

Pradeep Elance
Updated on 08-Aug-2019 06:38:47

5K+ Views

In Python sometimes we have link the output and we want to clear the screen in the cell prompt we can clear the screen by pressing Control + l . But there are situations when we need to clear the screen programmatically depending on the amount of output from the program and how we want to format the output. In such case we need to put some commands in the python script which will clear the screen as and when needed by the program.We need the system() from the OS module of python to clear up the screen. For different ... Read More

Generating random number list in Python

Pradeep Elance
Updated on 22-Aug-2023 00:49:42

153K+ Views

There is a need to generate random numbers when studying a model or behavior of a program for different range of values. Python can generate such random numbers by using the random module. In the below examples we will first see how to generate a single random number and then extend it to generate a list of random numbers.Generating a Single Random NumberThe random() method in random module generates a float number between 0 and 1.Exampleimport random n = random.random() print(n)OutputRunning the above code gives us the following result −0.2112200Generating Number in a RangeThe randint() method generates a integer between ... Read More

gcd() function Python

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

2K+ Views

Greatest common divisor or gcd is a mathematical expression to find the highest number which can divide both the numbers whose gcd has to be found with the resulting remainder as zero. It has many mathematical applications. Python has a inbuilt gcd function in the math module which can be used for this purpose.gcd()It accepts two integers as parameter and returns the integer which is the gcd value.SyntaxSyntax: gcd(x, y) Where x and y are positive integers.Example of gcd()In the below example we print the result of gcd of a pair of integers.import math print ("GCD of 75 and 30 ... Read More

frozenset() in Python

Pradeep Elance
Updated on 08-Aug-2019 06:31:31

257 Views

This function helps in converting a mutable list to an immutable one. This is needed when we have declared a list whose items are changeable but after certain steps we want to stop allowing the elements in it to change. In such scenario, we apply the frozenset() function as shown below.SyntaxSyntax: frozenset(iterable_object_name)In the below example we take a list, change its element and print it. Then in the next step we apply the frozenset function, and try changing the element again. In the second step we get the error showing that the list can not be modified anymore.Example# Before applying ... Read More

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

217 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

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

819 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

821 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

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

Advertisements