Simple ' Hello world! '
Python command line
Assuming you have already installed Python, enter it on the Linux command line:
The code is as follows:
$python
Will go directly to Python. Then, at the command line prompt >>> later enter:
The code is as follows:
>>>print (' Hello world! ')
You can see it and then output it on the screen:
The code is as follows:
Hello world!
Print is a commonly used function that outputs strings in parentheses.
(in Python 2.x, print can also be a keyword that can be written as print ' Hello world! ', but this won't work in 3.x)
Write a small program
Another way to use Python is to write a Python program. Write a file with a. py ending in a text editor, such as hello.py
Write the following in hello.py, and Save:
The code is as follows:
Print (' Hello world! ')
Exit the text editor and enter it at the command line:
The code is as follows:
$python hello.py
To run hello.py. You can see that Python then outputs
The code is as follows:
Hello world!
Script
We can also change the Python program hello.py to an executable script that executes directly:
The code is as follows:
#!/usr/bin/env python
Print (' Hello world! ')
You need to modify the permissions of the above program to be executable:
The code is as follows:
chmod 755 hello.py
Then, in the command line, enter
The code is as follows:
./hello.py
You can run it directly.
Summarize
Print
Command-line mode: Run Python, enter commands at the command line, and execute.
Program mode: Write a Python program and run it.