What is the difference between print(var,var) and print(var),(var)? - python

**>>>a=2
>>>b=5
>>>print(a,b)
2 5
>>>print(a),(b)
2
(None, 5)**
Can someone help me understand why the 2nd print statement returns (None,5)?
I'm new to this, so sorry if this is a silly question.
Thanks.

There are three pertinent facts here.
The print function returns nothing. Well, technically it returns "None". Its usefulness is entirely in its side effects -- printing to the console.
The command line interpreter (what you get with ">>>") doesn't print anything if the line returned None. Thus, for print(a,b), you see the effects of the print function, but the return value of the print function (None) is suppressed.
Commas are used to create tuples. Your third statement creates a two-element tuple. The first element is the return from print, which is None. The second element is the value 5, from the name b. So, on your console, you see the effect of the print function (printing a which is 2). Then, you see the command line interpreter displaying the final result of your statement, which is the tuple (None, 5).
(Actually, "print is a function not a statement" is also a pertinent fact...)

Related

Recursion - why does python printing this values?

I'm beginner python fan.
Note - I know, one "return" is missing.
Purpose of function: sequences with a repeating beginning are given. We don't know how long these "prefixes" are, but we do know there are 7 different ones (the same length). We need to find these prefixes.
However, I am curious how the program works without that word. Why does the function print sets (last line) - shouldn't it still stop at else: search (...), go back to the top, and throw out just None when set is long enough?
I also noticed that if I put print between 'else:' and 'search(...)', the sets are printed in the normal order, and if under 'search(...)'- in the reverse order (ie from where the words are longest).
I would be grateful if someone could explain why it prints at all and in reverse order :)
def search(seq_list, length, position=1):
seq_set = set()
for a in seq_list:
seq_set.add(a[:position])
if len(seq_set) == length:
return seq_set
else:
search(seq_list, length, position + 1)
print(seq_set)
No, it won't stop. A function doesn't return just because a recursive call returns. search finishes, returns its value, then the caller ignores that value and continues on to print(seq_set).
It's not clear that it "works", since you are printing the local value of seq_set, not the one the recursive call returns and you ignore.

List slices: Why the difference of output of these two similar code snippets?

What is happening in these two simple lines of code?
I guess it is storing the print function as a part of the variable.
a=[0,1,2,3,4,5,6,7,8,9]
print(a[-2:-7:-1])
a=[0,1,2,3,4,5,6,7,8,9], print(a[-2:-7:-1])
This is two statements:
a=[0,1,2,3,4,5,6,7,8,9]
print(a[-2:-7:-1])
This is one statement. Let me add the implicit parentheses:
# a=[0,1,2,3,4,5,6,7,8,9], print(a[-2:-7:-1])
a = (
[0,1,2,3,4,5,6,7,8,9],
print(a[-2:-7:-1])
)
If a was previously declared and assigned a value, then the print() function would still execute properly, ([0,1,2,3,4,5,6,7,8,9], None) would get assigned to a (because the return value of print() is None).
If a hadn't been declared and assigned yet, then you'd get an error on this line saying as much.

Difference between print(<variable_name>) and <variable_name> in python shell?

When i was going through the codes i found an interesting thing, if we initialize a variable in python shell, and we print that variable we get the value, if we use type the variable and press enter we get the same value. I could't figure out the reason behind it?? Can anyone help....
>a = 5
>print(a)
>5
>a
5
print will work in the shell AND in a script while giving the variable name is just a syntactic sugar for the shell.
The Python console is a Read Evaluate Print Loop (REPL). It first reads your input, evaluates it, prints any output, and repeats until you exit. It's a very common way to interact with a language, and exists for many many languages.
Another example of what you've observed is
>>> 1+2
3
Here the console evaluates 1+2 and prints the returned result. Try it with any function or expression.
print() works as expected because when it is evaluated, it does its own printing before returning None, just how any other function does all of its own code before returning to the console whatever will be printed. You could try this, for example
>>> type(print('test'))
test
<class 'NoneType'>
Here the console evaluates print('test'), which causes the first, manual print, and then automatically prints the return of the type call, resulting in the second line.
To answer your question, the difference between >>> print(a) and >>> a is that, in the first case, the console evaluates the print and has no return value to print, and in the second case, the console evaluates the expression and prints the result, which is a's value.

The None Value/Code in Automate the Boring Stuff

The text says the following:
In Python there is a value called None, which represents the absence of a value. None is the only value of the NoneType data type. (Other programming languages might call this value null, nil, or undefined.) Just like the Boolean True and False values, None must be typed with a capital N.
This value-without-a-value can be helpful when you need to store some-thing that won’t be confused for a real value in a variable. One place where None is used is as the return value of print(). The print() function displays text on the screen, but it doesn’t need to return anything in the same way len() or input() does. But since all function calls need to evaluate to a return value, print() returns None. To see this in action, enter the following into the interactive shell:
>>> spam = print('Hello!')
Hello!
>>> None == spam
True
Behind the scenes, Python adds return None to the end of any function definition with no return statement. This is similar to how a while or for loop implicitly ends with a continue statement. Also, if you use a return statement without a value (that is, just the return keyword by itself), then None is returned.
I think I understand what None is, but I am not understanding the code. Why is it that spam is then equal to None when it was assigned to be print('Hello!')? When I enter spam into the interactive shell immediately after the assignment it returns nothing. I get a feeling it is because the argument Hello! is immediately forgotten when print() is called and returns a value, but if I have defined spam to be the print() function with the argument Hello! passed through should it not always return Hello!?
To add to the comments and be more clear, your print() function printed 'Hello!' to the screen and returned None to the program. Printing and returning are not the same thing--printing is for the user, returning is for the program. The print goes to the screen only and (usually) cannot be further used by the program. The returned value can be stored in a variable, such as spam, and used further.
The distinction between printing and returning is important enough that the Python standard is that if a function prints something it should not return any value other than None, and if the function returns a value it should not print anything. This standard is not followed by many other languages (most notoriously C) and is not consistently followed in Python, but this distinction does help the clarity of Python. If you want to study this concept further, do a search on "side effects" in programming.
You are assigning spam to the print() function which doesn't return anything, aka None

print function not printing appropriate output using eval

I am getting 2 different outputs using 2 similar commands:
>>> inp = 'print("hi")'
>>> print(eval(inp))
hi
None
>>> eval(inp)
hi
How to I make print(eval(inp)) print just 'hi'? Why is None printing as well?
So here's what happens when you do print(eval('print("hi")')):
eval() is called, with the argument 'print("hi")'
Accordingly, the code print("hi") is executed
"hi" is printed to the console
Having finished executing, print() returns None.
Having executed the code 'print("hi")', the eval() function records the return call of that function. Which was None.
Accordingly, eval() returns None, since that was the result of the code it ran.
The outer print() call is supposed to print whatever the eval() function returned. Now it looks like print(None).
None is printed to console.
tl;dr, print() is called two different times. Thus, two different things are printed: "hi" the first time, and None the second time.
If all you want is to print "hi", you can just do eval('print("hi")') - or you could do print(eval("hi")), since in this case eval() would return "hi" and that's what would be printed. In either of those cases you would only ever be executing one print statement.
Though, in general, please do not use eval() for anything. It's notoriously risky, prone to errors that can completely break your program, and there's nothing you can do with it that you can't do with the code you'd put inside it. The only feasible reason for using eval() would be to respond dynamically to user-generated code, which is a terrible idea because it allows code injections. The user shouldn't be able to do that.

Categories