In this Python code example, all odd numbers in a list are determined and output.
Line | Description |
---|---|
1 | Declaration and initialization of the list myList with the values 12, 44, 55, 66, 77, 4, 5, 6 and 7 |
2 | Get length of myList |
4 | Run through list with for -loop |
5 | For each list element it is checked whether the remainder is not 0 when divided by 2. The modulo operator is used for this purpose. If this is the case, it is an odd number. |
6 | Outputs odd list elements as strings. |
myList = [12, 44, 55, 66, 77, 4, 5, 6, 7]
length = len(myList)
for i in range(0, length):
if myList[i] % 2 != 0:
print(str(myList[i]), end=' ')
55 77 5 7