Found 10783 Articles for Python

Python - Check if all elements in a list are identical

Pradeep Elance
Updated on 10-Jul-2020 11:30:02

678 Views

There may be occasions when a list will contain all the values which are same. In this article we will see various way to verify that.With allWe use the all function to find the result of comparison of each element of the list with the first element. If each comparison gives a result of equality then the result is given as all elements are equal else all elements are not equal.Example Live DemolistA = ['Sun', 'Sun', 'Mon'] resA = all(x == listA[0] for x in listA) if resA:    print("in ListA all elements are same") else:    print("In listA ... Read More

Python - Check if a list is contained in another list

Pradeep Elance
Updated on 10-Jul-2020 11:28:07

882 Views

Given two different python lists we need to find if the first list is a part of the second list.With map and joinWe can first apply the map function to get the elements of the list and then apply the join function to cerate a comma separated list of values. Next we use the in operator to find out if the first list is part of the second list.Example Live DemolistA = ['x', 'y', 't'] listB = ['t', 'z', 'a', 'x', 'y', 't'] print("Given listA elemnts: ") print(', '.join(map(str, listA))) print("Given listB elemnts:") print(', '.join(map(str, listB))) res = ', '.join(map(str, ... Read More

Python - Check if a given string is binary string or not

Pradeep Elance
Updated on 10-Jul-2020 11:25:39

6K+ Views

In this article we check if a given string has characters which are only 1 or 0. We call such strings as binary strings. If it has any other digit like 2 or 3 etc., we classify it as a non-binary string.With setThe set operator in python stores only unique elements. So we take a string and apply the set function to it. Then we create another set which has only 0 and 1 as its elements. If both these sets are equal then the string is definitely binary. Also the string may have only 1s or only 0s. So ... Read More

Password validation in Python

Pradeep Elance
Updated on 10-Jul-2020 11:22:16

2K+ Views

It is a general requirement to have a reasonably complex password. In this article we will see how to validate if a given password meats certain level of complexity. For that will use the regular expression module known as re.Example -1First we create a regular expression which can satisfy the conditions required to call it a valid password. Then we e match the given password with the required condition using the search function of re. In the below example the complexity requirement is we need at least one capital letter, one number and one special character. We also need the ... Read More

Multi-dimensional lists in Python

Pradeep Elance
Updated on 10-Jul-2020 11:19:39

7K+ Views

Lists are a very widely use data structure in python. They contain a list of elements separated by comma. But sometimes lists can also contain lists within them. These are called nested lists or multidimensional lists. In this article we will see how to create and access elements in a multidimensional list.Creating Multi-dimensional listIn the below program we create a multidimensional list of 4 columns and 3 rows using nested for loops.Example Live Demomultlist = [[0 for columns in range(4)] for rows in range(3)] print(multlist)OutputRunning the above code gives us the following result −[[0, 0, 0, 0], [0, 0, 0, 0], ... Read More

MongoDB and Python

Pradeep Elance
Updated on 10-Jul-2020 11:17:08

555 Views

MongoDB is a widely used document database which is also a form of NoSQL DB. Python can interact with MongoDB through some python modules and create and manipulate data inside Mongo DB. In this article we will learn to do that. But MongoDB should already be available in your system before python can connect to it and run. To setup MongoDB in your system please visit our  MongoDB tutorial here..Install pymongoTo interact with MongoDB we need the module names pymongo. Install it in your python environment using the below command.pip install pymogoCheck the Existing DBsWe now use this python module ... Read More

Launching parallel tasks in Python

Pradeep Elance
Updated on 10-Jul-2020 11:15:01

297 Views

If a Python program can be broken into subprograms who is processing do not depend on each other, then each of the subprogram can be run in parallel when the overall program is being run. This concept is known as parallel processing in Python.With multiprocessingThis module can be used to create many child processes of a main process which can run in parallel. In the below program we initialize a process and then use the run method to run the multiple sub-processes. We can see different sub processes in the print statement by using the process id. We also use ... Read More

Interpreting stat() results using Python

Pradeep Elance
Updated on 10-Jul-2020 11:12:28

290 Views

The stat() method is part of OS module which describes various OS related operations on files and directories. For example, if we want to know various user defined flags for a file or size of the file in bytes.Functions in os.stat() moduleBelow is a list of some sample functions available in stat() and their meaning.st_size − It represents the size of the file in bytes.st_atime − It represents the time of most recent access. It is expressed in seconds.st_ctime − It represents the time of most recent metadata change on Unix and creation time on Windows. It is expressed in seconds.st_blocks ... Read More

Importing Data in Python

Pradeep Elance
Updated on 10-Jul-2020 11:10:27

15K+ Views

When running python programs, we need to use datasets for data analysis. Python has various modules which help us in importing the external data in various file formats to a python program. In this example we will see how to import data of various formats to a python program.Import csv fileThe csv module enables us to read each of the row in the file using a comma as a delimiter. We first open the file in read only mode and then assign the delimiter. Finally use a for loop to read each row from the csv file.Exampleimport csv with ... Read More

GET and POST requests using Python Programming

Pradeep Elance
Updated on 10-Jul-2020 10:59:24

3K+ Views

Python can be used to access webpages as well as post content to the webpages. There are various modules like httplib, urllib, httplib2 etc but the requests module is simplest and can be used to write simpler yet powerful programs involving GET and POST methods.GET methodThe GET method is part of the python requests module which is used to obtain data from a web URL. In the below example we reach out to our own website and find out various responses through the get method. We get the encoding, response time and also the header and part of the body.Example Live ... Read More

Advertisements