Pythonista has Published 23 Articles

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

Pythonista

Pythonista

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

156 Views

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

1K+ Views

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

What is vertical bar in Python bitwise assignment operator?

Pythonista

Pythonista

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

1K+ Views

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

4K+ Views

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

15K+ Views

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 to use continue statement in Python loop?

Pythonista

Pythonista

Updated on 27-Feb-2020 05:24:32

119 Views

The loop control statement continue abandons the pending statements in current iteration of the looping block and starts next iteration. The continue statement appears in a conditional block inside loopExamplex=0 while x

What keyboard command we have to stop an infinite loop in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 12:44:52

382 Views

Any loop is formed to execute a certain number of times or until a certain condition is satisfied. However, if the condition doesn't arise, loop keeps repeating infinitely. Such an infinite loop needs to be forcibly stopped by generating keyboard interrupt. Pressing ctrl-C stops execution of infinite loop>>> while True: ... Read More

What does the >> operator do in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 07:38:32

395 Views

It is a bitwise right shift operator. The bit pattern is shifted towards right by number of places stipulated by operand on right. Bits on left are set to 0For example a=60 (00111100 in binary), a>>2 will result in 15 (00001111 in binary)>>> a=60 >>> bin(a)result #39;0b111100' >>> b=a>>2 >>> ... Read More

How do we define dictionary in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:23:19

195 Views

Dictionary object is an unordered collection of key-value pairs, separated by comma and enclosed in curly brackets. Association of value with key is marked by : symbol between them.>>> D1={'a':1, 'b':2, 'c':3}Key can appear in a dictionary object only once, whereas single value can be assigned to multiple keys. Key ... Read More

How to convert an integer to a octal string in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:22:01

427 Views

We have use oct() function from Python’s library to convert any integer to its octal equivalent number. We get a string having octal representation>>> oct(100) '0o144' >>> oct(0x10) '0o20' >>> oct(10) '0o12'

Advertisements