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

16th Dec 2023, 9:46 AM
Vel Murali
Vel Murali - avatar
35 odpowiedzi
+ 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)
17th Dec 2023, 12:35 PM
Ajaiy P
Ajaiy P - avatar
+ 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))
17th Dec 2023, 7:45 AM
Lothar
Lothar - avatar
+ 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))
17th Dec 2023, 10:04 AM
Lothar
Lothar - avatar
+ 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
16th Dec 2023, 11:20 AM
Rain
Rain - avatar
+ 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))
17th Dec 2023, 12:33 PM
Ajaiy P
Ajaiy P - avatar
+ 3
Thank you Bob, Lothar, Rain, Raghavan, Ajay👍
17th Dec 2023, 1:24 PM
Vel Murali
Vel Murali - avatar
+ 2
Rain your code was exactly the same as the first one I thought of, but then those commas became problematic...😁
16th Dec 2023, 12:50 PM
Bob_Li
Bob_Li - avatar
+ 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)
17th Dec 2023, 3:09 AM
Bob_Li
Bob_Li - avatar
+ 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))
17th Dec 2023, 12:59 PM
Bob_Li
Bob_Li - avatar
+ 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)
16th Dec 2023, 11:00 AM
Bob_Li
Bob_Li - avatar
+ 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.
16th Dec 2023, 12:20 PM
Rain
Rain - avatar
+ 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 " )
17th Dec 2023, 2:18 AM
Ragavan Kumar
Ragavan Kumar - avatar
+ 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))
17th Dec 2023, 10:38 AM
Bob_Li
Bob_Li - avatar
+ 1
Hi all
17th Dec 2023, 10:53 AM
Vel Murali
Vel Murali - avatar
+ 1
Just I logged into system and tried , I got the output that I wanted
17th Dec 2023, 10:53 AM
Vel Murali
Vel Murali - avatar
+ 1
yeah lol
17th Dec 2023, 1:03 PM
Ajaiy P
Ajaiy P - avatar
+ 1
welcome
17th Dec 2023, 1:29 PM
Ajaiy P
Ajaiy P - avatar
+ 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.
17th Dec 2023, 10:36 PM
Rain
Rain - avatar
+ 1
Thanks for the correction Bob_Li
23rd Dec 2023, 12:15 PM
Ajaiy P
Ajaiy P - avatar