Found 10400 Articles for Python

Reading and Writing to text files in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

Like other languages, Python provides some inbuilt functions for reading, writing, or accessing files. Python can handle mainly two types of files. The normal text file and the binary files. For the text files, each lines are terminated with a special character '' (It is known as EOL or End Of Line). For the Binary file, there is no line ending character. It saves the data after converting the content into bit stream. In this section we will discuss about the text files. File Accessing Modes Sr.No Modes & Description 1 r It is Read ... Read More

Generate a graph using Dictionary in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

2K+ Views

The graphs can be implemented using Dictionary in Python. In the dictionary, each key will be the vertices, and as value, it holds a list of connected vertices. So the entire structure will look like Adjacency list of a graph G(V, E). We can use the basic dictionary object, but we are using default dict. It has some additional features. It has one additional writable instance variable. We are providing a text file, which contains the number of vertices, number of edges, names of vertices, and the list of edges. For undirected graph, we are providing two edges like ... Read More

Python program to find common elements in three lists using sets

Sumana Challa
Updated on 09-Jun-2025 13:49:15

1K+ Views

List is one of the built-in data types in Python, which is a sequence of comma-separated items, enclosed in square brackets [ ]. For example, consider the following lists as input - list1 = [5, 10, 15, 20, 25] list2 = [2, 5, 6, 7, 10, 15, 18, 20] list3 = [10, 20, 30, 40, 50, 60] We will find the common elements in three lists, and the output returned should be - [10, 20] Following are the ways to find common elements in three lists using Sets in Python - Using set.intersection() The set.intersection() method is used ... Read More

Break a list into chunks of size N in Python

Sumana Challa
Updated on 09-Jun-2025 14:25:31

552 Views

The objective is to break a list into chunks of a specific size (n), that is, splitting a list into sublists where each sublist contains n elements. For example, if the list is [12, 33, 11, 56, 44, 89, 23, 34, 12] and the chunk size is 3. Then the output has to be - [[12, 33, 11], [56, 44, 89], [23, 34, 12]]. To break a list into chunks of size n in Python, we use list comprehensions, Numpy library, slicing and others. Let's discuss all the approaches in detail with examples - Using List Comprehension List comprehension provides ... Read More

Send mail from your Gmail account using Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

17K+ Views

In this article, we will see how we can send email with attachments using Python. To send mail, we do not need any external library. There is a module called SMTPlib, which comes with Python. It uses SMTP (Simple Mail Transfer Protocol) to send the mail. It creates SMTP client session objects for mailing. SMTP needs valid source and destination email ids, and port numbers. The port number varies for different sites. As an example, for google the port is 587. At first we need to import the module to send mail. import smtplib Here we are also ... Read More

Tracking bird migration using Python-3

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

408 Views

In some Research works, Researchers uses GPS modules to track the animal behavior. They can track how they are travelling to different places in different time of a year etc. In this example we use that kind of dataset to get an idea, how Birds are moving in different places. In this dataset there are the location details from GPS module are stored. The complete dataset is in CSV form. In that file, there are different fields. The first one is Bird Id, then date_time, Latitude, longitude and speed. For this Task, we need some modules that can be used ... Read More

Simple Chat Room using Python

Samual Sam
Updated on 03-Sep-2020 16:10:11

3K+ Views

In this article we will see how to make a server and client chat room system using Socket Programming with Python.The sockets are the endpoints of any communication channel. These are used to connect the server and client. Sockets are Bi-Directional. In this area, we will setup sockets for each end and setup the chatroom system among different clients through the server. The server side has some ports to connect with client sockets. When a client tries to connect with the same port, then the connection will be established for the chat room.There are basically two parts. The server side ... Read More

Python Program to detect the edges of an image using OpenCV

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

610 Views

In this problem, we will see how Python can detect edges of an image or video file. To achieve this, we need the OpenCV library. The OpenCV library is mainly designed for computer vision. It is open source. Originally it was designed by Intel. This is free to use under open-source BSD license. To use the OpenCV functionality, we need to download them using pip.The OpenCV will download the Numpy module. That will also be needed. sudo pip3 install opencv-python As input, in this case, we have used one video file. We can also use our webcam to ... Read More

Send mail with attachment from your Gmail account using Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

10K+ Views

In this article, we will see how we can send email with attachments using Python. To send mail, we do not need any external library. There is a module called SMTPlib, which comes with Python. It uses SMTP (Simple Mail Transfer Protocol) to send the mail. It creates SMTP client session objects for mailing. SMTP needs valid source and destination email ids, and port numbers. The port number varies for different sites. As an example, for google the port is 587. At first we need to import the module to send mail. import smtplib Here we are also ... Read More

Class method vs static method in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

21K+ Views

The class method in Python is a method, which is bound to the class but not the object of that class. The static methods are also same but there are some basic differences. For class methods, we need to specify @classmethod decorator, and for static method @staticmethod decorator is used. Syntax for Class Method. class my_class: @classmethod deffunction_name(cls, arguments): #Function Body return value Syntax for Static Method. class my_class: @staticmethod deffunction_name(arguments): ... Read More

Advertisements