Found 10805 Articles for Python

degrees() and radians() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:08:06

549 Views

The measurements of angles in mathematics are done using these two units of measurement called degree and radian. They are frequently used in math calculations involving angles and need conversion from one value to another. In python we can achieve these conversions using python functions.degrees() FunctionThis function takes radian value as parameter and return the equivalent value in degrees. The return is a float value.Example Live Demoimport math # Printing degree equivalent of radians. print("1 Radians in Degrees : ", math.degrees(1)) print("20 Radians in Degrees : ", math.degrees(20)) print("180 Radians in Degrees : ", math.degrees(180))OutputRunning the above code gives us the ... Read More

Counting the frequencies in a list using dictionary in Python

Pradeep Elance
Updated on 23-Aug-2019 11:53:22

2K+ Views

In this article we develop a program to calculate the frequency of each element present in a list.Using a dictionaryHere we capture the items as the keys of a dictionary and their frequencies as the values.Example Live Demolist = ['a', 'b', 'a', 'c', 'd', 'c', 'c'] frequency = {} for item in list:    if (item in frequency):       frequency[item] += 1    else:       frequency[item] = 1 for key, value in frequency.items():    print("% s -> % d" % (key, value))OutputRunning the above code gives us the following result −a -> 2 b -> 1 c ... Read More

chr () in Python

Pradeep Elance
Updated on 23-Aug-2019 11:42:08

615 Views

This function return the string representing a character whose Unicode code point is the integer supplied as parameter to this function. For example, chr(65) returns the string 'A', while chr(126) returns the string '~'.SyntaxThe syntax of the function is as below.chr(n) where n is an integer valueExampleThe below program shows how the chr() is used. We supply various integer values as parameter and get back the respective characters. Live Demo# Printing the strings from chr() function print(chr(84), chr(85), chr(84), chr(79), chr(82))Running the above code gives us the following result −T U T O RUsing a series of numbersWe can take help ... Read More

Change Data Type for one or more columns in Pandas Dataframe

Pradeep Elance
Updated on 23-Aug-2019 11:30:14

2K+ Views

Many times we may need to convert the data types of one or more columns in a pandas data frame to accommodate certain needs of calculations. There are some in-built functions or methods available in pandas which can achieve this.Using astype()The astype() method we can impose a new data type to an existing column or all columns of a pandas data frame. In the below example we convert all the existing columns to string data type.Example Live Demoimport pandas as pd #Sample dataframe df = pd.DataFrame({    'DayNo': [1, 2, 3, 4, 5, 6, 7],    'Name': ['Sun', 'Mon', 'Tue', 'Wed', ... Read More

Calculate n + nn + nnn + ? + n(m times) in Python

Pradeep Elance
Updated on 23-Aug-2019 10:47:57

331 Views

There are a variety of mathematical series which python can handle gracefully. One such series is a series of repeated digits. Here we take a digit and add it to the next number which has two such digits and again the next number is three such digits and so on. Finally, we calculate the sum of all such numbers in the series.ApproachWe take a digit and convert it to string. Then concatenate two such strings to get the double digit number and keep concatenating to get higher numbers of such digits. Then we implement a recursive function to add all ... Read More

Binary Search (bisect) in Python

Arnab Chakraborty
Updated on 02-Jul-2020 06:15:22

1K+ Views

Here we will see the bisect in Python. The bisect is used for binary search. The binary search technique is used to find elements in sorted list. The bisect is one library function.We will see three different task using bisect in Python.Finding first occurrence of an elementThe bisect.bisect_left(a, x, lo = 0, hi = len(a)) this function is used to return the left most insertion point of x in a sorted list. Last two parameters are optional in this case. These two are used to search in sublist.Examplefrom bisect import bisect_left def BinSearch(a, x):    i = bisect_left(a, x)   ... Read More

10 Interesting Python Cool Tricks

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

4K+ Views

With increase in popularity of python, more and more features are becoming available for python coding. Using this features makes writing the code in fewer lines and cleaner. In this article we will see 10 such python tricks which are very frequently used and most useful.Reversing a ListWe can simply reverse a given list by using a reverse() function. It handles both numeric and string data types present in the list.ExampleList = ["Shriya", "Lavina", "Sampreeti" ] List.reverse() print(List)OutputRunning the above code gives us the following result −['Sampreeti', 'Lavina', 'Shriya']Print list elements in any orderIf you need to print the values ... Read More

howdoi in Python

Pradeep Elance
Updated on 08-Aug-2019 06:58:23

208 Views

Create a Python ListExampleC:\Py3Project>howdoi create a python listOutputRunning the above code gives us the following result −>>> l = [None] * 10 >>> l [None, None, None, None, None, None, None, None, None, None]Printing Today’s DateExamplec:\python3>howdoi print today's date in pythonOutputRunning the above code gives us the following result −for date in mylist : print str(date)Examplec:\python3>howdoi create fibonnaci series in pythonOutputRunning the above code gives us the following result −def F(n):    if n == 0: return 0    elif n == 1: return 1    else: return F(n-1)+F(n-2)Examplec:\python3>howdoi use calendar in javascriptOutputRunning the above code gives us the following ... Read More

How to print without newline in Python?

Pradeep Elance
Updated on 08-Aug-2019 06:57:22

899 Views

In python the print statement adds a new line character by default. So when we have multiple print statements the output from each of them is printed in multiple lines as you can see in the example below. Our goal is to print them in a single line and use some special parameters to the print function to achieve that.Normal Print()The below example prints multiple statements with new lines in each of them.Exampleprint("Apple") print("Mango") print("Banana")OutputRunning the above code gives us the following result −Apple Mango BananaUsing end ParameterWe can use the end parameter to use a specific character at the ... Read More

How to download Google Images using Python

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

993 Views

Google offers many python packages which minimize the effort to write python code to get data from google services. One such package is google images download. It takes in the key words as parameters and locates the images with those keywords.ExampleIn the below example we limit the number of images to 5 and also allow the program to print the urls from where the files were generated.from google_images_download import google_images_download #instantiate the class response = google_images_download.googleimagesdownload() arguments = {"keywords":"lilly, hills", "limit":5, "print_urls":True} paths = response.download(arguments) #print complete paths to the downloaded images print(paths)OutputRunning the above code gives us the following ... Read More

Advertisements