Nội dung

  1. Installing Python
  2. Why Program?
  3. Variables, expressions and statements
  4. Conditional Execution
  5. Functions
  6. Loops and Iterations
  7. Strings
  8. Files
  9. Lists
  10. Dictionaries
  11. Tuples
  12. Regular Expressions
  13. Network Programming
  14. Using Web Services
  15. Object-Oriented Programming
  16. Databases
  17. Data Visualization

Thực hành

File Handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 1) without using with statement
file = open('text.txt', 'w')
file.write('hello world !')
file.close()
 
# 2) without using with statement
file = open('text.txt', 'w')
try:
    file.write('hello world')
finally:
    file.close()

# 3) using with statement
with open('text.txt', 'w') as file:
    file.write('hello world')

# 4) write & read
with open('text.txt', 'w') as file:
    for i in range(1, 5):
        file.write(str(i))
with open('text.txt', 'r') as file:
    print(file.read())

# 5) writelines & readlines & readline
file.writelines(["Arsenal\n", "Chelsea\n", "Spurs\n"])
print(file.readlines())
print(file.readline())

# 6) binary files
with open('test.txt', 'wb') as f :
        f.write("hello world".encode('ascii'))
with open('test.txt', 'rb') as f :
        print(f.read())
with open('test.txt', 'r') as f :
        print(f.read())
with open('test.txt', 'r') as f :
        print(f.read().decode('utf-8'))

Reading CSV Files

1
2
3
4
5
6
7
8
9
10
import csv
with open('chocolate.csv') as f :
 rf = csv.reader(f, delimiter=',')
 for row in rf :
  print(row)
  print("The {} company is located in {}.".format(row[0], row[5]))

 dict_f = csv.DictReader(f, delimiter=',')
 for row in dict_f :
  print("The {} company is located in {}.".format(row['Company'], row['Company Location']))

Reading JSON Files

1
2
3
4
import json
with open('movie.json') as f :
 content = json.load(f)
 print(content)

Networking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# server.py
import socket
 
# next create a socket object
s = socket.socket()
print ("Socket successfully created")
 
# reserve a port on your computer in our case it is 40674 but it can be anything
port = 8882
 
# Next bind to the port we have not typed any ip in the ip field instead we have inputted an empty string this makes the server listen to requests coming from other computers on the network
s.bind(('', port))
print ("Socket binded to %s" %(port))
 
# put the socket into listening mode
s.listen(5)    
print ("Socket is listening")
 
# a forever loop until we interrupt it or an error occurs
while True:
    # Establish connection with client.
    c, addr = s.accept()
    print ('Got connection from', addr)
 
    # send a thank you message to the client.
    c.send(b'Thank you for connecting')
 
    # Close the connection with the client
    c.close()

###############################################
#client.py
import socket
 
# Create a socket object
s = socket.socket()
 
# Define the port on which you want to connect
port = 8882
 
# connect to the server on local computer
s.connect(('127.0.0.1', port))
 
# receive data from the server
print(s.recv(1024))
 
# close the connection
s.close()

Bài tập lấy điểm

  • Xem các bài trong các nguồn sau: Set 1, Set 2, Set 3
  • Mỗi bạn chọn ít nhất 03 bài từ các nguồn trên và đặt tên file tương ứng với các bài đã chọn theo quy tắc Nguồn-SốCủaBài.py, ví dụ 1-4.py, 2-3.py, 2-4.py,…
  • Ai chọn sau thì phải chọn không trùng với các người chọn trước, một người có thể chọn cả 3 bài trong cùng một nguồn.
  • Khi chạy chương trình phải hiểu tất cả dòng code trong bài làm của mình.
  • Điểm 03 bài sẽ cộng cho 03 đầu điểm cũ.

Tài liệu

  • Berajah Jayne, Python Programming Language, ISBN: 978-1423241881, Publisher: QuickStudy Reference Guides, 2019.
  • Dr. Charles Russell Severance, Sue Blumenberg, Elliott Hauser, Aimee Andrion, Python for Everybody: Exploring Data in Python 3, ISBN: 978-1530051120, 2016.
  • David Beazley, Brian Jones, Python Cookbook: Recipes for Mastering Python 3, 3rd ed. Edition, ISBN: 978-1449340377, O’Reilly Media, 2013.
  • John C. Shovic, Alan Simpson, Python All-in-One For Dummies, John Wiley & Sons, 2019.
  • Bill Lubanovic, Introducing Python: Modern Computing in Simple Packages, O’Reilly Media, Inc., 2014.
  • Emily Rosemary Collins, 5 Best Ways to Read or Write Binary Data in Python, Finxter, February 28, 2024
  • Datasets: chocolate.csv, movie.json