The First usual step as we did in almost all the Programming languages is "Hello World!". This will teach you how to write, save and run Python programs. There are two ways of using Python to run your program - using the interactive interpreter prompt or using a source file. We will now see how to use both the methods one by one.
Using the interpreter prompt
Start the interpreter on the command line by entering python at the shell prompt. Now enter print 'Hello World' followed by the Enter key. You should
see the words Hello World as output. You can run the interpreter by IDLE program. IDLE is short for Integrated DeveLopment Environment. Click on Start -> All Programs -> Python 2.5 -> IDLE (Python GUI).
Note that the >>> signs are the prompt for entering Python statements.
EXAMPLE: Using the Python Interpreter prompt
2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.2.2
>>> print 'Hello World'
Hello World
>>>
Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text Hello World and this is promptly printed to the screen.
Choosing an Editor
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.2.2
>>> print 'Hello World'
Hello World
>>>
Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text Hello World and this is promptly printed to the screen.
Choosing an Editor
Before we move on to writing Python programs in source files, we need an editor to write the source files. The choice of an editor is crucial indeed. You have to choose an editor as you would choose a car you would buy. A good editor will help you write Python programs easily, making your journey more comfortable and helps you reach your destination (achieve your goal) in a much faster and safer way.
One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can see your program and visualize its running.
If you are using Windows, then I suggest that you use IDLE. IDLE does syntax highlighting and a lot more such as allowing you to run your programs within IDLE among other things. A special note: don't use Notepad - it is a bad choice because it does not do syntax highlighting and also importantly it does not support indentation of the text which is very important in our case as we will see later. Good editors such as IDLE (and also VIM) will automatically help you do this.
If you still want to explore other choices of an editor, see the comprehensive list of Python editors [http://www.python.org/cgi-bin/moinmoin/PythonEditors] and make your choice. You can also choose an IDE (Integrated Development Environment) for Python. See the comprehensive list of IDEs that support Python [http://www.python.org/cgi-bin/moinmoin/IntegratedDevelopmentEnvironments] for more details. Once you start writing large Python programs, IDEs can be very useful indeed.
I repeat once again, please choose a proper editor - it can make writing Python programs more fun and easy.
Using a Source File
Now let's get back to programming. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the 'Hello World' program - all it does is just say 'Hello World' when you run it.
Start your choice of editor, enter the following program and save it as helloworld.py
Example: Using a Source File
# Filename : helloworld.py
print 'Hello World'
First Check Module by menu Run -> Check Module or keyboard Shortcut Alt+X, Then Run this program by menu Run -> Run Module Script or the keyboard shortcut F5. The output will be same as previous.
If you got the output as shown above, congratulations! - you have successfully run your first Python program.
In case you got an error, please type the above program exactly as shown and above and run the program again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character in each line - we will see why this is important later.
How It Works
Let us consider the first line of the program. This is called comment - anything to the right of the # symbol is a comment and is mainly useful as notes for the reader of the program.
Python does not use comments except for the special case of the first line here. Note that you can always run the program on any platform by specifying the interpreter directly on the command line such as the command python helloworld.py .
One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can see your program and visualize its running.
If you are using Windows, then I suggest that you use IDLE. IDLE does syntax highlighting and a lot more such as allowing you to run your programs within IDLE among other things. A special note: don't use Notepad - it is a bad choice because it does not do syntax highlighting and also importantly it does not support indentation of the text which is very important in our case as we will see later. Good editors such as IDLE (and also VIM) will automatically help you do this.
If you still want to explore other choices of an editor, see the comprehensive list of Python editors [http://www.python.org/cgi-bin/moinmoin/PythonEditors] and make your choice. You can also choose an IDE (Integrated Development Environment) for Python. See the comprehensive list of IDEs that support Python [http://www.python.org/cgi-bin/moinmoin/IntegratedDevelopmentEnvironments] for more details. Once you start writing large Python programs, IDEs can be very useful indeed.
I repeat once again, please choose a proper editor - it can make writing Python programs more fun and easy.
Using a Source File
Now let's get back to programming. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the 'Hello World' program - all it does is just say 'Hello World' when you run it.
Start your choice of editor, enter the following program and save it as helloworld.py
Example: Using a Source File
# Filename : helloworld.py
print 'Hello World'
First Check Module by menu Run -> Check Module or keyboard Shortcut Alt+X, Then Run this program by menu Run -> Run Module Script or the keyboard shortcut F5. The output will be same as previous.
If you got the output as shown above, congratulations! - you have successfully run your first Python program.
In case you got an error, please type the above program exactly as shown and above and run the program again. Note that Python is case-sensitive i.e. print is not the same as Print - note the lowercase p in the former and the uppercase P in the latter. Also, ensure there are no spaces or tabs before the first character in each line - we will see why this is important later.
How It Works
Let us consider the first line of the program. This is called comment - anything to the right of the # symbol is a comment and is mainly useful as notes for the reader of the program.
Python does not use comments except for the special case of the first line here. Note that you can always run the program on any platform by specifying the interpreter directly on the command line such as the command python helloworld.py .
ImportantUse comments sensibly in your program to explain some important details of your program - this is useful for readers of your program so that they can easily understand what the program is doing. Remember, that person can be yourself after six months!
The comments are followed by a Python statement - this just prints the text 'Hello World'. The print is actually an operator and 'Hello World' is referred to as a string - don't worry, we will explore these terminologies in detail later.
Summary
Summary
You should now be able to write, save and run Python programs at ease. Now that you are a Python user, let's learn some more Python concepts.

No comments:
Post a Comment