Found 10784 Articles for Python

Adding K to each element in a Python list of integers

Pradeep Elance
Updated on 18-Feb-2020 11:31:07

787 Views

In data analysis, sometimes it becomes necessary to add some value to each element in a python list to judge about the outcome of a new scenario. This helps in testing the multiple scenarios on how the data set will behave with different values hence make a model or algorithms which can handle those scenarios. In this article, we will see how we can handle this requirement.Using List ComprehensionList comprehension is a normal way of handling the list where we loop through each element of the list. In the below example we add the same number to each element of ... Read More

Add one Python string to another

Pradeep Elance
Updated on 18-Feb-2020 11:28:35

202 Views

By adding strings in python we just concatenate them to get a new string. This is useful in many scenarios like text analytics etc. Below are the two approaches we consider for this task.Using += OperatorThe + operator can be used for strings in a similar was as it is for numbers. The only difference being, in case of strings the concatenation happens and not a numeric addition.Example Live Demos1 = "What a beautiful " s2 = "flower " print("Given string s1 : " + str(s1)) print("Given string s2 : " + str(s2)) #Using += operator res1 = s1+s2 print("result ... Read More

Add leading Zeros to Python Program string

Pradeep Elance
Updated on 18-Feb-2020 10:29:31

194 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Exampleimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") print(dataframe) ... Read More

Add list elements with a multi-list based on index in Python

Pradeep Elance
Updated on 18-Feb-2020 11:24:15

494 Views

Lists can be nested. Which means we have smaller lists as elements inside a bigger list. In this article we solve the challenge of adding the elements of a simple list to the elements of a nested list. If the length of the lists are different then the length of the smaller list becomes the maximum length of the resulting list.Below are the various methods to accomplish this.Using for LoopIn this method, we take the length of the smaller list and loop through the elements of this list adding it to the elements of the bigger list. Here we use ... Read More

Add image on a Python Tkinter button

Pradeep Elance
Updated on 18-Feb-2020 10:23:22

1K+ Views

Tkinter, which is the GUI library for python programming has a feature to add images to the GUI buttons. This is useful for users to remember the symbols in the GUI rather than the text in the GUI. In the below Tkinter program we show how we can add an image to a GUI button. The photoimage method from the imageKT module is used. We mention the local path to the image file.Examplefrom tkinter import * from PIL import ImageTk ,Image base = Tk() base.title('Start Button') img=ImageTk.PhotoImage(Image.open ("D:\button.jpg")) lab=Label(image=img) lab.pack() button=Button(base, text='exit', command=base.quit) button.pack() base.mainloop()OutputRunning the above ... Read More

Working with csv files in Python Programming

Pradeep Elance
Updated on 14-Feb-2020 07:51:42

1K+ Views

The CSV file or comma separated values file are one of the most widely used flat files to store and hare data across platforms. The columns are separated by comma and there is optional header row also which will indicate the name of each column. Python can read the CSV files using many modules. In this article we will see how the CSV library in python can be used to read and write a CSV file. We can also see the pandas library onl to read the CSV file.Reading CSV file using csv moduleWe can get the CSV file from ... Read More

URL handling Python modules (urllib)

Pradeep Elance
Updated on 14-Feb-2020 07:42:20

1K+ Views

Python language is used extensively for web programming. When we browser website we use the web address which is also known as URL or uniform resource locator. Python has inbuilt materials which can handle the calls to the URL as well as pass the result that comes out of visiting the URL. In this article we will see a module named as urllib. We will also see the various functions present in this module which help in getting the result from the URL.Installing urllibTo install urllib in the python environment, we use the below command using pip.pip install urllibRunning the ... Read More

Twitter Sentiment Analysis using Python Programming.

Pradeep Elance
Updated on 14-Feb-2020 07:38:20

210 Views

Sentiment Analysis is the process of estimating the sentiment of people who give feedback to certain event either through written text or through oral communication. Of course the oral communication also has to be converted to written text so that it can be analysed through python program. The sentiment expressed by people may be positive or negative. By assigning weightage to the different words in the sentiment text we calculate a numeric value and that gives us a mathematical evaluation of the sentiment.UsefulnessCustomer Fedback − It is vital for business to know the customer’s opinion about product or services. When ... Read More

Python - Deque

Pradeep Elance
Updated on 14-Feb-2020 07:27:43

351 Views

In Python deque is a data structure like stack and queue. It allows append and pop operations from both the ends of the queue. And that makes it different from the rest of the data structures. There are various operations which are listed below that is applicable to deque. In this article we will see the examples on each of those operations. The collections module is used to implement deque.Deque OperationsBelow are some of the useful operations carried out using dequeappend() − This function is used to insert the value in its argument to the right end of deque.appendleft() − ... Read More

Add trailing Zeros to a Python string

Pradeep Elance
Updated on 14-Feb-2020 07:14:37

3K+ Views

As part of data processing activity we sometimes need to append one string with another. In this article we will see how to append dynamic number of zeros to a given string. This can be done by using various string functions as shown in the programs below.Using ljust and lenPython string method ljust() returns the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The len() returns the length of the string. We add trailing zeros to the string by manipulating the length of the given string and the ... Read More

Advertisements