Python Code Example: even numbers in a list

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 0 when divided by 2. The modulo operator is used for this purpose. If this is the case, it is an even 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
12 44 66 4 6