Problem
Create a Hello World application in Python.
tl;dr
Make sure Python is installed on your system first.
Create a Python file:
nano helloworld.py
Type in your script:
#!/usr/bin/python
print "Hello World!"
Hit Ctrl-O then Ctrl-X to save your file. Then execute the script:
chmod +x helloworld.py ./helloworld.py
Solution
First, create your program:
nano helloworld.py
Python programs usually have a .py extension, although in Linux this is just a convention for user readability. It does not matter what extension you give the program, as what is used to execute it is determined by the hashbang, or the first line of the script beginning with #!, followed by the path to the interpreter. Where in bash you have #!/bin/bash, for python you usually have #!/usr/bin/python. Add this line to your program:
#!/usr/bin/python
Then, add your script:
print "Hello World!"
Hit Ctrl-O + Ctrl-X to save and exit your Hello World program.
Run the programBefore you can actually run the program, you need allow it to execute:
chmod +x helloworld.py
Finally, run it:
./helloworld.py
Output will look like this:
[root@user ~]# ./helloworld.py
Hello World!
- Updated