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
Articles by Mohd Mohtashim
185 articles
Insert the string at the beginning of all items in a list in Python
In this tutorial, we'll learn how to insert a string at the beginning of all items in a list in Python. For example, if we have a string "Tutorials_Point" and a list containing elements like "1", "2", "3", we need to add "Tutorials_Point" in front of each element to get "Tutorials_Point1", "Tutorials_Point2", "Tutorials_Point3". Using List Comprehension with format() The most straightforward approach is using list comprehension with string formatting − sample_list = [1, 2, 3] result = ['Tutorials_Point{0}'.format(i) for i in sample_list] print(result) ['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3'] Using map() with format() ...
Read MorePython - Insert list in another list
When working with lists in Python, you often need to insert one list into another. Python provides several methods to accomplish this: append(), extend(), insert(), and list concatenation with + operator. Using append() Method The append() method adds the entire second list as a single element ? first_list = [1, 2, 3, 4, 5] second_list = [6, 7, 8, 9, 10] first_list.append(second_list) print("Using append():", first_list) Using append(): [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]] Using extend() Method The extend() method adds each element of the second ...
Read MorePython - Increasing alternate element pattern in list
This article demonstrates how to create an increasing alternate element pattern in a list where each original element is followed by a string of asterisks that increases in length. We'll use list comprehension with enumerate() to achieve this pattern efficiently. Understanding the Pattern The increasing alternate element pattern takes a list like [1, 2, 3] and transforms it to [1, '*', 2, '**', 3, '***']. Each element is followed by asterisks equal to its position (1-indexed). Using List Comprehension with enumerate() The enumerate() function adds a counter to each element, starting from 1. We use nested ...
Read MoreParsing XML with DOM APIs in Python
The Document Object Model (DOM) is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. Unlike SAX parsers that process XML sequentially, DOM loads the entire document into memory as a tree structure, allowing random access to any element. The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time, while DOM provides complete access to the entire document structure simultaneously. Basic DOM Parsing Here is the easiest way to quickly load an XML document and create a ...
Read MoreMultithreaded Priority Queue in Python
The Queue module in Python allows you to create thread-safe queue objects for multithreaded applications. A priority queue processes items based on their priority rather than insertion order, making it ideal for task scheduling and resource management. Queue Methods The Queue module provides several methods to control queue operations ? get() − Removes and returns an item from the queue put() − Adds an item to the queue qsize() − Returns the number of items currently in the queue empty() − Returns True if queue is empty; otherwise, False full() − Returns True if queue is ...
Read MoreSending an HTML e-mail using Python
When you send a text message using Python, all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to HTML syntax. However, Python provides the option to send an HTML message as actual HTML content. While sending an email message, you can specify a MIME version, content type and character set to send an HTML email that renders properly in email clients. Basic HTML Email Structure To send HTML email, you need to set the ...
Read MoreDisconnecting Database in Python
When working with databases in Python, it's essential to properly close connections to free up resources and ensure data integrity. The close() method is used to disconnect from the database. Basic Syntax To disconnect a database connection, use the close() method − connection.close() Complete Example with SQLite Here's a complete example showing how to connect to a database, perform operations, and properly close the connection − import sqlite3 # Create connection connection = sqlite3.connect(':memory:') # In-memory database for demo cursor = connection.cursor() # Create a table and insert ...
Read MoreCommit & RollBack Operation in Python
Database transactions in Python require careful management of commit and rollback operations. These operations ensure data integrity by allowing you to either save changes permanently or undo them completely. Understanding Transactions A transaction is a sequence of database operations that must be completed as a single unit. If any operation fails, the entire transaction can be rolled back to maintain data consistency. COMMIT Operation The commit() method finalizes all changes made during the current transaction. Once committed, changes become permanent and cannot be reverted. Example import sqlite3 # Connect to database conn ...
Read MoreDatabase INSERT Operation in Python
The INSERT operation is used to add new records to a database table in Python. This operation requires establishing a database connection, preparing SQL statements, and handling transactions properly. Basic INSERT Statement The following example executes an SQL INSERT statement to create a record in the EMPLOYEE table ? #!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB") # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database sql = """INSERT INTO EMPLOYEE(FIRST_NAME, ...
Read MoreHow To Raise a "File Download" Dialog Box in Python?
Sometimes, you want to provide a download link that triggers a "File Download" dialog box instead of displaying the file content directly in the browser. This can be achieved by setting specific HTTP headers that instruct the browser to treat the response as a downloadable file. Basic File Download Example Here's how to create a simple file download response using Python CGI ? #!/usr/bin/python3 import sys # Set HTTP headers for file download print("Content-Type: application/octet-stream; name="example.txt"") print("Content-Disposition: attachment; filename="example.txt"") print() # Empty line to separate headers from content # Read and output file ...
Read More