0
Python question about ranges(Beginner)
a=range(1,7,2) if(a[1]==3): print(int(type(a) == list),end='r') print(sum(a)) "output" is 0r9. How our code end up 0r9? I couldn't understand end='r' part
3 Answers
+ 5
By default end='\n' which gets your end of line after your contents is printed. Your 'int(type(a) == list)' tests the expression as false because a is a range not a list so the false is turned into your '0'. The 'r' gets added to the end. 'sum(a)' adds up the values of the range 1, 3, & 5 getting 9.
+ 2
first, lets take a look at the output that a will give: it is a range from 1-7 with 2 between each.
so a will output the following: 1,3,5
if (a[1] (which is 3) == 3 (true)):
so it prints (type(a) == list). since a is an range, it will ouput 0, and at the end it will output 'r' because it is what end does
then it prints the sum of a, which is 1+3+5 = 9 and then it adds that to 0r which now is 0r9
but I hope I explained the "hard" part for you correctly and understandable
+ 1
Thank you both of you for help,it was easier than what i've thought! :)