PDA

View Full Version : Python programming


gi_josh
09-08-2007, 02:43 AM
Okay, this may seem a bit unorthodox, but I've just started taking programming in college, and we are using python. I'm trying to write a program that will compute the sum 1 + 2 + 3 + … + n.

I've got this so far:

def summing():

n = input("Input value to count to ")+1
sum = 0
for i in range(1,n,1):
sum = sum + i
print sum

summing()

but it keeps throwing an error (it highlights the first sum in "sum = sum +1"). Does anyone know what is wrong?

gi_josh
09-08-2007, 02:47 AM
Nevermind, it was a strange quirk. I needed to indent this line:
sum = sum + i

rabidbadger
09-08-2007, 03:11 AM
Glad you got that straightened out. Friggen Geek!






;)

gi_josh
09-08-2007, 03:25 AM
Yeah, and I'm lovin' every minute of it too. I mean, look at me, I'm coding on a Friday nite.... I guess I AM a geek :)

rabidbadger
09-08-2007, 03:53 AM
Hey, that's why we are all here. but meanwhile....

what the hell is python? What is it good for? Links to python aps?

rabidbadger
09-08-2007, 03:54 AM
Hell, I'm sitting at home on a friday night googlin CSS! haha!

rabidbadger
09-08-2007, 03:55 AM
Oh, and hey! get your ass on chat! see ya there!

gi_josh
09-08-2007, 03:57 AM
Python is sort of a light weight programming language. It doesn't use a compiler, the program interprets on the fly. Not good for big programs, but it is great for smaller stuff and beginners because it is so streamlined. It's still a darn good little language though.
www.python.org

gi_josh
09-08-2007, 03:58 AM
Oh, and hey! get your ass on chat! see ya there!

Aaaaaaaaahhhhh....I'd love you, but I'm getting to bed...early day tomorrow buddy.

phatlip12
09-08-2007, 05:06 AM
I want to learn Python but I decided I need to stop jumping from language to language and simply learning the "basics". I want to really emerse myself in a specific language and REALLY learn it before moving on to anything else (seems to be C/C++).

blazes816
09-08-2007, 06:39 AM
Rev3 uses python.

I'll have to learn it if I want to get hired some day :(. It's really weak compared to Ruby. And, although it's better than php, I know php.

tokenuser
09-08-2007, 01:17 PM
Okay, this may seem a bit unorthodox, but I've just started taking programming in college, and we are using python. I'm trying to write a program that will compute the sum 1 + 2 + 3 + … + n.

I've got this so far:

def summing():

n = input("Input value to count to ")+1
sum = 0
for i in range(1,n,1):
sum = sum + i
print sum

summing()

Save calculation loops ...

sum = ( n * (n+1) ) / 2

Mightn't seem like a big saving adding 1-100, but its a huge save on adding up 1-1000000 or more, and it runs in a predicatable time - which can be important as you move into more complext tasks. Also, the program becomes shorter, but is just as readable.

Anyone programming should grab a copy of Numerical Recipes in C. Forget the fact it is in C, the algorithms presented are easy to translate to just about any language. They ften save the need to do brute force "looping" approaches where a more elegant, and faster forumla works just as well - if not better.

def summing():

n = input("Input value to count to ")
sum = ( n * (n+1) ) / 2
print sum

summing()or evendef summing():

n = input("Input value to count to ")
print (n*(n+1))/2

summing()

gi_josh
09-09-2007, 12:59 AM
Token, Those are more elegant, but my prof wanted us to use "range" because that is what we were learning. Thanks for the help though!

tokenuser
09-09-2007, 01:38 AM
Token, Those are more elegant, but my prof wanted us to use "range" because that is what we were learning. Thanks for the help though!Bah - idiot profs reinforcing bad programming paradigms. They should have a better example.

rabidbadger
09-09-2007, 01:50 AM
Token, maybe you should take the Rev3 Job, maybe?

tokenuser
09-09-2007, 02:48 AM
Token, maybe you should take the Rev3 Job, maybe?:) My curent job is a mashup of the Rev3 "Product Manager, Revision3.com" with a generous dash of "Director of Business Development and Distribution" thrown in. I'd love to apply, and having worked in a start up before, think I'd do a great job at it ... but the thing about being married is that there are two people that need to move, and my wife's job is firmly planted at UNC.

I have the advantage in my current job that I can work from pretty much anywhere with an internet connection and cell phone reception.

Pretty much rules me out unfortunately ... plus I am half way through my Greencard application. Changing jobs now means I need to start that again - and no job is worth that level of pain.

Its a pity. If I was single with no work restriction - I'd be jumping at either of those positions (especially the Product Mgr - the Dir Bus Dev would be a stretch, but does involve things I currently do).

tokenuser
09-09-2007, 02:52 AM
Token, Those are more elegant, but my prof wanted us to use "range" because that is what we were learning. Thanks for the help though!OK, how about this ...

