When you’re learning to code, you must to understand many terms (like class, variable, object, etc.). Especially when we’re focused in an object oriented language. Knowing these concepts isn’t needed for starting to code with Python. It has some features which help to students, those with an excellent math base. The knowledge curve is faster, so we can could with interactive sessions to learn the Python syntax and later we could make complete programs with advanced logic structures and expressions.
Math-like language. Comparators association.
An uncommon, but clear, example is the comparators association.
What’s the result of “1 > 2 < 3” expression using C or C++? If we evaluate the math expression, the result should be “False”. But the solution that C provides us is “True”. What is the reason? The reason is C evaluate by pairs the expression:
C/C++ |
1 > 2 = False = 0 |
1 > 2 < 3 = 0 < 3 |
1 > 2 < 3 = True |
But someone who is learning, this expression could mingle him.
On the other hand, Python solve this expression thus:
Python |
1 > 2 |
1 > 2 < 3 = False |
Then Python don’t evaluate the other part of the expression.
Other examples could be:
1 < 2 > 0
C/C++ | Python | Math |
1 < 2 = True = 1 | 1 < 2 = True | 1 < 2 > 0 = True |
1 < 2 > 0 = 1 > 0 | 2 > 0 = True | |
1 < 2 > 0 = True | 1 < 2 > 0 = True |
1 < 2 < 0
C/C++ | Python | Math |
1 < 2 = True = 1 | 1 < 2 = True | 1 < 2 < 0 = False |
1 < 2 > 0 = 1 > 0 = True | 2 < 0 = False | |
1 < 2 > 0 = True | 1 < 2 > 0 = False |
But C# don’t let that expressions if we want to do it, we have to write:
C# (C/C++ solution) | C# (Python solution) | Math |
Convert.ToInt32(1 > 2) < 3 = True | 1 > 2 & 2 < 3 = False | 1 > 2 < 3 = False |
C/C++ and Python also let the solution:
C/C++ | Python |
1 > 2 & 2 < 3 = False | 1 > 2 and 2 < 3 = False |
In short, both ways are logic solutions, but the simple python solution is easier to understand than the harder C/C++ solution that requires knowledge about type conversion.