Help Wanted

Learning Python--Part 2 – CPUFreak91

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
I got Learning Python, and am reading it. here's where I'll post my Q's.
BTW--What's a floating point?

------------------
Love, Life and Linux

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
Also, please talk only in code and python related things. In Changes, D-SIPL mention that the place is a bit slow and boring (that's my own words) so when you post, please post only about python: terms, codes, and definitions. Please, no switching off to other topics (that goes for me too ).

------------------
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.

--C. A. R. Hoare
[B][/B]

[This message has been edited by CPUFreak91 (edited February 18, 2005).]

graynod

Member

Posts: 17
From: richmond, VA, USA
Registered: 09-28-2004
CPUFreak91,
A floating point number, or 'float', is just a number with a decimal, or fractional, part. If the fractional part is 0, it might not be displayed. Python seems actually to be using doubles but that's OK. Also, you hafta remember "Everything's an object!" in Python, so later on I'm sure there will be use made of, for instance, float object attributes. Now compare a float to an integer, which is a number with only a whole part, like 1, 2, 3... What's easy with Python is you really don't have to worry about the type up front (this bothers me no end though as a C++ fan...).

Try this in your Python programming:

# create a float to use:
pi = 3.141592
# now print it to the screen
print 'pi is approximately', pi

Have you checked out:
http://www.byteofpython.info/

It's _really_ simple, not for old hands at all, but should be perfect for newbies (please don't take offense at that, I consider myself a newbie at many things, including Python ). I'm ripping through this tutorial, the guy did a lot of work to make it so even I can understand it!

A little later on it might be time for this one:
http://www.mindview.net/Books/TIPython

What are your goals with Python, by the way? I'm just curious about it as a language, and finding many features I do _not_ like a language I have to do a lot of coding in to have (I like to ignore whitespace all I can!). I hope that your curiosity will lead you on to other languages, maybe even Lisp and C++ someday. Happy hacking! May God bless you.

------------------
graynod -> "my head was feeling scared but my heart was feeling free" -- the Pixies

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
Well my goal is to learn python enough that I could make a game, word proccessor and lotsa ods and ends. Plus it will help me learn C Java and C++ later in the furture. I finished a QBASIC book about 5 months ago so I was looking for a nice "pretty" more complex language.

I already have Learning Python (O'Reilly) TIPython, How to think like a computer scientist, A byte of Python and Python in a Nutshell (O'Reilly)

------------------
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.

--C. A. R. Hoare
[B][/B]

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
I've heard that PyGame is a great tool for coding games in Python. You may already be aware of it, but in case you're not, the link is: http://www.pygame.org/

Cheers!

------------------
http://www.includingjudas.com/christiangame.html

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
Ah. Will try that. Thanks.

------------------
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.

--C. A. R. Hoare
[B][/B]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
hmm, I still don't undrstand what doc srtings are for.
OOP is new to me, A byte of python says I should use self and not other names. What's up with self and why?

Also, I got py2exe, how do you use it?

[This message has been edited by CPUFreak91 (edited February 22, 2005).]

graynod

Member

Posts: 17
From: richmond, VA, USA
Registered: 09-28-2004
hmm, I still don't undrstand what doc srtings are for.

hey,
Here's my take on these things:
DocStrings are for documenting your Python code. They're implemented in Python as object 'attributes'. In other words they are data members encapsulated by an object, or rather, they are data that belongs to the object, or stored in the object, however you want to think of it. In Python, remember, even functions are objects, and have attributes! Since they are attributes of an object, dot-notation can be used to access them, try this:

def test_by_val(x):
'''Tests what happens to a variable passed by value in Python.\
Check out the returned value of x!!!!'''
z = x + 10
while (x < z):
print 'x ==', x
x = x + 2
return x

print '-------------------------------------------------------'
x = 10
test = test_by_val(x)
print 'x after call to test_by_val ==', x
print 'value returned by test_by_val ==', test
print 'test_by_val.__doc__ ==', test_by_val.__doc__

The last line accesses the data member __doc__, the DocString stored in the object test_by_val. 'A Byte of Python' kinda glosses over some OOP details, you might wanna check a handy-dandy Java or C++ for a little more depth and understanding.
##########################################################################

