coding, developer, Java, programming, python

Python_indentation is_part of_the code_itself

>>>Python_indentation == "__Can drive you crazy in the beginning__!"

python

My Python journey began exactly a month ago, and nothing about it drove me crazy like it indentation. My sole source of learning the language was the Python 101  book by Michael Driscoll.

Well, the Python 101  book did a very good job at telling me indentation was important but what I didn’t get was why and what will happen if I didn’t indent. I mean when I was learning Java and JavaScript, there were numerous books that said indentation was a good practice. So I told myself when I was writing my python code that my code will still run even if I didn’t indent.

That thought and carelessness of knowledge acquisition became my worst nightmare as I found myself spending over eight hours trying to figure out why my code wasn’t running.

The syntax error below became my worse nightmare with it recurrence:

SyntaxError: unindent does not match any outer indentation level

After series of youtube videos and stack Overflow, I finally came to understand the relevance of the Python indentation, which I low-key started to refer to as Python curly bracket -{}. The relevance and significance of the indentation in Python is due to the absence of curly bracket in the language. Unlike other programming languages that use curly braces to show the context or inclusion of lines of codes, python uses indentation.

indent

The code above results into the syntax error below:

error

So why is the code not running? its simple, after the if-statement, Python was expecting a line of code to end it but all it found was a print-statement, which occurs on the same line as the if-statement, making it independent of the if-statement and confusing the python shell which knows or assumes the print-statement should belong to the context of the if-statement.

How do we solve this? we make the print-statement dependent on the if-statement by making it occur on a column(col) greater than it:

Note: a column increase leads to a shift to the right of a character’s position.

col "if-statement" == 0
if-statement

col "print-statement" == 4
print-statement

so now the code will run because the print-statement has been made dependent on the if-statement.

run

The indentation(col) of the print-statement could have been anything aside zero as zero will make it independent or out of the context of the if-statement. The 4-length indentation is the most popular length used by Python programmers.

Also: because I started my code with an indentation of length-4, I need to maintain the same length across the current Python module. indent.py

To exit the if-statement, I will move back to the column 0 of whichever line I am currently on or prefer to be(i.e Ln:x col:0).

col0

I would not have faced the problem of indentation if Python were to be my first programming language because then I would not have taken indentation for granted. Before you start learning Python, bare in mind that indentation is_part of_the code_itself.

Advertisements
coding, developer, Java, programming, Uncategorized

The Sensitive Ancestor : Java

Looking back now after getting very familiar with other programming languages i.e JavaScript, PHP and Python, I know for sure, Java could never have worked as choice two.

meHN! never say never, it could have but all I am trying to say is it would have been hundred percent harder for me to transition from a weakly typed language like JavaScript and Python to Java.I mean imagine learning that a variable upon declaration could belong to any datatype (int, string, float) and also that at any given point in time you could also change it’s content to a whole new different datatype i.e weakly typed language:

Only to transition to a strongly typed language like Java where you not only need to specify the datatype of the variable upon declaration but also need to note that the variable datatype could never be changed, i.e if you declare a variable with a particular datatype, perhaps an array of type int, int[] array; you cannot insert string values into it i.e array = new int {"Maya","Java"}this won’t work! It will result in a big fat error which I can guarantee will drive you crazy!

Java is an interesting language to start with as a developer if you are hoping to gain more exposure to the very basic and fundamentals of programming. It was only when I was learning Java that I knew I had to be careful with anything and everything I typed.

Don’t get me wrong every programming language has it own frustrating side but for this particular blog, we are talking about the ancestor of all programming languages_ Java! The language I still owe most of my comfort and easy transition into other programming languages to.

If you are not sure about the language to start with as a new developer, I will advise you start with Java because even though it will frustrate and most likely make you feel dumb, you will thank the universe for it for every other new programming language you transition into.

Uncategorized

The Developer Journey Begins

Thanks for joining me!

Good company in a journey makes the way seem shorter. — Izaak Walton

post

I am Sumaya Ahmed Salihs, a current computer science student at the African Leadership College, Mauritius (ALC ). I started this blog to share my experience as a Student developer and lover of programming. This blog is my way of sharing the joy, passion and adventures I pass through in the technical field with like minded individuals.