简体   繁体   中英

Need help printing uneven numbers from a list using a for loop

Using python, I need to make a list of numbers from 3-30, and print all the uneven numbers using a for loop. Thanks

You can split lists into odd or even numbers using the modulus function with 2. Even numbers ( 2%2 , 4%2 , etc.) will evaluate to 0 because there is no remainder.

numbers = [1,10,20,30,40,50]
output=[i for i in numbers if i%2 != 0 ]

Oops, now reading that you want a for loop, you can:

numbers = [1,10,20,30,40,50]
output = [] 
for i in numbers
 if i%2 != 0
  output.append(i)
l = list(range(3, 31))
for num in l:
    if num % 2 == 1:
        print(num)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM