Javaist learning Python – Part 2 – conditions
On February 8, 2016 by Andra With 0 Comments
- Programming
I will write about what something I found funny – the conditions
- Python has a elif statement. This makes the code easier to read. Python you have a +1 from me for this
- Ok… now we go to the part where we find that one does not need to check for a boolean condition. Not only it does not need to check for this, it is advised NOT to check against False, True or None (a singleton that is somehow similar to the null in Java). Everything except the next statements are considered True(This reminded me of highschool and C++):
- None
- False
- zero – for numeric types
- empty dictionaries, sequences
- 0 returned when calling __length__
- False returned when __nonzero__ is called
- Now, an even stranger thing: one can have an else after a for… say whaat? yep… One can face something like
for loop:
if condition:
….
break
else:
statement
So, what is this all about? The statement is executed when after the program finished the loop… Every time, with one exception: when a break was executed inside the loop. I still have mixed feelings about this, but probably after using more of it, it will come natural.
This was all from my Python adventure till this moment.