Python Programming/Creating Python programs
From Wikibooks, the open-content textbooks collection
| Previous: Interactive mode | Index | Next: Variables and Strings |
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.
[edit] Hello, World!
The first program that every programmer writes is called the "Hello, World!" program. This program simply outputs the phrase "Hello, World!" and then quits. 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 statement, which simply outputs the rest of that line to the terminal. print ends with a newline character, which simply moves the cursor to the next line: if you want to continue printing on the same line, you can end the print statement with a comma, like so:
print "Hello, world!",
Now that you've written your first program, let's run it in Python! This process differs slightly depending on your operating system.
[edit] Windows
- 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 is cause the Windows terminal to open. - Type
cd \pythonpracticeto change directory to yourpythonpracticefolder, and hit Enter. - Type
python hello.pyto run your program!
If it didn't work, make sure your PATH contains the python directory. See Getting Python.
[edit] Mac
- 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!
[edit] Linux
- 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!
[edit] Result
The program should print Hello, world!. Congratulations! You're well on your way to becoming a Python programmer.
[edit] Interactive mode
Instead of Python exiting when the program is finished, you can use the -i flag to start an interactive session. This can be very useful for debugging and prototyping.
python -i hello.py
[edit] Exercises
- Modify the
hello.pyprogram to say hello to a historical political leader (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.
[edit] Notes
- ↑ Sometimes, Python programs are distributed in compiled form. We won't have to worry about that for quite a while.
| Previous: Interactive mode | Index | Next: Variables and Strings |