OOP is new to me, A byte of python says I should use self and not other names. What's up with self and why?

The 'self pointer' is apparently like the 'this pointer' in C++. It is a pointer (for now, let's just say the location in memory) to the curent object itself, so the object can refer to itself to do stuff. This can be pretty important and nice. For instance, if your object has a print_me method, you can call print_me from any of the object's other methods, and the object will know you're talking about it and not some other object. Here's an example that may be a little clearer:

#!/usr/bin/python

# object.py

###############################################################
class point:
'''point class represents a vertex in 3D space'''
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
pass
def print_me(self):
print '(%f, %f, %f)' %(self.x, self.y, self.z)
pass
def set_coords(self, a, b, c):
self.print_me()
self.x = a
self.y = b
self.z = c
self.print_me()
pass
###############################################################


###############################################################
print '-------------------------------------------------------'

# create a point object
p0 = point()
# create & initialize a point
p1 = point(1, 2, 3)
print 'calling p0.print_me(): '
p0.print_me()
print 'calling p1.print_me(): '
p1.print_me()
print 'calling p1.set_coords(1.23, 2.34, 3.45):'
p1.set_coords(1.23, 2.34, 3.45)

print 'Done.'
###############################################################

# Here's the output:
-------------------------------------------------------
calling p0.print_me(): (0.000000, 0.000000, 0.000000)
calling p1.print_me(): (1.000000, 2.000000, 3.000000)
calling p1.set_coords(1.23, 2.34, 3.45):
(1.000000, 2.000000, 3.000000)
(1.230000, 2.340000, 3.450000)
Done.

In say, p1.set_coords above, p1 uses the self pointer to know to call its print_me method. If you leave off the self keyword, you'll get weird error messages, as poor old p1 thinks you're trying to call a method with global scope to p1, or in other words, p1 thinks you're trying to call a function called 'print_me' that has been declared somewhere else that p1 is expected to see and can't (if you think of scope as 'what something can see' it helps). If you were to pass another point to set_coords for some reason, say to set a point's coordinates to those of the passed point, the self point will help ensure that you, the object, and everyone else will know which point you mean to use, for instance:
def set_coords_from_point(self, p2):
self.print_me()
self.x = p2.x
self.y = p2.y
self.z = p2.z
self.print_me()
pass
Using self lets an instance of the point class (or a point object) know that you mean for the point to assign p2's (x, y, z) values to its own (x, y, z) values. Here's some code to add to the above to try this new method:

print '-------------------------------------------------------'
p2 = point(10, 11, 12)
print 'calling p1.set_coords_from_point(p2):'
p1.set_coords_from_point(p2)

And the output:

-------------------------------------------------------
calling p1.set_coords_from_point(p2):
(1.230000, 2.340000, 3.450000)
(10.000000, 11.000000, 12.000000)
#########################################################################

Also, I got py2exe, how do you use it?

I don't know a thing to say about py2exe, sorry. Hope my other ramblings are helpful, if not I hope someone else can set us both straight. When you get into pointers it can get really hairy, for now you might wanna just consider them as addresses of data in memory. Using such addresses allows fastfast access. This is a _really_ over-simplified view of pointers but will maybe get you over the hump for a little while.

Take it easy, may God bless your studies!

------------------
graynod -> "my head was feeling scared but my heart was feeling free" -- the Pixies

graynod

Member

Posts: 17
From: richmond, VA, USA
Registered: 09-28-2004
Crud!
In the above example, if you happen to try cutting & pasting code, be SURE to indent properly, my nice indenting was eaten when I hit the 'Submit Reply' button. Guess I have to figure out how to turn HTML on & edit the mess...

Take care, and may God bless you.


[EDIT] Oh well, I guess a moderator or admin has to turn HTML on/off. Just be real careful indenting the above Python code as Python, believe it or not, uses indentation to define scope.
------------------
graynod -> "my head was feeling scared but my heart was feeling free" -- the Pixies

[This message has been edited by graynod (edited February 23, 2005).]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
ok, i'll study it

------------------
There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.

--C. A. R. Hoare
[B][/B]