Introduction: Python Tutorial No.1
A brief introduction to Python will serve to give you a basic understanding of the history and what it is, before we move on to lessons.
What is Python?
Taken from www.python.org:
Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days.Many Python programmers report substantial productivity gains and feel the languageencourages the development of higher quality, more maintainable code.
In other words, Python is easy to learn, read, and write, ensuring that you'll be able to understand what you wrote a few months after you wrote it.
A Brief History of Python.
Python was conceived in the late 1980s by Guido van Rossum in the Netherlands as a successor of the ABC programming language, and it soon gathered popularity, especially after it was released under the GPL in version 1.6.1.
Today, some of the largest projects that use Python are the Zope application server, and the original BitTorrent client. It is also extensively used by Google and NASA.
Note: This instructable is being actively edited and improved by it's author(ZN13) and collaborator(Hugo.B) so please be patient and keep visiting, you'll find learning Python a rewarding experience.
Step 1: Download Python
To program in Python you'll need to download the Python libraries, and
the Integrated DeveLopment Environment, IDLE
As of 16/6/07, the release is version 2.5.1
Python Download here.
We'll assume that you are using windows here, but if you're using any
Linux-based OS, it'll likely already have it installed.
To find out, open console/konsole/terminal(varies with distro), and type
python. If it is installed, the Python command-line will open up.
After you have installed it, go Start>All Programs>Python>IDLE and we'll get started!
Step 2: Program Output, Print Statement, and "Hello World"
Anyway, here goes:
At the primary prompt(>>>) enter:
>>>print "Hello World"
And you'll receive the output of:
N.B. You need the (" ") characters to indicate to Python that you want it printed, otherwise
you'll receive this:
>>> print hello worldSyntaxError: invalid syntax
with "world" would be highlighted in red, to show where you went wrong.
Step 3: Variables
Type this into IDLE:
>>> myvar = "Hello World!">>> &apos&apos&aposprint&apos&apos&apos myvarHello World!
myvar is the variable in this example, but variables can also be numbers.
That is a short example of how a variable works.
Making things a little more complex now, an introduction to the string format operator:
The percent sign: "%" can be use it to replace text/data in a string:
>>> print "%s is number %d! " % ("Python", 1)Python is number 1!
"%s" means to substitute a string while "%d" indicates an integer should be substituted.
Another popular one is "%f" for floating point numbers.
Step 4: Program Input and the Raw_input() Function
raw_input()It reads from standard input and assigns the string value to the variable you designate.
For example, this is how it is used:
name = raw_input("Enter your name here: ")age = raw_input("Enter your age here: ")print "Your name is:", nameprint "And you are", age
When the Python interpreter reads the first line, it'll print the content in parenthesis(Enter your name here:), and when you input your name, it'll go on to the next line, do the same, but when it come across the "print" statement it prints the content in parenthesis, and comes across
"name" which is a variable, basically acting as a link to the content you entered earlier, with the following result:
>>>Enter your name here: Hugo.BEnter your age here: 16Your name is: Hugo.BAnd you are 16
At this stage, it'd be a good idea to introduce to the method of leaving comments.
As with most scripting and Unix-shell languages, the hash or pound (#) sign signals that a comment begins from the # and continues until the end of the line.
Note, in IDLE, whenever you type the # sign, it and all following text on that line turns red.
#Warning!!! This will put CPU usage up to 100% !!!counter = 0while counter < 1000000: counter += 1 print counter
Step 5: Ongoing...
Awaiting new content, please be patient.