Python Programming/Creating Python Programs
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 standard text editor program.[1] What text editor you use will probably depend on your operating system: any text editor can create Python programs. It is easier to use a text editor that includes Python syntax highlighting, however.
Hello, World![edit]
The first program that every programmer writes is called the "Hello, World!" program. This program simply outputs the phrase "Hello, World!" and then ends. Let's write "Hello, World!" in Python!
Open up your text editor and create a new file called hello.py containing just this line (you can copy-paste if you want):
print('Hello, world!')
This program uses the print function, which simply outputs its parameters to the terminal. By default, print appends a newline character to its output, which simply moves the cursor to the next line.
Now that you've written your first program, let's run it in Python! This process differs slightly depending on your operating system.
Windows[edit]
- Create a folder on your computer to use for your Python programs, such as
C:\pythonpractice, and save yourhello.pyprogram in that folder. - In the Start menu, select "Run...", and type in
cmd. This will cause the Windows terminal to open. - Type
cd \pythonpracticeto change directory to yourpythonpracticefolder, and hit Enter. - Type
hello.pyto run your program!
If it didn't work, make sure your PATH contains the python directory. See Getting Python.
Mac[edit]
- Create a folder on your computer to use for your Python programs. A good suggestion would be to name it
pythonpracticeand place it in your Home folder (the one that contains folders for Documents, Movies, Music, Pictures, etc). Save yourhello.pyprogram into this folder. - Open the Applications folder, go into the Utilities folder, and open the Terminal program.
- Type
cd pythonpracticeto change directory to yourpythonpracticefolder, and hit Enter. - Type
python ./hello.pyto run your program!
Linux[edit]
- Create a folder on your computer to use for your Python programs, such as
~/pythonpractice, and save yourhello.pyprogram in that folder. - Open up the terminal program. In KDE, open the main menu and select "Run Command..." to open Konsole. In GNOME, open the main menu, open the Applications folder, open the Accessories folder, and select Terminal.
- Type
cd ~/pythonpracticeto change directory to yourpythonpracticefolder, and hit Enter. - Type
python ./hello.pyto run your program!
Linux (advanced)[edit]
- Create a folder on your computer to use for your Python programs, such as
~/pythonpractice.
- Open up your favorite text editor and create a new file called
hello.pycontaining just the following 2 lines (you can copy-paste if you want):[2]
#! /usr/bin/python print('Hello, world!')
- save your
hello.pyprogram in the~/pythonpracticefolder. - Open up the terminal program. In KDE, open the main menu and select "Run Command..." to open Konsole. In GNOME, open the main menu, open the Applications folder, open the Accessories folder, and select Terminal.
- Type
cd ~/pythonpracticeto change directory to yourpythonpracticefolder, and hit Enter. - Type
chmod a+x hello.pyto tell Linux that it is an executable program. - Type
./hello.pyto run your program! - In addition, you can also use
ln -s hello.py /usr/bin/helloto make a symbolic linkhello.pyto/usr/binunder 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 invoke mkdir ~/.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 into you're shell rc file for exemple ~/.bashrc)
Result[edit]
The program should print:
Hello, world!
Congratulations! You're well on your way to becoming a Python programmer.
Exercises[edit]
- Modify the
hello.pyprogram to say hello to someone from your family or your friends (or to Ada Lovelace). - Change the program so that after the greeting, it asks, "How did you get here?".
- Re-write the original program to use two
printstatements: one for "Hello" and one for "world". The program should still only print out on one line.
Notes[edit]
- ↑ Sometimes, Python programs are distributed in compiled form. We won't have to worry about that for quite a while.
- ↑ A Quick Introduction to Unix/My First Shell Script explains what a hash bang line does.