Python Articles

Page 30 of 855

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Hafeezul Kareem
Updated on 15-Mar-2026 659 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example. Input a = 3 b = 5 Output a = 5 b = 3 Let's see how to achieve this in different programming languages. Python We can swap variables with one line of code in Python using tuple unpacking − Example # initializing the variables a, b = 3, 5 # printing before swapping print("Before swapping:-", a, b) ...

Read More

Run a Python program from PHP

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 15-Mar-2026 7K+ Views

In PHP, you can execute Python scripts using built-in functions like exec() or shell_exec(). These functions allow you to run Python programs via the shell and capture their output. exec() Function The exec() function executes commands in the shell and optionally captures the output. It returns only the last line of output by default. Syntax exec(string $command, array &$output = null, int &$return_var = null); shell_exec() Function The shell_exec() function is similar to exec() but captures and returns the entire output of the command as a string, instead of just the last ...

Read More

How to call Python file from within PHP?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 15-Mar-2026 11K+ Views

In PHP, you can execute Python scripts using built-in functions like shell_exec(), exec(), or proc_open(). Each method offers different levels of control over the process. Using shell_exec() The simplest approach − runs a command via the shell and returns the output as a string. Use escapeshellcmd() to sanitize the command ? The square of 4 is 16 Specifying the Python Interpreter If the script lacks a shebang line (#!/usr/bin/env python3), explicitly call the interpreter ? The square of 5 is 25 ...

Read More

Is there something available in Python like PHP autoloader?

Sumana Challa
Sumana Challa
Updated on 15-Mar-2026 471 Views

No, Python does not have a direct equivalent to PHP's autoloader, and it doesn't need one. The two languages handle module loading fundamentally differently. Why PHP Needs Autoloaders PHP starts a fresh process for each web request, loading all code from scratch every time. Autoloaders optimize this by loading classes only when they are first used, avoiding the cost of loading unnecessary files on every request. Why Python Doesn't Need One Python imports modules once when the application starts. The module stays in memory (sys.modules) for the lifetime of the app. Subsequent requests reuse the already-loaded ...

Read More

How to access a collection in MongoDB using Python?

Priya Mishra
Priya Mishra
Updated on 15-Mar-2026 1K+ Views

MongoDB is a well-known NoSQL database that offers a scalable and flexible approach to store and retrieve data. Using Python with MongoDB enables developers to interact with database collections effortlessly through the PyMongo library. This article explains how to access a MongoDB collection using Python, covering the required steps, syntax, and techniques for connecting to, querying, and manipulating data. Syntax from pymongo import MongoClient # Connect to MongoDB client = MongoClient('mongodb://localhost:27017/') # Access database and collection db = client.database_name collection = db.collection_name # Perform operations collection.insert_one(document) collection.find(query) collection.update_one(filter, update) collection.delete_one(filter) PyMongo ...

Read More

How to prepare a Python date object to be inserted into MongoDB?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 14-Mar-2026 2K+ Views

MongoDB stores dates in ISODate format. PyMongo (the official MongoDB driver for Python) directly supports Python's datetime.datetime objects, which are automatically converted to ISODate when inserted. There are three common ways to prepare date objects for MongoDB. Method 1: Current UTC Timestamp Use datetime.datetime.utcnow() to insert the current UTC time ? import datetime from pymongo import MongoClient client = MongoClient() db = client.test_database result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Date Object inserted") Date Object inserted Method 2: Specific Date Use datetime.datetime(year, month, day, hour, minute) for a fixed date ? ...

Read More

Difference between Python and Bash

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

Python and Bash are both widely used in automation and scripting, but they serve different purposes. Python is a full-featured, object-oriented programming language, while Bash is a command-line interpreter (shell) designed for running system commands and small scripts. Python Python is a dynamically typed, object-oriented programming language designed to be simple and easy to understand. It supports a rich ecosystem of third-party libraries and is used for web development, data science, automation, AI, and much more. Bash Bash (Bourne Again Shell) is a command-line interpreter and the default user shell on Linux and macOS. It was ...

Read More

Explain difference between == and is operator in Python.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 359 Views

In Python, == and is are both comparison operators but they check different things. == checks if two objects have the same value (equality), while is checks if two variables point to the same object in memory (identity). == Operator (Equality) The == operator compares the values of two objects. If the values are equal, it returns True, regardless of whether they are stored at different memory locations. is Operator (Identity) The is operator checks whether two variables refer to the exact same object in memory (same id()). Even if two objects have equal values, is ...

Read More

Difference between Python and PHP.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 579 Views

Python and PHP are both popular programming languages but serve different primary purposes. Python is a general-purpose language used across many domains, while PHP is primarily a server-side scripting language designed for web development. Python Python is a high-level programming language with a large built-in standard library. It was developed by Guido van Rossum, with its first version released in 1990. Python emphasizes clean syntax and readability, making it suitable for a wide range of applications from web development to data science and AI. Example # Python: clean, concise syntax languages = ["Python", "PHP", "Java"] ...

Read More

How to call a function with argument list in Python?

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 4K+ Views

The purpose of a function is to perform a specific task using code blocks. Functions save time by eliminating unnecessary copying and pasting of code. If you need to make a change, you only update the function in one place rather than searching through your entire program. This follows the DRY (Don't Repeat Yourself) principle in software development. Defining a Function in Python Python functions are created using the following syntax − def function_name(parameters): function body A function is defined using the def keyword followed by the function name and ...

Read More
Showing 291–300 of 8,547 articles
« Prev 1 28 29 30 31 32 855 Next »
Advertisements