Python parameters and functions - python

I just started learning Python and I've just been messing around typing different codes for practice to learn, and I made this code:
import math
def lol():
print (math.cos(math.pi))
print ("I hope this works")
def print_twice(bruce):
print bruce
print bruce
print_twice(lol())
When I run it, my output is:
-1.0
I hope this works
None
None
How come it isn't printing the function lol() twice?

Your code print_twice(lol()) is saying to execute lol() and pass it's return value into print_twice(). Since you didn't specify a return value for lol(), it returns None. So, lol() is printed once when it gets executed, and both print statements in print_twice() print passed value of None.
This is what you want:
def lol():
print (math.cos(math.pi))
print ("I hope this works")
def print_twice(bruce):
bruce()
bruce()
print_twice(lol)
Instead of passing the return value of lol(), we are now passing the function lol, which we then execute twice in print_twice().

You should note that printing is different from returning.
When you call print_twice(lol()) it will first call lol() which will print -1.0 and I hope this works and will return None, then it will continue calling print_twice(None) which will call print None twice.

How you might run as expected:
def lol():
print "lol"
def run_twice(func):
func()
func()
run_twice(lol)

Related

Function called inside a loop doesn't show any screen output

I'm unable to figure out how return works. In the following code, the first two pieces of code work just fine, but it fails to return any output at all when using return in a loop(#3).
# 1
def test(x):
return 'Testing'*x
test(3)
#2
def tst():
return 'Testing'
tst()
#3
for i in range(2):
tst()
The third one works, what's happening is that you don't do anything with the output because it's in a for loop.
We can see the results if we would print out the value returned from tst() function, i.e:
def tst():
return 'Testing'
for i in range(2):
print(tst())
And the output of the program will be:
Testing
Testing
The first 2 function calls in your code work because python just prints the value of the last line you called, but because it's in a for loop, it wouldn't.
The python REPL is not equivalent to the actual python interpreter. In the REPL/IDLE, the last expression evaluated implicitly prints. When you have a for loop, the REPL does not implicitly print the last expression, which is why you think that nothing's happening. You can see that's not the case if you explicitly print everything:
print(test(3)) # prints "TestingTestingTesting"
print(tst()) # prints "Testing"
for i in range(2):
print(tst) # prints "Testing" twice

Python - Can't seem to get return statement to work in simple function?

I have a simple function accessing a dictionary with an if statement.
langdict = {"english": "Hello, World!", "french":"Bonjour, tout le monde!"}
def hello(language):
if language in langdict:
return langdict[language]
else:
return None
Now everything works fine and dandy with print statements obviously. But nothing happens when I use the return statement on both lines 5 and 7. What am I missing?
I am using python 2.7 and couldn't reproduce your error:
By calling
hello("english")
I get the expected result 'Hello, World!' while calling for example
hello("german")
results in no/ empty result
If you want to store the result into a variable you would do something like:
result = hello("english")
You may use this one
def hello(language):
return langdict.get(language, None)
print hello('english') #Hello, World!
print hello('japanese') #None
return will return that value from function and print will display that value in console

Python print None

I'm having trouble with print None as a value.
Suppose here's my python code:
def a(b):
b = None
print b
def c(a):
if a:
return True
else:
return False
>>> a(1)
None # I need this "None" to show.
>>> c(a(1))
None # I don't want to print out this, but don't know how.
False
My problem is I have to "print" the None when only calling function a.
And when I pass function a to function c, I don't want the "None" to print out.
If I type "return None", Python shell will not show "None" for function a. That's why I thought I can only use "print" if I want to show the "None". But when I pass function a to function c, the "None" also gets printed out. Is there a way to only get the result secretly without printing out the "None" in the 2nd function?
I hope my question is making sense.
Thank you very much for your help.
The call c(a(1)) will execute a(1) first and then c(a(1)). Since here a(1) is not returning true, c(a(1)) will evaluate to False.
That's the reason you get following:
None
False
Try calling the function as c(a) this will return as follows:
True
This happens because a has not executed and it is having some value to it.
Hope this helps!

what is the difference of built-in function and myfunction

If I use this
def myfunction():
print('asd')
print(myfunction)
The IDE tells me None
but if I use this
import math
print (math.cos(90))
The IDE tells me the COS90°
Why?
It's all about return value.
def myfun(x):
return x
print(myfun("hello")) will return hello.
Your function (myfunction) does not return a value, that's to say it returns None value. So, print (a built in python function) returns that value.
When functions are called they always return something they processes.
def myfunction():
print('asd')
This will print the output. Since there is nothing explicitly returned, the function by default return None
Now lets add a bit of complexity to your function:
def myfunction(text):
print(text * 2)
This will print the text it gets twice. And it works just fine. But lets say you need to store the "printed twice" text to a variable.
Try this:
def myfunction(text):
print(text * 2)
twoText = myFunction("some text foo")
print(twoText)
Output should look like this:
some text foosome text foo
None
This is happening because you are in your function first printing twice some text foo and then printing what your function returned. In this case it returned None since nothing was explicitly returned.
To fix this you just replace print with return.
def myfunction(text):
return text * 2
twoText = myFunction("some text foo")
print(twoText)
The output is correct because you print only the return of the function.
some text foosome text foo
The math function returnes data like this:
def cos(number):
# Insert super complex math calculation here
return result
If it didn't do this you would not be able to store the result in a variable, instead it would just be printed.
If a Question is general than there is no difference except built-in function is one that is properly tested and approved by author's command; You can contribute too if You write something good and useful - don't shy offer it to community; ppl will thank You.
But if You mean exactly Your example then You have to change code to be:
def myfunction():
print('asd')
myfunction()
this is the way to call function without arguments, You could have
def myfunction(n):
print(n)
myfunction('hi')
this would print hi and so on

can't do return function in condewrite

I'm trying to understand the difference between returning and printing (I get the theory behind it, but when actually designing the code, I'm a bit lost as to how they're different from each other). I'm trying to repeat the middle character in an inputted string coupled with a repetition (int).
Why doesn't this work? Either in python idle or ion codewrite?
def mid_repeated (st, rep):
if len(st)%2==0:
middle = (len(st)/2)
center = (st[middle])*rep
rep = "!"*rep
return center + " " + str(rep)
else:
middle = (len(st)/2)
center = (st[middle])*rep
rep = "!"*rep
return center + " " + str(rep)
return mid_repeated
As soon as a function returns something, it breaks. This is probably what you're wondering about.
However, I'm not exactly sure what you're trying to accomplish by returning the function itself. You may want to look at that again.
I'm not sure if this helps at all, but return, returns something that can then be used outside of the function, where as print, just prints something...
The difference between
def a(x):
print x
and
def b(x):
return x
is that they do different things. (No. Really?)
a() outputs the "thing" given as x and (implicitly) returns None.
b() does nothing but returning x.
The difference becomes clearer if you do
def examine(function):
print "Calling:"
ret = function(42)
print "Call done."
print "Function returned", ret
if I use this function to examine my functions,
examine(a)
prints
Calling:
42
Call done.
Function returned None
so you clearly see that the 42 is printed while the function runs and the function's return value is None.
OTOH,
examine(b)
prints
Calling:
Call done.
Function returned 42
which proves that the function prints nothing but giving the value provided back to the caller (as a return value), who is able to print it whenever appropriate, or do anything else with it.
Other point of view: print prints the given value immediately, return just gives it to the caller to do whatever is desired.

Categories