1 of 28

Python Programming

Hoang-Giang Cao (高黃江)

Fall - 2024

2 of 28

I need to assume that the

Data Structure and Python Programming

are different

==> some content is similar

3 of 28

Advance Python Programming

Homework 1

  • 11/41 students submitted. -> 30/41 NOT SUBMITTED
  • Deadline: 11:59 PM Oct 09

4 of 28

Python

  • Login
  • Upload corresponding files
  • Submit.

Please name your file as : A.py B.py C.py D.py

5 of 28

Announcement:

No Advanced Python class on 09 Oct (Wednesday )

Makeup class: +1 hour in 14 Oct

6 of 28

Exercises Basic Python

Loop

7 of 28

Open favourite IDE

Get ready for coding

8 of 28

For loop. Ex-1

Declare an array

Write a program to:

  • First, enter a number integer N, which is the number elements you want to add into the array
  • Enter N integer number from keyboard and assign to the array
  • Print out the array

Hint: use function append() to add new item to the array

9 of 28

For loop. Ex-2

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to find maximum value

Expected results:

33

10 of 28

For loop. Ex-3

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to find minimum value

Expected results:

-16

11 of 28

For loop. Ex-4

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print all odd value

Note: odd value is 2,4,6,8

Expected results:

-12

20

-16

8

2

12 of 28

For loop. Ex-5

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print all even value

Note: odd value is 1,3,5,7

Expected results:

5

-7

33

-3

15

13 of 28

For loop. Ex-6

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print all value that divisible by 3

Expected results:

-12

33

-3

15

14 of 28

For loop. Ex-6

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print all negative value that divisible by 3

Expected results:

-12

-3

15 of 28

For loop. Ex-7

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print all positive value that divisible by 3

Expected results:

33

15

16 of 28

For loop. Ex-8

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to calculate the sum of all value in the array

Expected results:

45

17 of 28

For loop. Ex-9

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to calculate the average value of the array

Expected results:

4.5

18 of 28

For loop. Ex-10

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to calculate the sum of all positive value in the array

Expected results:

83

19 of 28

For loop. Ex-10

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to calculate the sum of all negative value that is divisible by 3.

Expected results:

-15

20 of 28

For loop. Ex-11

A prime number is a number greater than 1 that is divisible by exactly two distinct numbers: 1 and itself.

For example 7, 11, 71, 97, 101

Task: Write a program that check a number is prime number or not.

If the number is prime number, print out “It is a PRIME number”. Else print “It is NOT prime number”

Hint: check if the number N is divisible by any number in range [1,N-1]

Input

Expected Output

11

It is a PRIME number

9

It is NOT prime number

97

It is a PRIME number

Test case:

21 of 28

For loop. Ex-12

  • Given the array with 10 value: [-12, 5, -7, 20, 33, -16, 8, -3, 2, 15]
    • Write a program to print the array in reverse order

Expected results:

15, 2, -3, 8, -16, 33, 20, -7, 5, -12

22 of 28

Exercises Basic Python

Nested Loop

23 of 28

Nested Loop. Ex-1

Printing the half triangle size N. Note: N is odd number: 1,3,5,7, etc

N = 5

N = 3

N = 1

*

*

**

***

*

**

***

****

*****

24 of 28

Nested Loop. Ex-2

Printing the half number triangle size N. Note: N is odd number: 1,3,5,7, etc

N = 5

N = 3

N = 1

0

01

012

0123

01234

0

01

012

0

25 of 28

Nested Loop. Ex-3

Printing the half reversed triangle size N. Note: N is odd number: 1,3,5,7, etc

N = 5

N = 3

N = 1

*****

****

***

**

*

***

**

*

*

26 of 28

Nested Loop. Ex-4

Printing the triangle size N. Note: N is odd number: 1,3,5,7, etc

N = 5

N = 3

N = 1

*

***

*****

*******

*********

*

***

*****

*

27 of 28

Nested Loop. Ex-5

Printing the reversed triangle size N. Note: N is odd number: 1,3,5,7, etc

N = 7

N = 5

N = 1

*********

*******

*****

***

*

*****

***

*

*******

*****

***

*

28 of 28

Advanced Python Programming

Good luck to you with this course!

Any questions?