Wednesday, April 30, 2008

Variables in Python 1

Honestly, I got so excited learning Python after I discovered how to write the "Hello World!" program in Python. From my "Hello World!" program, I learned how to place comments in my program, in the beginning of your comment, you just got to place a sharp(#) to denote that the line is not part of the code, its only a comment.

Displaying a string in python was never hard at all, all I did was to call the instruction 'print' then preceded with the string I want to display which is of course the "Hello World!" string enclosed with a double-quote.

But wait! Is the first line of my code also a comment? I didn't know that writing a blog would make me feel like crazy. I got to answer my own questions! hehehe!

Here's the first line of the hello.py code:
#!/usr/bin/env python

That's the first line I was referring to. Hmmm... It looks like its also a comment right? This first line is very important in python programming. Just like Shell programming or Perl programming, this first line tells the my FreeBSD or the Unix box that the hello.py is to be executed with the python interpreter.

So much for the hello.py. In programming, it is very important that you know how to use variables. So this is my first program in python that shows you how to use variables:

#!/usr/bin/env python
# Filename: variables.py
# Description: A simple program that shows how to use variables in python
# Author: Obispo

# Lets declare a variable called a and assign it with a value equal to 1
a = 1

# Lets declare a variable called b and assign it with a value equal to 2
b = 2

# Lets declare a variable called c and assign it with a value equal to 3
c = 3

print "Variable a is ", a
print "Variable b is ", b
print "Variable c is ", c
print "Sum of a + b + c is ", a + b + c

Again, I used vi editor to type this code and saved it with a filename variables.py. I also made the program executable using the command 'chmod +x variables.py'. Excited to see the output of this code? Here's the output:

# ./variables.py
Variable a is 1
Variable b is 2
Variable c is 3
Sum of a + b + c is 6

That's it! Waaa... Those variables contains only numbers, how about having a variable that contains strings?

Hmm... I wanna learn that one too, but I've got no time to study and write anymore, it 11:21AM now and I have a lunch date with my wife. Got to go now, til next time...


Hello World in Python

I'm trying to study programming or scripting with Python and I remember when I first learned programming in C, I started with writing a "Hello World!" program.

Since this is my first time in python too, I want to convert my "Hello World!" program in C into Python and this is how it looks like:

#!/usr/bin/env python
# This is a comment in python
# Filename: hello.py
# Description: This program will display the very famous "Hello World!" in python
# Author: Obispo

print "Hello World!"

# The program ends here

I installed my python interpreter at my FreeBSD box and used the vi editor to write this code. I named the program "hello.py" and after saving it, I make it executable by changing the user permission of the file to executable. I used the command:
# chmod +x hello.py

Now that its executable, I got very excited to see the output. And so I run the program like this:
# ./hellopython.py
Hello World!


The "Hello World!" message did appear! Wow!
Digg