Python List - Ex01

Write a program to count how many times "2" element appears in a given list using a while loop.

1.

Use a while loop to iterate through the numbers.

1

Increase count variable if digit is equal to "2"

2

digits = "1263451234123426346435"

(  The variable "i" increases from 0 to len(digits) )

Save your file as "ClassNumber_YourName_Ex01.py"

Print the accumulated result

3

4

Expected output:

Python List - Ex02

Collect Five Positive Numbers

1.

numbers = [12, -7, 34, -19, 8, -3, 25, -42, 16, -11, 7, -28, 45, -9, 5, -14, 20, -6, 33, -21]

Save your file as "ClassNumber_YourName_Ex02.py"

Use a while loop to collect exactly five positive numbers from the numbers variable. Append each valid number to a list. If the number is a negative number, ignore it. Print the list at the end.

2

3

Use a while loop to iterate through the numbers.

1

(  The variable "i" increases from 0 to len(numbers) )

Append only the positive numbers (greater than 0) to a new list if the number of items in list is less than 5 items.

Print the final list containing the first positive numbers.

Python List - Ex03

Separate Even and Odd

1.

numbers = [12, -7, 34, -19, 8, -3, 25, -42, 16, -11, 7, -28, 45, -9, 5, -14, 20, -6, 33, -21]

Loop through the numbers variable. Append even numbers to a list called evens and odd numbers to a list called odds. Print both lists.

If an element is even, append it to the evens list. If it is odd, append it to the odds list.

2

Finally, print both the evens and odds lists.

3

Save your file as "ClassNumber_YourName_Ex03.py"

Use a while loop to iterate through the numbers.

1

(  The variable "i" increases from 0 to len(numbers) )

F4 Lesson - Test Revision

By Mr Peter

F4 Lesson - Test Revision

  • 232