(My beloved) Python can trick you

Daniel Costa
codeburst
Published in
5 min readJun 18, 2018

--

“Learning to code” via Giphy

This article is a real case of how my dear Python tricked my teammate. More than that, a questioning about something I’ve been reading a lot about in last few days. Is Python a good language to learn programming?

The main argument I read from the people that think Python is the best language for this purpose is: “It’s easy to learn because the language is natural, it’s like you’re reading something”.

I agree that Python reading is natural, but, is that a good thing? It’s an amazing thing when you know some programming languages, programming logic, algorithms and other basic stuff. But, when you’re beginning, how good is to learn something that is syntactically different of most languages that you’ll probably need to learn someday?

I started to learn programming with 8 years old, in 2002, sitting next to my older brother, when he was learning Visual Basic 6 and Delphi 7. Yes, I know it’s an uncommon way to learn (I think). When I started programming classes, in 2009, in a technical school, they started with Pascal. When I started in University, in 2012, they also started with Pascal. Nowadays, the same University’s programming classes start with Python.

So, what’s the best choice? Before some considerations, here is a snippet of how Python tricked my teammate (he is an experient PHP Developer that was learning Python, so he could help in a side project)

My teammate forgot the ‘and’ word, and, the python silently charged him for it

I don’t remember exactly the variables or reason of the code, just the issue. Supposing that some Order X came from somewhere (a database, for example), and, after handled, we had two variables derived from Order X: the status (some_received_ret) and the categories (some_received_list_of_order_categories). And, for some other reason, we need to check if the status is equal to ‘APPROVED’ and ‘S1’ is one of the categories to execute something.

Yes, I know that is weird the string ‘APPROVED’ not be a constant, the same for ‘S1’. But, the code was like that. He forgot the reserved word and and didn’t created test cases for that code, and, Python charged him for it.

He came to ask for help, saying that he logged the variables status and categories before the if statement, and that they were ‘APPROVED’ and [‘S1’, ‘S2’, ‘S3’], respectively, and, for some arbitrary reason, the Python didn’t entered the if statement.

So, I read the code and realized that the conditional in the if statement was missing the reserved word and. And he asked me: “How can it work with no syntax errors?”. And that is the bad point I think about start programming with Python. His experience with other languages syntax was not enough to realize what Python interpreter did.

Summing up, when He (my teammate) broke the line, he didn’t realize that he forgot the and. So, Python interpreter concatened the strings ‘APPROVED’ and ‘S1’ and evaluated if some_received_ret was equal to ‘APPROVEDS1’, that, obviously, returned False, and, finally, Python evaluated if False was in some_received_list_of_order_statuses. That always would be False, and never would go into the if.

And that is my point: I know that every language has its particularities, but Python was built about particularities and with his beautiful unique syntax, that can be a problem to the evolution of beginners that started with Python.

And we don’t need to go so far away, here is some comparisons of some popular languages and its similarities, or not.

For Loop

Simple loop in a range of 0 to 10.

C++

for(int i = 0; i < 10; i++) {
do something
}

PHP

for($i = 0; $i < 10; $i++) {
do something
}

Javascript

var i;
for(i = 0; i < 10; i++) {
do something
}

Java

for(int i = 0; i < 10; i++){
do something
}

Python

for i in range(10):
do something

Functions

Simple declaration of a function that sums two integer numbers.

C++

int sum(int a, int b) {
return a + b;
}

PHP

function sum($a, $b) {
return $a + $b;
}

Javascript

function sum(a, b) {
return a + b;
}

Java

public int sum(int a, int b) {
return a + b;
}

Python

def sum(a, b):
return a + b

If Statement

  • if 1 is equal to 2 and 1 is equal to 3, do something 1.
  • else, if 3 is not equal to 10 or some variable X is not true, do something 2.
  • else, do something 3.

C++

if (1 == 2 && 1 == 3) {
do something 1;
} else if (3 != 10 || X != true) {
do something 2;
} else {
do something 3;
}

PHP

if (1 == 2 && 1 == 3) {
do something 1;
} else if (3 != 10 || $X != true) {
do something 2;
} else {
do something 3;
}

Javascript

if (1 == 2 && 1 == 3) {
do something 1;
} else if (3 != 10 || X != true) {
do something 2;
} else {
do something 3;
}

Java

if (1 == 2 && 1 == 3) {
do something 1;
} else if (3 != 10 || X != true) {
do something 2;
} else {
do something 3;
}

Python

if 1 == 2 and 1 == 3:
do something 1
elif 3 != 10 or X is not True:
do something 2
else:
do something 3

Those are just some few examples, and there is a lot of more, like the simple fact that the indentation in Python is a syntax rule, the existence of or/and reserved words, or the fact that the True and False booleans start with uppercase.

So, is not Python the best language to begin with programming? What is the best language for that purpose?

Unfortunately, I don’t have those answers, and I really wanna know too. I really really love Python and that mix of power and easiness, but, Python can be cruel too. So, my post was a kind of warning for when you need to recommend some language to a beginner. But, my major tip is: Think with the beginner’s mind, that knows nothing (or almost nothing) about programming and needs to learn (sometimes without a teacher), and improve his skills in the future. Think about a beginner that can get lost in the infinite areas of programming and will try to learning about Front-end with Javascript, about Game Development with C# or about desktop application with Java.

How Python could help the beginner programmer in his start of the magical discovering? How make this journey always interesting to him as that is for us?

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

--

--

Senior Software Engineer @ Netflix | ex-Amazon | Opinions are my own.