Python Programming
Hoang-Giang Cao (高黃江)
Fall - 2024
I need to assume that the
Data Structure and Python Programming
are different
==> some content is similar
Advance Python Programming
Homework 1
Python
Please name your file as : A.py B.py C.py D.py
Announcement:
No Advanced Python class on 09 Oct (Wednesday )
Makeup class: +1 hour in 14 Oct
Exercises Basic Python
Loop
Open favourite IDE
Get ready for coding
For loop. Ex-1
Declare an array
Write a program to:
Hint: use function append() to add new item to the array
For loop. Ex-2
Expected results:
33
For loop. Ex-3
Expected results:
-16
For loop. Ex-4
Note: odd value is 2,4,6,8
Expected results:
-12
20
-16
8
2
For loop. Ex-5
Note: odd value is 1,3,5,7
Expected results:
5
-7
33
-3
15
For loop. Ex-6
Expected results:
-12
33
-3
15
For loop. Ex-6
Expected results:
-12
-3
For loop. Ex-7
Expected results:
33
15
For loop. Ex-8
Expected results:
45
For loop. Ex-9
Expected results:
4.5
For loop. Ex-10
Expected results:
83
For loop. Ex-10
Expected results:
-15
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:
For loop. Ex-12
Expected results:
15, 2, -3, 8, -16, 33, 20, -7, 5, -12
Exercises Basic Python
Nested Loop
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
*
*
**
***
*
**
***
****
*****
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
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
*****
****
***
**
*
***
**
*
*
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
*
***
*****
*******
*********
*
***
*****
*
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
*********
*******
*****
***
*
*****
***
*
*******
*****
***
*
Advanced Python Programming
Good luck to you with this course!
Any questions?