Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Python #4 - Various syntax

Sign in to follow this  


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'


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.

>>> i = 1000;
>>> print "The value is", i
The value is 1000
>>> i = 0
>>> while i<10
print i,
i = i + 1
0 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.

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.
Sign in to follow this  


Recommended Comments

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.

Share this comment

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.

Share this comment

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

Share this comment

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" ?

Share this comment

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.

Share this comment

Link to comment

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

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!