You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/learnpython.org/en/Input and Output.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -31,20 +31,20 @@ this is especially used to input integers. The advantage of input() over raw_inp
31
31
Here we make use split() and map() functions
32
32
33
33
#give two integers in first line and more than two integers in third line
34
-
a,b=map(int,raw_input().split()
35
-
array=raw_input().split()
36
-
sum=0
34
+
a, b = map(int,raw_input().split())
35
+
array = raw_input().split()
36
+
sum = 0
37
37
for each in array:
38
-
sum=sum+int(each)
39
-
print a,b,sum #prints first two integers from first line and sum of integers of second line
38
+
sum = sum + int(each)
39
+
print(a, b, sum) # prints first two integers from first line and sum of integers of second line
40
40
41
41
###Output formatting
42
42
You might have already noticed that print statement automatically inserts a new line. The use of comma like in the above code prints the values in a single line separated by a space.
43
43
The sys module provides various functions for output formatting but here we learn how to use basic knowledge of formatting to output in our required way. Lets see a few examples to learn output formatting
result = list(map(round, circle_areas, range(1,7)))
54
+
result = list(map(round, circle_areas, range(1,7)))
55
55
56
56
print(result)
57
57
58
58
See the beauty of ```map()```? Can you imagine the flexibility this evokes?
59
59
60
-
The ```range(1,7)``` function acts as the second argument to the ```round``` function (the number of required decimal places per iteration). So as ```map``` iterates through ```circle_areas```, during the first iteration, the first element of ```circle_areas```, ```3.56773``` is passed along with the first element of ```range(1,7)```, ```1``` to ```round```, making it effectively become ```round(3.56773, 1)```. During the second iteration, the second element of ```circle_areas```, ```5.57668``` along with the second element of ```range(1,7)```, ```2``` is passed to ```round``` making it translate to ```round(5.57668, 2)```. This happens until the end of the ```circle_areas``` list is reached.
60
+
The ```range(1,7)``` function acts as the second argument to the ```round``` function (the number of required decimal places per iteration). So as ```map``` iterates through ```circle_areas```, during the first iteration, the first element of ```circle_areas```, ```3.56773``` is passed along with the first element of ```range(1,7)```, ```1``` to ```round```, making it effectively become ```round(3.56773, 1)```. During the second iteration, the second element of ```circle_areas```, ```5.57668``` along with the second element of ```range(1,7)```, ```2``` is passed to ```round``` making it translate to ```round(5.57668, 2)```. This happens until the end of the ```circle_areas``` list is reached.
61
61
62
-
I'm sure you're wondering: "What if I pass in an iterable less than or more than the length of the first iterable? That is, what if I pass ```range(1,3)``` or ```range(1, 9999)``` as the second iterable in the above function". And the answer is simple: nothing! Okay, that's not true. "Nothing" happens in the sense that the ```map()``` function will not raise any exception, it will simply iterate over the elements until it can't find a second argument to the function, at which point it simply stops and returns the result.
62
+
I'm sure you're wondering: "What if I pass in an iterable less than or more than the length of the first iterable? That is, what if I pass ```range(1,3)``` or ```range(1, 9999)``` as the second iterable in the above function". And the answer is simple: nothing! Okay, that's not true. "Nothing" happens in the sense that the ```map()``` function will not raise any exception, it will simply iterate over the elements until it can't find a second argument to the function, at which point it simply stops and returns the result.
63
63
64
-
So, for example, if you evaluate ```result = list(map(round, circle_areas, range(1,3)))```, you won't get any error even as the length of ```circle_areas``` and the length of ```range(1,3)``` differ. Instead, this is what Python does: It takes the first element of ```circle_areas``` and the first element of ```range(1,3)``` and passes it to ```round```. ```round``` evaluates it then saves the result. Then it goes on to the second iteration, second element of ```circle_areas``` and second element of ```range(1,3)```, ```round``` saves it again. Now, in the third iteration (```circle_areas``` has a third element), Python takes the third element of ```circle_areas``` and then tries to take the third element of ```range(1,3)``` but since ```range(1,3)``` does not have a third element, Python simply stops and returns the result, which in this case would simply be ```[3.6, 5.58]```.
64
+
So, for example, if you evaluate ```result = list(map(round, circle_areas, range(1,3)))```, you won't get any error even as the length of ```circle_areas``` and the length of ```range(1,3)``` differ. Instead, this is what Python does: It takes the first element of ```circle_areas``` and the first element of ```range(1,3)``` and passes it to ```round```. ```round``` evaluates it then saves the result. Then it goes on to the second iteration, second element of ```circle_areas``` and second element of ```range(1,3)```, ```round``` saves it again. Now, in the third iteration (```circle_areas``` has a third element), Python takes the third element of ```circle_areas``` and then tries to take the third element of ```range(1,3)``` but since ```range(1,3)``` does not have a third element, Python simply stops and returns the result, which in this case would simply be ```[3.6, 5.58]```.
Copy file name to clipboardExpand all lines: tutorials/learnpython.org/en/Multiple Function Arguments.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -15,15 +15,15 @@ It is possible to declare functions which receive a variable number of arguments
15
15
print("Third: %s" % third)
16
16
print("And all the rest... %s" % list(therest))
17
17
18
-
The "therest" variable is a list of variables, which receives all arguments which were given to the "foo" function after the first 3 arguments. So calling `foo(1,2,3,4,5)` will print out:
18
+
The "therest" variable is a list of variables, which receives all arguments which were given to the "foo" function after the first 3 arguments. So calling `foo(1, 2, 3, 4, 5)` will print out:
19
19
20
20
def foo(first, second, third, *therest):
21
21
print("First: %s" %(first))
22
22
print("Second: %s" %(second))
23
23
print("Third: %s" %(third))
24
24
print("And all the rest... %s" %(list(therest)))
25
25
26
-
foo(1,2,3,4,5)
26
+
foo(1, 2, 3, 4, 5)
27
27
28
28
It is also possible to send functions arguments by keyword, so that the order of the argument does not matter, using the following syntax. The following code yields the following output:
0 commit comments