Python Code Example: Odd Numbers in a List

In this Python code example, all odd numbers in a list are determined and output.

Code Explanation

LineDescription
1Declaration and initialization of the list myList with the values 12, 44, 55, 66, 77, 4, 5, 6 and 7
2Get length of myList
4Run through list with for-loop
5For 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.
6Outputs 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=' ')
Output
55 77 5 7