• entries
73
131
• views
54765

# Python #4 - Various syntax

108 views

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.

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.

##### Link to comment
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.

##### Link to comment
I will, I haven't quite looked into Python deeply yet.

##### Link to comment
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

##### Link to comment
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" ?

##### Link to comment
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.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account