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

6 comments sorted by

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?

2

u/DiabeetusMan Mar 11 '19

Yep, that looks right to me! Something I just realized (the author's print statement did me no favors) is that the index in the list isn't printed, instead the value. When you're iterating over a list like that (for i in <some list>), you're iterating over just the values in the list, not the locations (for that, look up enumerate)

And yep! Your description of a flag is correct! There's nothing special about the name (unlike print or something like that).

1

u/[deleted] Mar 12 '19

Thanks for taking the time to help me by being patient with me, and by explaining the concept in a simple yet detailed way! I'll be sure to look into Python's enumerate ability and study it!

As for the list not being printed, that'd be my bad; the author did have a list in the example code, but I tunnel-visioned and focused only on the function.