Python if…else Statement – [ Control statements ] in Python with examples
I'm sorry to say that the overall impression of your tutorial is kind of sloppy. There are even examples that simply don't match their actual output.
Some remarks here and there:
In Python, control statement falls under three category:
a control statement falls under three catogories
Decision Statements.
Iteration Statements.
Jump Statements.
There's no need to use periods ('.') behind each line.
some statement a set of statements
You're either missing some interpunction or an 'or' there.
which gives us a boolean value
Here the term 'boolean' comes out of nowhere because in the earlier part you're talking strictly about the values 'TRUE' or 'FALSE', which for a novice aren't automatically known as being called Boolean.
if a>b: print(a) print(b) print(a,"is greater than",b) print(a+b)
This example uses a weird indentation of just one space.
Output:
value of a= 100
The 'value of' part can't be printed as the code prints
print("a=",a); <--- why do you use ; here?!
print("b=",b); <--- why do you use ; here?!
print(a,"is greater than",b)So you would see
a= 100 b= 20 a is greather than 20
We have following iteration statement of Python,
While lo0p.
for loop.
nested loops.
First sentence should end in a :, then there's again no need for ., then the first misspells 'loop' and uses a captial W while the others use lower case.
a=0
while a:
print(a,"T4tutorial")
print("Value of a",a)Output:
0 T4tutorial 1 T4tutorial 2 T4tutorial 3 T4tutorial 4 T4tutorial Value of a 5
How on earth can a while loop be run if a equals 0? And then how can a even increase?!
At the next for loop example, the code block stops after the first line, leaving #statements to execute hanging in regular markup.
'Nested loops in Python' is missing a heading style. Then both examples heave big heading styles instead of the smaller kind.
n=1 while n<10: print(n) n=n+1 if n==5: continue
Again the indentation is just one space (applies to both examples of break and continue), but the issue I have here is that the continue statement is redundant. The loop would have continued anyway without it. You should show continue as a way to change the flow:
n=0
while n<10:
n=n+1
if n==5:
continue
print(n)This will make the reader aware (if you include the correct output) that the continue has changed the expected result as value 5 is skipped from being printed.
Overall, please try to fix your examples to follow PEP8, it will make your code more readable and match the formatting of many other guides and tutorials that your readers will come across.
More on reddit.comPython Koans - Struggling with about_control_statements.py line 81
FYI, just to make sure I covered my bases, but I looked here as well
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRegex
assertRegex(s, r) | r.search(s) | 3.1
assertNotRegex(text, regex, msg=None)
Test that a regex search matches (or does not match) text. In case of failure, the error message will include the pattern and the text (or the pattern and the part of text that unexpectedly matched). regex may be a regular expression object or a string containing a regular expression suitable for use by re.search().
That confuses me more, making me think the result and text should be swapped...but I tried it and it doesn't work. Another error about searching lists when I have strings
TypeError: expected string or bytes-like object, got 'list'
Does programming involve a lot of IF statements or am I doing something wrong?
Think about it this way.
Learning to code is gathering tools, your if statements, functions, classes. That sort of stuff.
Programming is applying these tools in an appropriate way to solve your problem.
Now there are multiple appropriate ways to solve problems. Like “There are more than one ways to skin a cat”.
Now will all of these methods look pleasing or will some look like a hot mess?
A “good” programmer should be able to apply your tools in an appropriate manner to solve your problem.
If you can think that there is probably a simpler way to solve your problem, there probably is. That is when you need to look around and see what you can do.
More on reddit.com