Pythonista has Published 15 Articles

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

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 does the >> operator do in Python?

Pythonista

Pythonista

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

410 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 we can create a dictionary from a given tuple in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:14:12

410 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a', 'b', 'c', 'd') >>> T2=(1, 2, 3, 4) >>> dict((x, y) for x, y in zip(t1, t2))Dictionary comprehension syntax can ... Read More

How to insert a Python tuple in a MySQL database?

Pythonista

Pythonista

Updated on 18-Feb-2020 08:03:23

2K+ Views

Assuming that MySQL database named as test is present on server and a table named employee is also created. The table has five fields fname, lname, age, gender, and salary.A tuple object containing data of a record is defined ast1=('Mac', 'Mohan', 20, 'M', 2000)To establish interface between MySQL  and Python ... Read More

C++ 'a.out' not recognised as a command

Pythonista

Pythonista

Updated on 10-Feb-2020 10:47:17

5K+ Views

Having entered following command from linux terminal −$ g++ helloworld.cppThe a.out file should be created in the current working directory if the compilation is successful. Check if a.out is created.To execute enter following from command line −$ ./a.outIn most cases, output of your source program is displayed. However, as in ... Read More

How to convert a string to a integer in C

Pythonista

Pythonista

Updated on 27-Jan-2020 12:41:27

611 Views

First extract characters from left bracket '(' using strchr() function.char *name="The Matrix(1999)"; char *ps; ps=strchr(name,'(');Then add each character within brackets () to an char arraychar y[5]=""; int  p; for (p=1;p

compareTo() definition mistake?

Pythonista

Pythonista

Updated on 30-Jul-2019 22:30:22

164 Views

The example works correctly. The compareTo() method is called by a string object and takes another string object as argument. The return value is integer and is difference in Unicode values of characters of respective strings when they are not equal. The value can be -ve, 0 or +ve.

Advertisements