We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

range(x,y) will create a range of elements from x to y-1
if only one parameter is passed x is assumed to be 0
so range(y) will create elements in range from 0 till y-1

so here range(4) will create a range which includes 0,1,2,3 hence the loop will iterate 4 times as there are 4 elements

it's because since it has to take 4 inputs
for the first iteration it takes the 'x' input
and for second it takes 'y'
similarly for third it takes "z"
finally foe third it takes "n"

It's also an old Perl trick. If you just performed the input command without assigning the data to anything, it was still available in the $_ variable by default.

It just like a while loop (Ex: while(a<5)) . If we need to store in the seperte varible you can use like this x,y,z,a = (int(input()) for n in range(4). It will be stored seperatly

range() is inclusive of the first number (which is 0, by default), but exclusive of the last one. So "for i in range(5): print i" will give you 0,1,2,3,4 (but not five). In this case, we want to also include the number given, hence the +1. The 0 for the first part range(0,5), is optional, since we are starting at the beginning.

read the question carefully you can understand easily.in the question they have given values inside the nested list must not equal to n so we have given a+b+c!=n

range(4) means range(0,4) that is 0,1,2,3 which is because we ahve to read 4 inputs
Read list comprehensions for more. It is an easy way to build a list.

Makes code less readable, also that doesn't improve time complexity or space complexity so I don't find it that useful other than save some lines of code :S

Just curious why the underscore before 'in range 4' is prefered over assigning a variable, is that just some kind of community thing i dont know about?

In the above code, the i will print from 0 to 5. To include 6 also, we need to provide 7 in the place of 6 as range() uses a number less than the provided one.
So, as per the question, we also need the number provided in the range i.e, x. So we provide a number just above x, that is x+1.
Hope you have understood.

## List Comprehensions

You are viewing a single comment's thread. Return to all comments →

Looks good, but to avoid those repetitive input calls you could do something like:

Thanks for the input. Will use this from now! :D

Please explain for me why you use

`range(4)`

did you got the output for the above one by using list comprehension?

This expression is used for reducing the repetitive statements in reading the inputs from console.

here is the solution of problem

list comprehensionshttps://solution.programmingoneonone.com/2020/06/hackerrank-list-comprehensions-problem-solution-python.htmlMy god. That isn't an educational site with ads, that is an ad site with a sprinkle of educational info.

range(x,y) will create a range of elements from x to y-1 if only one parameter is passed x is assumed to be 0 so range(y) will create elements in range from 0 till y-1

so here range(4) will create a range which includes 0,1,2,3 hence the loop will iterate 4 times as there are 4 elements

can you write the complete code snippet using the range(4), I'm getting error. Thanks!

it will take 4 inputs i.e from 0 to 3...

because we have four variables x,y,z,n

because there is 4 inputs ! hope you understand

he want to take input of 4 numbers so in range it counts from 1 then x later until 4 means till n

to get 4 inputs. we are looping the input().

@annguyen bcz its starts from 0 end to 3

it's because since it has to take 4 inputs for the first iteration it takes the 'x' input and for second it takes 'y' similarly for third it takes "z" finally foe third it takes "n"

beacuse of there are only 4 inputs use in the solution

It is for the input

if u dont mention lower limit it will start from 0 and upper up one exclusive so it will return 0,1,2,3

so we will get 4 input for our program

to take inputs from single line

because range 4 means 0,1,2,3 which is in total 4 and 4 itself is excluded.

x,y,z,n=map(int,input('enter vals with spaces').split(" ")) use this its better

x,y,n,z = [int(x) for x in input().split()] can also be used to generalize it more.

My solution for this problem is given here.

HackerRank List Comprehensions solution in Python

Great tip! BTW is there any reason for using '_' as the dummy variable? What's wrong with using letter, for example 'i'?

_ is used to signify that even though something is being returned, we don't plan to use that variable any where.

gives the following error in above code in python 2.7.13 : at 0x7f60c947e910>

You need to put an additional [] in addition to [a,b,c]. Because the output that you want is are lists within a list.

It's also an old Perl trick. If you just performed the input command without assigning the data to anything, it was still available in the $_ variable by default.

It just like a while loop (Ex: while(a<5)) . If we need to store in the seperte varible you can use like this x,y,z,a = (int(input()) for n in range(4). It will be stored seperatly

no (lol u w8ted 3yrs 4 answer ;D)

it depends on programmer what to choose , its your wish

just a small suggestion ..the 4 in for loop will be n+1

N is the number to compare the sum of x, y, z. The loop should actually have 4, as mentioned, for the inputs : x,y,z & n.

in py2 can be w/o int()

Nice solution

WHAT IS THE 4 INDICATES HERE

range(4) indicates 0,1,2,3 i.e the numbers we have given are allocated to the x,y,z,n respectively

4 input() s

cuboid has 4*2 vertices

what is the reason for x+1 y+1 and many more

In simple words it signifies that the range is inclusive of max limit

like the control statement

range() is inclusive of the first number (which is 0, by default), but exclusive of the last one. So "for i in range(5): print i" will give you 0,1,2,3,4 (but not five). In this case, we want to also include the number given, hence the +1. The 0 for the first part range(0,5), is optional, since we are starting at the beginning.

thanku

in python iteration occurs one less than the give variable

Can you help me with this logic ? why we check for a+ b +c !=n

read the question carefully you can understand easily.in the question they have given values inside the nested list must not equal to n so we have given a+b+c!=n

why does this code run only for python 2 and not python 3?

you need () bracket outside the print

It doesn't work on py3 right! Why?

Doesn't work on python3.5

int is not required for input()

Yes it is in Python 3

And this comment is 3 years old

I'm using this one line, but my compiler reads "wrong answer". Am I missing something? What else do I need to write?

u didnt arrange the list in lexicographic order

Disregard, I figured it out.

can u send the solution

I am a newbie to python and its not clear to me. WHY 4? And how the variable iterate from 0 to 1.

range(4) means range(0,4) that is 0,1,2,3 which is because we ahve to read 4 inputs Read list comprehensions for more. It is an easy way to build a list.

How is this syntax even valid. Just wondering.

what does for _ in range(4) do ? what's the significance of the underscore? @omalsa04

I think that _ is used as a dummy variable name. Since it's meaning isn't really significant the name can be something representing that.

Wow nice way to take inputs.

why range (4) ???

why do i need to add parentheses around

in order for this code to work?

coz you have to assign these values to different variables

in place '_' you should 'n'

both methods are coprrect :0

Makes code less readable, also that doesn't improve time complexity or space complexity so I don't find it that useful other than save some lines of code :S

You can also take in the entire buffer at once with

or you could use functions

x, y, z, n = (int(input()) for _ in range(4)) isn't working for me. I'm getting a EOF when reading line error.

@vorayash0510, this is great!

why do you use the '_' symbol?

_ is used to indicate that you don't care about the value of the variable, and are just using as a placeholder.

"_" is a valid variable name.

So for example if we need an instruction to be executed thrice, one way of writing would be '''

'''

but since we're not using variable 'i' inside the loop, value of 'i' doesn't matter

hence same could be written as,

'''

'''

p.s.

even better way would be

'''

'''

But that's a different story :P, I hope this helps ! :D

kindly explain its working please

Thanks for this code

What is for "_" in range(4) _ this underscore refers to?

Just curious why the underscore before 'in range 4' is prefered over assigning a variable, is that just some kind of community thing i dont know about?

can someone tell me why do we use the range x+1 or y+1? why not just the highest limit x

In the above code, the i will print from 0 to 5. To include 6 also, we need to provide 7 in the place of 6 as range() uses a number less than the provided one. So, as per the question, we also need the number provided in the range i.e, x. So we provide a number just above x, that is x+1. Hope you have understood.

this type of input doesn't work it gives error

x,y,z,n=map(int,input('enter vals with spaces').split(" ")) use this its better

x,y,z,n=map(int,input().split())