Introduction to Python

Form 4 - Computer

2023-2024

Floor 4 - Computer Room

Mr. Peter

Outline

Outline

While Loop

1

List Structure

2

Exercises

3

While Loop Statement

1.

2.

Tab

while Condition :

Command1

While loop statement - Ex15_1

Result all numbers that can be multiplied by 3

Write a Python program that iterate each element in a provided List and check if it can be multiplied by 3, then append it into a new list called results

elements = [ 3, 2, 3, 9, 10, 5, 1, 2, 2, 10, 3, 12, 21 ]

Code Provided

Expected outcome after your program executed

[ 3, 3, 9, 3, 12, 21 ]

Print a list in the console

Save the file as "XX_YYYY_Ex15_1.py"

Code Hints

1. elements.append( element )

While loop statement - Ex15_2

Remove all duplicate elements from the list

Write a Python program that iterate each element in a provided List and remove all the duplicate element from the provided list.

elements = [ 3, 2, 3, 5, 10, 5, 1, 2, 2, 10, 3, 1, 5 ]

Code Provided

Expected outcome after your program executed

[ 3, 2, 5, 10, 1 ]

Print a list in the console

Save the file as "XX_YYYY_Ex15_2.py"

Code Hints

2. element in elements

1. elements.append( element )

While loop statement - Ex15_3

Ralindrome Validation

Given a list elements, write a Python code snippet to check if the list is a palindrome. A list is considered a palindrome if it reads the same backward as forward. Your code should output `True` if the list is a palindrome, and `False` otherwise.

elements = [4, 1, 3, 7, 2, 6, 2, 7, 3, 1, 4]

Code Provided

Examples

1. For elements = [1, 2, 3, 2, 1], the output should be `True`.
2. For elements = ['a', 'b', 'b', 'a'], the output should be `True`.
3. For elements = [1, 2, 3, 4, 5], the output should be `False`.
4. For elements = [], the output should be `True`.
5. For elements = [42], the output should be `True`.

Save the file as "XX_YYYY_Ex15_3.py"

Python - List02

By Mr Peter

Python - List02

  • 93