Python #2

posted in Programmology
Published April 20, 2005
Advertisement
First off, for the extra 10 points towards this project, I'm documenting that I read this article: http://www.linuxjournal.com/article/3882.
I can directly identify with this guy because out of all the scripting languages, I probably know Perl the best. And I am well aware of how flexible it is- way too flexible. It would take way too much time to keep Perl code in a large project consistent, and even more time learning the best/fastest way to do things since many things can be done different ways. Even in the short time I've been using Python, I can tell that I'll have the same sentiment as this guy. Without even knowing the language, I wrote a small script that hit the internet every 5 minutes so that I don't have to always logon to my college's network when I come back to my computer. I think it worked on the first run.

Anyway, back to what I've been doing.

I've been reading information about everything except the language of Python itself. I don't feel the need to know everything about the interpreter, how to specify source code encoding, etc. But it's nice to get a general feeling about the environment. I was especially interested in how to deal with script files instead of just entering commands interactively.

It seems pretty easy to write code in a file and simply execute the file, as the OS runs it through the python interpreter for you. I can't figure out how to run a file from within the interpreter though. I bet that you can't run a script from within the interpreter, but you can import a module that you built. Or at least import some functions defined in a file. I don't know, honestly I don't know anything abot Python yet so I'm not sure how modules and functions work in it yet.

I'll worry about those problems later. Lets learn Python.

I don't really know how to start besides just listing the initial observations about Python's syntax:

- Only single-line comments are allowed, and a comment is specified with "#"

- It doesn't use any characters to specify the end-of-line or nesting hierarchies. The columns are signficant in Python. Nesting is specified with indentation, and end-of-line character actually specifies the end of the line.

- Control statements such as 'if' or 'while' use the ':' character to signify the end of the test clause and beginning of the loop. For example:
if x < 0:     print "X is less than 0."


- The 'for' statement uses a general iteration instead of forcing the developer to specify temporary variables and how to iterate over the sequence. Example:
a = ['a', 'b', 'c']for x in a:     print a

This makes it very easy to iterate over arrays or list structures.

- Slicing is something I'll have to look into deeper. There's some strange syntax with it and it seems very powerful. Maybe I'll focus my next post on that.


I also have some observations about types.

- You don't have to declare variables

- Python supports integer and floating point, as well as implicit casting in mixed expressions (integers are converted to floating point).

- Complex numbers are also supported. Imaginary numbers are written with the suffix 'j' or 'J'. You can form a complex number by using (r+ij) format or by calling the function complex(r,i).

- Assignment is given with the "=" operator

- Strings are very easy in Python. They can be enclosed either in single quote or double quotes. You can easily do multi-line strings by enclosing the whole string in 3 single/double quotes (''' or """). You can even multiply strings, so that "a" * 5 would become "aaaaa". I think that is extremely interesting. Obviously concatenating and slicing strings are easy as well. There are a lot of other things that you can do with strings as well.



That's it for now. Just gotta get the basics down first.
Previous Entry Python
0 likes 1 comments

Comments

Brandon N
Just fyi, you could just as easily do:
a = list( "abc" )
April 21, 2005 10:39 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement