This block of code begins with a comment, followed by two more statements. In this example, we’ve identified the block of code that lies below the print_upper_case() function. Print_upper_case("taylor") # outside of the block # these statements make up a single block Example 1: Identifying a Block of Code in Python def print_upper_case(name): In Python, blocks of code rest on the same indentation level. This can include several statements as well as comments.
In general, a block of code refers to multiple likes of code that are grouped together. This is especially true with a large program, spanning multiple files. Why? Chances are, when you come back to the program in a month or two, you won’t remember exactly how everything works.
You don’t want your coworkers tearing out their hair trying to figure out what some badly named variable does.Īnd even if you are working alone, writing software in solo-mode, it’s a good idea to put some comments in the program. And without comments to help decipher the code, it can be challenging for new developers to get up to speed. Poor choices early on can lead to code that is difficult to maintain. For that team to function correctly, everyone needs to be on the same page. This means a larger codebase and a larger team. Why Writing Comments is ImportantĪs a project grows, it becomes necessary to expand. This allows us to test alternative logic or troubleshoot our programs. By blocking out a line of code, we can prevent it from being compiled.
We can also use comments to remove parts of a program for testing purposes.