What is the groups() method in regular expressions in Python?

Rajendra Dharmkar
Published on 03-Jan-2018 12:13:35
The re.groups() methodThis method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.  In later versions (from 1.5.1 on), a ... Read More

What are regular expression repetition cases in Python?

Rajendra Dharmkar
Published on 03-Jan-2018 11:43:12
Things become more interesting when you use + and * to specify repetition in the pattern• + -- 1 or more occurrences of the pattern to its left, e.g. 'i+' = one or more i's• * -- 0 or more occurrences of the pattern to its left• ? -- match ... Read More

why Python can't define tuples in a function?

Pythonista
Published on 02-Jan-2018 23:40:43
Since Python 3.0, it is no longer possible to define unpacked tuple as a parameter in a function (PEP 3113). It means if you try to define a function as below:def fn(a, (b, c)):    passPython interpreter displays syntax error at first bracket of tuple. Instead, define tuple objects as ... Read More

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

Pythonista
Published on 02-Jan-2018 23:38:07
The index() method available to List (as well as other sequence type such as string and tuple) is useful to find first occurrence of a particular element in it.>>> L1=['a', 'b', 'c', 'a', 'x'] >>> L1 ['a', 'b', 'c', 'a', 'x'] >>> L1.index('a') 0To obtain indexes of all occurrences of ... Read More

What is syntax of tuple declaration in Python?

Pythonista
Published on 02-Jan-2018 23:35:57
A tuple is a comma separated sequence of items. The sequence can be optionally put inside parentheses.>>> t1=(1, "Ravi", 23, 546) >>> t1 (1, 'Ravi', 23, 546) >>> type(t1) <class 'tuple'> >>> t1=1, "Ravi", 23, 546 >>> t1 (1, 'Ravi', 23, 546) >>> type(t1) <class 'tuple'>Parentheses are essential if a ... Read More

How I can convert a Python Tuple into Dictionary?

Pythonista
Published on 02-Jan-2018 23:33:50
A dictionary object can be constructed using dict() function. This function takes a tuple of tuples as argument. Each tuple contains key value pair.>>> t=((1, 'a'), (2, 'b')) >>> dict(t) {1: 'a', 2: 'b'}If you want to interchange key and value, >>> t=((1, 'a'), (2, 'b')) >>> dict((y, x) for ... Read More

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

Pythonista
Published on 02-Jan-2018 23:32:14
Python’s built-in list class has append() method. We can get user input and add it to list till user presses Enter key. An infinite while loop contains input() function and append() methodL=[] while True:   item=input("enter new item")   if item=='':     break   L.append(item) print ("List : ", ... Read More

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

Pythonista
Published on 02-Jan-2018 23:29:49
In Python, variable is really a label or identifier given to object stored in memory. Hence, same object can be identified by more than one variables.>>> a=b=c=5 >>> a 5 >>> b 5 >>> c 5a, b and c are three variables all referring to same object. This can be ... Read More

How does Python generate random numbers?

Pythonista
Published on 02-Jan-2018 23:27:45
Standard distribution of Python has a random module that has functions for random number generation. Basic random() function returns a random floating point number between 0 and 1>>> import random >>> random.random() 0.5204702770265925From same module, there is randrange() function which returns a random number between a sequential range.>>> random.randrange(0, 10) ... Read More

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

Pythonista
Published on 02-Jan-2018 23:25:31
If you have a list object where each item is a list itself, and you put all items of all lists in another list object, it is called flat list. For example,
Advertisements