I was surprised when I first ran a simple code sample, which I had found online, that it did not work, considering that I had the same exact language as I was supposed to. When I fixed my indentation, everything worked. It is important, when coding in Python, to observe the proper structure of your conditional statements, especially the use of white space, because, whereas white space doesn't matter in other aspects of coding in Python, for conditional statements, its use is critical.
The three key words for conditional statements in Python are "if," "elif," and "else." These words introduce conditions and can form a simple constructs or more complex nested conditional statements.
Comparison operators in Python, which are used in conditions and result in the answer of true or false, are mostly the same as we are used to in languages such as Java. They include ==, !=, <> (same as !=), <, >, <=, and >=.
A colon must follow immediately after every condition, and statements and nested conditions must be indented. Multiple statements can follow a single condition (see the last two lines in the code sample below). The following snippet, demonstrates Python conditional syntax:
The result is:
Conditional statements in Python are pretty easy to get used to, but definitely watch out and make sure you use proper language ("elif," not "else if"), colons after conditions, and proper indentation!
In response to the questions left in the comments:
There is a something like a ternary conditional in Python. Two examples are provided below, using the Python shell, yielding immediate results:
Additionally, one is certainly able to construct as simple if-statement in Python. And here is an example of that, using a Python module followed by a snip of the output code from the shell:
Sources:
https://www.tutorialspoint.com/python/nested_if_statements_in_python.htm
https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator
Does Python have a ternary conditional operator or something similar to construct a conditional statement in one line?
ReplyDeleteYes, thank you for bringing this to my attention! I have to be honest, I have never used a ternary conditional before seeing this, so the concept is new to me. I answered your question with an example I added to the blog.
DeleteThis would make some intro students very unhappy as there are many who take consistent indentation as suggestions.
ReplyDeleteTo follow up on Truc's question - can you have a simple conditional statement - just a conditional and the "then" cause without an 'else' or 'elif'? Your example covers a lot of cases - GOOD - but it doesn't show an isolated 'if' - is that kind of construct allowed?
Yes, Dr. Palmer. I added an example of a simple conditional to the blog.
DeleteThat's really interesting about the indentation. Does it matter if the indentation is made with spaces or tabs? And is there a minimum number of spaces required to make the code run?
ReplyDeleteThe Python default is typically 4 spaces. The default number of spaces in an indentation might depend on your text editor. You can change the default number of spaces in an indentation in the settings of your text editor (Preferences > Fonts & Tabs > Indentation Width). But the easiest way to indent is to use 'tab' on your keyboard. This ensures that you get it right!
Delete