Found 27104 Articles for Server Side Programming

Overload unary minus operator in C++?

Nikitha N
Updated on 11-Feb-2020 07:09:40

6K+ Views

The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands.The unary operators operate on a single operand and following are the examples of Unary operators −The increment (++) and decrement (--) operators.The unary minus (-) operator.The logical not (!) operator.The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in ... Read More

How to generate prime numbers using Python?

Pythonista
Updated on 26-Feb-2020 12:46:08

5K+ Views

A prime number is the one that is not divisible by any other number except 1 and itself.In Python % modulo operator is available to test if a number is divisible by other. Assuming we have to find prime numbers between 1 to 100, each number (let us say x) in the range needs to be successively checked for divisibility by 2 to x-1. This is achieved by employing two nested loops.for x in range(1,101): for y in range(2,x): if x%y==0:break else: print (x,sep=' ', end=' ')Above code generates prime numbers between 1-1001 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

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

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

320 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: print ('hello') hello hello hello hello hello hello Traceback (most recent call last): File "", line 2, in print ('hello') KeyboardInterrupt

How to use else conditional statement with for loop in python?

Pythonista
Updated on 27-Feb-2020 05:22:06

167 Views

The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −Syntaxwhile expr==True:     #statements to be iterated while expr is true. else:    #this statement(s) will be executed afteriterations are over#this will be executed after the program leaves loop bodyexamplefor x in range(6): print (x) else: print ("else block of loop") print ("loop is over")OutputThe output is as shown below −0 1 2 3 4 5 else block of loop loop is over

How to emulate a do-while loop in Python?

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

241 Views

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 (x)      if x>=5: condition=FalseOutputThe output is as follows −1 2 3 4 5

How to use continue statement in Python loop?

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

107 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

Why there is not do...while loop in Python?

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

942 Views

PEP 315 (Python Enhancement Proposal) to include do..while statement has been rejected because it doen't fit in the general format of indented block statement: indented block used by every other Python compound statement. In words of Guido Van Rossum -  "Please reject the PEP. More variations along these lines won't make the language more elegant or easier to learn. They'd just save a few hasty folks some typing while making others who have to read/maintain their code wonder what it means".

How to break a for loop in Python?

Gireesha Devara
Updated on 23-Aug-2023 19:08:22

2K+ Views

In Python normally for loops are constructed to iterate over a block for each item in a range. If a premature termination of loop is sought before all iterations are completed, then we can use break keyword. It is invariably used in a conditional statement inside the body of a loop. Using the break statement Let’s take a few examples to see how the break statement works on for loops. Example In this example the for loop is defined to iterate upto 20 loops, but the break statement terminates the for loop at 10th iteration i.e., x=10. If we ... Read More

How to convert a Python for loop to while loop?

Pythonista
Updated on 11-Feb-2020 06:47:06

8K+ Views

Unlike while loop, for loop in Python doesn't need a counting variable to keep count of number of iterations. Hence, to convert a for loop into equivalent while loop, this fact must be taken into consideration.Following is a simple for loop that traverses over a rangefor x in range(5):      print (x) To convert into a while loop, we initialize a counting variable to 0 before the loop begins and increment it by 1 in every iteration as long as it is less than 5x=0 while x

How to stop an infinite loop safely in Python?

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

280 Views

Infinite loop is the one that doesn't stop on its own. It happens when the looping condition continues to remain true forever. In such a case, the loop must be forcibly stopped by pressing ctrl-C to generate keyboard interrupt

Advertisements