Hello. Can someone please explain a couple of things about this bit of code so I understand how it works? #to test if a number is prime def isprime(n): for x in range(2, int(n**0.5)+1): if n%x == 0: return False return True So first question is: 1. what's going on here: (2, int(n**0.5)+1)? Why does the int have to be there in front of the (n**0.5) and why the +1? 2. When does one use 'return' and when 'print'? Whether I use print or return, it has the same effect...although perhaps not in other situations? Thanks in advance for any info.
Ho, okay, int is there to make the square root an integer obviously and not a float. Maybe the +1 is saying check up to rounded off sqr root and then go 1 number further?
Isn't this kind of math?
2. Return is used within a function to "return" a value to the function caller. Suppose I have some foo function that is running now, designed such that: def foo(): numToTest = int(raw_input('Enter num.:')) if isprime(numToTest): print 'Given num. is prime!' When the caller function (foo here) calls isprime, execution in foo stops, and isprime is started. When it is done, it returns something (True or False), and the caller function can evaluate it (You can do anything to isprime(numToTest) that you can do with a boolean variable, including assigning to some local variable). print, on the other way around, prints to standard output(your monitor), so the caller function can't keep track on the result of calling isprime. Long story short: if you want to inform something to the user(if it is the final step on your program, or something like that), print it. If you intend to call the function somewhere else, so you can check a result from calling the function, add a return statement.
the range function only takes integers for arguments one reason for writing functions is to group/encapsulate logical portions of code so that it can be re-used. it is often quite useful for the function to return something. so you can have a function called isPrime and then write an easily readable line that says if isPrime, doSomething - the code of the isPrime function doesn't interfere with being able to see what that if statement is doing. Also, you can use your function lots of times without have to re-write those lines - it makes your code modular -functions that are written for to help solve one problem can be adapted to another problem. functions are cool.
Thanks guys. I guess I'll go read up a bit on 'return' to make sure I understand what it is telling the computer. @Sunny....my question about this (2(int(n**0.5)+1) was aimed at trying to make explicit what this is telling the computer. Is it saying "test each number from 2 until the square root of n and then one number after the square root"??
range is a built-in function - if you need to understand how Python statements, keywords, functions, modules work a good source is the documentation that should have been installed on your computer when you installed Python. If you are using windows/Idle pressing F1 in Idle should bring up the docs. The documentation is also available on-line http://docs.python.org/library/functions.html
the Tutorial in the documentation is a must.
Join our real-time social learning platform and learn together with your friends!