If you want to print everything at once, you can use join
along with a Generator Expression:
z = (1,2,3,4)
print(f'os números pares foram: {", ".join(str(i) for i in z if i % 2 == 0)} são números pares')
In the case, the join
will use the string ", "
to separate the numbers (i.e., they will be written with a comma and space between them). I had to use str
to turn numbers into strings, otherwise join
makes a mistake.
The result is a string with even numbers, separated by comma and space. This prints:
even numbers were: 2, 4 are even numbers
Of course, depending on the size of the expression, it can get confusing to put everything at once inside the f-string. So it might get less confusing if you do it this way:
z = (1,2,3,4)
pares = ", ".join(str(i) for i in z if i % 2 == 0)
print(f'os números pares foram: {pares} são números pares')
You may have an extra line, but code with fewer lines is not always "best".
Another option is to create a list, like this:
print(f'os números pares foram: {[i for i in z if i % 2 == 0]} são números pares')
Notice the brackets around the for
. This will create a list of even numbers, and this list will be printed. The difference is that in this case the output will be:
even numbers were: [2, 4] are even numbers
When printing the list directly, the elements are shown in square brackets. It is still an option, but using join
(or any other method that mounts the string before it is printed) you have more control over the desired format.
Sorry for not giving any contribution to your answer despite having helped a lot I still don’t have enough reputation :P
– João Gustavo
@Joãogustavo In fact, if the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.
– hkotsubo
Interesting guy! Vlw by the link, so taking advantage that the answer was very pertinent I’ll accept now. But on the most general question of methods, can you tell me if it is pertinent or possible?
– João Gustavo
@Joãogustavo To count how many times a number occurs, there is the
Counter
(if I understand what you’re trying to do). Anyway, it’s like I said, if you have another question, please ask another question - not forgetting to search before, because you already have several questions aboutCounter
on site :-) (unless your doubt is another, of course, I didn’t quite understand if the problem is the count or something else).– hkotsubo
@Joãogustavo I tell you to ask another question (after researching and making sure there is no such question on the site) to make the site more organized (a question by specific doubt), and also because new questions are visible to everyone on the main page, which increases the chances of having an answer (already here in the comments only I will see)
– hkotsubo
In fact are really different problems that I put on the agenda so I brought the first discussion, but really the chances of being solved is more if I restructure them as different issues on the main page. I’ll do it, thanks there :D.
– João Gustavo
There is also the possibility of using recursive functions. The method is created within a function, where the function can perform itself
– yoyo
@Yoyo In the case of this question, I don’t think it makes sense to use recursion...
– hkotsubo