![]() ![]() (which is signaled by its lack of indentation relative to the The final print function is outside of the conditional The cases of whether y is or is not an int are made explicit Y = if type ( y ) = int : print ( "Oh good, an integer value:", y ) else : print ( "Oh darn, a non-integer value:", y ) y = int ( y ) print ( "Now I have *made* it an integer value:", y ) print ( "Whether you wanted it or not, you have an int now!" ) Variable have been properly assigned values): Some other examples of valid if conditions (again, assuming each The conditionals get larger and more complicated. What is "inside" and "outside" of an if expression, especially as OnĪ human level, Python's use of indentation here makes it easy to read Has been chosen to balance readability with space considerations. Portability to different computers and text editors, using only Indentation may be made up of spaces and/or TABs. Regardless of the value of x, because it is outside of the The colon in Line 1, the Python interpreter will know that there areĮxactly two lines to be read if x is greater than ten, due to LinesĢ-3 being indented and Line 4 not being so (signalling and end of theĬontents of the condition). (This assumes that x has been assigned a value previously.) After If x > 10 : xsq = x * x print ( "x-squared is:", xsq ) print ( "I am printing from *outside* the condition!" ) X-squared and print the result" could be translated as: So, for example, the conditional, "If x is greater than 10, calculate Reaches the end of file ( EOF), or the end of the Reaches a line that is not indented relative to if, The Python interpreter knows that it has reached the end of the list ![]() The to be done may be one or more lines.Įach line must be indented the same amount. The Python standard for indentation is 4 spaces. ![]() Relative to if indentation is the set of spaces at the ![]() The line following the if statement must be indented Mapping this to a spoken sentence, in this context you could think Several pieces (relational, comparative, etc.), but it is evaluatedĪ colon : follows the statement. Note that the statement may be complicated and contain Python interprets it as a Boolean value: whether it is anĮxpression or a single quantity, it is evaluated as either True If is a keyword in Python, and it is the start of theĪ statement (also known as a "predicate") follows the starting Syntax in several other Python structures): Syntax (particularly since we will see some similar features of this There are several important things to note about the conditional's Members of the if-condition family could deal with differently, WeĪnd this implicitly denotes that in the alternate case that theĬondition is not met, nothing special or new is done (different In fact there is a family of them, with related meaningĪnd syntax, but just differing in the number of branching points. Therefore, conditionals are also known as if The simplest and most common way to start a conditional in a standardĮnglish sentence is to use if, and this is also the keyword to This is anotherįundamental item in our toolbox of algorithmic programming. This kind of branching of command flow can be done using aĬonditional statement: if something is true, do commands A, B, C, Program in a non-trivial (and hopefully useful!) way. We are affecting and controlling the flow of the Possible that not every line of code will be executed: some might be Of commands (say, double it and add three), but if it were negative doĪ different set (say, cube it and subtract five). Perhaps, we could check a result, and if it were positive do one set Outcome (for example, if a number is a perfect square, print it). However, we might want to set up our code to calculate some values,Īnd then execute certain lines only in the case of a particular The features we have discussed so far: math functions, operator This is what would happen if our script only contained By default, the Python interpreter will move down a script file ofĬommands, executing each successive line once until it reaches the end ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |