Python Class_2

Multi-lines and comments

WEN LOU
4 min readMay 20, 2020

This week we’re going to step out another baby step. Last time we learned how to use Colab and print in the cell. NOW it’s time to know what is this print() meaning in Python.

In the coding world, we call print() is a “function” in Python. I know the first time it’s really hard to understand what does that means. But it just all illusion to make everything looking cool.

When you hear “function” in a programming language, just think it’s a way to call your machine to work for you. For example, you walk to an ATM, you are not going to talk to the machine to take out your money. You will put in your PIN and press a button “withdraw”, then everything will work inside the ATM, finally you will get your money. This button “withdraw” just like a function, you send a message to the machine, tell them how to work. Like print() in Python, you send a message to Python you need to print out something.

Let’s open your Colab, and practice some print() function. You can create a new notebook name it as “Multi-lines and comments” or use the one last time we created.

print("We")
print("are")
print("so")
print("COOOOOOOL")
print("!")

As you can see, we turned a single line code into multiple lines. Let’s try something different.

print("The first line.")
print()
print("Is this the second line?")
print("")
print("End line")

See the result, you can see some empty lines. print() is Python builtin function, if we don’t pass any value to the function (just keep “()” this empty), print() will just output an empty line. print() and print("") output the same result, so now you know how to print an empty line. What if we put some empty lines in the code?

print("try this")print("Is this the second line?")print("End line")
Empty lines in code

In this part when you print("Is this the second line?") the answer is “YES”. Those empty lines are not going to change anything in the result. Now you can use empty lines to make your code clear and pretty.

But how to make your code more readable?

Use comments in your code!

Comments do not execute in Python, it’s a way to explain your code and show your code to other humans to read it. It starts with #. Let’s write some comments.

# This is a comment.
print("Good job!")
The comment didn’t print out in the result.

You can put your comment at the end of the code. In Python Style Guide said when using inline comments, make sure they are separated by a least 2 spaces then start with # and 1 space. Like the example below, use the spaces to make your code clear.

print("Good Job!")  # This is a inline comment.

Of course, we can use multiple lines of comments in the code.

# Start the comments.
# Make sure you are using spaces.
# -----------
# LET'S BEGIN
# -----------
print("Try to make your comments")
print("End line")

Comments can also be used as a control when you need to test or change your code. Try to delete the # and print out the answer below in the code and run it.

print("What day is today?")# Uncomment the answer and run the code.
# print("Monday")
# print("Tuesday")
# print("Wednesday")
# print("Thursday")
# print("Friday")
# print("Saturday")
# print("Sunday")

Last practice, run the code first, then uncomment the # and run it again to see the difference.

print("  /\\  /\\  /\\  /\\")
print(" / \\/ \\/ \\/ \\")
print(" | ___ ___ |")
print(" | O O |")
print(" | ___ |")
# print(" . \\_/ . ")
print(" \\____________/")
Yea~! Did you see me?

In this class, we learn how to write multi-lines codes and comments. With this basic knowledge, we can start with data types in Python next time.

--

--