Python for Beginners: Why Does Python Look the Way It Does?

python-for-beginners-code-generic

Beginning Python courses often start with the basics of programming, like how to print a string of text or how to perform mathematical operations. But they don't often cover why code looks the way it does. 

Consider, for example, the code snippet below. If you're a total beginner, you might feel a little confused and intimidated. Why are some sections of the code in different colors? Why are there breaks between some of the lines but not others? Why are there spaces around some of the characters but not others?

Screenshot 2020-01-31 at 10.51.25 PM

Let's dig in and answer some of those questions so that when you start writing your first lines of Python code, you have a better idea of what you're looking at!

Why does the code have different colors in different places?

The different colors you can see on most coding platforms and programs are just there to help us distinguish different sections of code. They don't actually have any effect on how the code runs​

In the gif below, you can see code with colors pasted into a notepad where there is no color highlighting. Both of these code snippets will do exactly the same thing, and the computer reads them as identical. The colors are just there for us humans to make the code more readable.

Screen Recording 2020-01-31 at 11.42 PM

As you learn more about programming, you'll find that the colors are helpful because they highlight different elements of your code. For example, the yellow in the code snippet above highlights numerical values (i.e., numbers).

(Note: these colors aren't universal. Depending on where you're writing your code, you might see different color choices for different code elements, or no colors at all. The examples in this article all show how text is colored on Dataquest).

Why are there spaces around some characters (like + and = )?

Much like the colors, these spaces are there for us humans, to make the code a little bit easier to read. The computer will simply ignore them, so your code will run the same whether or not you have spaces.

However, adding spaces makes the code easier on the eyes. Even though it's not required for the code to run properly, you will generally see Python written with these spaces anyway.

Screen Recording 2020-01-31 at 11.45 PM

(There are some exceptions to this convention, but if you're just a Python beginner, you don't have to worry about them right now!)

Why are some lines just plain blank?

Just as with the colors, and the spaces, those blank lines are just there to help make the code more readable.

In the gif below, the program will execute in exactly the same way whether or not there are blank lines. However, those blank lines make it easier for us humans to spot particular sections of code at a glance.

Screen Recording 2020-01-31 at 11.38 PM

So, again, while the blank lines aren't strictly necessary, it's good practice to split up the sections of your code that do different things using blank lines. That will make it easier for you (or somebody else) to scan and read your code later.

Why are different parts of the code typed on different lines?

Each line in a code represents a programming statement. You can think of each statement as being like a single instruction in a list of instructions. 

Consider, for example, instructions on how to drink water:

  1. Take a glass
  2. Fill it with water
  3. Drink the water

There is a sequence of steps here. To drink the water, we need to do them one at a time, in the correct order.

The same concept applies to programming. To get the computer to do what we want it to, we need to give it instructions (programming statements) one at a time, in a logical sequence.

Now, consider the following set of instructions:

  1. Take a glass Fill it with water Drink the water

Although it's not presented very clearly, a human could read this and figure out that it's actually three sequential steps. Computers aren't that smart, though. They need to be given steps one at a time, in sequence. 

That's what the line breaks are for. When we break code into a new line, the computer understands that each line is a unique step, and it will execute them in sequence.

  1. Take a glass
  2. Fill it with water
  3. Drink the water

For example, in the gif below, we first tell Python that the variable string has the value 'Hello'. Then, on the next line, we tell Python to print that variable. We must separate these two steps with a line break, or Python will attempt to execute them simultaneously, resulting in an error.

Screen Recording 2020-01-31 at 11.32 PM

Does capitalization matter in Python?

Yes. Python is a “case-sensitive” language, which means that capitalization does matter. 

For example, Facebookfacebook, and FACEBOOK are just as different to Python as Facebook and Google are. Because of the different capitalization, Python will read Facebookfacebook, and FACEBOOK as three completely different variables.

In the gif below, you can see that if we create a variable called Learn_DataScience and then attempt to view the variable learn_datascience, we’ll get an error.

That’s because of the capitalization — Learn_DataScience and learn_datascience are read as different variables by Python.

Screen Recording 2020-01-31 at 11.51 PM

Now you're ready to write your own code!

Hopefully, at this point you have a better understanding of why Python code looks the way it does. Now, it's time for you to dive in and start writing your own code!

Sign up for a free Dataquest account by clicking the button below. Our Python Fundamentals course will teach you to start writing Python from scratch, even if you have no prior experience!

Additional authors: Sahil Sunny and Nityesh Agarwal