0
Python 1 to 11 numbers which are divided by 3 those numbers wanted to print and count also
Python 1 to 11 numbers which are divided by 3 those numbers wanted to print and count also https://sololearn.com/compiler-playground/cutXuVtN3Kon/?ref=app
36 Answers
+ 5
One liner method:
print("The numbers are:", ", ".join([str(i) for i in range(3, 11, 3)]), "\n and number of numbers is:", 11//3)
+ 4
Vel Murali ,
just an other suggestion and some comments from my side:
> by using a *list* to store the desired numbers, we can omit the counter for the numbers. the counting of numbers can be achieved by using *len() built-in function*.
> also try to use *meaningful variable names* and try to follow the *python style guide pep-0008*:
numbers = []
for num in range(1, 11):
if num % 3 == 0:
numbers.append(num)
print(numbers,'these numbers are divisible by 3 and the total count is', len(numbers))
+ 4
an other possible version can be to use the *filter()* built-in function. it requires *2 arguments*:
> a function that can be a *named function* or a *lambda function*.
lambda function is perfect here, it is short, simple to read and understand.
> the iterable with the input values, in this case a range of numbers.
numbers = list(filter(lambda x: x % 3 == 0, range(1, 11)))
print(numbers,'these numbers are divisible by 3 and the total count is', len(numbers))
+ 3
Vel Murali ,
# This will get you closer.
numbers = [] # empty list
count = 0
for i in range(1, 11):
if i % 3 == 0:
numbers.append(i) # grow the list
count += 1
print(*numbers, 'These numbers are divisible by 3, and their count is', count)
# Output:
# 3 6 9 These numbers are divisible by 3, and their count is 3
+ 3
The range function also has an additional step argument, by which we can iterate to the next number obtained by adding the step value with the current number.
Now, we can use a list to store the numbers and print the length of the list to show count.
divis = []
for i in range(3, 11, 3):
divis.append(i)
print(divis)
print(len(divis))
+ 3
Thank you Bob, Lothar, Rain, Raghavan, Ajay👍
+ 2
Rain your code was exactly the same as the first one I thought of, but then those commas became problematic...😁
+ 2
Ragavan Kumar
print( ,end=' ') is a good alternative.
three =0
for i in range(1,11):
if i%3==0:
print(i, end=' ')
three=three+1
print('these numbers are divisible by 3 and the total count is',three)
+ 2
Ajaiy P
yes, that is the ultimate, simplest method we all missed. 😎😎😎
but a minor correction. you need a list of string to be able to use join. I also used the walrus operator to assign the list to variable n, so that there is no need for the magic number 11//3. so
print("The numbers are:", ", ".join(n:=[str(i) for i in range(3, 11, 3)]), "\n and number of numbers is:", len(n))
+ 1
three =0
numbers = []
for i in range(1,11):
if i%3==0:
numbers.append(str(i))
three=three+1
print(','.join(numbers),'these numbers are divisble by 3 and total count is',three)
+ 1
Bob_Li ,
Oops. I didn't see you had posted code while I was in Pydroid 3 writing mine, and I unknowingly posted almost the same code right after.
I like that you used join to get the commas. I used *, which gets rid of the [ ] brackets, but of course also loses the commas.
When I said "get you closer", I didn't mean than your code.
+ 1
count=0
range=11
i=1
while(i<=11):
if(i%3==0):
count+=1
print(i)
i+=1
else:
i+=1
print("There are ",count," numbers which are divisible dy Three between 1 to 11 " )
+ 1
Lothar nice one with filter.
Your direct-to-list method gave me this idea for the following one-liner:
print(*(n:=[i for i in range(1,11) if i%3==0]),'these numbers are divisble by 3 and count is',len(n))
or if you want comma separated:
print(','.join(n:=[str(i) for i in range(1,11) if i%3==0]),'these numbers are divisble by 3 and count is',len(n))
+ 1
Hi all
+ 1
Just I logged into system and tried , I got the output that I wanted
+ 1
yeah lol
+ 1
welcome
+ 1
Vel Murali ,
You're writing too many messages. To clear each one from my notifications list, I have to tap it, get taken to the thread, and go back.
Figure out what you want to say and say it all in one message.
I'm glad you're happy with your code now, but I'm unfollowing this thread. I don't want to have to clear a bunch more dots tomorrow.
+ 1
Thanks for the correction Bob_Li