#!/usr/bin/env python
# This is a comment
"""
THis is a multiline comment
also used for code documentation
"""
print "Hello World!"
print """---------------------
Triple quotes can be used
to define multline strings
--------------------------------------"""Python is another very popular interpreted language. Programs in
Python use the .py extension. Python allows programs to be
compiled to bytecode, in which case the .pyc extension is
often used.
Single-line comments use the # symbol, and the
print function is used to print messages.
print in Python automatically adds a carriage return to the
string, just like puts in C.
Python allows you to define multi-line strings using triple quotes
and also allows you to document the program (very similar to the POD
system of Perl), adding so-called docstrings immediately
after the definition of any object or function in your program. When you
do this, the help function (especially useful when using
Python from an interactive interpreter) will allow you to access the
information you’ve added.
You can use these docstrings to add multi-line comments
to your programs.
Python indicates the end of instructions with a line return, instead of using a special character. In other words, Python only allows one instruction per line. Instructions are grouped by indenting them the same distance, which is equivalent to the braces used in other languages. In our simple Hello World program, it is not necessary to group any instructions, but we will soon see multiple examples.
DID YOU KNOW
Version 3 of the language is not fully compatible with the previous version 2, which is no longer officially supported since 2020. The latest version of Python 2 is 2.7.18, and although it is no longer supported, a lot of code developed in recent years is still running all over the world.
Parentheses in function calls are mandatory. In this case, what
happens is that in Python 2.7, print is a statement and not
a function. This has changed in Python 3.0 where we should modify our
program as follows:
#!/usr/bin/env python
# This is a line of comment
This is a multi-line comment
also used to document the code
print ("Hello World!")
print ("""---------------------
Triple quotes can be
used to define multiline strings
--------------------------------------""")To run our program, we must give it execution permissions and use the
appropriate interpreter in case we have not added the
she-bang to the program:
$ python hello.py
$ chmod +x hello.py
$ ./hello.py
If we have Python 2.7 and Python 3 installed on our system, we must
indicate python2 or python3 as the command
interpreter to execute our program.
We can compile our program to Python bytecode with the following instruction:
python -m compileall hello.py
This command will generate the file hello.pyc which will
generally take up considerably less space. We can execute these files in
the same way as the .py files or directly from the command
line if they have execution permissions and our system is configured
appropriately.
binfmt_miscGNU/Linux systems have a kernel module for executing different types of files, not just the standard ELF format. To see if the module is installed and which formats it supports:
ls /proc/sys/fs/binfmt_misc
Python was created by Guido van Rossum in the Netherlands and is based on the ABC language. The first official version was in 1991. Python 2 is no longer supported, and Python 3 is the current version, although there is still a lot of Python 2 code out there, so, for the time being, it is interesting to know the differences between the two versions.
Summary
- Interpreted language (compilable to bytecode)
- Extension:
py/pyc - Line terminator: Carriage return
- Groups instructions with: Indentation
- Multi-line comments:
doc-strings""" comment """ - Single-line comments:
# - String delimiter:
"string"or'string'or"""string""" - You can print text with
print(adds carriage return) - Supports multi-line strings:
""" string """ - Does not support
HERE-DOCS - Entry point: Beginning of the file