Home > Software engineering >  Inputs vs print functions.. CONFUSED
Inputs vs print functions.. CONFUSED

Time:12-07

I am BRAND NEW to learning python and coding at all really.. I did tech support for years but now trying to learn coding. Anyway I am in a UDEMY course called 100 days of coding. The first exercise I will list here.. print("Hello " input("What is your name ") "!")

What I dont quite understand is when you run this code.. It prints out the input("What is your name") and totally ignores the print("Hello"
Why doesnt it print the ("Hello" before it prints out the input string?? Do inputs always get run first??

I mean if your take out the input then it would just print hello... So since hello is typed before the input.. Why doesnt it read the print before the input..

It does the same thing in this next exercise.. They had us type in the following print( len( input("What is your name? "))) But again it prints out the input("What is your name") before it goes back to the print( len( and prints the length of the string put in by the user even though the print is typed in BEFORE the input on the same line?

I hope im making sense on what im confused about? Any help would be appreciated?

CodePudding user response:

When you run the following:

print("Hello "   input("What is your name ")   "!")

the interpreter of python (whose duty is to understand your code and execute it), wants to print something that is "Hello" input(...) "!" here, so it has to wait for the result of input, and then print out the whole argument.

CodePudding user response:

Welcome to programming, In the above statement print("Hello " input("What is your name ") "!") whenever the print is called, print it evaluates the parameter to print. In the print parameter the string also contains a function call. So the function in the print statement is called i.e. input("What is your name") this input function prints the string What is your name and reads the input from console, concatenates with the param of print statement.

CodePudding user response:

No, you fundamentally misunderstand how the program is being executed. Python code is (mostly) executed from top to bottom, that is relatively safe to assume. But it isn't executed "left to right" exactly. Expressions can have subexpressions. print is a function. If you do function(<sub-expression1>, <sub-expression2>), the the sub-expressions are fully evaluated from left-to-right so their values can be passed as the function arguments. So, if you have three different functions, foo, bar, and baz, and you do:

foo(bar(), baz())

then first bar() is evaluated (called), then baz(), then finally, foo is called, with the results of the previous calls as the arguments. See:

def foo(x, y):
    print("In foo")
    return x   y

def bar():
    print("In bar")
    return 1

def baz():
    print("In baz")
    return 10

result = foo(bar(), baz())
print("result was", result)

So running the above program outputs:

In bar
In baz
In foo
result was 11

So input prints the value you pass to it as an argument, then waits for a newline delimited user input.

You must learn how the language constructs you choose to use are evaluated. Consider another type of expression, a conditional expression, which has three subexpressions e.g <expression 1> if <expression 2> else <expression 3>, then <expression 2>, the condition, is evaluated first, if it is truthy, then <expression 1> is evaluated as the return value of the entire expression, otherwise if it is falsey, <expression 2> is evaluated as the return value of the entire thing.

So consider:

...     return n < 10
...
>>> condition(1)
True
>>> condition(12)
False
>>> "small" if condition(5) else "big"
'small'
>>> "small" if condition(25) else "big"
'big'

So note, conditional expressions "short circuit", this is to potentially avoid an unneeded, expensive operation.

And to prove that only one of <expression 1> or <expression 2> is evaluated based on the truthiness of the condition, see:

>>> def a():
...     print("a executed")
...     return "small"
...
>>> def b():
...     print("b executed")
...     return "big"
...
>>> a() if condition(4) else b()
a executed
'small'
>>> a() if condition(14) else b()
b executed
'big'

CodePudding user response:

print("Hello "   input("What is your name ")   "!")

is interpreted as:

print(something)

where

something = "Hello "   input_answer   "!"

where

input_answer = input("What is your name ")

To know what to print, you need to know what something is, for which you need to know what input_answer is. So that's the order in which the statement is evaluated.

You can do the same for your second example.

CodePudding user response:

every programming language breaks down a line of code into something called tokens so what usually happens is the "print" and the "input" function is split into two tasks and they are queued based on how you called them.

In your case you called "input" first (as it is inside the bracket) and hence the statement inside "input" gets printed. Another example is math.cos(math.sin(30)) so in this case sin of 30 will be calculated and then cos of sin(30) and not sin of cos(30)

Hope this explanation helps

CodePudding user response:

You can see input() as a function when you typed print(),it started to run "Hello" and then your program will go into input() and your print() keeps running until you give your input a value. Hope my explanation can help you :)

  • Related