Pythonista has Published 60 Answers

How can I convert a Python Named tuple to a dictionary?

Pythonista

Pythonista

Updated on 24-Jun-2020 07:01:01

Namedtuple class is defined in the collections module. It returns a new tuple subclass. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. The constructor takes type name and field list as arguments. For example, a ... Read More

Why we can't use arrow operator in gets and puts?

Pythonista

Pythonista

Updated on 22-Jun-2020 09:11:02

You can't read user input in an un-initialized pointer. Instead, have a variable of the struct data type and assign its address to pointer before accessing its inner elements by → operatorexample#include <stdio.h> struct example{    char name[20]; }; main(){    struct example *ptr;    struct example e;    puts("enter ... Read More

Extract decimal numbers from a string in Python

Pythonista

Pythonista

Updated on 22-Jun-2020 07:56:03

Using RegEx module is the fastest way.>>> import reAssuming that the string contains integer and floating point numbers as well as below −>>> s='my age is 25. I have 55.50 percent marks and 9764135408 is my number'the findall() function returns a list of numbers matching given pattern which includes digit ... Read More

Python - How to convert this while loop to for loop?

Pythonista

Pythonista

Updated on 20-Jun-2020 07:41:33

Usin count() function in itertools module gives an iterator of evenly spaced values. The function takes two parameters. start is by default 0 and step is by default 1. Using defaults will generate infinite iterator. Use break to terminate loop.import itertools percentNumbers = [ ] finish = "n" num = ... Read More

How to emulate a do-while loop in Python?

Pythonista

Pythonista

Updated on 19-Jun-2020 11:55:40

Python doesn't have an equivalent of do-while loop as in C/C++ or Java. The essence of do-while loop is that the looping condition is verified at the end of looping body. This feature can be emulated by following Python code −Examplecondition=True x=0 while condition==True:      x=x+1      print ... Read More

How to split Python tuples into sub-tuples?

Pythonista

Pythonista

Updated on 17-Jun-2020 10:15:55

Here is a tuple with 12 integers. In order to split it in four sub-tuple of three elements each, slice a tuple of three successive elements from it and append the segment in a lis. The result will be a list of 4 tuples each with 3 numbers.>>> tup=(7, 6, ... Read More

How can we convert a list of characters into a string in Python?

Pythonista

Pythonista

Updated on 16-Jun-2020 08:11:03

Python has an in-built join() function that returns a string by joining elements in a sequence object by inserting separator between elements. If we need a string without any separator, we initialize it with null string>>> lst=['h', 'e', 'l', 'l', 'o'] >>> str='' >>> str.join(lst) 'hello'Read More

How to check if a given key already exists in a Python dictionary?

Pythonista

Pythonista

Updated on 02-Mar-2020 09:54:14

The membership operator in can also be used with dictionary object>>> d1={1:'aaa', 2:'bbb', 3:"ccc", 4:'ddd', 5:'eee'} >>> 3 in d1 True >>> 9 in d1 FalseAdditionally, keys() method returns a view object of keys in dictionary. Membership operator in also tells you if key is present>>> 3 in d1.keys() TrueRead More

What is vertical bar in Python bitwise assignment operator?

Pythonista

Pythonista

Updated on 02-Mar-2020 09:53:31

Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two>>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101'Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

Pythonista

Pythonista

Updated on 02-Mar-2020 09:52:46

Python has magic methods to define overloaded behaviour of operators. The comparison operators (<, <=, >, >=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods.  Following program overloads < and > operators to compare objects of distance class. class distance: ... Read More

Advertisements