![]() If it fails, the second test (after the ‘elif:’) is analyzed, and so on. If the first test passes, that block of code is executed, and the rest of the conditional is skipped. This creates a chain of tests that happen in order. 5: print 'num1 is greater than 5' elif num2 > 5: print 'num2 is greater than 5' else: print "they're both too small!".The most common comparison operators are: This is the standard way to check equality, since the single equals ’=’ is reserved for assigning values to variables. In this example I use the double equals ’=’ operator to check if one thing equals another. Or the contents of a string: t = 'this is text' if t = 'this is text': print 'the text matches' For example, a conditional can test the size of a number: num = 7 if num > 5: print 'num is greater than 5' In addition to using Booleans, you can also create conditionals using various comparison operators. In this case, when b is True the first statement will execute, and when b is False the second statement will execute. ![]() Moving on, if a conditional test does not pass and the first block of code is passed over, it can be caught by an ‘else’ statement: b = True if b: print 'b is True' else: print 'b is False' On the bright side, this focus on proper indenting enforces a visual clarity in Python scripts that is often missing in other languages. Such errors are extremely common and can be quite annoying, since they may come either from improper indentation, mixing spaces with TABs or both. In this case you have indented to a level that does not exist in the code structure. Which means that something is wrong with your indenting. You will get an error saying IndentationError: unexpected indent On the other hand, if you inset the last line one level further: b = False if b: print 'b is True' print 'b is False' However, by deleting the indent on the last line, you take that line out of the nested structure, and it will now execute regardless of whether b is True or False: b = False if b: print 'b is True' print 'b is False' Will skip both print lines if b is False. For instance, this code: b = False if b: print 'b is True' print 'b is False' You have the option of using TABS or a series of spaces, but you cannot mix and match, and you have to be very explicit about the number of each that you use based on the level of the structure. The trick is that Python is very particular about how these insets are specified. In Python, a line ending with a ’:’ followed by inset lines of code is a basic syntax for creating hierarchical structure, and is used with all higher codes structures including conditionals, loops, functions, and objects. If you want to test for Falseness, you could use the Python shorthand ‘if not b:’ or write the full ‘if b is False:’. In this code, ‘if b:’ is shorthand for ‘if b is True:’. In this case, if b does not pass the test, the entire block of inset code after the first conditional line is skipped over and ignored. Try to run the code again, this time setting b to False to see that nothing happens. Here, since b is in fact True, it passes the test, causing the code that is inset after the ‘if b:’ line to execute. In Python, the most basic type of conditional will test a Boolean to see if it is True, and then execute some code if it passes: b = True if b: print 'b is True' ConditionalsĬonditionals are structures within the code which can execute different lines of code based on certain ‘conditions’ being met. ![]() The following two concepts, conditionals and loops, are the two basic ‘flow control’ structures which can actually alter the sequence in which our code is executed, thereby creating more complex behavior and more interesting functionality. Up to this point, our scripts have been pretty basic, and limited to only executing in a top-down order, with one command or operation per line. Now that we understand variables, we can start to develop more complex code structures which can build more interesting functionality into our scripts. Fundamentals of Python - Conditionals and Loops
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |