COURSE LESSONS:
PYTHON PROGRAMMING
Duration: 60 periods
Lecturer: MSc. Luu Thi Thao
Phone number: 0978131419
Email: luuthao23@[Link]
Slide:
NOTE
DO NOT USE YOUR
NO PRIVATE TALK PHONE FOR NO SLEEPING FULL RECORDING
PERSONAL WORK
On time No phone No sleeping Writting
THAO THI LUU Slide: 2
TABLE OF CONTENTS
DETAILED COURSE CONTENT
Chapter 1: Introduction to Python programming language
Chapter 2: Control structures and functions
Chapter 3: Advanced data types in python
Chapter 4: String - file and exception handling in python
Chapter 5: Object Oriented Programming in Python
Chapter 6: Real Project
THAO THI LUU Slide: 3
DOCUMENTS
BOOKS AND INTERNET
[1] Fundamentals of Programming Python, Richard L. Halterman. Southern
Adventist University 2019
[2] Tài liệu giảng dạy do Khoa biên soạn bằng Tiếng Việt bổ trợ đi kèm
[3] Nguyễn Ngọc Giang, Phan Xuân Vọng, Nguyễn Quốc Anh, Đường vào
lập trình Python, NXB Đại học Quốc Gia, 2020.
[4] Wes McKinney, Python for Data Analysis, 3rd Edition, O’Reily Media,
Inc, 2022.
THAO THI LUU Slide: 4
Chapter 2: Control structures
and functions
THAO THI LUU Slide: 5
2.2. Looping structure
In Python, statements are
executed sequentially from top
to bottom. However, in some
cases, the programmer wants to
execute a block of statements
multiple times, in which case, a
loop can be used.
THAO THI LUU Slide: 6
2.2. Looping structure
Python provides two types of loops:
for loop
while loop
Statements that can be used inside loops:
continue
break
pass
Statements associated with loops:
else
THAO THI LUU Slide: 7
2.2. Looping structure
Command Describe
break This is the statement that stops the loop.
This is the statement to skip the remaining statements
continue in the block, and immediately recheck the condition
before continuing to execute the block again.
The pass statement in a loop is simply a marker,
pass reminding the programmer to add some code in the
future. It is a null statement (Does nothing).
THAO THI LUU Slide: 8
Break Continue
start loop
start loop
Loop test
Loop test
condition
condition
Break? Continue?
yes exit yes
loop
no no
continue command continue command
in loop in loop
THAO THI LUU Slide: 9
For loop with range() function
The simplest for loop in Python is to use 'for' with 'range‘.
If the variable '_i' has a value in the range (1, 6) it means that x can take
on the values 1, 2, 3, 4, 5. The syntax would be as follows:
for _i in range (1, 6) :
THAO THI LUU Slide: 10
For loop with range() function
Example 1: Print consecutive natural numbers from 1 to 9.
Algorithm: Use a loop with natural number _i running from 1 to 10.
In each loop, print the value of _i.
Sample code:
for _i in range (1,10):
print (_i)
THAO THI LUU Slide: 11
For loop with range() function
Example 2: Print the squares of consecutive natural numbers from 1
to 5.
Algorithm: Use a loop with natural number _i running from 1 to 5. In
each loop, print the value of _i*_i (ie _i squared).
Sample code:
for _i in range (1,6): for _i in range(1, 6):
print (_i*_i) _a = _i * _i
print(_a)
THAO THI LUU Slide: 12
For loop with range() function
The general syntax of range(): range([start], stop, [step])
Where:
start: The starting value. The default value of start is 0. The start value is
arbitrary and if not used in the function, its default value will be 0.
stop: The stop value. The last element in the loop is always less than stop.
step: The distance between two adjacent elements in the iterated list. The
step value is arbitrary and if not used in the function, its default value will
be 1.
THAO THI LUU Slide: 13
For loop with range() function
#Print numbers from 0 to 9
for _i in range(10):
print(_i)
#Print numbers from 0 to 9, each number is 2 units apart
for _i in range(1,10,2):
print(_i)
THAO THI LUU Slide: 14
Nesting for loop statements
Other loop commands can be nested within loop commands.
for _i in range(1, 6):
for _j in range (3,5):
_a = _i * _j
print(_a)
THAO THI LUU Slide: 15
Nesting for loop statements
It is possible to nest loops within control statements
(if/elif/else) or vice versa.
--- Code 2: For trong if ---
--- Code 1: If trong for ---
_n = int(input(Enter a natural
for _i in range(1, 6):
number: '))
if _i > 3:
if _n <= 5:
_a = _i * _i
for _i in range (1,_n):
print('Square of %r:
print(_i)
%r' % (_i, _a))
else:
else:
print(The number _n is
_a = _i
greater than 5.')
print(_a)
THAO THI LUU Slide: 16
Practice for
Exercise 1: Enter an integer n from the keyboard. Print the product of 2
times the values less than n. For example: Enter n = 4 then print 2 = 2*1, 4
=2*2, 6 = 2*3.
Exercise 2: Enter an integer n from the [Link] the number is greater
than 10 then print the text: The number entered must be less than 10. If the
number is less than or equal to 10: Print the even numbers in the range
from 1 to n.
Exercise 3: Print the numbers in the range from 80 - 100 that satisfy the
condition of being divisible by both 2 and 3.
Exercise 4: Enter an integer n < 20 from the keyboard. Print the numbers
that satisfy the condition of being divisible by 5 or divisible by 7.
THAO THI LUU Slide: 17
Reference code for exercise 1
Analysis:
With n = 4, we need to print:
2=2*1
4=2*2
6=2*3
That is, with i running from 1 to n-1, we print 2*i.
LƯU THỊ THẢO Slide: 18
Reference code for exercise 2
Problem Description Recap:
Ask the user to enter an integer n.
If n > 10: print "The number entered must be less than 10."
If n <= 10: print all even numbers from 1 to n.
INPUT: n = 12; n = 8
LƯU THỊ THẢO Slide: 19
2.3. Loop with while
while condition:
while block
THAO THI LUU Slide: 20
2.3. Loop with while
# Example: Print positive integers less than 9
count = 0
n=0
while (count < 9):
print ('number', n,' is:', count)
n=n+1
count = count + 1
print ("Finish!")
THAO THI LUU Slide: 21
2.3. Loop with while
# Example: Enter the integer n from the keyboard. Calculate the sum of the numbers from 1 to n
n = int(input("Enter number n: ")) # Enter any number n
sum = 0 # declare and assign values to total
_i = 1 # declare and assign a value to the counter variable i
while _i <= n:
sum = sum + _i
_i = _i + 1 # update counter variable
print("SUM: ", sum)
THAO THI LUU Slide: 22
Infinite while loop
Taking the above example again, just remove the line i=i+1
and the loop will become infinite.
n = int(input("Enter any number n: ")) # Enter any number n
sum = 0 # declare and assign values to sum
_i = 1 # declare and assign a value to the counter variable i
while _i <= n:
sum = sum + _i
print("SUM: ", sum)
THAO THI LUU Slide: 23
While combined with else
Combining while and else:
_ count = 0
while _ count < 3:
print(" Currently in while loop ", _ count)
_ count = _ count + 1
else:
print(" Currently in else ")
THAO THI LUU Slide: 24
Practice while
Exercise 5: Calculate and print the product of the first 10 natural numbers.
Exercise 6: Enter a positive integer n from the keyboard. Calculate and print
n! (factorial of n).
Exercise 7: Enter a positive integer n from the keyboard. Check whether n is
a prime number or not. If it is a prime number, print the text: This is a prime
number. Otherwise, print: Not a prime number.
Exercise 8: Enter an integer n from the keyboard. Print the sum of numbers
that satisfy two conditions: less than n and are even.
THAO THI LUU Slide: 25
THAO THI LUU Slide: 26