Found 10784 Articles for Python

How to emulate a do-while loop in Python?

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

247 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

113 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

961 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 stop an infinite loop safely in Python?

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

285 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

How to prevent loops going into infinite mode in Python?

Gireesha Devara
Updated on 23-Aug-2023 19:02:26

2K+ Views

In python the while loop needs to be controlled by making some provision inside the body of the loop to drive the condition mentioned in the beginning to false. This is usually done by keeping count of iterations. If the while loop condition never evaluates to False, then we will have an infinite loop, which is a loop that never stops automatically, in this case we need to interrupt externally. count=0 while condition: stmt1 stmt2 . . count=count+1 Example Let’s take an example and ... Read More

How to print a value for a given key for Python dictionary?

Jayashree
Updated on 26-Feb-2020 11:22:05

5K+ Views

Python dictionary is collection of key value pairs. Value associated with a certain key is returned by get() method.>>> D1={'a':11,'b':22,'c':33} >>> D1.get('b') 22You can also obtain value by using key inside square brackets.>>> D1['c'] 33

How to convert float to integer in Python?

Gireesha Devara
Updated on 23-Aug-2023 18:42:17

1K+ Views

In python there are two number data types: integers and floats. In general integers do not have any decimal points and base value is 10 (i.e., Decimal). Whereas floats have decimal points. Python provides some built−in methods to convert floats to integers. In this article we will discuss some of them. Using the int() function The int() function converts the floating point numbers to integers, by removing the decimals and remains only the integer part. Also the int() function does not round the float values like 49.8 up to 50. Example In the example the data after the decimal ... Read More

How to convert an integer to a character in Python?

Gireesha Devara
Updated on 23-Aug-2023 18:36:02

22K+ Views

To convert an integer to a character in Python, we can use the chr() method. The chr() is a Python built−in method that returns a character from an integer. The method takes an integer value and returns a unicode character corresponding to that integer. Syntax char(number) Parameter The method takes a single integer between the range of 0 to 1, 114, 111. Return Value A unicode character of the corresponding integer argument. And it will raies a ValueError if we pass an out of range value (i, e. range(0x110000)). Also it will raise TypeError − for a non−integer argument. ... Read More

How to print out the first character of each list in Python?

Jayashree
Updated on 18-Jun-2020 13:35:47

4K+ Views

Assuming that the list is collection of strings, the first character of each string is obtained as follows −>>> L1=['aaa','bbb','ccc'] >>> for string in L1: print (string[0]) a b cIf list is a collection of list objects. First element of each list is obtained as follows −>>> L1=[[1,2,3],[4,5,6],[7,8,9]] >>> for list in L1: print (list[0]) 1 4 7

Advertisements