0

While using keyword argument,and fetching getting only one pair of data:

def all(**student):
    for k,v in student.items():
        return k,v
all(name='ashu',age=25)

Getting both pair of data, why so?

def all(**student):
    for k,v in student.items():
        print(k,v)
all(name='ashu',age=25)
ChasedByDeath
  • 183
  • 3
  • 15
  • 2
    Return returns once and exits. Print keeps printing in each step till the end of loop. It is their nature. Btw, please indent your code properly. – FatihAkici Apr 23 '20 at 19:39
  • means irrespective of no of data,it will return only first one? – Ashuosh Mishra Apr 23 '20 at 19:41
  • Because `return` and `print` do two, **completely** different things. – juanpa.arrivillaga Apr 23 '20 at 19:41
  • `all()` is a built-in function, so it isn't the best choice of a name for your own function (though that is perfectly legal and probably harmless for a small homework assignment). – John Coleman Apr 23 '20 at 19:42
  • Yes, return means "execute and terminate". Print and yield continue executing. – FatihAkici Apr 23 '20 at 19:42
  • `return` is a statement that *returns control to the caller*. So it will *terminate the execution of the function if it is reached*. All python functions implicitly return `None` if no `return` statement is added. `print` is a function which prints the string form of its arguments to the standard output device (by default), or optionally, to whatever file-object is passed to the `file` argument (which defaults to `file=sys.stdout`) – juanpa.arrivillaga Apr 23 '20 at 19:44
  • In the first example, you never actually _use_ the return value of `all()`, so how do you know it only returns one value? – John Gordon Apr 23 '20 at 20:06

0 Answers0