def summing():

n = input("Input value to count to ")
sum = (n*(n+1))/2
for i in range(1,n,1):
print "%d: The answer is still %d\n" % (i, sum)

summing()

:D

macguffin
09-09-2007, 03:24 AM
We should most definitely turn this into a Python discussion thread.

patm1987
09-09-2007, 07:36 PM
you can always do something fun like

summing = lambda n:[1,0][n>0] or summing(n-1)*n

but you don't get to use range which seems to be a requirement, but lambda functions are fun, and I use them if I'm using python (prefer the c's myself).

gi_josh
09-09-2007, 09:02 PM
While we are talking about coding...do you guys code to any music? It seems like wherever I'm at there is noise, so I was considering listening to some tunes while I worked. What would you guys suggest as good music program to?

tokenuser
09-09-2007, 09:04 PM
Something ambient - not a thumping beat with thrashed lyrics.

I have pretty much the entire Tangerine Dream catalog for late night coding :)

masherscf
09-09-2007, 09:27 PM
OK, how about this ...

def summing():

n = input("Input value to count to ")
sum = (n*(n+1))/2
for i in range(1,n,1):
print "%d: The answer is still %d\n" % (i, sum)

summing():D

Can you prove this formula works for every possible value? I can.

I actually have 2 proofs, one is combinatorial and much cooler than the one I'll include here. This one should appeal to you recursion geeks anyway.

Let Sum(n)=n*(n+1)/2

(notice that you don't need to group the product in the numerator together since it has left-to-right priority)

Case. n=1

Notice sum(1) =1*(1+1)/2 = 1 is true so the n=1 case is valid.

Let be any positive integer greater than 1.

Assume the sum(n) formula holds for n-1.

sum(n)=sum(n-1)+n

= [(n-1)*n/2]+n

= (n^2-n)/2+n

= (n^2-n)2 +2n/2

= (n^2-n+2n)/2

= (n^2+n)/2

= n*(n+1)/2

QED


So here is my program to do it.

begin
f:= function(n); if n=1 then return 1 else return f(n-1)+n;
Input n
print f(n)
end

Hardy, har har

gi_josh
09-10-2007, 12:20 AM
QED reminded me of my high school calculus class. Instead of using QED, we enjoyed using CRAH, which was short for Camera Revolves Around Head, which is what happens in "A Beautiful Mind" when he figures out a problem.

rabidbadger
09-10-2007, 01:30 AM
HAHA! (and I suck at math and never saw the movie!)

masherscf
09-10-2007, 02:26 AM
HAHA! (and I suck at math and never saw the movie!)

I wasn't about Math. It was about crazy. Most great movies about "Math" turn out to be about crazy. It's enough to make a guy self conscience.

tokenuser
09-10-2007, 02:56 AM
Most great movies about "Math" turn out to be about crazy. It's enough to make a guy self conscience.A Beautiful Mind and Proof ... what other great Math movies are there? Given that sample size - mathematicians should all be locked away.

gi_josh
09-10-2007, 02:27 PM
Those are really the only two that I can think of...I'd forgotten about proof until right now.

tokenuser
09-10-2007, 03:04 PM
Those are really the only two that I can think of...I'd forgotten about proof until right now.BTW - Proof (2005) (http://www.imdb.com/title/tt0377107/) is good, but Proof (1991) (http://www.imdb.com/title/tt0102721/) was better.

Since it involves cryptography, would Enigma (2001) (http://www.imdb.com/title/tt0157583/) count as a math movie as well? It definately falls into a computer themed movie.

masherscf
09-10-2007, 03:48 PM
A Beautiful Mind and Proof ... what other great Math movies are there? Given that sample size - mathematicians should all be locked away.

There's truth to this. My greatest single breakthrough happened when I had a fever of 105 and started hallucinating. I was working on a problem earlier that day and noticed that two objects had the same structure. I wondered if that was a coincidence or something more general. As I got sicker, I saw this mapping kind of dancing in from of me. I can't explain it any other way. I wrote it down and passed out. After I got better, I checked it out and it worked. No shit. I can't say that I would not have come up with the idea otherwise, but it was freaky. I all it my "John Nash" moment. My friends refer to the mapping as my "flux capacitor." I built my entire Ph.D. thesis around this one mapping.

gi_josh
09-10-2007, 08:33 PM
BTW - Proof (2005) (http://www.imdb.com/title/tt0377107/) is good, but Proof (1991) (http://www.imdb.com/title/tt0102721/) was better.

Since it involves cryptography, would Enigma (2001) (http://www.imdb.com/title/tt0157583/) count as a math movie as well? It definately falls into a computer themed movie.

I wasn't aware of the earlier Proof movie. Thanks for the heads up. I've never seen Enigma either.