Python #4 - Various syntax

posted in Programmology
Published April 20, 2005
Advertisement

I'll start posting some more interesting things once I learn all these basics. For now, I'm just posting what I learned when I read through another part of a tutorial. [grin]

I learned ML earlier this semester for the same class. ML has some cool pattern matching techniques. While it's not exactly pattern matching, it goes along with it. ML expanded lists so that you could assign separate variables values all at once, like:
(a,b,c) = (1,2,3);


Python takes on this ability, which I think it pretty cool.
>>> a, b, c = 1, 2, 'c'


Print

I've been playing around with the built-in function "print". It's a pretty useful function as you can do the "printf" type formatting, but not having to worry about type-safety. It's also has a weird thing about adding new-lines: it automatically adds a new-line unless you put a comma after the print statement. It also automatically adds a space after printing something, and though it's cool, I couldn't find a way for it not to do that which might be annoying.
Examples:
>>> i = 1000;>>> print "The value is", iThe value is 1000>>> i = 0>>> while i<10      print i,      i = i + 10 1 2 3 4 5 6 7 8 9


I'm a little confused about functions though. You can do print("Hello") and it seems to do the same as print "Hello". You can't do print("Hello", i) though, so I'm not exactly sure what "print" is. I guess I'll figure that out when I learn more about functions.

Conclusion
I think I've covered a lot of the basic syntax of Python. I'm sure there are some other weird syntactual quirks or cool things about Python, but I'm going to leave them be and journal about more serious stuff from now on. I'm gotten comfortable enough to program basic stuff in Python. I'm also not going to journal about every library. I'll probably focus on functions and other major parts of the language.
0 likes 6 comments

Comments

Rob Loach
So ugly! [smile]

... You should take all your journal entries when you're done learning python and compile them into a nice easy to read tutorial series.
April 20, 2005 08:51 PM
okonomiyaki
Ugly? You talkin about me or Python syntax? [smile]

I actually need to compile these into something so that I can turn it in to my professor. I'll keep that in mind though, I hadn't really though of that and it might be a good idea. I suppose someone else might find my insights useful.
April 20, 2005 11:31 PM
Rob Loach
I will, I haven't quite looked into Python deeply yet.
April 21, 2005 07:40 AM
Brandon N
Use
print("Hello%d" % i)
or better yet,
print "Hello%d" % i

For printing a loop:

>>> for i in range( 10 ):
>>>     print i
0 1 2 3 4 5 6 7 8 9
April 21, 2005 10:25 AM
okonomiyaki
I discovered that way of formatting the output after I posted this journal. I do like that way better. Thanks for the advice. I've always liked the printf way of doing things.

And that's how you would easily do a incremental for loop. Cool.

What's the difference between "print("Hello%d" % i)" and "print Hello%d % i" ?
April 21, 2005 02:02 PM
Brandon N
Honestly, I don't think that there is a difference. As it is in a few other languages (PHP comes to mind), its more a way of enabling the programmer to do more with less.
April 21, 2005 03:02 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement