Lists in Python are ordered collections that store and manipulate a sequence of elements. In this article, we are going to learn alternate cycling in Python. It is used when we need to work with the alternate element, instead of processing every element in the list.Alternate cycling is the process of accessing or iterating through list elements by skipping elements in a fixed pattern. Following is an example scenario - Scenario Input:[1, 2, 3, 4, 6] Output:[1, 3, 5] Explanation: In this case, the alternate cycling starts from the index 0 and skips the next ... Read More
In this article, we will learn how to add a custom column in a tuple list (i.e., a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses as shown below: Tuples = (11, 22, 33) And the list of tuples is represented as follows: List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)] Suppose we have a list called "students" that stores student data (i.e., name, age) as tuples. If we represent it in the form of rows and columns, the first element of the tuples ... Read More
Our task is to add list items to a tuple list (i.e, a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses, as shown below: Tuples = (11, 22, 33) And the list of tuples is represented as follows: List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)] Scenario Suppose we have a list of tuples, "a", and another list of items, "b". We have to add all items of the "b" to each item of the "a" as follows - Input Lists: a = [(2, 3), ... Read More
In Python, some special methods have names that start and end with double underscores. These are called dunder methods. One such method is __exit__, which is used in context managers for cleanup tasks. Context Manager in Python Context Mangers helps to manage resources such as files, database connections, or network links. It sets up a temporary environment to run a block of code. When the block ends, Python closes the file or disconnects the connection automatically. This prevents unnecessary resource consumption when we leave a file open or keep a connection (such as, network, database) active. Python runs context managers ... Read More
Python does not require a main function to start execution like many other programming languages. Instead, it uses a special built-in variable called __name__ to determine how a Python script is being executed (directly or, is it imported as a module into another script). In this article, we will learn about the __name__ variable in Python. Understanding the __name__ Variable The __name__ variable is a built-in variable that holds the name of the current module. When you run a script directly, Python sets __name__ to __main__. If the same script is imported into another file as a module, __name__ ... Read More
What is the __future__ Module? The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python. It is mainly used when you are migrating code from Python 2 to Python 3, or when you want to try out new features before they become standard (default) in future releases. For example, in older Python versions, dividing two integers would return an integer. But if you import the division feature like this: ... Read More
A Python list is a built-in, mutable datatype that stores multiple items or elements, separated by commas, within square brackets [ ]. The index of a list in Python starts from 0 up to length-1. We can retrieve/access elements at a particular index as follows - list_name[index] The given task is to write a Python program that prints the first character of each element in a list. But, before that, let's see some example scenarios: Scenario 1 For example, if our list contains string values, the output should be the first character of each string. Input: list = ... Read More
With the increase in popularity of the Python programming language, more and more features are becoming available for Python developers. Usage of these features helps us to write efficient code. In this article, we will see 10 Python tricks that are very frequently used. Reversing a List We can reverse a given list by using the reverse() function. It returns the elements of the current list in reverse order. It works with both numeric and string datatypes. Example Let's see how to use the reverse() function in your Python program to reverse a list of strings: List = ["Shriya", "Lavina", "Sampreeti" ... Read More
Tokenization is the process of splitting a string into smaller pieces (tokens). In the context of natural language processing (NLP), tokens are words, punctuation marks, and numbers. Tokenization is a preprocessing step for many NLP tasks, as it allows you to work with individual words and symbols rather than raw text. In this article, we will look at five ways to perform tokenization in Python: Using the split() Method Using the NLTK Library Using Regular Expressions Using the shlex Module Using the ... Read More
In C, the "#" and "##" are the pre-processor operators that are used to convert a macro parameter to a String Literal. The macro parameters are the parameters of the macro definition, which are declared using the #define directive. The "#" operator is known as the Stringize operator, whereas the "##" operator is known as the Token Pasting operator. The #define Directive in C The #define directive is a preprocessor command that is used to create macros in the C programming language. Here is a syntax to create macros: #define MACRO_NAME replacement_text Or #define MACRO_NAME(param1, param2, …paramN) replacement_text ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP