Do you ever need a simple program and you dont want to fumble with a
complex programming language like C to make it? Need something Quick, easy to make,
and works efficient? Believe it or not, if you have Linux/Unix, then you also have a very
powerful, yet simple programming tool called Python.
First thing you need to do is open the Terminal shell from your *nix, type:
python then hit enter.
A python interactive shell/IDLE will show up when you first
enter the program,
hit CTRL+D or type exit() to close this interactive shell.
Lets start by writing the forever popular Hello World! program.
>>> print 'Hello World!'
then Enter. If you did not
make any typos it will
run the program and you will see a the screen with 'Hello World!' at the bottom.
How it worked:
print 'Hello World!' (Prints any text encircled by the 's)
Now lets go a little more in-depth with some more powerful commands.
>>> name = raw_input('What is your name? ')
>>> print 'Hello ' + name
Now if you run this program you will first be prompted a question, 'What is
your name?', you can
type in anything you want here. Next when you press enter it will say 'Hello Index' (or
whatever you said your
name was), then it will end.
How it worked:
First I must point out the this ---> name
What is this you ask? It is called a variable, a variable in algebra is a
number with no given
value, in python it is the same thing, only it can be letters also. it can
also be called
anything you like it to be called as long as it does not have non readable name.
Example: dimaintindihangvariable
name = raw_input('What is your name? ') (Asks for input and assigns the input to the variable)
print 'Hello ' + name (says 'Hello ' and also prints the variable value)
Now your thinking, this is cool, but I have to open this python interactive shell/IDLE to
run the programs, Im sure that you don't want to do that. Well guess what,
there is a way around this, all you have to do is this:
Open your favorite text editor program and write your codes.
If you are good at ASCII art you could make a python program to print out
a kewl logo every time you started up your script. Or.... you could even make
your script asking for password
protected at startup like this:
>>> passwd = ''
>>> while not passwd == 'yourpassword':
... print 'Incorrect password! Please try again.'
... passwd = raw_input('Please enter your password: ')
>>>
Note: This program would be easy to bypass just by opening the script. Now
your thinking, so what
good is this program if somebody can just open it? Well it is only an experimental
in learning python.
How it worked:
passwd = '' (this tells that the variable passwd is string
while not passwd == 'yourpassword': (this compares the variable with the password, if it is correct
print 'Incorrect password! Please try again.' it will just continue the program)
passwd = raw_input('Please enter your password: ') (This tells the user to enter password, it will not stop until you input the correct password)
Don't be afraid to play with the programs a little bit, none of the commands
i included here can damage your computer. What you just learned is only the tip of the iceberg,
Python can do much much more than just this simple crap. If you enjoyed programming in this language, then I suggest you get a copy of this
ebook http://www.mediafire.com/download/86ytynazs7ldwyy/python27.chm
The book is very easy to follow, and gives a better understanding of Python. Hope you enjoyed this article.
- Index
**EOF**
complex programming language like C to make it? Need something Quick, easy to make,
and works efficient? Believe it or not, if you have Linux/Unix, then you also have a very
powerful, yet simple programming tool called Python.
First thing you need to do is open the Terminal shell from your *nix, type:
python then hit enter.
A python interactive shell/IDLE will show up when you first
enter the program,
hit CTRL+D or type exit() to close this interactive shell.
Lets start by writing the forever popular Hello World! program.
>>> print 'Hello World!'
then Enter. If you did not
make any typos it will
run the program and you will see a the screen with 'Hello World!' at the bottom.
How it worked:
print 'Hello World!' (Prints any text encircled by the 's)
Now lets go a little more in-depth with some more powerful commands.
>>> name = raw_input('What is your name? ')
>>> print 'Hello ' + name
Now if you run this program you will first be prompted a question, 'What is
your name?', you can
type in anything you want here. Next when you press enter it will say 'Hello Index' (or
whatever you said your
name was), then it will end.
How it worked:
First I must point out the this ---> name
What is this you ask? It is called a variable, a variable in algebra is a
number with no given
value, in python it is the same thing, only it can be letters also. it can
also be called
anything you like it to be called as long as it does not have non readable name.
Example: dimaintindihangvariable
name = raw_input('What is your name? ') (Asks for input and assigns the input to the variable)
print 'Hello ' + name (says 'Hello ' and also prints the variable value)
Now your thinking, this is cool, but I have to open this python interactive shell/IDLE to
run the programs, Im sure that you don't want to do that. Well guess what,
there is a way around this, all you have to do is this:
Open your favorite text editor program and write your codes.
If you are good at ASCII art you could make a python program to print out
a kewl logo every time you started up your script. Or.... you could even make
your script asking for password
protected at startup like this:
>>> passwd = ''
>>> while not passwd == 'yourpassword':
... print 'Incorrect password! Please try again.'
... passwd = raw_input('Please enter your password: ')
>>>
Note: This program would be easy to bypass just by opening the script. Now
your thinking, so what
good is this program if somebody can just open it? Well it is only an experimental
in learning python.
How it worked:
passwd = '' (this tells that the variable passwd is string
while not passwd == 'yourpassword': (this compares the variable with the password, if it is correct
print 'Incorrect password! Please try again.' it will just continue the program)
passwd = raw_input('Please enter your password: ') (This tells the user to enter password, it will not stop until you input the correct password)
Don't be afraid to play with the programs a little bit, none of the commands
i included here can damage your computer. What you just learned is only the tip of the iceberg,
Python can do much much more than just this simple crap. If you enjoyed programming in this language, then I suggest you get a copy of this
ebook http://www.mediafire.com/download/86ytynazs7ldwyy/python27.chm
The book is very easy to follow, and gives a better understanding of Python. Hope you enjoyed this article.
- Index
**EOF**
0 comments:
Post a Comment