print ('Hello')
for i in range(2,11,-2):
print(i)
Whats wrong with this code? I'm trying to print out :
Hello
10
8
6
4
2
but it only prints out hello. I ctrl+enter twice after print hello
If you specify a negative step in range you also have to make start bigger than stop:
for i in range(10,0,-2):
print(i)
Another way to do what you want, would be to use reversed:
for i in reversed(range(2,11,2)):
print(i)
The range() function will include the first value and exclude the second.
a = " ".join(str(i) for i in range(10, 0, -2))
print (a)
Read what range() does. First argument is a starting number, you can't start from smaller if you want to print from bigger to smaller.
You can do range(2, 11, 2) for increasingly list or range(10, 1, -2) for decreasingly.
There's also reverse range(2, 11, 2)[::-1] option, but its better to just use it as planned.
Since your range starts at 2, and ends at 11, this code won't work since you are stepping down by -2. You will have to START at 10 and then step down negatively instead of stepping down from 2. Below I have an example that gets you the output that you are seeking:
print('Hello')
for i in range(10, 1, -2):
print(i)
And here is your output:
Hello
10
8
6
4
2
Related
I have a code like this
for x in range(1,20):
print(x)
it works perfectly, but when I want to start from the end like => 60 to 1 i tried this first
for x in range(20,1):
print(x)
but it prints nothing!
also, I tried this,
for x in range(1,20):
count = 20
count -= 1
print(count)
but -= 1 only printed twenty samples of 19.
any help?
First answer:
Use range() built-in function whit negative step:
for x in range(20, 1, -1):
print(x)
Second answer:
You have to declare the count variable outside the for loop, otherwise it will be initialized to 20 each loop, and then subtracting 1 will always make 19 for each loop.
count = 20
for x in range(1,20):
count -= 1
print(count)
#Massifox already pointed out the -1 step but I can add more context. The optional third parameter in range() is step. If you did range(1, 20, 2) it would print 2, 4, 6, ..., 18 because you specified steps of 2.
For your example, you started 20, and incremented until you hit 1, but that will never happen, so nothing gets printed, so you specify a step of -1 like range(20, 1, -1), so that it will step backwards each iteration printing 20, 19, ..., 2 (second parameter is exclusive).
When you tried this
for x in range(1,20):
count = 20
count -= 1
print(count)
You created a temp variable named count, initialized to 20, subtracted one from it, and then printed it, and the result was the 19's you were getting. Everything inside the for loop will be a temporary variable and cannot affect the iteration of the for loop. LMK if this is too verbose.
I have a very simple piece of code but it's driving me nuts to understand the logic.
for a in range(6):
print("\t\t")
for j in range(a):
print(a, end=" ")
The output is:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
the first value to be printed is supposed to be 0 since the range(6) starts from 0 and a and j wrapped in for loops both are supposedly getting 0 as first element but it starts from 1 instead.
That comes, because the for loop in range 0 will not be executed. You can not execute something if that has to go 0 times. To make it run as you would like to be, you would have to write following code:
for a in range(6):
print("\t\t")
for j in range(a + 1):
print(a, end=" ")
The value of a is the number of iterations of the inner loop. (Note that the question should have the j loop indented, so that it is inside the a loop, in order to get the output shown.)
On the first iteration, a is 0, and then range(0) will give an empty sequence, which give zero iterations when iterating over it. (Specifically a StopIteration will occur the first time a value is fetched.)
On the next iteration of the outer loop, there will be one j value when iterating, and so forth.
So 0 is printed 0 times (i.e. not at all), then 1 is printed once, 2 is printed twice, ... , 5 is printed 5 times.
You are not using the values of j at all, but they will be:
when a is 0: no iterations
when a is 1: j is 0 only
when a is 2: j is 0, 1
... etc, up to ...
when a is 6: j is 0, 1, 2, 3, 4, 5
Note that in fact it is conventional to use a variable name _ for a loop variable whose value you do not use (at least assuming that it not inside a loop already using _). If you adhere to this convention (i.e. use _ instead of j) and also fix the indentation, then your code might look like:
for a in range(6):
print("\t\t")
for _ in range(a):
print(a, end=" ")
I am new to python. I have executed for loop with runtime input.
Loop 1:
a = int(input("enter\n"))
for i in range(1, a+1):
print(i)
The output prints all values from 1 to 5.
Loop2:
for i in range(1, 5):
print(i)
The output print the values from 1 to 4.
Please someone explain the different between the above two for loop.
In the range function in python, the last argument is exclusive. This means that it represents the stopping point of the range but is not included in the output. Since the first loop has a+1 as the last argument, a will be included in the output.
Did you have a look at https://docs.python.org/fr/3/library/stdtypes.html#range ?
To keep things easy, range(a, b) will return an iterator that goes from a to b - 1.
Thus, your first loop will print 1 to (a + 1) - 1 = a, and your second loop will print 1 to 5 - 1 = 4.
for i in range(0,5):
print(i)
Prints values from 0 to 4 because when the iteration comes to the value 5 it becomes excluded and it won't print the value.
So we add 1 to the final value(maximum) to print till the value we want.
for i in range(0,6):
print(i)
GIVES:
0
1
2
3
4
5
Is everything right with code_cademy here ?
cubes_by_four = [x*x*x for x in range(1,10) if (x*x*x) % 4 == 0]
for x in cubes_by_four:
print x
They are asking me to print each cube which is evenly divisible by four for numbers between 1 to 10. What am I doing wrong here ?
Also is this notation x^3 allowed to get the cube of x ? if yes then why does is it results in wrong output ?
Finally print that list to the console
>>> cubes_by_four = [x**3 for x in range(1,11) if x**3 % 4 == 0]
>>> print(cubes_by_four)
[8, 64, 216, 512, 1000]
It says print the list, not print each item in the list to console
When you write range(1,10), you include 1 but exclude 10.
So correct code is:
cubes_by_four = [x*x*x for x in range(1,11) if (x*x*x) % 4 == 0]
print cubes_by_four:
It will be a good practice to use x**3 for cubes.
cubes_by_four = [x**3 for x in range(1,11) if (x**3) % 4 == 0]
Your range is off. You need range(1, 11), because the second argument of range() is the first value to exclude. range(1, 10) only gives you number 1 through 9.
If you want to include the value 10 you must change the range to range(1, 11), since the range does not include the second parameter.
In regular Python, x^3 does not mean exponentiation but rather the binary operation "bitwise exclusive or". That is exponentiation in SageMath (which is based on Python) but not in regular Python, which uses x**3, or as in your code, x*x*x.
Since you want to print out the list all in one line, including the surrounding brackets, just print using print x. Use that instead of your last two lines of code.
The second argument to range is not included in the range.
Is it between 1 and 10? or is it 1 through 10?
between 1 and 10 is range(2,10)
1 through 10 is range(1,11)
Okay , a couple of things to keep in mind :-
1) if you want numbers 1-10 , do range(1,11) , since the last number is excluded, while the first number is (obviously) , included.
2) instead of (x*x*x) , you can something better like :- pow(x,3) , which essentially means x to the power 3,or x cubed.
So,your final code becomes :-
cubes = [pow(x,3) for x in range(1,11) if pow(x,3) % 4 == 0]
I hope this helps you , keep learning , getting stuck is a part of the wonderful journey in the world of programming . Cheers ! :)
There are two separate problems with the code you showed here:
First, change range(1,10) to range(1,11) because Python doesn't include the second parameter (10), and 10^3 is evenly divided by 4 (1000/4 = 250).
And finally, the tutorial wants you to print the numbers all in a single line, so just use print cubes_by_four instead of the for loop that you used to print each number in a different line.
script that prints the first 10 cube numbers (x**3), starting with x=1 and ending with x=10
for x in range(1,11):
print(x*x*x)
The thing that helped me, was realizing that the range excludes 1, i.e.: range(0,9) will only do 0-8, the right way to do it unless you want 0-8 is 0-10.
Taken from Codecademy:
Use a list comprehension to create a list, cubes_by_four.
The comprehension should consist of the cubes of the numbers 1 through
10 only if the cube is evenly divisible by four.
Finally, print that list to the console.
Note that in this case, the cubed number should be evenly divisible by
4, not the original number.
cubes_by_four = [x ** 3 for x in range(1,11) if (x ** 3) % 4 == 0]
print cubes_by_four
Result:[8, 64, 216, 512, 1000] ie: Range of 5 cubed numbers (evenly divisible by 4)
for x in range(1,11):
print(x*x*x)
def countMe(num):
for i in range(0, num, 3):
print (i)
countMe(18)
def oddsOut(num1, num2):
for i in range(num1):
for j in range(num2):
print(i*j)
oddsOut(3, 8)
I don't understand how the range function works:
in countMe shouldn't the code go up till 18 ;
why is the last number printed in countMe 15, and not 18 ;
why is that in the second function oddsOut the function only counts till 7 for j and not 8 even though j is 8 ;
why is the last number printed in oddsOut 14.
well, from the help:
>>> help(range)
range(...)
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
so the last increment is not stop, but the last step before stop.
in countMe shouldn't the code go up till 18 ;
why is the last number printed in countMe 15, and not 18 ;
why is that in the second function oddsOut the function only founts till 7 for j and not 8 even though j is 8 ;
why is the last number printed in oddsOut 14.
more generally speaking the answer to those questions is that in most of the languages, a range is defined as [start:stop[, i.e. the last value of the range is never included, and the indexes start always at 0. The mess being that in a few languages and when working on algorithmics, ranges start at 1 and are inclusive with the last value.
In the end, if you want to include the last value you can do:
def closed_range(start, stop, step=1):
return range(start, stop+1, step)
or in your example:
>>> def countMe(num):
>>> for i in range(0, num+1, 3):
>>> print (i)
>>>
>>> countMe(18)
0
3
6
9
12
15
18
>>>
The stop parameter in a range does not include that number for example
for i in range(0,5):
print i
would print 0-4 but not 5.
Ranges in Python do not include the ending value. This is consistent with slices.
If you need a way to remember this, consider that range(10) has 10 elements - the numbers 0 through 9.