Showing only pairs in the list - Python

Asked

Viewed 991 times

4

My code needs show an int number greater than 2 and need to print the sequence of even numbers smaller than itself and need to start from scratch.

If you are less than 2 have to print Invalid number

Example:

Entrada = [20]
Saída = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

My code (not running):

seqpair = input("Type a number pair: ")

seqpair = list(map(int, seqpair.split()))

if seqpair > 2:
    print (seqpair)
else:
    print ('Invalid number')

3 answers

4


I did it that way:

number = int(input('Digite um número: '))

if(number >= 2):
  # O range(0, numer + 1, 2) cria um objeto do tipo range.
  # Iniciando com 0 indo até o number + 1, pulando de 2 em 2.
  # Como 0 é par e está com saltos de 2 os numeros sempre serão par.

  # O list(range(0, number + 1, 2)) converte o objeto range gerado para um objeto list.
  list_numbers = list(range(0, number + 1, 2))

  print(list_numbers)  
else:  
  print('Invalid number')
  • list = list(range(0, number + 1, 2)) explain this part of the code?

  • @Alexfeliciano The range(0, number + 1, 2) means that it will create a range object starting from 0 until number + 1 jumps from 2 to 2, then the range is converted into a list and assigned to its list variable

  • Good solution, but ai tb has an iteration of type list comprehension, with the additional cost of the if

  • @Alexfeliciano commented the code to facilitate the explanation

  • @Sidon how so if? additional cost if I have not used any if

  • if number>2 additional line I meant. Actually an additional block

  • @Sidon thus, ta specified in Alex’s question that if the number is less than 2 should print Invalid number

  • It’s true @Viniciusfernandes I ate ball, Sorry! :-)

Show 3 more comments

4

As a curiosity, see another way to solve this problem using filter and lambda

lista = list(range(1,1+int(input('Digite o numero: '))))
# Suponha que a pessoa digitou 20.
print(lista)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

pares = list(filter(lambda x: x%2==0, lista))
print(pares)
#[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
  • 1

    What is the purpose of comprehensilist on x => x? It would not be enough to do lista = range(...)?

  • Thanks for the function tip filter()

  • You’re right Anderson, I edited the answer. For nothing Alex

3

Thus:

Using list comprehension:

number = int(input('digite o numero: '))
lst= [n for n in range(number+1) if n%2==0]
print('','list comprehension:',lst, sep='\n')

Using the loop for:

# usando for
lst = []
for n in range(number+1):
    if n%2==0:
        lst.append(n)

print('','Usando for:',lst, sep='\n')

Entree: 30

Exit:

Usando list comprehension:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

Usando for:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

Run the code in the repl.it.

  • Could show code without using loop?

  • The first option does not use loop (list comprehension), uses only one line, by the way, even uses, but not explicitly.

  • I think not @Sidon, see that there is a for no (list comprehension)

  • True, but I’m not sure if in that case the is can be considered a loop, maybe an iteration. :-)

  • had never seen comprehensilist on and the college teacher that I do also does not know because he is seeing Python now this semester together with the students rsrsrs.

  • List comprehension at first glance seems complicated, but after you use it sometimes you will want to always use, and the worst you will ever want to use lambda. :-)

  • @Andersoncarloswoss true, I remember I was extrapolating in the tests and I found the two solutions: n<=number or number+1 but I ended up implementing them both. Fail! [Edited]

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.