Many modern languages use both processes. They are first compiled into a lower level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language.
For the core material in this book, you will not need to install or run Python natively on your computer. Instead, you’ll be writing simple programs and executing them right in your browser.
At some point, you will find it useful to have a complete Python environment, rather than the limited environment available in this online textbook. To do that, you will either install Python on your computer so that it can run natively, or use a remote server that provides either a command line shell or a jupyter notebook environment.
1.5.1 Comments in Python
When you write code, it's important to explain why you're doing certain things in your script. This is where comments come in. Comments are like notes that you add to your code to help others (and yourself) understand what's going on. They're especially useful when you're dealing with complex ideas, formulas, or steps.
When you run your code, the computer ignores the comments completely. It only pays attention to the actual code and follows the instructions you've written. So, comments are just there to help humans understand the code better, while the computer doesn't pay any attention to them. Python employs three types of comments: single-line comments, multi-line comments, and documentation strings. By incorporating comments into code, developers can enhance its comprehensibility and aid others in understanding the code's purpose and functionality.
Types of comments in Python
Single-line comments are used to explain a single line of code. They start with the # symbol and continue until the end of the line.
Example:
# This is a single-line comment explaining the purpose of the following line
age = 25 # Save the user's age.
Multi-line comments are used to explain larger sections of code. They are enclosed within triple quotes (''' or""") and can span multiple lines.
Example:
'''
This is a multi-line comment explaining
the process of calculating the average.
'''
total = 0
count = 0
for num in numbers:
total += num
count += 1
average = total / count
- Documentation String (Docstring):
Docstrings are used to provide documentation for functions, classes, modules, and methods. They are enclosed within triple quotes and can be accessed using the help() function or various documentation tools.
Example:
def calculate_square(num):
"""
This function calculates the square of a given number
:param num: The input number.
:return: The square of the input number.
"""
return num ** 2
These comments help explain the code's intent and functionality, making it easier for developers to understand and work with the code, especially when collaborating or revisiting the code at a later time.
<