September 20th, 2004, 10:55 AM

simple square root program
X = input("Number please? ")
print ("the square root is"), X*X
This is THE first program I am writing.
I am a freshman in college and I am just interested in trying out python so if anyone could help me and/or direct me towards a good guide that is complete in laying out all the basics.
Bryan Behrens
TheUndeadFrog
September 20th, 2004, 12:46 PM

Is it something like this that your after:
Code:
def sqrt(y):
return y**0.5
X = input("Number please? ")
print ("the square root is"), sqrt(X)
raw_input()
September 20th, 2004, 01:37 PM

Originally Posted by theundeadfrog
X = input("Number please? ")
print ("the square root is"), X*X
This is THE first program I am writing.
I am a freshman in college and I am just interested in trying out python so if anyone could help me and/or direct me towards a good guide that is complete in laying out all the basics.
Bryan Behrens
TheUndeadFrog
Did you try running your program? It works just as you would expect.
The brackets round the string in the print statement is unneccessary, but are ignored. It is also conventional to start variable names with a lower case letter, so use x rather than X.
There are lots of resources for people new to Python. The tutorial that comes as part of the docs is very good, and there are lots of other resources on the web (but I do not have any URLs to hand).
Dave  The Developers' Coach
September 20th, 2004, 05:19 PM

Originally Posted by Karl_bertil
Is it something like this that your after:
Code:
def sqrt(y):
return y**0.5
X = input("Number please? ")
print ("the square root is"), sqrt(X)
raw_input()
or
Code:
x = input("Number please? ")
print "The square root is ", (lambda y: y ** 0.5)(x)
raw_input()
September 20th, 2004, 05:29 PM

yes I did run it and it would just ask me "number please?"
then it wouldnt return any info when I hit enter after I put a number in
x = input("Number please? ")
print "The square root is ", (lambda y: y ** 0.5)(x)
raw_input()
can you explain what I did wrong and why that is right?
whats lambda = ?
September 21st, 2004, 05:02 AM

Originally Posted by theundeadfrog
yes I did run it and it would just ask me "number please?"
then it wouldnt return any info when I hit enter after I put a number in
x = input("Number please? ")
print "The square root is ", (lambda y: y ** 0.5)(x)
raw_input()
can you explain what I did wrong and why that is right?
whats lambda = ?
plz copy & paste the original code I supplied and save it as e.g. 'sqrt.py' and run it via 'python sqrt.py' ... you probably have a typo somewhere b/c I tested the exact same code here and it works just fine....
lambda is a onelineminifunction(TM) ... I will try to explain this with a little code evolution
Code:
def sqrt(y):
return y ** 0.5
.... # and later when we need it we call it like
sqrt(x)
... so for that oneline function we have that whole work...
Code:
g = lamda y : y ** 0.5
.... # and later when we need it we call it like
g(x)
...looks a bit better ... we just used 2 lines instead of 3
Code:
(lambda y: y ** 0.5)(x)
....hush.. now we are at the real deal... one line instead of three with the least use of additional variables and whatnot...
ok?
... check out Python.org and go trough all the documentation you can find ...
I'd also suggest you buy 'Dive into Python' by Mark Pilgrim if you're really interested in it... greetz
September 25th, 2004, 11:12 PM

It may be worth mentioning that where lambda can be useful in some situations, if your going for preformance then your not gonna want to use it too often. So, if you have a function you want to call more than once. It's best to use the standard function declaration.
Note: This observation is based on documents describing earlier versions of Python. But i wouldn't expect this to change too much.
Mark.