PDA

View Full Version : python help



c0mput3r_n3rD
July 6th, 2009, 02:42 AM
Hey all!
I'm just learning python on ubuntu, which is v2.5 (already installed), but I can't find a tutorial for v2.5, only v2.6.2. Can any one help me out with either upgrading and/or a tutorial on v2.5, which I should do

thanks a lot

Can+~
July 6th, 2009, 02:57 AM
You're on Intrepid? I don't know if there's an available version of python 2.6 from the repository for that version. Try to check on synaptic.

If not, well, there's not much difference between 2.5.2 to 2.6, so I guess you could still learn from the documentation. Or install python3.

btw: In case you're wondering, there's no problem on installing two different versions of python at the same time; I've got python2.6 and 3 installed at the same time.

c0mput3r_n3rD
July 6th, 2009, 03:01 AM
Yeah I just checked synaptic, and their are a million versions haha, so that didn't really help to much. And I thought their wouldn't be a huge difference b/t 2.5 and 2.6 either, how ever as the programs will run, the results are not correct, even when the syntax is. hmmmmm. :-\

Can+~
July 6th, 2009, 03:06 AM
Yeah I just checked synaptic, and their are a million versions haha, so that didn't really help to much. And I thought their wouldn't be a huge difference b/t 2.5 and 2.6 either, how ever as the programs will run, the results are not correct, even when the syntax is. hmmmmm. :-\

What problems do you experience? Try to show some code and I may point you what's wrong.

c0mput3r_n3rD
July 6th, 2009, 03:16 AM
#! /usr/bin/env python
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equal', x, '*', n/x
break
else:
#loop feel throughw with out finding a factor
print n, 'is a prime number'
--------------------------------------------------------------------------------------
Output:

3 is a prime number
4 equal 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equal 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equal 2 * 4
9 is a prime number
9 equal 3 * 3


So the code should only print out each number once, and I've double checked my code with the example in the tutorial.

Can+~
July 6th, 2009, 03:24 AM
#! /usr/bin/env python
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equal', x, '*', n/x
break
else:
#loop feel throughw with out finding a factor
print n, 'is a prime number'


Actually, you have a little typo, the "else" statement can also be used paired with the for _ in ____. For's else is called when the for statement ends, for example:


>>> for i in xrange(0, 10):
... print i
... else:
... print "END!"
...
0
1
2
3
4
5
6
7
8
9
END!

So, to fix your code, remove some indentation the else block, like this:


#!/usr/bin/env python
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equal', x, '*', n/x
break
else:
#loop feel throughw with out finding a factor
print n, 'is a prime number'

Which outputs


2 is a prime number
3 is a prime number
4 equal 2 * 2
5 is a prime number
6 equal 2 * 3
7 is a prime number
8 equal 2 * 4
9 equal 3 * 3

c0mput3r_n3rD
July 6th, 2009, 03:30 AM
Oh yeah you're right! So you're saying I should be alright (at least for a little bit) learning v.2.6 and v2.5 compiler?

c0mput3r_n3rD
July 6th, 2009, 03:36 AM
And is that else for the loop???

Can+~
July 6th, 2009, 03:43 AM
Oh yeah you're right! So you're saying I should be alright (at least for a little bit) learning v.2.6 and v2.5 compiler?

You'll notice the difference when you need newer libraries, but as for learning the base language, 2.5 does the job.


And is that else for the loop???

Yup. In the doc you can read more about it (http://docs.python.org/reference/compound_stmts.html#for).

c0mput3r_n3rD
July 6th, 2009, 03:49 AM
Sweet deal man thanks for the help!

Empire537
July 6th, 2009, 06:24 AM
For the record 2.6 has no major changes over 2.5. Mainly bug fixes. You can run 2.6 commands in 2.5 python no problem.