Server Side Programming Articles - Page 1915 of 2650

Add Two Numbers in Python

gireesha Devara
Updated on 20-Jun-2025 19:37:53

3K+ Views

Adding two numbers in Python is one of the most basic tasks, where we need to combine their values to get a total. In this article, we will see different ways to add two numbers in Python. Using Arithmetic Operator To add two numerical values in Python, we can use the addition operation, represented by the "+" symbol. It is one of the arithmetic operators in Python that performs the addition operation. Example Here is the basic example that adds the two numbers using the addition operator (+). # Assign values to the variables a = 7 b = ... Read More

Locating File Positions in Python

gireesha Devara
Updated on 01-Sep-2025 11:44:37

1K+ Views

Locating file positions in Python means identifying the current position of the file pointer within an open file. Python provides the tell() and seek() methods of the file object for this task. Locating the File Position The tell() method tells you the current position within the file. In other words, it specifies that the next read or write will occur at that many bytes from the beginning of the file. Example The following example shows how to get the current file position using the tell() method. Initially, when a file is opened, the pointer is placed at the beginning. After ... Read More

Reading and Writing Files in Python

gireesha Devara
Updated on 01-Sep-2025 11:41:08

18K+ Views

Reading data from a file and writing to a file can be easily done in Python by using the file object. The file object in Python provides a set of methods for handling files. These methods make our lives easier for interacting with files on our computer. Below, we will see how to read and write files in Python using the read() and write() methods. The write() Method The write() method writes any string to an opened file. It is important to note that Python strings can have binary data and not just text. This method does not add a ... Read More

Opening and Closing Files in Python

Mohd Mohtashim
Updated on 01-Sep-2025 11:39:37

11K+ Views

Until now, you have been reading and writing to the standard input and output. Now, we will see how to interact with actual data files. Python provides built-in functions and methods necessary to manipulate files by default. To work with a file in Python, we first need to create a file object. Using this object you can do most of the file manipulation such as reading, writing, or closing a file. Opening a File in Python Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, ... Read More

Reading Keyboard Input in Python

gireesha Devara
Updated on 01-Sep-2025 11:30:40

3K+ Views

Reading keyboard input in Python can be easily done by using the built-in functions. Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are: raw_input() input() Using the raw_input() Function The Python raw_input() function reads keyboard input and returns it as a string (removing the trailing newline). It is important to note that this function is available for the Python 2.x version only. It was renamed to input() in the Python 3.x versions. Example Let's take ... Read More

Printing to the Screen in Python

Akshitha Mote
Updated on 12-Dec-2024 19:32:44

713 Views

Printing to the Screen in Python In Python, the print() function is used to display the data on the screen. The data can be a string or any other object. However, the resultant object will be converted into a string before printing it on the console or standard output device. In this article let's understand a brief about print() function with various operations. Syntax of print() Function Following is the syntax of the print() function in Python − print(value(s), sep= ‘ ‘, end = ‘’, file=file, flush=flush) Following are the parameters of the print() function − ... Read More

The globals(), locals() and reload() Functions in Python

gireesha Devara
Updated on 01-Sep-2025 11:25:38

2K+ Views

The globals() and locals() are the Python built-in functions that are used to return the names in the global and local namespaces, depending on the location from where they are called. Whereas the reload() method is used to manage the Python modules. The globals() Function The globals() function in Python is used to get a dictionary representing the global namespace, containing all the global variables and their corresponding values in the current scope. If globals() is called from within a function, it will return all the names that can be accessed globally from that function. Example The following example demonstrates ... Read More

Namespaces and Scoping in Python

Mohd Mohtashim
Updated on 30-Jan-2020 06:28:56

547 Views

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values).A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable.Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions.Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local.Therefore, in order to assign a value to a global variable within a ... Read More

Locating Modules in Python

gireesha Devara
Updated on 01-Sep-2025 11:22:04

1K+ Views

Locating modules in Python refers to the process of how Python finds and loads a module into our current program when we are trying to import it. Python's standard library comes with a large number of modules that we can use in our programs with the import statement. Locating Modules in Python When you write an import statement like import mymodule, the Python interpreter searches for the module in the following sequences: The current directory: Python first checks the directory of the current script. PYTHONPATH Environment Variable: If the module isn't ... Read More

The import Statements in Python

gireesha Devara
Updated on 01-Sep-2025 11:37:47

5K+ Views

In Python, accessing and using the code from one module to another module is possible by the importing process. That means, you can use any Python source file as a module by executing an import statement in some other Python source file. This is done using the import statement in Python. The import Statement When the Python interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. This import operation in Python internally uses the built-in ... Read More

Advertisements