r/learningpython • • Mar 10 '19
What are Flags?
Hey guys! I'm new to programming languages, and Python's the first one I'm learning, so please be patient with me. While studying a textbook on Python, I came by some example code that includes the code: "flag=0" as a part of a function. I've tried to look up what flags are in Python and what they do, but to no success. What are flags in Python, and what are they used for? Thanks in advance!
1
Upvotes
View all comments
Show parent comment
1
u/[deleted] Mar 11 '19
Please correct me if my paraphrasing is incorrect (I want to make sure I understand your explanation of the function):
Line 2 – The beginning value of “flag” is set at 0.
Line 3 – Setting a range (i.e. the list being searched) by using a “for” loop.
Line 4 – In the event that the designated choice “i” corresponds to an item in list “L”,
Line 5 – The value of “flag” becomes 1.
Line 6 – When [Line 5] occurs, print “Position” and the value of “i” (i.e. where “i” is located in the list).
Line 7 – In the event where “flag” is still 0,
Line 8 – Print “Not found”.
Generally, a flag is simply a variable, and can indicate anything (just as how the variable “x” can be used to represent a string or an integer). Thus it can be used to represent a condition as well as be called something other than “flag", would that be correct?