+ 3
Intervals in Python3
Hi! Could someone teach me how to create an interval in Python? I've already searched on the internet but came without a good answer. I get an error whenever I type ---> x=interval ( [2,y-1] ). I need integers to build my very first program. I tried to import interval from interval module but Py doesn't recognize it. Thanks in advance!
8 Antworten
+ 15
Virgil Baclanov Let's say you want to divide the integer 294 by every integer in the interval from 13 up to 54.
The results can make a list like this:
x = 294
results = [x/y for y in range(13, 55)]
Observation: the `%` symbol is for modulus operation, not for division.
+ 11
Assume you need an interval from 1 to 9.
So you do:
my_interval = list(range(1, 10))
# the end of the interval is non-inclusive
# so if you need an interval from "m" up to "n", you should do range(m, n+1)
+ 6
You would do something like this:
new_list = []
for i in interval:
new_list.append(i % 5)
print(new_list)
--------------
To shorten that you could do:
print([i % 5 for i in interval])
+ 5
You might be looking for range objects:
>>> x = range(50, 100)
>>> len(x)
50
>>> 83 in x
True
>>> x[3]
53
>>> x[-1]
99
>>> x[13:24]
range(63, 74)
>>> for i in range(11): print(i*i, end = ' ')
0 1 4 9 16 25 36 49 64 81 100
+ 4
Eduardo Petry Just A Rather Ridiculously Long Username
Thanks a lot, guys! Both versions seem to work for my program. Have a nice day!
+ 4
Oh okay thanks, happy coding!
+ 3
Just A Rather Ridiculously Long Username happy coding, too!
+ 2
Eduardo Petry thanks! But, what do I do when I wanna divide an integer by *each* integers in the interval?
example -- I type ---> if dividend%interval==0: ...
but Phyton is telling me that it is an unsupported operand type(s) for %: 'int' and 'list'