Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 655 of 2547
Extract decimal numbers from a string in Python
To extract decimal numbers from a string in Python, regular expressions are used. A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions. The re module in Python is used to work with regular expressions. In this article, we will learn how to extract decimal numbers from a string in Python using regular expressions. Regular Expression Pattern To retrieve decimal numbers from a string, we use the following regular expression − \d+\.\d+ ...
Read MoreHow to check if a list is empty in Python?
In Python, a list is an ordered sequence that can hold several object types, such as integers, characters, or floats. In this article, we will show you how to check if the given input list is empty or not using Python. Below are the 5 methods to accomplish this task ? Using not operator Using len() function By Comparing with an Empty List Using __len__() Method Using NumPy Module Using the NOT Operator In Python, empty lists evaluate to False in boolean context. The not operator provides the most Pythonic way to check for ...
Read MoreHow do make a flat list out of list of lists in Python?
A nested list is a list that contains other lists as elements. For example: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is a nested list with 3 sublists as its elements. To flatten a list of lists (convert a 2D list into a 1D list), Python offers several approaches: nested for loops, list comprehension, built-in functions like itertools.chain(), or using libraries like NumPy. In this article, we will explore these methods to flatten a list in Python. Using Nested For Loops The most straightforward approach is to iterate through each sublist and add its ...
Read MoreHow do we assign a value to several variables simultaneously in Python?
Python is not a "statically typed" programming language. We do not need to define variables or their types before utilizing them. Once we initially assign a value to a variable, it is said to be created. Each variable is assigned with a memory location. The assignment operator (=) assigns the value provided to right to the variable name which is at its left. The syntax of the assignment operator is shown below − var_name = value Example of Assignment Operator The following is an example that shows the usage of the assignment operator − ...
Read MoreHow do we assign values to variables in a list using a loop in Python?
In Python, lists are a useful way to store multiple elements in a single variable. To assign values to each entry in a list, loops are often needed. This strategy simplifies the process and improves the clarity of your code. This chapter will look at how to assign values to variables in a list using different loop types, using basic examples. Using Simple Loop Iterations In this method, we use the while loop to continuously prompt the user for input and append elements to the list. When we do not enter any element into the list and just ...
Read MoreHow I can convert a Python Tuple into Dictionary?
The Python tuple elements are enclosed inside the parentheses, while dictionary elements are present in the form of a key-value pair and are enclosed between curly brackets. In this article, we will show you how to convert a Python tuple into a dictionary. The following are the methods to convert a tuple into a dictionary − Using dict() Function Using Dictionary Comprehension and enumerate() Function ...
Read MoreHow to find what is the index of an element in a list in Python?
In Python, an index is an element's location within an ordered list. The first entry has a zero index, and the last element has an n-1 index, where n is the length of the list. In this tutorial, we will look at how to find the index of an element in a list in Python. There are different methods to retrieve the index of an element. Using index() Method The list index() method in Python accepts three arguments − element: element that has to be found. start ...
Read Morewhy Python can\'t define tuples in a function?
The title "why Python can't define tuples in a function?" contains a misconception. Python can define tuples in functions perfectly well. This article will clarify how to work with tuples in functions, showing various ways to create, return, and manipulate tuples within functions. A tuple is an ordered, unchangeable collection of items in Python. Once created, its values cannot be modified. Tuples are defined with parentheses "()" and can store multiple data types. Basic Tuple Creation Here is how you define a tuple ? # Defining a tuple my_tuple = (1, 2, 3) print(my_tuple) print(type(my_tuple)) ...
Read MoreHow to add space before and after specific character using regex in Python?
When working with text data in Python, we often need to format strings to make them more readable. Regular expressions (regex) provide a powerful way to add spaces before and after specific characters. In this article, we'll explore how to use Python's re module for this purpose. Understanding the Regex Pattern The key to adding spaces around characters is using the re.sub() function with the pattern r'\s*character\s*'. This pattern: \s* − Matches zero or more whitespace characters before the target character character − The specific character we want to format \s* − Matches zero or more ...
Read MoreHow regular expression grouping works in Python?
Regular expression grouping in Python allows you to capture specific parts of a pattern match using parentheses (). This feature is essential for extracting structured data from strings and organizing match results into meaningful components. Understanding Regex Grouping Basics Groups are created by placing parentheses around parts of your regex pattern. The re module provides two main methods for working with groups ? group() − Returns the whole match or a specific group by number groups() − Returns all captured groups as a tuple Syntax import ...
Read More