Getting Started
After you have downloaded and installed Python you might want to create some programs. The simplest way is to use the Idle editor that comes with the main installation. There are many other editors for Python out there that are a lot more graphical, have more debugging features etc but for starters stick with Idle.
When you first start Idle you are in 'interactive mode' and you can just start typing statements that will be executed immediately after hitting the 'Enter' key. A good place to stop and explain the difference between translators and compilers. This is Ok for very simple programs and for demostration purposes but for anything useful you will want to place your code in a file. One useful tip for programming in this mode is that if you place the cursor at the end of a line and press 'Enter' that line will be copied as a new line to allow you to edit and execute, a huge time saver.
try out some simple code in interactive mode...
#Anything placed after the hash character is considered a comment
#-----------------------------------------
age = input('What is your age: ')
name = raw_input('What is your name: ')
if age >25:
print 'Hi ' + name + ' you are really old'
else:
print 'Hi ' + name
#---------------------------------------
Accepting data from the command line
You will notice that there are two different ways in Python to accept input interactively from the command line, input() and raw_input().
input() is more 'intelligent' than raw_input() and it tries to work out what type of data the user typed in. In the example above, after the program printed the prompt "What is your age: ", the user might have typed in 16. In this case, the input() function would turn it into an integer, which your program can then use calculations. On the other hand, the user might have typed in 'sixteen'. In this case, the input() function would have turned this into a string and this would give some very unexpected results if you tried to use it in any calculations. (We'll talk more about types this later).
raw_input() is not as intellegent as input(), but is often safer to use. No matter what the user types in to the program, the raw_input() function will always turn it into a string. Why is this safer? Because you always know what type of data you have and whether it can be used in calculations or not. When you use input(), you might end up with an integer, a string, or even a list or a dictionary or any of the other types available in Python.
So if you want to always be able to add up the ages typed into your program, what do you do? You use the raw_input() function to get a string. Then you use the int() function to change it into an integer.
age_string = raw_input(''What is your age: ')
age = int(age_string)
or, we can do it in a single statement:
age = int(raw_input(''What is your age: '))
In this way if the user types in 16, you will get an integer, while if the user types in sixteen, your program will crash with an error. Crash with an error? And this is meant to be safer? Well, yes actually. But in practice, Python lets us catch any errors like this and lets us tell the user he has made a mistake. You'll learn more about catching errors later.
The importance of Indenting
In Python indenting is crucial as the langauge does not use ending statements or special characters to denote the start and end of a block of code. It uses indenting to determine the start and end of a block of code.
For Instance the code fragment below will loop 10 times, during each loop it will print the current value of count (automatically incremented from 0 to 9) and then print count + 10. After the 10th loop it will print "The loop is finished", as this line is not indented it is not part of the for loop.
#---------------------------
for count in range(10):
print count
print count + 10
print "The loop is finished"
#--------------------------
Saving and running your program
To create a program that can be edited and saved click on 'File' and then 'New Window'. Save your file with the .py extension to associate it with Python and go to the 'Run' menu item and then 'Run Module" (or F5) to excute your program.
[Python_Tutorial] [Sequence_Python] [Interation_Python] [Decision_Making_Python] [Arrays_Python] [Files_Python]
Comments (0)
You don't have permission to comment on this page.