0
print("Enter number of test cases and length of each string")
t,s=map(input,range(2))
print(t,s)

It prompts as Enter number of test cases and length of each string 03 13. Why is there 0 and 1 before 3's?

The below line shows ouput

t,s=map(lambda x :input ,range(2))
 OUTPUT-<built-in function input> <built-in function input>
WHY IS THAT?

Whereas this line :

t,s=map(int(input()),range(2))
OUTPUT: Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(int(input()),range(2))
TypeError: 'int' object is not callable.

WHY IS IT NOT CALLABLE?

t,s=map(input(),range(2))

THE ABOVE LINE HOWEVER CAUSES THIS ERROR:
Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(input(),range(2))
TypeError: 'str' object is not callable.

WHY IN THE FIRST CASE NO ERROR AND IN LAST ONE THERE IS.
python
t,s=map(lambda x: int(input()),range(2))


ALSO THIS ONE WORKS JUST FINE.
WHY USING LAMBDA MAKES IT WORK FINE BUT WITHOUT LAMBDA AS IN THE 3rd ONE IT THROWS ERROR.

v7676
  • 31
  • 2
  • `t,s=map(input, ['']*2)`? – deadshot Sep 15 '20 at 13:06
  • For your first snippet, I think that when `map` calls `input()` is also passes the parameter of the items coming from `range(2)` which is `0` and then `1`. It seems that `input()` just prints its parameter. The last snippet results in the error because, as I have mentioned, `map` calls its first parameter. – quamrana Sep 15 '20 at 13:07
  • In the middle snippet, when `map` calls the `lambda`, it returns the built-in `input` without calling it. Did you mean: `t,s=map(lambda _ :input() ,range(2))`? – quamrana Sep 15 '20 at 13:11
  • Does this answer your question? [Understanding the map function](https://stackoverflow.com/questions/10973766/understanding-the-map-function) – quamrana Sep 15 '20 at 13:19
  • And, please stop SHOUTING. – quamrana Sep 15 '20 at 13:20
  • Thanx for the help . – v7676 Sep 15 '20 at 14:06

1 Answers1

1
print("Enter number of test cases and length of each string")
t,s=map(input,range(2))
print(t,s)

You can understand this by breaking it down into pieces.

range(2) returns a sequence of two numbers, starting from 0. So the first number in the sequence will be 0 and the second will be 1.

map(f, sequence) will call function f once for each item in sequence, and collect the results into a new sequence. Since we have map(input, range(2)) it will call the function input twice, once with 0 as input and once with 1 as input, i.e. input(0) then input(1).

Each time input(prompt) is called, it displays the prompt and waits you to type an input string. So the first time the prompt is 0 and the second time the prompt is 1.


t,s=map(lambda x :input ,range(2))
 OUTPUT-<built-in function input> <built-in function input>

WHY IS THAT?

lambda x: input defines a lambda-function that takes one argument (x) and returns the function input as a value. Functions can be passed around as values, indeed we want to give map a function as its first argument. But in this case we probably don't want to return the function input as a value when our lambda-function is called. Instead we want to call it. To call a function with no arguments, you write a pair of parentheses with nothing between them. E.g. input(). So here's what you could write instead:

t,s=map(lambda x :input() ,range(2))

t,s=map(int(input()),range(2))
OUTPUT: Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(int(input()),range(2))
TypeError: 'int' object is not callable.

The first argument to map should be a function, because map itself will call it (repeatedly). But in this case, rather than passing it a function we are passing it a number. You can try instead:

t,s=map(lambda x :int(input()) ,range(2))

In this case the lambda function tells Python that we don't want to evaluate int(input()) immediately, but instead to create a function that will do that when it is called.


t,s=map(input(),range(2))

THE ABOVE LINE HOWEVER CAUSES THIS ERROR:

Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(input(),range(2))
TypeError: 'str' object is not callable.

WHY IN THE FIRST CASE NO ERROR AND IN LAST ONE THERE IS.

This is again the same issue. map(input(), range(2)) will call the input function straight away and pass its return value (a string) to map as an argument, whereas map(input, range(2)) passes the function input itself as an argument.

Weeble
  • 17,058
  • 3
  • 60
  • 75