Writing Our First Code
Get familiar with using print statements, comments, and docstrings.
We'll cover the following
Let's get started with the fun stuff by diving directly into code.
The print()
function
When learning any new programming language, a common tradition is to start by displaying the text "Hello, World!" on the screen. Each language has its own syntax for outputting or printing information. Python, known for its readability, makes this task simple by using the print()
function.
Here’s how the print()
function works in Python:
print("Data to be printed")
All content to be printed is enclosed within the parentheses that follow the print
keyword. Now, let’s attempt to print “Hello World” in the terminal.
print("Hello World")
It's important to note that the text Hello World
is enclosed in quotation marks because it represents a string, which is a sequence of characters.
Next, we’ll print some numbers. Each call to print
will move the output to a new line.
print(50)print(1000)print(3.142)
Did you notice that we haven't enclosed the numbers inside quotation marks? If we had done that, the interpreter would have treated the numbers as strings. The difference between the two will become more clear when we discuss data types.
Printing multiple pieces of data
We can print multiple items in a single print
command by simply separating them with commas.
Try it yourself
See if you can repair the following code and make it run successfully. Our AI Mentor can guide you as well.
print(50, 1000, 3.142 "Hello World"
By default, each print
statement prints text in a new line. If we want multiple print
statements to print in the same line, we can use the following code:
print("Hello", end="")print("World")print("Hello", end=" ")print("World")print("Hello", end=", ")print("World")
The value of end
is added to the output, and the next print
statement will continue from that point.
Comments
Comments are pieces of text used to describe what is happening in the code. They have no effect on the code whatsoever. However, comments in coding serve several important purposes. They enhance code readability, and make it easier to understand, debug, and maintain code. In large teams, comments facilitate collaboration because team members can use them to understand each other’s code, as well as their own code.
Best practices for writing comments:
Comments should be accurate, clear, to the point, and meaningful—explaining the code's functionality in a way that is easy to understand.
The level of detail in comments can vary depending on the complexity of the code—ranging from a one-liner for simpler code to a more detailed explanations for intricate algorithms.
Maintain a consistent style and formatting for your comments throughout the codebase for better readability.
A comment can be written using the #
character in Python. The following code widget shows how to write single line comments. Note that each line of comment needs to start with the #
character.
print(50) # This line prints 50print("Hello World") # This line prints Hello World# This is just a comment hanging out on its own!# For multi-line comments, we must# add the hashtag symbol# each time
An alternative to these multi-line comments are docstrings—short for documentation strings. They are encased in triple quotes, '''
, and can be used to replace multi-line comments.
''' Docstrings are pretty coolfor writing longer commentsor notes about the code '''print(50) # This line prints 50print("Hello World") # This line prints Hello World
In Python, comments and docstrings serve different purposes:
Comments are used to explain specific lines or blocks of code. They are meant for developers to understand the logic and intent behind the code. They are not executable. Use comments to clarify complex code, note assumptions, and highlight important points.
Docstrings take a broader view of different components of your code and are used to provide documentation at a more macro level. They are accessible via the
help()
function and are meant to describe the overall purpose and usage of the code. According to the Python Style Guide, each script and all publicly accessible pieces of code should have a docstring at the beginning to guide users on how to use the code effectively.