Pythonista has Published 49 Articles

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 struct example{    char name[20]; }; main(){    struct example *ptr;    struct example e;    puts("enter ... 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 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'

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() True

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'

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:   def __init__(self, ... Read More

How to overload Python comparison operators?

Pythonista

Pythonista

Updated on 02-Mar-2020 08:17:26

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:       def ... Read More

How does Python while loop work?

Pythonista

Pythonista

Updated on 27-Feb-2020 07:09:54

while statement is very popular looping statement in many languages including Python. Is general usage is −while expr==True:     stmt1     stmt2     .....The block of statements with increased indent after : symbol will be repeatedly executed as long as expr remains true. Obviously, certain provision must ... Read More

Advertisements