Found 10805 Articles for Python

Formatted text in Linux Terminal using Python

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

200 Views

In this section, we will see how to print formatted texts in Linux terminal. By formatting, we can change the text color, style, and some special features. Linux terminal supports some ANSI escape sequences to control the formatting, color and other features. So we have to embed some bytes with the text. So when the terminal is trying to interpret them, those formatting will be effective. The general syntax of ANSI escape sequence is like below − \x1b[A;B;C A is the Text Formatting Style B is the Text Color or Foreground Color C is the Background ... Read More

Reading and Writing to text files in Python

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

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

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

AmitDiwan
Updated on 12-Aug-2022 11:44:01

925 Views

In this article, we will learn how to find common elements in three lists. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. However, Set is a collection in Python, which is unordered, unchangeable, and unindexed. Let’s say we have the following input − a = [5, 10, 15, 20, 25] b = [2, 5, 6, 7, 10, 15, 18, 20] c = [10, 20, 30, ... Read More

Break a list into chunks of size N in Python

AmitDiwan
Updated on 12-Aug-2022 11:53:11

390 Views

In this example, we will learn how to break a list into chunks of size N. We will be using the list() function here. The list() function creates a list object. A list object is a collection which is ordered and changeable. Break a list into chunks of size N with List Comprehension The List Comprehension can be used to breal a list into chunks of size N − Example A = list() # User input for list size n = int(input("Enter the size of the List")) # User input for number print("Enter the number") for i in range(int(n)): ... Read More

Three way partitioning of an array around a given range using Python

AmitDiwan
Updated on 12-Aug-2022 14:36:50

240 Views

Given an array and the range of the array [startval, endval]. Array is divided by three parts. All elements smaller than startval come first. All elements in range startval to endval come next. All elements greater than endval appear in the end. Let’s say we have the following input − A = [1, 14, 51, 12, 4, 2, 54, 20, 87, 98, 3, 1, 32] startval = 14, endval = 54 The output should be − A = [1, 12, 4, 2, 3, 1, 14, 51, 20, 32, 54, 87, 98] Three-way partitioning of an array around ... Read More

Stack and Queue in Python using queue Module

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

422 Views

In Python, it is very easy to implement stack and queue data structures. Stack is called LIFO because Stack works on the principle of "Last-in, first-out" and Queue is called FIFO because Queue works on the principle of "First-in, first-out", and the inbuilt functions in Python make the code shorter and simple. The Queue module implements multi-producer, multi-consumer queues and It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics and it depends on the availability of thread support in Python. This ... Read More

Extracting MAC address using Python

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

2K+ Views

We know that the MAC address is a hardware address which means it is unique for the network card installed on our PC. It is always unique that means no two devices on a local network could have the same MAC addresses. The main purpose of MAC address is to provide a unique hardware address or physical address for every node on a local area network (LAN) or other networks. A node means a point at which a computer or other device (e.g. a printer or router) will remain connected to the network. Method1 Using uuid.getnode() In this example getnode() ... Read More

How to check if a string is a valid keyword in Python?

AmitDiwan
Updated on 11-Aug-2022 11:45:16

2K+ Views

To check if a string is a valid keyword, import the keyword module and use the iskeyword() method. With that, you can directly display all the keywords at once and verify. Let’s say the following is our input − else The following is the output. The “else” is a keyword in Python − Keyword Check if a string is a valid keyword in Python Example import keyword # Create a List myList = ["for", "amit", "val", "while"] # Display the List print("List = ", myList) keyword_list = [] non_keyword_list = [] # Looping and ... Read More

Send mail from your Gmail account using Python

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

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

Advertisements