21. Comprehensions trong Python
List Comprehensions
Là một cú pháp cho phép lập trình viên nhanh chóng tạo ra một biến dữ liệu list mới từ một list cũ hoặc vòng lặp dạng in-line, kết hợp với các điều kiện cho trước.
a = [0, 1, 2, 3]
# b will store values which are 1 greater than the values stored in a
b = [i + 1 for i in a]
print(b)
Output:
[1, 2, 3, 4]
Set Comprehension
Nó là một cú pháp ngắn hơn để tạo một tập hợp mới bằng cách sử dụng các giá trị của một tập hợp hiện có.
a = {0, 1, 2, 3}
# b will store squares of the elements of a
b = {i ** 2 for i in a}
print(b)
Output:
{0, 1, 4, 9}
Dict Comprehension
Đây là một cú pháp ngắn hơn để tạo một dictionary mới bằng cách sử dụng các giá trị của một dictionary hiện có.
a = {'Hello':'World', 'First': 1}
# b stores elements of a in value-key pair format
b = {val: k for k , val in a.items()}
print(b)
Output:
{'World': 'Hello', 1: 'First'}
22. Thao tác chuỗi (String) trong Python
Escape Sequences
Escape Sequences là các chuỗi ký tự bắt đầu bởi dấu gạch chéo ngược như \n hay \t , nhằm biểu diễn các ký tự vốn không thể biểu diễn theo cách thông thường trong Python.
Ví dụ:
Ký tự đặc biệt (Escape Sequence) | Ý nghĩa | |
\t | Ký tự Tab | |
\n | Ký tự xuống dòng | |
\\ | Dấu gạch chéo ngược | |
\’ | Dấu nháy đơn |
Hàm số (Function) | Ý nghĩa | |
isspace() | Trả về true nếu chuỗi chỉ chứa các ký tự khoảng trắng whitespace | |
isalnum() | Kiểm tra xem tất cả ký tự trong chuỗi có phải là ký tự chữ hoặc số (Alphanumeric) hay không | |
isalpha() | Trả về true nếu chuỗi có ít nhất 1 ký tự và tất cả ký tự là chữ cái | |
isTitle() | Trả về True nếu chuỗi bắt đầu bằng chữ hoa và sau đó phần còn lại của các ký tự là chữ thường |
Ký hiệu (Symbol) | Matches | |
+ | One or More of the preceding group | |
* | Zero or More of preceding group | |
? | Zero or One of preceding group | |
^name | String must begin with the name | |
name$ | String must end with the name | |
. | Any character except \n | |
{n} | Exactly n of preceding group | |
{n, } | >= n of preceding group | |
{,n} | [0, m] of preceding group | |
{n, m} | [n, m] of preceding group | |
*? | Non Greedy matching of the preceding group | |
[abc] | Any character enclosed in the brackets | |
[^abc] | Any character not enclosed in the brackets | |
\d, \w, \s | Digit, word, or space respectively | |
\D, \W, \S | Anything except digit, word, or space respectively |
Level | Function | What it does |
DEBUG | logging.debug() | Used for tracking any events that occur during program execution |
INFO | logging.info() | Confirms the working of things at the end of the module in the program |
WARNING | logging.warning() | Used to flag some issues that might hinder the program from working in the future, but allows it to work for now |
ERROR | logging.error() | Records errors that might have made the program fail at some point |
CRITICAL | logging.critical() | Indicates or flags fatal errors in the program |

Pum
Life is short. Smile while you still have teeth :)
Bài viết liên quan
Data Analyst là gì? Trở thành Data Analyst cần chuẩn bị gì?
Sep 20, 2023 • 25 min read
SQL Server là gì? Hướng dẫn tải & cài đặt Microsoft SQL Server
Sep 16, 2023 • 10 min read
SQL là gì? Ưu & nhược điểm của Structured Query Language
Sep 16, 2023 • 15 min read
MySQL là gì? Hướng dẫn cài đặt và sử dụng MYSQL
Sep 16, 2023 • 13 min read
Review khóa học Data Analyst Bootcamp của 200Lab: Học viên nói gì?
Sep 16, 2023 • 12 min read
28 loại biểu đồ thông dụng trong Trực Quan Hóa Dữ Liệu
Sep 16, 2023 • 17 min read