Found 33676 Articles for Programming

What does close() function do in Python?

Manogna
Updated on 01-Oct-2019 11:29:19

329 Views

The function close() closes an open file. For example: f = open('my_file', 'r+') my_file_data = f.read() f.close() The above code opens 'my_file'in read mode then stores the data it reads from my_file in my_file_data and closes the file. When you open a file, the operating system gives a file handle to read/write the file. You need to close it once you are done using the file. If your program encounters an error and doesn't call f.close(), you didn't release the file. To make sure it doesn't happen, you can use with open(...) as syntax as it automatically closes files regardless of whether ... Read More

What does print >> do in python?

Rajendra Dharmkar
Updated on 13-Jul-2023 16:30:52

448 Views

In Python 2, there was an alternative syntax for using the print statement for printing that involved using the >> operator, also known as the right shift operator. However, this syntax has been deprecated and removed in Python 3. Therefore, if you see code that uses print >> syntax, it is likely written in Python 2 and will not work in Python 3. The correct syntax in Python 2 for redirecting the output of the print statement to a file-like object is using the print statement followed by the >> operator and the file object. Here are ... Read More

What does print() function do in Python?

Rajendra Dharmkar
Updated on 13-Jul-2023 16:27:05

864 Views

In Python, among several functions and other tools that are used to achieve certain functionalities, the print() function happens to be a basic tool for displaying output to the console or terminal. This function allows programmers to achieve several tasks such as, among others, presenting information, messages, variables, and other data to users or for debugging purposes. In this article, we will investigate in detail the functionality and usage of the print() function in Python, through several code examples followed by comprehensive explanations. This will help you to add one more Python skill to your repertoire of code expertise ... Read More

How to print to the Screen using Python?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:34:57

808 Views

The basic way to do output to screen is to use the print statement.>>> print 'Hello, world' Hello, worldTo print multiple things on the same line separated by spaces, use commas between them. For example:>>> print 'Hello, ', 'World' Hello, WorldWhile neither string contained a space, a space was added by the print statement because of the comma between the two objects. Arbitrary data types can also be printed using the same print statement, For example:>>> import os >>> print 1, 0xff, 0777, (1+5j), -0.999, map, sys 1 255 511 (1+5j) -0.999 Objects can be printed on the same line ... Read More

How to generate XML documents with namespaces in Python?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:36:28

2K+ Views

Currently you cannot add namespaces to XML documents directly as it is not yet supported in the in built Python xml package. So you will need to add namespace as a normal attribute to the tag. For example,import xml.dom.minidom doc = xml.dom.minidom.Document() element = doc.createElementNS('http://hello.world/ns', 'ex:el') element.setAttribute("xmlns:ex", "http://hello.world/ns") doc.appendChild(element) print(doc.toprettyxml())This will give you the document,

How will you compare modules, classes and namespaces in Python?

Pranathi M
Updated on 11-May-2023 14:31:57

814 Views

Python allows you to save definitions to a file and then use them in a script or interactive instance of the interpreter. A module is a file that contains definitions that can be imported into other modules or the main module. So, a Python module is nothing more than a package that contains reusable code. Modules are stored in a folder that contains a __init .py file. Modules can contain both functions and classes. The import keyword is used to import modules. A file containing Python commands and definitions is referred to as a module. These files named .py ... Read More

How will you compare namespaces in Python and C++?

SaiKrishna Tavva
Updated on 27-Jan-2025 17:14:26

833 Views

Namespaces help in organizing code, managing the scope of variables and preventing naming conflicts. Python and C++ use namespaces, but they do so in different ways. Below is an overview of namespaces in both. Namespaces in C++ In C++, namespaces are created using the keyword 'namespace'. They are mainly intended to organize code into logical groups and avoid name conflicts, particularly when working with multiple libraries. Example In the following example we are going to how to use a namespace by utilizing '::' to access functions within that namespace. #include using namespace std; // first namespace namespace first_space ... Read More

Explain the visibility of global variables in imported modules in Python?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:24:41

1K+ Views

Globals in Python are global to a module, not across all modules. (Unlike C, where a global is the same across all implementation files unless you explicitly make it static.). If you need truly global variables from imported modules, you can set those at an attribute of the module where you're importing it.import module1 module1.a=3On the other hand, if a is shared by a whole lot of modules, put it somewhere else, and have everyone import it:global_module.py module1.py: import global_module def fun():     print global_module.var Other files: import global_module import module1 global_module.var = 3 module1.fun()Read More

How to import everything from a python namespace / package?

Sumana Challa
Updated on 18-Apr-2025 15:47:32

790 Views

What are Namespace Packages? Namespace packages are a special type of package that were introduced in Python 3.3. These allow you to split package contents across multiple directories. If you didn't include at least an empty _init_.py file in your package, then your package becomes a namespace package. Python supports three main types of namespace packages - Implicit namespace packages pkg_resources-style pkgutil-style Python Packages and Imports Python package is a directory containing a _init_.py file and one or more Python modules. When you import from a package, ... Read More

How do I import all the submodules of a Python namespace package?

Sumana Challa
Updated on 21-Apr-2025 16:06:42

911 Views

What are Namespace Packages? Namespace packages are a type of package in Python that allows you to split the sub-packages and modules within a single package across multiple, separate distribution packages. Unlike normal packages, the namespace packages don't require _init_.py file. Automatically importing all submodules within a namespace package serves several purposes, like auto-registration without manually importing, and to load all available plugins in a system. This article discusses the two methods that you can use to import all the sub-modules of a Python namespace package - Using pkgutil.iter_modules() Using ... Read More

Advertisements