Is there a label goto in Python

0 votes
is there a goto or any equivalent in Python to jump to a specific line of code?
Feb 3, 2022 in Python by Dev
• 6,000 points
747 views

1 answer to this question.

0 votes

Since Python is a structured programming language, it does not have a label or goto but by creating a function one can use goto in Python. In the official design Python History there is a mention to why there is no goto?
Exceptions can be used to provide a "structured goto" that works across function calls. 
Many people believe that exceptions can easily mimic all sensible uses of C, Fortran, and other languages' "go" and "goto" features

class label(Exception): pass  # declare a label

try:
    ...
    if condition: raise label()  # goto label
    ...
except label:  # where to goto
    pass
... 

You can't leap into the middle of a loop with this, but that's normally considered a goto misuse anyway. It is advised to use this function wisely and rarely.
Hope this helps!

answered Feb 4, 2022 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

Is there a label/goto in Python?

No, Python does not support labels and ...READ MORE

answered Aug 1, 2018 in Python by Priyaj
• 58,020 points
861 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,152 views
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 13,300 points
695 views
0 votes
1 answer

Is there a stack/heap in Python?

Memory management in Python involves a private ...READ MORE

answered Nov 9, 2018 in Python by SDeb
• 13,300 points
1,743 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Is there a way to create multiline comments in Python?

In Python, you can use '''  some ...READ MORE

answered Feb 7, 2022 in Python by Nandini
• 5,480 points
495 views
0 votes
1 answer

What is the use of "assert" in Python?

The statement assert exists in almost every programming ...READ MORE

answered Feb 7, 2022 in Python by Nandini
• 5,480 points
569 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP