Found 10784 Articles for Python

How to write a case insensitive Python regular expression without re.compile?

Rajendra Dharmkar
Updated on 03-Nov-2023 02:49:54

10K+ Views

We can pass re.IGNORECASE to the flags parameter of search, match, or sub −Example import re print (re.search('bush', 'BuSh', re.IGNORECASE)) print (re.match('bush', 'BuSh', re.IGNORECASE)) print (re.sub('bush', 'xxxx', 'Bushmeat', flags=re.IGNORECASE))Output xxxxmeat

Extract decimal numbers from a string in Python

Pranav Indukuri
Updated on 03-Nov-2023 02:48:14

10K+ Views

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 get to know how to extract decimal numbers from a string in python using regular expressions. We use \d+\.\d+ regular expression in python to get non digit characters from a string. Where, \d returns a match where ... Read More

How to check if a list is empty in Python?

Vikram Chiluka
Updated on 23-Aug-2023 13:12:10

56K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In this article, we will show you how to check if the given input list is an empty list 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__() Using NumPy Module Assume we have taken an empty list. We will check if the input list is empty or not and returns some random message for acknowledgment using different methods ... Read More

How do make a flat list out of list of lists in Python?

Pranav Indukuri
Updated on 08-Nov-2022 12:15:33

249 Views

A nested list is a list which has elements as lists itself. For example: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is a nested list as it has 3 lists ([1, 2, 3], [4, 5, 6], and [7, 8, 9]) as its elements. To flatten a list of lists (nested list) i.e., to convert a 2D list into a 1D list, there are different ways such as nested for loop, list comprehension, built-in functions or by importing libraries in python. In this article we will discuss the above methods to flatten a list in Python. Using for loops ... Read More

How does Python generate random numbers?

Vikram Chiluka
Updated on 19-Sep-2022 08:53:19

434 Views

Python includes a built-in package i.e random module for generating random numbers. In this article, we will show you how to generate random numbers in python using different methods − Using random.seed() method Using random.randrange() method Using random.randint() method Using random.random() Using random.choice() method Using random.uniform() method Method 1: Using random.seed() method The random number generator is initialized using the seed() method. To generate a random number, the random number generator requires a starting numeric value (a seed value). NOTE − The random number generator utilizes the current system time by default. To change the random number generator's ... Read More

How do we assign a value to several variables simultaneously in Python?

Pranav Indukuri
Updated on 08-Nov-2022 12:12:47

6K+ Views

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. Syntax The syntax of the assignment operator is shown below. var_name = value Example The following is the example which shows the usage of the assignment operator. Name = ‘Tutorialspoint’ In Python, variable is really ... Read More

How do we assign values to variables in a list using a loop in Python?

Pranav Indukuri
Updated on 04-Apr-2023 10:59:37

5K+ Views

In this article we will discuss different ways to assign values to variables in a list using loops in python. Using simple loop iterations In this method we use the for loop for appending elements into the lists. When we do not enter any element into the list and stop appending, then we just press the enter key. To append the elements into the list we use the append() method. Example 1 The following is an example to assign values to the variables in a list using a loop in python using append() method. L=[] while True: ... Read More

How I can convert a Python Tuple into Dictionary?

Vikram Chiluka
Updated on 26-Aug-2023 08:37:40

35K+ Views

The Python tuple elements are enclosed inside the parenthesis, 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 Using zip() and dict() functions Assume we have taken a tuple containing some elements. We will convert the input tuple into a python dictionary and return it using different methods as specified above. ... Read More

What is syntax of tuple declaration in Python?

Vikram Chiluka
Updated on 08-Nov-2022 11:52:44

857 Views

Python provides a variety of data structure collections such as lists, tuples, sets, and dictionaries. However, these tuples are very similar to lists. Because lists are a commonly used data structure, developers frequently mistake how tuples differ from lists. Python tuples, like lists, are a collection of items of any data type, but tuples are immutable, which means that once assigned, we cannot change the elements of the tuple or the tuple itself, whereas list elements are mutable In this article, we will explain to you what is a tuple in python and the various operations on it. Creating a ... Read More

How to find what is the index of an element in a list in Python?

Pranav Indukuri
Updated on 08-Nov-2022 11:48:30

267 Views

Index in python refers to the position of the element within an ordered list. The starting element is of zero index and the last element is of n 1 index where n is the length of the list. In this tutorial, we will look at how to find out the index of an element in a list in Python. They are different methods to retrieve the index of an element. Using index() method Three arguments can be passed to the list index() method in Python − element − element that has to be found. start (optional) − Begin your ... Read More

Advertisements