0

what is the output for the below code? Print(range(0, 8,2)) . Can anyone explain it how we are getting it

Range in python

4th Oct 2021, 11:19 AM
hemu
7 Antworten
+ 3
You can copy it onto playground and check out what happens! It's giving a range of integers The 1st argument is the starting value, the 2nd the end value (exclusive), the 3rd is the step-size.
4th Oct 2021, 11:22 AM
Lisa
Lisa - avatar
+ 3
Oh, okay. To be precise, range() returns the object that generates the sequence (not the sequence itself). If we put list() around range() we get the actual sequence.
4th Oct 2021, 11:34 AM
Lisa
Lisa - avatar
+ 3
range() gives a ***range object***
4th Oct 2021, 11:54 AM
Lisa
Lisa - avatar
+ 1
Hi hemu! That's because, range(10) returns an object that prints as range(10) (since it shows the starting value when it prints) and whose elements are the integers from 0 to 9, so range(10) gives the one-element list (range( 10)) and list(range(10)) gives the 10-element list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. We can also use list unpacking instead. https://code.sololearn.com/cHTazGEMLZvc/?ref=app
4th Oct 2021, 1:21 PM
Python Learner
Python Learner - avatar
+ 1
Thanks Lisa and python learner
6th Oct 2021, 12:51 PM
hemu
0
I tried in python got the Output as range(0, 8,2) . Didn't understand why it got print as the same
4th Oct 2021, 11:24 AM
hemu
0
Yeah i understood the range function how it works.... But confused with this output..... Do you mean giving range function inside print will take it as a statement and print the same
4th Oct 2021, 11:36 AM
hemu