Before we move any further, we just need to cover two important aspects of the language that you'll encounter throughout this track:

  1. The print() function.
  2. The use of comments.

The Print Function

print()

First things first, in programming, a function is a named section of a program that performs a specific task.

Think of it like a small machine inside a factory: you put something in, it does some work, and then it gives you something back or shows you a result.

One such important function is the print() function.

The print() function in Python outputs data to the "console," which, in many cases, is the same as the terminal you've been using.

That's because for the time being, we'll be running our python code from the terminal and we will see the result of our code in the terminal.

data/admin/2024/8/terminal.png

When you run Python code from the terminal, it becomes the active console for that script. So, any output from print() will appear directly in the terminal window.

This is similar to typing commands directly into the terminal and seeing the responses there.

For instance, when you use the print() function in your Python code to display a message or show the value of a variable, that information is printed out in the terminal where you executed the script.

In other words, we're using the terminal to interact with and get feedback from our Python programs.

The basic syntax of the print() function is straightforward, whatever you want to print goes in between the parenthesis after the word, print.

print("Hello, world!")

A line of code that uses a function is referred to as a statement.

Comments

Now, we'll quickly look at the concept of comments.

In programming, comments are lines that you include in your code that the computer ignores when it runs the program.

Think of comments as notes or explanations that only humans are meant to read. These notes can help anyone who is looking at the code to understand what it does, why certain decisions were made, or to remind you of something you need to work on later.

To write a comment, simply start with the hash symbol (#) followed by your comment text.

print("Hello, world!")  # This comment follows a statement

You can also add the hash symbol (#) in front of a line of code to "comment it out," which means Python will ignore that line when running the program. This is useful if you want to temporarily disable a piece of code or leave notes for yourself without affecting how the program works.

Why Use Comments?

Comments are crucial for several reasons:

  • Clarification: They can explain what specific parts of the code do, especially if the code is complex or has a lot of steps.
  • Collaboration: Comments are helpful when working in teams as they allow you to communicate your thought process to others.
  • Debugging: Temporarily commenting out certain lines of code is a common method for isolating problems.

Types of Comments in Python

Python supports a couple of different ways to include comments in your code:

Single-line Comments

Single-line comments are the most common and are marked by a hash symbol (#). Anything on the line after the # will be ignored by Python.


# This is a single-line comment explaining the code below

Multi-line Comments

Although Python does not have a specific multi-line comment syntax like some other languages, you can use triple quotes (''' or """) to create a block of text that Python will ignore.


# This is a multi-line comment.
# Everything enclosed within these triple quotes is ignored by Python.
# Useful for longer explanations or temporarily disabling blocks of code.

Best Practices for Writing Comments

  • Relevance: Write comments that are relevant and useful. Explain why something is done, not just what is done.
  • Brevity: Keep comments concise and to the point.
  • Maintenance: Update comments if you change the part of the code they describe. Outdated comments can be more confusing than no comments at all.