In Python, datastructures such as string, list etc., may contain duplicates of a value. In few scenarios, we may need to find which element appears only once while all others elements appear multiple times. Let's go through different methods to find which element occurs exactly once in Python. Using Bitwise XOR The XOR is abbrivated as Exclusive OR is a bitwise operator which returns 1 if the corresponding bits of two operands are different otherwise, it returns 0 if they are same. In Python, we can use the ^ between two operands to perform Exclusive OR operation. Example Following is ... Read More
In Python we work with variables, functions, libraries and modules, etc. Sometimes, the variable name you choose may already exist as the name of another variable, function, or method. This can be confusing or it might lead to unexpected behavior in your program. In such scenario, we need to learn about how all these names are managed in Python. This is the concept of namespaces and scoping. Namespaces in Python A namespace is a mapping from names to objects, which means stores the names you define in your program (such as, variable names, function names, class names, etc.) and maps ... Read More
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 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
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
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
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
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
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 Anonymous Functions in Python Python creates function objects when you write a lambda expression using the lambda keyword. These functions are called anonymous functions because they are not defined using the standard def keyword. These functions are useful when you need a small, single-line function to execute a single expression. Features of Anonymous Functions The following are the features of Anonymous Functions: It can be used wherever a function object is needed in your program, without assigning a name. A lambda function can take any number of arguments but can only ... Read More