Welcome to Python! This tutorial will show you how to start writing programs.
Python programs are nothing more than text files, and they may be edited with a standardtext editor program.[1] What text editor you use will probably depend on your operating system: any text editor can create Python programs. However, it is easier to use a text editor that includes Pythonsyntax highlighting.
The very first program that beginning programmers usually write or learn is the"Hello, World!" program. This program simply outputs the phrase "Hello, World!" then terminates itself. Let's write "Hello, World!" in Python!
Open up your text editor and create a new file calledhello.py
containing just this line (you can copy-paste if you want):
print('Hello, World!')
The below line is used for Python 3.x.x
print("Hello, World!")
You can also put the below line to pause the program at the end until you press anything.
input()
This program uses theprint
function, which simply outputs its parameters to the terminal. By default,print
appends anewline
character to its output, which simply moves the cursor to the next line.
Note: In Python 2.x, print is a statement rather than a function. As such, it can be used without parentheses, in which case it prints everything until the end of the line and accepts a standalone comma after the final item on the line to indicate a multi-line statement. In Python 3.x, print is a proper function expecting its arguments inside parentheses. Using print with parentheses (as above) is compatible with Python 2.x and using this style ensures version-independence.
Now that you've written your first program, let's run it in Python! This process differs slightly depending on your operating system.
C:\pythonpractice
, and save yourhello.py
program in that folder.cmd
. This will cause the Windows terminal to open.cd \pythonpractice
tochangedirectory to yourpythonpractice
folder, and hit Enter.hello.py
to run your program!If it didn't work, make sure your PATH contains the python directory. SeeGetting Python.
pythonpractice
and place it in your Home folder (the one that contains folders for Documents, Movies, Music, Pictures, etc). Save yourhello.py
program into it. Open the Applications folder, go into the Utilities folder, and open the Terminal program.cd pythonpractice
tochangedirectory to yourpythonpractice
folder, and hit Enter.python ./hello.py
to run your program!Note: If you have both Python 2 and Python 3 installed (Your machine comes with a version of Python 2 but you can installPython 3 as well), you should runpython3 hello.py
~/pythonpractice
, and save yourhello.py
program in that folder.cd ~/pythonpractice
tochangedirectory to yourpythonpractice
folder, and hit Enter.python ./hello.py
to run your program!Note: If you have both Python version 2.6.1 and Python 3.0 installed (Very possible if you are using Ubuntu, and ransudo apt-get install python3 to have python3 installed), you should runpython3 hello.py
~/pythonpractice
.hello.py
containing just the following 2 lines (you can copy-paste if you want):[2]#! /usr/bin/pythonprint('Hello, world!')
Note: If you have both python version 2.6.1 and version 3.0 installed (Very possible if you are using a debian or debian-based (Ubuntu, Mint, …) distro, and ransudo apt-get install python3 to have python3 installed),use
#! /usr/bin/python3print('Hello, world!')
hello.py
program in the~/pythonpractice
folder.cd ~/pythonpractice
tochangedirectory to yourpythonpractice
folder, and hit Enter.chmod a+x hello.py
to tell Linux that it is an executable program../hello.py
to run your program!ln -s hello.py /usr/bin/hello
to make asymboliclinkhello.py
to/usr/bin
under the namehello
, then run it by simply executinghello
.Note that this mainly should be done for complete, compiled programs, if you have a script that you made and use frequently, then it might be a good idea to put it somewhere in your home directory and put a link to it in /usr/bin. If you want a playground, a good idea is to invokemkdir ~/.local/bin
and then put scripts in there. To make ~/.local/bin content executable the same way /usr/bin does type$PATH = $PATH:~/local/bin
(you can add this line to your shell rc file, for example ~/.bashrc).Note: File extensions aren't necessary in UNIX-like file-systems. To linux, hello.py means the exact same thing as hello.txt, hello.mp3, or just hello. Linux mostly uses the contents of the file to determine what type it is.
johndoe@linuxbox~$file/usr/bin/hello/usr/bin/hello:Pythonscript,ASCIItextexecutable
The program should print:
Hello, world!
Congratulations! You're well on your way to becoming a Python programmer.
hello.py
program to say hello to someone from your family or your friends (or toAda Lovelace).print
statements: one for "Hello" and one for "world". The program should still only print out on one line.