Advertisement
     
 
 
Search:
General | Linux Hacking | Linux Networking | Linux Security | Windows Hacking
       
Introduction to Python. PDF Print E-mail
Written by 7he gener@l   
Tuesday, 12 April 2005
Ok, first part is going to be fairly boring, but put up with it. Im just going to go through a few reasons why you should choose Python as a language to learn. First off it free!, there is no cost involved in gettin Python, although there is a license, but this is just a standard license that goes with all free and open source software. Python is an open source language, as is the interpreter (Python is written in C). To do so visit http://www.python.org .


1) Why Python?

Ok, first part is going to be fairly boring, but put up with it. Im just going to go through a few reasons why you should choose Python as a language to learn. First off it free!, there is no cost involved in gettin Python, although there is a license, but this is just a standard license that goes with all free and open source software. Python is an open source language, as is the interpreter (Python is written in C). To do so visit http://www.python.org .

Secondly the platform capability is great, it will run on just about every operating system, including: Unix (as well as Mac OSX), Linux, Windows, MacOS. It is also an interpreted language, which means Python doesnt need a compiler to convert the code to Binary form. In Python you also dont have to worry to much about syntax (therefore the rules of the language), since Pythons syntax is very simple, and easily read. Unlike most other languages, Python uses way less bracketing. 

2) Program Development

When designing and creating software there are about seven stages you must follow for a program to turn out correct, and you will also become a better programmer if you follow these.

-Develop and document a set of program specifications.
-Design the program using a design tool (such as pseudo code which I will talk about later).
-Write the code.
-Debug the code.
-Write the program documentation.
-Install the new software.
-Maintain the software.

Although steps 3 and 4 are the actually programming part, steps 1 and 2 are the most important, you are also able to stop at step 4, but most professional programmers write documentation for software, and also install the software.

2a) Design The Program

As my example I will use the same example I used when learning Python. What we will be doing is converting fahrenheit to celsius. Now there are two ways you can desing the program, one being a flowchart, the other using pseudo code. Flowcharts are pretty much graphical representations of the problem. Some of the rules for using flowcharts are as followed:

- They do not have arrows to indicate the direction of flow unless a loop is being represented.
- Flow is from top to bottom.
- They have four basic symbols.

and that's basically it, after a few flowcharts you will get better, and understand them more.

Now Pseudo code! Ok, pseudo code isnt real code, and is pretty much plain english that looks like computer code. It is a good design toll since it doesn't have any firm syntax. You just have to write down you ideas without worrying that the computer is going to spit a range of errors at you. Here is a quick example using Fahrenheit conversion:

input fahr
celsius = (fahr - 32) * 1.8
write celsius

Now the equivelent in Python code is:

fahr = input("Please enter a temperature in fahrenheit>")
celsius = (fahr - 32) * 1.8
print celsius

Now, here is another piece of pseudo code from the "mystery algorithm" as it is known.

input aNum
count = 1
if count <= 12
product = count * aNum
count = count + 1
write product

Now notice that anything that is to be repeated (inside a loop) is intended to make it easier to follow the code.

Here it is again but in Python code:

aNum = input("Please enter an integer >")
count = 1
if count <= 12:
product = count * aNum
count = count + 1
print product

The block code is also indented in python code. Now we will go onto overleaf, it is now the addition of five numbers:

total=0
repeat five times
input number
add number to total
print total

In python the above would look like this:

total=0
print "enter you five marks."
for i in range(5):
myNum=input(">")
total=total + myNum
print total

By now, you should have noticed that python code is not that dissimilar to pseudo code, but python code has slightly different syntax.

2b) Write The Code

Now first part ill show you in writing source code is counted loops, also known as the 'for' loop. A counted loop is when we know exactly how many times we want to repeat a command. An example is if you want to input exactly ten numbers from the user and add them up, here is the pseudo code for it:

repeat ten times
input number from user
add number to total
print total

In python its:

total = 0
for i in range(10)
aNumber = input("Please enter a number: ")
total = total + aNumber
print total

Event Loops

The even loop is also called a while loop, this is for repeating the command untill something happens. So as an example, lets say you wanted to add numbers until it reached 10; the pseudo code will look like this:

total=0
repeat until total is at least 10
input number from user
add number to total
print total

Therefore the Python code would be:

total = 0
while total<10:
aNumber = input("Please enter a number: ")
total = total + aNumber
print total

Decision

Also known as the 'if' statement, the program will make the decision, and based on the result given will execute some code or other. Here is an example.

input mark from user
if mark < 50
print "You fail"
or else
print "You passed"

Now in Python it is almost identical:

mark = input("Enter your mark: ")
if mark < 50:
print "You fail"
else:
print "You pass"

Now that is basically it for this part of the tutorial I hope you enjoyed it. Have fun

Article by 7he gener@l


Add as favourites (100)

  Be first to comment this article

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
Comment:

Code:* Code

 
< Prev   Next >
 
© Copyright 2002-2008 - Linux Exposed - Sponsored by ConsultPlanet http://www.consultplanet.nl - Contact Linux Exposed