Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 721 of 855
Random access to text lines in Python (linecache)
The linecache module in Python's standard library provides random access to any text file by line number. This module is extensively used by Python's traceback system to generate error traces and caches previously read lines to improve performance when reading lines repeatedly. Key Functions getline(filename, lineno) Returns the specified line number from a file. If the line doesn't exist, it returns an empty string. If the file isn't in the current directory, it searches in sys.path. getlines(filename) Returns all lines from a file as a list object. clearcache() Clears the internal cache when ...
Read MoreIterate over lines from multiple input streams in Python
Python's built-in open() function handles single file operations, but when you need to process multiple files sequentially, the fileinput module provides a powerful solution. This module allows you to iterate over lines from multiple input streams as if they were a single continuous stream. Basic Usage with fileinput.input() The primary interface is the fileinput.input() function, which returns a FileInput object that can iterate over multiple files ? import fileinput # Reading from a single file for line in fileinput.input('data.txt'): print(line, end='') Processing Multiple Files You can pass multiple ...
Read MoreHow to install Python MySQLdb module using pip?
The MySQLdb module is a Python interface for connecting to MySQL databases. Since MySQLdb is not available for Python 3.x, we'll use PyMySQL or mysql-connector-python as modern alternatives. Why MySQLdb is Not Recommended MySQLdb only supports Python 2.x and is no longer maintained. For Python 3.x applications, use these alternatives ? PyMySQL − Pure Python MySQL client mysql-connector-python − Official MySQL driver Installing PyMySQL (Recommended) Open Command Prompt and install PyMySQL using pip ? pip install PyMySQL Example Usage import pymysql # Connection example (won't execute ...
Read MoreFormatted text in Linux Terminal using Python
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 using ANSI escape sequences. Linux terminal supports ANSI escape sequences to control the formatting, color and other features. We embed these special bytes with the text, and when the terminal interprets them, the formatting becomes effective. ANSI Escape Sequence Syntax The general syntax of ANSI escape sequence is ? \x1b[A;B;C Where: A is the Text Formatting Style B is the Text Color or Foreground ...
Read MoreGenerate a graph using Dictionary in Python
Graphs can be implemented using Dictionary in Python where each key represents a vertex and its value contains a list of connected vertices. This creates an adjacency list representation of a graph G(V, E). We'll use Python's defaultdict from the collections module, which provides additional features over regular dictionaries by automatically creating missing keys with default values. Graph Representation Consider this undirected graph with 6 vertices (A, B, C, D, E, F) and 8 edges ? A B ...
Read MoreTracking bird migration using Python-3
Bird migration tracking using GPS technology provides valuable insights into animal behavior patterns. Researchers use GPS modules to monitor how birds travel to different locations throughout the year, collecting data on their routes, timing, and movement patterns. In this tutorial, we'll analyze a bird tracking dataset containing GPS coordinates, timestamps, and movement data. The dataset is in CSV format with fields including bird ID, date_time, latitude, longitude, and speed. Required Libraries We need several Python libraries for data analysis and visualization. Install them using conda − conda install -c conda-forge matplotlib conda install -c anaconda ...
Read MoreSimple Chat Room using Python
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 tutorial, we will setup sockets for each end and create a 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 ...
Read MoreHow to flatten a shallow list in Python?
Flattening a shallow list means converting a nested list into a simple, single-dimensional list. In other words, converting a multidimensional list into a one-dimensional list. Flattening can be performed using different techniques like nested for loops, list comprehensions, list concatenation, and using built-in functions. In this article, we will discuss a few techniques to flatten a shallow Python list. Flattening a shallow list using a nested for loop By using a nested for loop and a list.append() method, we can flatten the shallow list. Let’s have a look and see how this can be done in a program. Example This simple example ...
Read MoreSend mail with attachment from your Gmail account using Python
In this article, we will see how to 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 and 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. For Gmail, the port is 587. Required Modules We need to import the following modules ? import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text ...
Read MoreFraction module in Python
Python's fractions module provides support for rational number arithmetic. Using this module, we can create fractions from integers, floats, decimals, strings, and other numeric values. The Fraction constructor accepts a numerator and denominator as parameters. The default numerator is 0 and the default denominator is 1. It raises ZeroDivisionError when the denominator is 0. Creating Fraction Instances Let's see how to create fractions using numerator and denominator values ? from fractions import Fraction print(Fraction(45, 54)) print(Fraction(12, 47)) print(Fraction(0, 15)) print(Fraction(10)) # denominator defaults to 1 5/6 12/47 0 10 ...
Read More