Tính tổng các số nguyên tố trong python Bài tập chuyên gia

Tôi cho rằng bạn đã quen thuộc với một số ngôn ngữ lập trình như C/C++/Java. Bài viết này KHÔNG nhằm mục đích giới thiệu về lập trình

Cá nhân tôi khuyên bạn nên học ngôn ngữ lập trình đa năng truyền thống (chẳng hạn như C/C++/Java) trước khi học ngôn ngữ kịch bản như Python/JavaScript/Perl/PHP vì chúng ít cấu trúc hơn các ngôn ngữ truyền thống với nhiều tính năng ưa thích

Python bằng ví dụ

Phần này dành cho các lập trình viên có kinh nghiệm xem xét các cú pháp của Python và những người cần làm mới bộ nhớ của họ. Đối với người mới, hãy chuyển sang phần tiếp theo

Tóm tắt và so sánh cú pháp

  • Nhận xét. Nhận xét của Python bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 và kéo dài cho đến cuối dòng. Python không hỗ trợ bình luận nhiều dòng.
    (Nhận xét cuối dòng C/C++/C#/Java bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. Họ hỗ trợ nhận xét nhiều dòng thông qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. )
  • Chuỗi. Chuỗi của Python có thể được phân định bằng dấu nháy đơn (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7) hoặc dấu nháy kép (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8). Python cũng hỗ trợ chuỗi nhiều dòng, được phân tách bằng dấu nháy đơn ba lần (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    9) hoặc dấu nháy kép ba lần (
    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
    49
    50
    51
    52
    0). Các chuỗi là bất biến trong Python.
    (C/C++/C#/Java sử dụng dấu nháy kép cho chuỗi và dấu nháy đơn cho kí tự. Họ không hỗ trợ chuỗi nhiều dòng. )
  • Khai báo kiểu biến. Giống như hầu hết các ngôn ngữ diễn giải tập lệnh (chẳng hạn như JavaScript/Perl), Python được nhập động. Bạn KHÔNG cần khai báo biến (tên và kiểu) trước khi sử dụng chúng. Một biến được tạo thông qua phép gán ban đầu. Python liên kết các loại với các đối tượng, không phải các biến, tôi. e. , một biến có thể chứa bất kỳ loại đối tượng nào.
    (Trong C/C++/C#/Java, bạn cần khai báo tên và kiểu biến trước khi sử dụng. )
  • Loại dữ liệu. Python hỗ trợ các kiểu dữ liệu này.
    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
    49
    50
    51
    52
    1 (số nguyên),
    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
    49
    50
    51
    52
    2 (số dấu phẩy động),
    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
    49
    50
    51
    52
    3 (Chuỗi),
    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
    49
    50
    51
    52
    4 (boolean của
    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
    49
    50
    51
    52
    5 hoặc
    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
    49
    50
    51
    52
    6), v.v.
  • Câu lệnh. Câu lệnh của Python kết thúc bằng một dòng mới.
    (Câu lệnh C/C++/C#/Java kết thúc bằng dấu chấm phẩy (________ 67))
  • Câu lệnh ghép và thụt đầu dòng. Python sử dụng thụt đầu dòng để biểu thị khối cơ thể. (C/C++/C#/Java sử dụng dấu ngoặc nhọn
    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
    49
    50
    51
    52
    8. ) Cú pháp này buộc bạn phải thụt lề chương trình một cách chính xác, điều này rất quan trọng để đọc chương trình của bạn. Bạn có thể sử dụng dấu cách hoặc tab để thụt đầu dòng (nhưng không được kết hợp cả hai). Mỗi cấp độ cơ thể phải được thụt vào ở cùng một khoảng cách. Nên sử dụng 4 dấu cách cho mỗi mức thụt đầu dòng
  • Toán tử chuyển nhượng.
    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
    49
    50
    51
    52
    9
  • toán tử số học.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 (cộng),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61 (trừ),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (nhân),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63 (chia), ________ 164 (chia số nguyên), ________ 165 (số mũ), ________ 166 (mô đun). (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68 không được hỗ trợ)
  • Toán tử gán hợp chất.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65
  • Toán tử so sánh.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65
  • Toán tử logic.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68. (C/C++/C#/Java sử dụng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601)
  • có điều kiện
  • Vòng. Python KHÔNG hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 giống C truyền thống với chỉ mục.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    603
  • Danh sách. Python hỗ trợ mảng động có kích thước thay đổi thông qua cấu trúc dữ liệu tích hợp có tên là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604, được ký hiệu là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606. Danh sách tương tự như mảng của C/C++/C#/Java nhưng KHÔNG có kích thước cố định. Bạn có thể tham khảo một phần tử qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    608 hoặc danh sách phụ qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    609. Bạn có thể sử dụng các hàm có sẵn như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    610,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    611,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    612
  • Cấu trúc dữ liệu
    • Danh sách.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      613 (mảng động có thể thay đổi)
    • Tuple.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      614 (Mảng có kích thước cố định không thay đổi)
    • Từ điển.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      615 (cặp khóa-giá trị có thể thay đổi, mảng kết hợp, bản đồ)
    • Bộ.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      616 (với khóa duy nhất và có thể thay đổi)
  • Các toán tử và hàm của chuỗi (Chuỗi, Tuple, Danh sách).
    • ______362,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      63. kiểm tra tư cách thành viên.
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      60. nối
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      62. lặp đi lặp lại
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      621,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      622. lập chỉ mục
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      623. cắt lát
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      624,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      625,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      626
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      627,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      628
    Chỉ dành cho các chuỗi có thể thay đổi (danh sách)
    • Chuyển nhượng qua
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      621,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      630 (lập chỉ mục) và
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      623 (cắt lát)
    • Chuyển nhượng qua
      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
      49
      50
      51
      52
      9,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      69 (ghép từ ghép),
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      61 (lặp từ ghép)
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      635. xóa bỏ
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      636,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      637,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      638
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      639,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      640,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      641,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      642,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      643,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      644
  • Định nghĩa hàm

Ví dụ grade_statistic. py - Cấu trúc và cú pháp cơ bản

Ví dụ này lặp đi lặp lại nhắc người dùng cho điểm (từ 0 đến 100 với xác thực đầu vào). Sau đó, nó tính tổng, trung bình, tối thiểu và in biểu đồ ngang

Ví dụ này minh họa các cấu trúc và cú pháp cơ bản của Python, chẳng hạn như nhận xét, câu lệnh, thụt lề khối, điều kiện

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
602, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647, đầu vào/đầu ra, chuỗi,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 và hàm

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
49
50
51
52
5

Để chạy tập lệnh Python

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6

Sản lượng dự kiến ​​là

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
49
50
51
52
7
Làm thế nào nó hoạt động
  1. #. /usr/bin/env python3 (Dòng 1) chỉ áp dụng cho môi trường Unix. Nó được gọi là Hash-Bang (hoặc She-Bang) để chỉ định vị trí của Trình thông dịch Python, để tập lệnh có thể được thực thi trực tiếp dưới dạng một chương trình độc lập
  2. # -*- mã hóa. UTF-8 -*- (Dòng 2, tùy chọn) chỉ định lược đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  3. Doc-String. Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu (chuỗi tài liệu) (Dòng 3-12) để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu là một chuỗi nhiều dòng (được phân định bằng dấu nháy đơn ba hoặc dấu ba kép), có thể được trích xuất từ ​​​​tệp nguồn để tạo tài liệu
  4. def my_sum(lst). (Dòng 15-20). Chúng tôi định nghĩa một hàm có tên là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    649 nhận vào một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 và trả về tổng của các mục. Nó sử dụng vòng lặp for-each-in để lặp qua tất cả các mục của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 đã cho. Vì Python là diễn giải, trước tiên bạn cần xác định hàm trước khi sử dụng nó. Ta chọn tên hàm là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    652 để phân biệt với hàm có sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    653
  5. thùng = [0]*10 (Dòng 38). Python hỗ trợ toán tử lặp lại (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62). Câu lệnh này tạo ra một danh sách mười số không. Tương tự, toán tử lặp (*) có thể áp dụng cho chuỗi (Dòng 59)
  6. cho hàng trong phạm vi (len (thùng)). (Dòng 48, 56). Python chỉ hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655. Nó KHÔNG hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 giống C truyền thống với chỉ mục. Do đó, chúng ta cần sử dụng hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    657 tích hợp để tạo một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 gồm các chỉ mục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    659, sau đó áp dụng vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655 trên chỉ mục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
  7. 0 <= lớp <= 100 (Dòng 68). Python hỗ trợ cú pháp này để so sánh
  8. Có một số cách in
    1. chức năng tích hợp print() (Dòng 75-80). Theo mặc định,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      662 in một dòng mới ở cuối. Bạn cần bao gồm đối số
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      663 để chặn dòng mới
    2. in (str. định dạng()) (Dòng 51, 53). Kiểu mới của Python 3 cho chuỗi được định dạng thông qua hàm thành viên lớp
      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
      49
      50
      51
      52
      3
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      665. Chuỗi mà phương thức này được gọi có thể chứa văn bản bằng chữ hoặc các trường thay thế được phân tách bằng dấu ngoặc nhọn
      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
      49
      50
      51
      52
      8. Mỗi trường thay thế chứa chỉ mục số của đối số vị trí hoặc tên của đối số từ khóa, với các chỉ định định dạng giống C bắt đầu bằng
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      667 (thay vì
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      66 trong C), chẳng hạn như
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      669 cho số nguyên,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      670 cho số dấu phẩy động,
    3. print('chuỗi định dạng' % args) (Dòng 81). Kiểu cũ của Python 2 cho chuỗi được định dạng bằng toán tử
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      66.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      676 có thể chứa các bộ xác định định dạng giống như C, chẳng hạn như
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      677 cho số nguyên,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      678 cho số dấu phẩy động,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      679 cho chuỗi. Dòng này được bao gồm trong trường hợp bạn cần đọc các chương trình cũ. Tôi khuyên bạn nên sử dụng kiểu định dạng mới của Python 3
  9. lớp = int(đầu vào('Nhập. ')) (Dòng 66, 72). Bạn có thể đọc đầu vào từ thiết bị đầu vào tiêu chuẩn (mặc định là bàn phím) thông qua chức năng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680 tích hợp. Vì hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680 trả về một chuỗi, nên chúng ta cần chuyển nó sang
    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
    49
    50
    51
    52
    1
  10. nếu __name__ == '__main__'. (Dòng 87). Khi bạn thực thi một mô-đun Python thông qua Trình thông dịch Python, biến toàn cục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 được đặt thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    684. Mặt khác, khi một mô-đun được nhập vào một mô-đun khác,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 của nó được đặt thành tên mô-đun. Do đó, mô-đun trên sẽ được thực thi nếu nó được tải bởi trình thông dịch Python, nhưng không được nhập bởi mô-đun khác. Đây là một thực hành tốt để thử nghiệm một mô-đun

Ví dụ số_đoán. py - Đoán một số

Đây là một trò chơi đoán số. Nó minh họa vòng lặp lồng nhau nếu (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
686), vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647 với cờ
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
49
50
51
52
4 và mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
689. Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
39
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
40
Làm thế nào nó hoạt động
  1. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    690 (Dòng 12). Chúng ta sẽ sử dụng hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    692 của mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    689 để tạo một số bí mật. Trong Python, bạn cần
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 mô-đun (thư viện bên ngoài) trước khi sử dụng nó
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    694 (Dòng 15). Tạo một số nguyên ngẫu nhiên giữa
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    696 (bao gồm cả hai)
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    697 (Dòng 17). Python hỗ trợ loại
    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
    49
    50
    51
    52
    4 cho các giá trị boolean của
    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
    49
    50
    51
    52
    5 hoặc
    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
    49
    50
    51
    52
    6. Chúng tôi sử dụng cờ boolean này để kiểm soát vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647 của chúng tôi
  4. Cú pháp cho vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647 (Dòng 19) là
  5. Cú pháp cho lồng nhau-
    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
    49
    50
    51
    52
    703 (Dòng 22) là

Ví dụ magic_number. py - Kiểm tra xem số có chứa chữ số ma thuật không

Ví dụ này nhắc người dùng nhập một số và kiểm tra xem số đó có chứa chữ số ma thuật không. Ví dụ này minh họa chức năng, hoạt động của

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
49
50
51
52
1 và
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
49
50
51
52
3. Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
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
49
50
51
52
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức chương trình thành các chức năng
  2. Chúng tôi triển khai hai phiên bản hàm để kiểm tra số ma thuật - phiên bản
    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
    49
    50
    51
    52
    1 (Dòng 10) và phiên bản
    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
    49
    50
    51
    52
    3 (Dòng 25) - cho mục đích học thuật
  3. def isMagic(số. int, ma thuậtDigit. int = 8) -> bool. (Dòng 10). Các phần nổi bật được gọi là chú thích gợi ý loại. Chúng bị Trình thông dịch Python bỏ qua và chỉ đóng vai trò là tài liệu
  4. nếu __name__ == '__main__'. (Dòng 51). Khi bạn thực thi một mô-đun Python thông qua Trình thông dịch Python, biến toàn cục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 được đặt thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    684. Mặt khác, khi một mô-đun được nhập vào một mô-đun khác,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 của nó được đặt thành tên mô-đun. Do đó, mô-đun trên sẽ được thực thi nếu nó được tải bởi trình thông dịch Python, nhưng không được nhập bởi mô-đun khác. Đây là một thực hành tốt để thử nghiệm một mô-đun

Ví dụ hex2dec. py - Chuyển đổi thập lục phân sang thập phân

Ví dụ này nhắc người dùng nhập chuỗi thập lục phân (hex) và in số thập phân tương đương của nó. Nó minh họa vòng lặp for với chỉ mục, lệnh lồng nhau, phép toán chuỗi và từ điển (mảng kết hợp). Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6
Làm thế nào nó hoạt động
  1. Công thức chuyển đổi là.
    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
    49
    50
    51
    52
    711, trong đó
    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
    49
    50
    51
    52
    712
  2. 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
    49
    50
    51
    52
    713 (Dòng 12). Chúng tôi sẽ sử dụng chức năng
    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
    49
    50
    51
    52
    715 của mô-đun
    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
    49
    50
    51
    52
    714 để chấm dứt chương trình cho đầu vào không hợp lệ. Trong Python, chúng ta cần import module (thư viện bên ngoài) trước khi sử dụng
  3. 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
    49
    50
    51
    52
    716 (Dòng 21). Python không hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 giống C truyền thống với chỉ mục. Nó chỉ hỗ trợ vòng lặp
    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
    49
    50
    51
    52
    718 để lặp qua từng
    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
    49
    50
    51
    52
    719 trong
    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
    49
    50
    51
    52
    720. Chúng tôi sử dụng hàm tích hợp sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    657 để tạo danh sách
    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
    49
    50
    51
    52
    722, sau đó lặp qua từng mục trong danh sách đã tạo
  4. Trong Python, chúng ta có thể lặp qua từng ký tự của một chuỗi thông qua vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655, e. g. , Đối với ví dụ này, chúng ta không thể sử dụng ở trên vì chúng ta cần chỉ mục của ký tự để thực hiện chuyển đổi
  5. 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
    49
    50
    51
    52
    724 (Dòng 22). Trong Python, bạn có thể sử dụng toán tử lập chỉ mục
    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
    49
    50
    51
    52
    725 để trích xuất ký tự thứ
    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
    49
    50
    51
    52
    726. Lưu ý rằng Python không hỗ trợ ký tự, nhưng coi ký tự là chuỗi 1 ký tự
  6. 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
    49
    50
    51
    52
    727 (Dòng 23). Python hỗ trợ toán tử lũy thừa (hoặc lũy thừa) ở dạng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65. Lưu ý rằng chỉ mục chuỗi bắt đầu từ 0 và tăng dần từ trái sang phải. Mặt khác, số mũ của chữ số hex bắt đầu từ 0, nhưng tăng dần từ phải sang trái
  7. Có ________ 9729 trường hợp chuỗi 1 ký tự cho ________ 9730, ________ 9731, _______ 9732,
    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
    49
    50
    51
    52
    733 và các trường hợp khác, có thể được xử lý bằng 5 trường hợp lồng nhau như sau
    1. 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
      49
      50
      51
      52
      734 (Dòng 24). chúng tôi chuyển đổi chuỗi
      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
      49
      50
      51
      52
      734 thành
      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
      49
      50
      51
      52
      1
      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
      49
      50
      51
      52
      737 thông qua chức năng tích hợp sẵn
      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
      49
      50
      51
      52
      738
    2. 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
      49
      50
      51
      52
      739 (Dòng 26). không có gì. Trong Python, bạn cần đưa một câu lệnh giả có tên là
      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
      49
      50
      51
      52
      740 (Dòng 28) vào khối nội dung
    3. 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
      49
      50
      51
      52
      732 (Dòng 28). Để chuyển đổi chuỗi 1 ký tự
      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
      49
      50
      51
      52
      732 thành
      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
      49
      50
      51
      52
      1
      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
      49
      50
      51
      52
      744, chúng ta sử dụng hàm có sẵn
      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
      49
      50
      51
      52
      745 để lấy mã Unicode
      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
      49
      50
      51
      52
      1 của
      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
      49
      50
      51
      52
      747, trừ cơ số
      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
      49
      50
      51
      52
      748 và cộng 10
    4. 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
      49
      50
      51
      52
      749 (Dòng 30). Python hỗ trợ cấu trúc dữ liệu được gọi là từ điển (mảng kết hợp), chứa các cặp khóa-giá trị. Chúng tôi đã tạo một từ điển
      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
      49
      50
      51
      52
      750 (Dòng 15) để ánh xạ
      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
      49
      50
      51
      52
      751 đến
      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
      49
      50
      51
      52
      752,
      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
      49
      50
      51
      52
      753 đến
      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
      49
      50
      51
      52
      754, v.v. Sau đó, chúng tôi có thể tham khảo từ điển qua
      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
      49
      50
      51
      52
      755 để lấy giá trị của nó (Dòng 31)
    5. khác (Dòng 32). chúng tôi sử dụng
      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
      49
      50
      51
      52
      756 để chấm dứt chương trình. Chúng tôi trả về một mã khác không để biểu thị việc chấm dứt bất thường

Ví dụ bin2dec. py - Chuyển đổi nhị phân sang thập phân

Ví dụ này nhắc người dùng về một chuỗi nhị phân (có xác thực đầu vào) và in số thập phân tương đương của nó. Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức mã trong các chức năng
  2. Công thức chuyển đổi là.
    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
    49
    50
    51
    52
    757, trong đó
    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
    49
    50
    51
    52
    758
  3. Bạn có thể sử dụng hàm tích hợp sẵn
    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
    49
    50
    51
    52
    759 để chuyển đổi một chuỗi số từ
    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
    49
    50
    51
    52
    760 đã cho thành số thập phân (Dòng 38)

Ví dụ dec2hex. py - Chuyển đổi thập phân sang thập lục phân

Chương trình này nhắc người dùng nhập số thập phân và in số thập lục phân tương đương của nó. Ví dụ,

_______461____462
Làm thế nào nó hoạt động
  1. Chúng tôi sử dụng mô đun/phép chia nhiều lần để lấy các chữ số hex theo thứ tự ngược lại
  2. Chúng tôi sử dụng danh sách tra cứu (Dòng 11) để chuyển đổi
    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
    49
    50
    51
    52
    1
    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
    49
    50
    51
    52
    762 thành chữ số hex
    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
    49
    50
    51
    52
    763
  3. Bạn có thể sử dụng hàm tích hợp sẵn
    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
    49
    50
    51
    52
    764,
    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
    49
    50
    51
    52
    765,
    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
    49
    50
    51
    52
    766 để chuyển đổi số thập phân sang hệ thập lục phân, bát phân và nhị phân tương ứng; . e. g. ,

wc ví dụ. py - Số từ

Ví dụ này đọc tên tệp từ dòng lệnh và in số lượng dòng, từ và ký tự (tương tự như tiện ích

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
49
50
51
52
768 trong Unix). Nó minh họa việc nhập tệp văn bản và xử lý chuỗi văn bản

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60
Làm thế nào nó hoạt động
  1. nhập sys (Dòng 14). Chúng tôi sử dụng mô-đun
    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
    49
    50
    51
    52
    714 (@ https. // tài liệu. con trăn. org/3/library/sys. html) từ thư viện chuẩn của Python để truy xuất các đối số dòng lệnh được lưu trong
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    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
    49
    50
    51
    52
    771 và để kết thúc chương trình qua
    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
    49
    50
    51
    52
    772. Trong Python, bạn cần
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 mô-đun trước khi sử dụng nó
  2. Các đối số dòng lệnh được lưu trữ trong một biến
    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
    49
    50
    51
    52
    771, là một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 (mảng động của Python). Mục đầu tiên của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    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
    49
    50
    51
    52
    777 là tên tập lệnh, theo sau là các đối số dòng lệnh khác
  3. nếu len(sys. argv). = 2. (Dòng 15). Chúng tôi sử dụng chức năng tích hợp sẵn
    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
    49
    50
    51
    52
    778 để xác minh rằng độ dài của đối số dòng lệnh
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 là 2
  4. với mở (sys. argv[1]) dưới dạng tệp tin. (Dòng 25). Chúng tôi mở tệp thông qua câu lệnh
    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
    49
    50
    51
    52
    780, câu lệnh này sẽ tự động đóng tệp khi thoát
  5. cho dòng trong infile. (Dòng 26). Chúng tôi sử dụng vòng lặp
    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
    49
    50
    51
    52
    781 (Dòng 29) để xử lý từng
    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
    49
    50
    51
    52
    782 của
    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
    49
    50
    51
    52
    783, trong đó
    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
    49
    50
    51
    52
    782 thuộc về lớp dựng sẵn "
    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
    49
    50
    51
    52
    3" (có nghĩa là hỗ trợ chuỗi @ https. // tài liệu. con trăn. org/3/library/stdtypes. html#str). Chúng tôi sử dụng các hàm thành viên của lớp
    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
    49
    50
    51
    52
    3
    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
    49
    50
    51
    52
    787 để loại bỏ các khoảng trắng ở đầu và cuối;
  6. Chúng tôi cũng gọi tiện ích Unix "
    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
    49
    50
    51
    52
    768" thông qua lệnh shell bên ngoài theo 2 cách. thông qua
    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
    49
    50
    51
    52
    791 và
    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
    49
    50
    51
    52
    792

Ví dụ htmlescape. py - Thoát các ký tự HTML dành riêng

Ví dụ này đọc tên tệp đầu vào và đầu ra từ dòng lệnh và thay thế các ký tự HTML dành riêng bằng các thực thể HTML tương ứng của chúng. Nó minh họa đầu vào/đầu ra tập tin và thay thế chuỗi

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
64
Làm thế nào nó hoạt động
  1. nhập sys (Dòng 14). Chúng tôi
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 mô-đun
    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
    49
    50
    51
    52
    714 (@ https. // tài liệu. con trăn. org/3/library/sys. html). Chúng tôi truy xuất các đối số dòng lệnh từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    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
    49
    50
    51
    52
    771, trong đó
    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
    49
    50
    51
    52
    777 là tên tập lệnh;
  2. với mở (sys. argv[1]) dưới dạng tệp tin, mở(sys. argv[2], 'w') dưới dạng tệp ngoài. (Dòng 21). Chúng tôi sử dụng câu lệnh
    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
    49
    50
    51
    52
    780, tự động đóng các tệp khi thoát, để mở
    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
    49
    50
    51
    52
    783 để đọc (mặc định) và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3901 để ghi (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3902)
  3. cho dòng trong infile. (Dòng 22). Chúng tôi sử dụng vòng lặp
    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
    49
    50
    51
    52
    781 để xử lý từng
    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
    49
    50
    51
    52
    782 của
    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
    49
    50
    51
    52
    783, trong đó
    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
    49
    50
    51
    52
    782 thuộc về lớp dựng sẵn "
    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
    49
    50
    51
    52
    3" (có nghĩa là hỗ trợ chuỗi @ https. // tài liệu. con trăn. org/3/library/stdtypes. html#str). Chúng tôi sử dụng hàm thành viên của lớp
    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
    49
    50
    51
    52
    3
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3909 để loại bỏ các khoảng trắng ở cuối (bên phải);
  4. Trăn 3. 2 giới thiệu một mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3911 mới, với chức năng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3912 để thoát khỏi các ký tự dành riêng cho HTML.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65

Ví dụ files_rename. py - Đổi tên tập tin

Ví dụ này đổi tên tất cả các tệp trong thư mục đã cho bằng biểu thức chính quy (regex). Nó minh họa quá trình xử lý thư mục/tệp (sử dụng mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3913) và biểu thức chính quy (sử dụng mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3914)

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66
Làm thế nào nó hoạt động

Giới thiệu

Python được tạo ra bởi Dutch Guido van Rossum vào khoảng năm 1991. Python là một dự án mã nguồn mở. Trang mẹ là www. con trăn. tổ chức

Các tính năng chính của Python là

  • Python là một ngôn ngữ dễ dàng và trực quan. Tập lệnh Python dễ đọc và dễ hiểu
  • Python (như Perl) là biểu cảm. Một dòng mã Python có thể thực hiện nhiều dòng mã trong các ngôn ngữ có mục đích chung truyền thống (chẳng hạn như C/C++/Java)
  • Python là mã nguồn mở và miễn phí. Nó đa nền tảng và chạy trên Windows, Linux/UNIX và Mac OS X
  • Python rất phù hợp để phát triển ứng dụng nhanh (RAD). Bạn có thể viết mã ứng dụng bằng Python trong thời gian ngắn hơn nhiều so với các ngôn ngữ có mục đích chung khác (chẳng hạn như C/C++/Java). Python có thể được sử dụng để viết các ứng dụng nhỏ và nguyên mẫu nhanh, nhưng nó cũng có thể mở rộng quy mô tốt để phát triển dự án quy mô lớn
  • Python là một ngôn ngữ kịch bản và được gõ động. Giống như hầu hết các ngôn ngữ kịch bản (e. g. , Perl, JavaScript), Python liên kết các loại với các đối tượng, thay vì các biến. Nghĩa là, một biến có thể được gán một giá trị thuộc bất kỳ kiểu nào, một danh sách (mảng) có thể chứa các đối tượng thuộc các kiểu khác nhau
  • Python cung cấp quản lý bộ nhớ tự động. Bạn không cần phân bổ và giải phóng bộ nhớ trong các chương trình của mình
  • Python cung cấp các kiểu dữ liệu cấp cao như mảng động và từ điển (hoặc mảng kết hợp)
  • Python là hướng đối tượng
  • Python không phải là một ngôn ngữ được biên dịch đầy đủ. Nó được biên dịch thành mã byte nội bộ, sau đó được diễn giải. Do đó, Python không nhanh bằng các ngôn ngữ được biên dịch đầy đủ như C/C++
  • Python đi kèm với một bộ thư viện khổng lồ bao gồm bộ công cụ giao diện người dùng đồ họa (GUI), thư viện lập trình web, mạng, v.v.

Python có 3 phiên bản

  • Trăn 1. phiên bản ban đầu
  • Trăn 2. phát hành năm 2000, với nhiều tính năng mới như bộ thu gom rác và hỗ trợ Unicode
  • Python 3 (Python 3000 hoặc py3k). Một bản nâng cấp lớn được phát hành vào năm 2008. Python 3 KHÔNG tương thích ngược với Python 2
Python 2 hay Python 3?

Hiện tại, hai phiên bản Python được hỗ trợ song song, phiên bản 2. 7 và phiên bản 3. 5. Rất tiếc là không tương thích. Tình huống này phát sinh vì khi Guido Van Rossum (người tạo ra Python) quyết định mang lại những thay đổi quan trọng cho Python 2, anh ấy nhận thấy rằng những thay đổi mới sẽ không tương thích với các mã hiện có. Anh ấy quyết định bắt đầu một phiên bản mới có tên Python 3, nhưng tiếp tục duy trì Python 2 mà không giới thiệu các tính năng mới. Trăn 3. 0 được phát hành vào năm 2008, trong khi Python 2. 7 năm 2010

MỘT LẦN NỮA, HÃY LƯU Ý RẰNG PYTHON 2 VÀ PYTHON 3 KHÔNG TƯƠNG THÍCH. Bạn cần quyết định nên sử dụng Python 2 hay Python 3. Bắt đầu các dự án mới của bạn bằng Python 3. Chỉ sử dụng Python 2 để duy trì các dự án cũ

Để kiểm tra phiên bản Python của bạn, hãy ra lệnh này

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67

Cài đặt và Bắt đầu

Cài đặt

Dành cho người mới sử dụng Python (Windows, Mac OSX, Ubuntu)

Tôi khuyên bạn nên cài đặt "bản phân phối Anaconda" của Python 3, bao gồm Dấu nhắc lệnh, IDE (Jupyter Notebook và Spyder) và đi kèm với các gói thường được sử dụng (chẳng hạn như NumPy, Matplotlib và Pandas được sử dụng để phân tích dữ liệu)

Goto trang mẹ Anaconda (@ https. //www. trăn anaconda. com/) ⇒ Chọn Tải xuống "Phân phối Anaconda" ⇒ Chọn "Python 3. x" ⇒ Làm theo hướng dẫn để cài đặt

Kiểm tra xem Python đã được cài đặt chưa và phiên bản của nó

Để kiểm tra xem Python đã được cài đặt chưa và phiên bản của nó, hãy ra lệnh sau. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68
Ubuntu (16. 04LTS)

Cả Python 3 và Python 2 đều đã được cài đặt sẵn theo mặc định. Nếu không, bạn có thể cài đặt Python qua

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69

Để xác minh cài đặt Python

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
49
50
51
52
70
các cửa sổ

Bạn có thể cài đặt một trong hai

  1. "Phân phối Anaconda" (Xem phần trước)
  2. Python đơn giản từ Python Software Foundation @ https. //www. con trăn. org/download/, tải xuống trình cài đặt MSI 32-bit hoặc 64-bit và chạy trình cài đặt đã tải xuống
  3. Trong Cygwin (môi trường Unix cho Windows) và cài đặt Python (trong danh mục "phát triển")
hệ điều hành Mac

[LÀM]

Tài liệu

Tài liệu tham khảo ngôn ngữ và tài liệu Python được cung cấp trực tuyến @ https. // tài liệu. con trăn. tổ chức

Bắt đầu với Trình thông dịch Python

Bắt đầu Trình thông dịch Python tương tác

Bạn có thể chạy "Trình thông dịch Python" ở chế độ tương tác trong "Command-Line Shell" (chẳng hạn như Anaconda Prompt, CMD của Windows, Terminal của Mac OS X, Bash Shell của Ubuntu)

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
49
50
51
52
71

Dấu nhắc lệnh Python được ký hiệu là _______ 13915. Bạn có thể nhập câu lệnh Python tại dấu nhắc lệnh của Python, e. g. ,

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
49
50
51
52
72

Để thoát khỏi Trình thông dịch Python

  • 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
    49
    50
    51
    52
    715
  • (Mac OS X và Ubuntu)
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3917
  • (Windows)
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3918 theo sau là Enter

Viết và chạy tập lệnh Python

Tập lệnh Python đầu tiên - xin chào. py

Sử dụng trình soạn thảo văn bản lập trình để viết tập lệnh Python sau và lưu dưới dạng "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" trong thư mục bạn chọn

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
49
50
51
52
73
Làm thế nào nó hoạt động
  1. Theo quy ước, tên tập lệnh Python (mô-đun) ở dạng chữ thường (e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3920)
  2. Nhận xét EOL. Các câu lệnh bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3921 cho đến cuối dòng (EOL) là các nhận xét
  3. #. /usr/bin/env python3 (Dòng 1) chỉ áp dụng cho môi trường Unix. Nó được gọi là Hash-Bang (hoặc She-Bang) để chỉ định vị trí của Trình thông dịch Python, để tập lệnh có thể được thực thi trực tiếp dưới dạng một chương trình độc lập
  4. # -*- mã hóa. UTF-8 -*- (Dòng 2, tùy chọn) chỉ định lược đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  5. """ xin chào. """ (Dòng 3-5). Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu thường là một chuỗi nhiều dòng (được phân định bằng dấu nháy đơn ba hoặc dấu ba kép), có thể được trích xuất từ ​​​​tệp nguồn để tạo tài liệu
  6. Biến. Ta tạo các biến ________ 13922, ________ 13923, ________ 13924, ________ 13925,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3926 (Dòng 6, 8, 10, 12, 14) bằng cách gán giá trị cho chúng
  7. Các chuỗi của Python có thể được đặt trong dấu nháy đơn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7 (Dòng 6) hoặc dấu nháy kép
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8
  8. Số nguyên của Python có kích thước không giới hạn (Dòng 8)
  9. Python hỗ trợ số dấu phẩy động (Dòng 10)
  10. Python hỗ trợ số phức (Dòng 12) và các kiểu dữ liệu cấp cao khác
  11. Python hỗ trợ một mảng động gọi là danh sách (Dòng 14), được đại diện bởi
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3929. Phần tử có thể được truy xuất thông qua chỉ mục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 (Dòng 15)
  12. in(aVar). Hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    662 có thể được sử dụng để in giá trị của một biến ra bàn điều khiển
Sản lượng dự kiến

Các kết quả đầu ra dự kiến ​​là

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
49
50
51
52
74
Chạy tập lệnh Python

Bạn có thể phát triển/chạy tập lệnh Python theo nhiều cách - được giải thích trong các phần sau

Chạy tập lệnh Python trong Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)

Bạn có thể chạy tập lệnh python thông qua Trình thông dịch Python trong Trình bao dòng lệnh

Executable Shell Script của Unix

Trong Linux/Mac OS X, bạn có thể biến tập lệnh Python thành chương trình thực thi (gọi là Shell Script hoặc Executable Script) bằng cách

  1. Bắt đầu bằng một dòng bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3932 (được gọi là "hash-bang" hoặc "she-bang"), theo sau là tên đường dẫn đầy đủ đến Trình thông dịch Python, e. g. , Để định vị Trình thông dịch Python, hãy sử dụng lệnh "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3933" hoặc "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3934"
  2. Làm cho tệp có thể thực thi được thông qua lệnh
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3935 (thay đổi chế độ tệp)
  3. Sau đó, bạn có thể chạy tập lệnh Python giống như bất kỳ chương trình thực thi nào. Hệ thống sẽ tìm kiếm Trình thông dịch Python từ dòng she-bang.
    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
    49
    50
    51
    52
    75

Hạn chế là bạn phải mã hóa cứng đường dẫn đến Trình thông dịch Python, điều này có thể khiến chương trình không thể di động trên các máy khác nhau

Ngoài ra, bạn có thể sử dụng cách sau để chọn Trình thông dịch Python từ môi trường

Tiện ích

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3936 sẽ định vị Trình thông dịch Python (từ các mục nhập của
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3937). Cách tiếp cận này được khuyến nghị vì nó không mã hóa cứng đường dẫn của Python

Chương trình thực thi của Windows

Trong Windows, bạn có thể liên kết phần mở rộng tệp "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3938" với Python Có thể hiểu được, để làm cho tập lệnh Python có thể thực thi được

Chạy tập lệnh Python bên trong Trình thông dịch của Python

Để chạy tập lệnh "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" bên trong Trình thông dịch của Python

  • Bạn có thể sử dụng đường dẫn tuyệt đối hoặc tương đối cho tên tệp. Nhưng,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3940 (đối với thư mục gốc) không hoạt động?
  • Hàm tích hợp sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3941 sẽ mở tệp ở chế độ chỉ đọc mặc định;

Môi trường phát triển tương tác (IDE)

Sử dụng IDE có gỡ lỗi đồ họa có thể cải thiện đáng kể năng suất của bạn

Đối với người mới bắt đầu, tôi khuyên bạn nên

  1. Trình thông dịch Python (như mô tả ở trên)
  2. Python IDLE
  3. Jupyter Notebook (đặc biệt dành cho Phân tích dữ liệu)

Đối với các nhà phát triển Webapp, tôi khuyên bạn nên

  1. Nhật thực với PyDev
  2. PyCharm

Xem "Python IDE và trình gỡ lỗi" để biết chi tiết

Các cú pháp cơ bản của Python

Bình luận

Nhận xét Python bắt đầu bằng dấu thăng (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3921) và kéo dài đến cuối dòng hiện tại. Các nhận xét bị Trình thông dịch Python bỏ qua, nhưng chúng rất quan trọng trong việc cung cấp giải thích và tài liệu cho người khác (và chính bạn ba ngày sau) để đọc chương trình của bạn. Sử dụng bình luận một cách tự do

KHÔNG có nhận xét nhiều dòng trong Python?. (C/C++/Java hỗ trợ nhận xét nhiều dòng thông qua

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6. )

Các câu lệnh

Một câu lệnh Python được phân định bởi một dòng mới. Một tuyên bố không thể vượt qua ranh giới, ngoại trừ

  1. Một biểu thức trong dấu ngoặc đơn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3945, dấu ngoặc vuông
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946 và dấu ngoặc nhọn
    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
    49
    50
    51
    52
    8 có thể trải rộng trên nhiều dòng
  2. Dấu gạch chéo ngược (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3948) ở cuối dòng biểu thị sự tiếp tục của dòng tiếp theo. Đây là một quy tắc cũ và KHÔNG được khuyến nghị vì nó dễ bị lỗi

Không giống như C/C++/C#/Java, bạn không đặt dấu chấm phẩy (

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
49
50
51
52
7) ở cuối câu lệnh Python. Nhưng bạn có thể đặt nhiều câu lệnh trên một dòng, được phân tách bằng dấu chấm phẩy (
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
49
50
51
52
7). Ví dụ như,

Câu lệnh khối, thụt đầu dòng và hợp chất

Một khối là một nhóm các câu lệnh thực thi như một đơn vị. Không giống như C/C++/C#/Java, sử dụng dấu ngoặc nhọn

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
49
50
51
52
8 để nhóm các câu lệnh trong một khối nội dung, Python sử dụng thụt đầu dòng cho khối nội dung. Nói cách khác, thụt đầu dòng có ý nghĩa về mặt cú pháp trong Python - khối cơ thể phải được thụt lề đúng cách. Đây là một cú pháp tốt để buộc bạn phải thụt lề các khối một cách chính xác để dễ hiểu

Một câu lệnh ghép, chẳng hạn như điều kiện (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645), vòng lặp (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
602) và định nghĩa hàm (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3955), bắt đầu bằng một dòng tiêu đề kết thúc bằng dấu hai chấm (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667);

Ví dụ như,

Python không chỉ định sử dụng bao nhiêu thụt đầu dòng, nhưng tất cả các câu lệnh của khối nội dung CÙNG phải bắt đầu ở khoảng cách CÙNG từ lề phải. Bạn có thể sử dụng dấu cách hoặc tab để thụt đầu dòng nhưng bạn không thể trộn chúng trong cùng một khối nội dung. Nên sử dụng 4 dấu cách cho mỗi mức thụt đầu dòng

Dấu hai chấm (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667) và thụt lề nội dung có lẽ là tính năng kỳ lạ nhất trong Python, nếu bạn đến từ C/C++/C#/Java. Python áp đặt các quy tắc thụt lề nghiêm ngặt để buộc các lập trình viên viết mã có thể đọc được

Biến, Định danh và Hằng số

Giống như tất cả các ngôn ngữ lập trình, một biến là một vị trí lưu trữ được đặt tên. Một biến có tên (hoặc mã định danh) và giữ một giá trị

Giống như hầu hết các ngôn ngữ diễn giải tập lệnh (chẳng hạn như JavaScript/Perl), Python được nhập động. Bạn KHÔNG cần phải khai báo một biến trước khi sử dụng nó. Một biến được tạo thông qua phép gán ban đầu. (Không giống như các ngôn ngữ gõ tĩnh có mục đích chung truyền thống như C/C++/Java/C#, nơi bạn cần khai báo tên và loại biến trước khi sử dụng biến. )

Ví dụ,

Tính tổng các số nguyên tố trong python Bài tập chuyên gia

Như đã đề cập, Python được gõ động. Python liên kết các loại với các đối tượng, không phải các biến, tôi. e. , một biến có thể chứa bất kỳ loại đối tượng nào, như được minh họa trong các ví dụ trên

Quy tắc định danh (Tên)

An identifier starts with a letter (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3958,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3959) or an underscore (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3960), followed by zero or more letters, underscores and digits (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3961). Python does not allow special characters such as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3962 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3963

Keywords

Python 3 có 35 từ hoặc từ khóa dành riêng, không thể được sử dụng làm định danh

  • 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
    49
    50
    51
    52
    5,
    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
    49
    50
    51
    52
    6,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966 (boolean và chữ đặc biệt)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3968,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3969
  • 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
    49
    50
    51
    52
    703,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3971,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3972,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3976,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3977,
    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
    49
    50
    51
    52
    740,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3979 (điều khiển luồng)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3955,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3981,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3982,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3983,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3984 (chức năng)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3985
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    635 (nhà điều hành)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3993,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3994,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3995 (xử lý lỗi)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3996,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3997,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3998
Variable Naming Convention

Tên biến là một danh từ hoặc một cụm danh từ được tạo thành từ nhiều từ. Có hai quy ước

  1. In lowercase words and optionally joined with underscore if it improves readability, e. g. , num_students,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3999,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4000,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4001, etc
  2. In the so-called camel-case where the first word is in lowercase, and the remaining words are initial-capitalized, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4002,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4003,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4004,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4005,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4006, and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4007. (This is the Java's naming convention. )
Recommendations
  1. Điều quan trọng là chọn một tên tự mô tả và phản ánh chặt chẽ ý nghĩa của biến, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4002, but not
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4009 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010, to store the number of students. It is alright to use abbreviations, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4011 for index
  2. Do not use meaningless names like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4012,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4013,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4014,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4017,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4009,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4019,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4020,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4021,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4022,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4023 (what is the purpose of this exercise?), and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4024 (What is this example about?)
  3. Avoid single-letter names like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4017,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4012,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4013,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4014, which are easier to type but often meaningless. Ngoại lệ là các tên phổ biến như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4032,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4033 cho tọa độ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015 cho chỉ mục. Long names are harder to type, but self-document your program. (I suggest you spend sometimes practicing your typing. )
  4. Use singular and plural nouns prudently to differentiate between singular and plural variables.   For example, you may use the variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4035 to refer to a single row number and the variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4036 to refer to many rows (such as a list of rows - to be discussed later)
Constants

Python does not support constants, where its contents cannot be modified. (C supports constants via keyword

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4037, Java via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4038. )

It is a convention to name a variable in uppercase (joined with underscore), e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4039,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4040, to indicate that it should not be modified in the program. Tuy nhiên, không có gì ngăn cản nó được sửa đổi

Data Types. Số, Chuỗi và Danh sách

Python supports various number type such as

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
49
50
51
52
1 (for integers such as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4042,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4043),
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
49
50
51
52
2 (for floating-point number such as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4045,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4046,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4047), and
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
49
50
51
52
4 (for boolean of either
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
49
50
51
52
5 and
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
49
50
51
52
6)

Python hỗ trợ chuỗi văn bản (một chuỗi ký tự). In Python, strings can be delimited with single-quotes or double-quotes, e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4051,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4052,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4053 hoặc
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4054 (chuỗi trống)

Python supports a dynamic-array structure called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, denoted as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4056. Bạn có thể tham chiếu phần tử thứ i là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
607. Python's
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 is similar to C/C++/Java's array, but it is NOT fixed size, and can be expanded dynamically during runtime

I will describe these data types in details in the later section

Đầu vào/đầu ra của bảng điều khiển. Các chức năng tích hợp sẵn input() và print()

You can use built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680 to read input from the console (as a string) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 to print output to the console. For example,

print()

The built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 has the following signature

Ví dụ như,

dấu phân cách của print() (sep) và kết thúc (end)

You can use the optional keyword-argument

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4062 to set the separator string (default is space), and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4063 for ending string (default is newline). For examples,

print in Python 2 vs Python 3

Recall that Python 2 and Python 3 are NOT compatible. In Python 2, you can use "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4064", without the parentheses (because
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4065 is a keyword in Python 2). Trong Python 3, bắt buộc phải có dấu ngoặc đơn vì
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 là một hàm. Ví dụ,

Important. Always use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 function with parentheses, for portability

Kiểu dữ liệu và gõ động

Python có một số lượng lớn các kiểu dữ liệu dựng sẵn, chẳng hạn như Số (Số nguyên, Số thực, Boolean, Số phức), Chuỗi, Danh sách, Tuple, Bộ, Từ điển và Tệp. More high-level data types, such as Decimal and Fraction, are supported by external modules

You can use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4068 to check the type of a variable or literal

Number Types

Python supports these built-in number types

  1. Integers (type
    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
    49
    50
    51
    52
    1). e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4042,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4043. Unlike C/C++/Java, integers are of unlimited size in Python. For example, You can also express integers in hexadecimal with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4072 (or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4073); in octal with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4074 (or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4075); and in binary with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4076 (or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4077). For examples,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4078,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4079,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4080,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4081
  2. Floating-point numbers (type
    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
    49
    50
    51
    52
    2). e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4083,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4084,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4085,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4086, with a decimal point and an optional exponent (denoted by
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4087 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4088).
    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
    49
    50
    51
    52
    2s are 64-bit double precision floating-point numbers. For example,
  3. Booleans (type
    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
    49
    50
    51
    52
    4). nhận giá trị là
    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
    49
    50
    51
    52
    5 hoặc
    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
    49
    50
    51
    52
    6. Take note of the spelling in initial-capitalized. Trong Python, số nguyên
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695, một giá trị trống (chẳng hạn như chuỗi rỗng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4053,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4054, danh sách trống
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946, bộ dữ liệu trống
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3945, từ điển trống
    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
    49
    50
    51
    52
    8) và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966 được coi là
    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
    49
    50
    51
    52
    6; . Booleans can also act as integers in arithmetic operations with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 for
    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
    49
    50
    51
    52
    5 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 for
    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
    49
    50
    51
    52
    6. Ví dụ, ______976
  4. Số phức (loại
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    06). e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    07,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    08. Complex numbers have a real part and an imaginary part denoted with suffix of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016 (or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    10). Ví dụ,
  5. Other number types are provided by external modules, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    11 module for decimal fixed-point numbers,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    12 module for rational numbers

Dynamic Typing and Assignment Operator

Recall that Python is dynamic typed (instead of static typed)

Python liên kết các loại với các đối tượng, thay vì các biến. Nghĩa là, một biến không có kiểu cố định và có thể được gán cho một đối tượng thuộc bất kỳ kiểu nào. Một biến chỉ đơn giản là cung cấp một tham chiếu đến một đối tượng

You do not need to declare a variable before using a variable. Một biến được tạo tự động khi một giá trị được gán lần đầu tiên, giá trị này liên kết đối tượng được gán với biến

Bạn có thể sử dụng chức năng tích hợp sẵn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
13 để lấy loại đối tượng được tham chiếu bởi một biến

loại đúc. int(x), float(x), str(x)

Bạn có thể thực hiện chuyển đổi kiểu (hoặc ép kiểu) thông qua các hàm tích hợp sẵn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
14,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
15,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
16,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
17, v.v. Ví dụ,

Tóm lại, một biến không liên kết với một loại. Thay vào đó, một loại được liên kết với một đối tượng. A variable provides a reference to an object (of a certain type)

Check Instance's Type. isinstance(instance, type)

You can also use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
18 to check if the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
19 belong to the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
20. Ví dụ,

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
49
50
51
52
77
The Assignment Operator (=)

In Python, you do not need to declare variables before using the variables. The initial assignment creates a variable and links the assigned value to the variable. For example,

Chuyển nhượng theo cặp và Chuyển nhượng theo chuỗi

Ví dụ,

Assignment operator is right-associative, i. e. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
21 is interpreted as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
22

del Operator

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
635 operator to delete a variable. For example,

Number Operations

Toán tử số học (+, -, *, /, //, **, %)

Python supports these arithmetic operators

OperatorModeUsageDescriptionExample+Binary
Unaryx + y
+xAddition
Positive-Binary
Unaryx - y
-xSubtraction
Negate*Binaryx * yMultiplication/Binaryx / yFloat Division
(Returns a float)1 / 2 ⇒ 0. 5
-1 / 2 ⇒ -0. 5//Binaryx // yInteger Division
(Returns the floor integer)1 // 2 ⇒ 0
-1 // 2 ⇒ -1
8. 9 // 2. 5 ⇒ 3. 0
-8. 9 // 2. 5 ⇒ -4. 0 (floor. )
-8. 9 // -2. 5 ⇒ 3. 0**Binaryx ** yExponentiation2 ** 5 ⇒ 32
1. 2 ** 3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus (Remainder)9 % 2 ⇒ 1
-9 % 2 ⇒ 1
9 % -2 ⇒ -1
-9 % -2 ⇒ -1
9. 9 % 2. 1 ⇒ 1. 5
-9. 9 % 2. 1 ⇒ 0. 6000000000000001
Compound Assignment Operators (+=, -=, *=, /=, //=, **=, %=)

Each of the arithmetic operators has a corresponding shorthand assignment counterpart, i. e. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
61,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
62,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
63,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
64 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
65. For example
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 is the same as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32

Tăng/Giảm (++, --)?

Python does not support increment (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67) and decrement (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68) operators (as in C/C++/Java). You need to use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 for increment

Python accepts

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
37, and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
38. Don't get trap into this. But Python flags a syntax error for
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
39 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
40

Mixed-Type Operations

For mixed-type operations, e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
41 (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
42), the value of the "smaller" type is first promoted to the "bigger" type. Sau đó, nó thực hiện thao tác ở kiểu "lớn hơn" và trả về kết quả ở kiểu "lớn hơn". Trong Python,
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
49
50
51
52
1 "nhỏ hơn" so với
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
49
50
51
52
2, "nhỏ hơn" so với
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
06

Relational (Comparison) Operators (==, !=, <, <=, >, >=, in, not in, is, is not)

Python hỗ trợ các toán tử quan hệ (so sánh) này trả về giá trị

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
49
50
51
52
4 của
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
49
50
51
52
5 hoặc
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
49
50
51
52
6

OperatorModeUsageDescriptionExample==
. =
<
<=
>
>=Binaryx == y
x != y
x < y
x
x > y
x >
Return
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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
6in
not inBinaryx in seq
x not in seqCheck if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 is contained in the sequence
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53
Return bool of either True or Falselst = [1, 2, 3]
x = 1
x in lst ⇒ Falseis
is notBinaryx is y
x is not yCheck if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53 are referencing the same object
Return
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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
6

Example. [TODO]

Logical Operators (and, or, not)

Python supports these logical (boolean) operators, that operate on boolean values

OperatorModeUsageDescriptionExampleandBinaryx and yLogical ANDorBinaryx or yLogical ORnotUnarynot xLogical NOT

Notes

  • Python's logical operators are typed out in word, unlike C/C++/Java which uses symbols
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601
  • Python does not have an exclusive-or (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62) boolean operator

Example. [TODO]

Built-in Functions

Python provides many built-in functions for numbers, including

  • hàm toán học.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65, etc
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66 để lấy loại
  • Chức năng chuyển đổi loại.
    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
    49
    50
    51
    52
    738,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    70, etc
  • Các hàm chuyển đổi cơ số.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    71,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    72,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    73

Ví dụ như,

Bitwise Operators (Advanced)

Python supports these bitwise operators

OperatorModeUsageDescriptionExample
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
74
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
75&binaryx & ybitwise ANDx & y ⇒ 0b10000001. binaryx . ybitwise ORx . y ⇒ 0b10001111~Unary~xbitwise NOT (or negate)~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110<

String

In Python, strings can be delimited by a pair of single-quotes (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
7) or double-quotes (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
8). Python also supports multi-line strings via triple-single-quotes (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
9) or triple-double-quotes (
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
49
50
51
52
0)

To place a single-quote (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
80) inside a single-quoted string, you need to use escape sequence
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
81. Similarly, to place a double-quote (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
82) inside a double-quoted string, use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
83. There is no need for escape sequence to place a single-quote inside a double-quoted string; or a double-quote inside a single-quoted string

A triple-single-quoted or triple-double-quoted string can span multiple lines. There is no need for escape sequence to place a single/double quote inside a triple-quoted string. Triple-quoted strings are useful for multi-line documentation, HTML and other codes

Python 3 uses Unicode character set to support internationalization (i18n)

Escape Sequences for Characters (\code)

Like C/C++/Java, you need to use escape sequences (a back-slash + a code) for

  • Special non-printable characters, such as tab (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    84), newline (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    85), carriage return (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    86)
  • Resolve ambiguity, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    83 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    82 inside double-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    81 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    80 inside single-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    91 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    406)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    93 for character in hex value and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    94 for octal value
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    95 cho ký tự Unicode 4 chữ số hex (16 bit) và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    96 cho ký tự Unicode 8 chữ số hex (32 bit)
Raw Strings (r'. ' or r". ")

You can prefix a string by

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
97 to disable the interpretation of escape sequences (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
98), i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
99 is
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
49
50
51
52
00 (two characters) instead of newline (one character). Raw strings are used extensively in regex (to be discussed in module
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3914 section)

Strings are Immutable

Strings are immutable, i. e. , their contents cannot be modified. String functions such as

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
49
50
51
52
02,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3910 returns a new string object instead of modifying the string under operation

Built-in Functions and Operators for Strings

You can operate on strings using

  • built-in functions such as
    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
    49
    50
    51
    52
    04;
  • các toán tử như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (chứa),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 (nối),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (lặp lại), lập chỉ mục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    621 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    622, và cắt lát
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    623

Note. These functions and operators are applicable to all

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
49
50
51
52
11 data types including
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
49
50
51
52
12,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, and
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
49
50
51
52
14 (to be discussed later)

Function /
OperatorUsageDescriptionExamples
s = 'Hello'len()len(str)Lengthlen(s) ⇒ 5insubstr in strContain?
Return
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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
6'ell' in s ⇒ True
'he' in s ⇒ False+
+=str + str1
str += str1Concatenations + '. ' ⇒ 'Hello. '*
*=str * count
str *= countRepetitions * 2 ⇒ 'HelloHello'[i]
[-i]str[i]
str[-i]Indexing to get a character.
The front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695;
back index begins at
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
49
50
51
52
19 (
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
49
50
51
52
20). s[1] ⇒ 'e'
s[-4] ⇒ 'e'[m. n. step]
[m. n]
[m. ]
[. n]
[. ]str[m. n. step]
str[m. n]
str[m. ]
str[. n]
str[. ] Cắt để lấy một chuỗi con.
Từ chỉ mục
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
49
50
51
52
21 (bao gồm) đến
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
49
50
51
52
22 (không bao gồm) với kích thước
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
49
50
51
52
23.
The defaults are.
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
49
50
51
52
24
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
49
50
51
52
25,
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
49
50
51
52
26,
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
49
50
51
52
27. s[1. 3] ⇒ 'el'
s[1. -2] ⇒ 'el'
s[3. ] ⇒ 'lo'
s[. -2] ⇒ 'Hel'
s[. ] ⇒ 'Hello'
s[0. 5. 2] ⇒ 'Hlo'

Ví dụ như,

Character Type?

Python does not have a dedicated character data type. Một ký tự chỉ đơn giản là một chuỗi có độ dài 1. You can use the indexing operator to extract individual character from a string, as shown in the above example; or process individual character using

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 loop (to be discussed later)

The built-in functions

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
49
50
51
52
29 and
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
49
50
51
52
30 operate on character, e. g. ,

Unicode so với ASCII

In Python 3, strings are defaulted to be Unicode. ASCII strings are represented as byte strings, prefixed with

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4013, e. g. ,
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
49
50
51
52
32

In Python 2, strings are defaulted to be ASCII strings (byte strings). Unicode strings are prefixed with

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
49
50
51
52
33

You should always use Unicode for internationalization (i18n)

Các hàm thành viên dành riêng cho chuỗi

Python supports strings via a built-in class called

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
49
50
51
52
3 (We will describe class in the Object-Oriented Programming chapter). The
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
49
50
51
52
3 class provides many member functions. Vì chuỗi là bất biến nên hầu hết các hàm này đều trả về một chuỗi mới. The commonly-used member functions are as follows, supposing that
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
49
50
51
52
36 is a
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
49
50
51
52
3 object

  • 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
    49
    50
    51
    52
    38
    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
    49
    50
    51
    52
    39,
    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
    49
    50
    51
    52
    40,
    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
    49
    50
    51
    52
    41. loại bỏ các khoảng trắng đầu và cuối, khoảng trắng bên phải (dấu);
  • 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
    49
    50
    51
    52
    38
    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
    49
    50
    51
    52
    43,
    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
    49
    50
    51
    52
    44. Return a uppercase/lowercase counterpart, respectively
  • 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
    49
    50
    51
    52
    38
    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
    49
    50
    51
    52
    46,
    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
    49
    50
    51
    52
    47. Check if the string is uppercase/lowercase, respectively
  • 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
    49
    50
    51
    52
    38
    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
    49
    50
    51
    52
    49
  • 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
    49
    50
    51
    52
    50
  • 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
    49
    50
    51
    52
    51
  • 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
    49
    50
    51
    52
    52
  • 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
    49
    50
    51
    52
    53,
    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
    49
    50
    51
    52
    54
    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
    49
    50
    51
    52
    55
Định dạng chuỗi 1 (Kiểu mới). Using str. format() function

There are a few ways to produce a formatted string for output. Python 3 introduces a new style in the

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
49
50
51
52
3's
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
49
50
51
52
767 member function with
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
49
50
51
52
8 as place-holders (called format fields). For examples,

When you pass lists, tuples, or dictionaries (to be discussed later) as arguments into the

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
49
50
51
52
767 function, you can reference the sequence's elements in the format fields with
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
49
50
51
52
60. For examples,

String Formatting 2. Using str. rjust(n), str. ljust(n), str. center(n), str. zfill(n)

You can also use

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
49
50
51
52
3's member functions like
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
49
50
51
52
62 (where
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 is the field-width),
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
49
50
51
52
64,
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
49
50
51
52
65,
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
49
50
51
52
3
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
49
50
51
52
67 to format a string. For example,

String Formatting 3 (Old Style). Sử dụng toán tử %

The old style (in Python 2) is to use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66 operator, with C-like
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
49
50
51
52
69 format specifiers. For examples,

Avoid using old style for formatting

Conversion between String and Number. int(), float() and str()

You can use built-in functions

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
49
50
51
52
738 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68 to parse a "numeric" string to an integer or a float; and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69 to convert a number to a string. For example,

Concatenate a String and a Number?

Bạn KHÔNG THỂ nối một chuỗi và một số (kết quả là

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
49
50
51
52
73). Thay vào đó, bạn cần sử dụng hàm
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69 để chuyển số thành chuỗi. For example,

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
49
50
51
52
78

The None Value

Python provides a special value called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3966 (take note of the spelling in initial-capitalized), which can be used to initialize an object (to be discussed in OOP later). For example,

List, Tuple, Dictionary and Set

Liệt kê [v1, v2,. ]

Python has a powerful built-in dynamic array called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604

  • A
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 is enclosed by square brackets
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946
  • A
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 can contain items of different types. It is because Python associates types to objects, not variables
  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 tự động tăng và giảm kích thước (động). You do not have to specify its size during initialization
  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 có thể thay đổi. You can update its contents
Hàm và toán tử tích hợp cho danh sách

A

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, like string, is a
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
49
50
51
52
11. Hence, you can operate lists using

  • built-in
    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
    49
    50
    51
    52
    11 functions such as
    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
    49
    50
    51
    52
    04
  • built-in
    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
    49
    50
    51
    52
    11 functions for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 of numbers such as
    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
    49
    50
    51
    52
    88,
    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
    49
    50
    51
    52
    89, and
    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
    49
    50
    51
    52
    90
  • built-in operators such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (contains),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 (concatenation) and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (repetition),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    635,
    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
    49
    50
    51
    52
    95 (indexing), and
    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
    49
    50
    51
    52
    96 (slicing)

Notes

  • You can index the items from the front with positive index, or from the back with negative index. e. g. , if
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 is a list,
    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
    49
    50
    51
    52
    98 and
    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
    49
    50
    51
    52
    99 refer to its first and second items;
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601 refer to the last and second-to-last items
  • Bạn cũng có thể tham khảo danh sách phụ (hoặc lát cắt) bằng cách sử dụng ký hiệu lát cắt
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 (từ chỉ mục
    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
    49
    50
    51
    52
    21 (bao gồm) đến chỉ mục
    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
    49
    50
    51
    52
    22 (không bao gồm) với kích thước
    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
    49
    50
    51
    52
    23)
OperatorUsageDescriptionExamples
lst = [8, 9, 6, 2]in
not inx in lst
x not in lstContain? Return
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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
69 in lst ⇒ True
5 in lst ⇒ False+
+=lst + lst1
lst += lst1Concatenationlst + [5, 2]
⇒ [8, 9, 6, 2, 5, 2]*
*=lst * count
lst *= countRepetitionlst * 2
⇒ [8, 9, 6, 2, 8, 9, 6, 2][i]
[-i]lst[i]
lst[-i]Indexing to get an item.
Front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695;
back index begins at
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
49
50
51
52
19 (or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
611). lst[1] ⇒ 9
lst[-2] ⇒ 6[m. n. bước]
[m. n]
[m. ]
[. n]
[. ]lst[m. n. step]
lst[m. n]
lst[m. ]
lst[. n]
lst[. ]Slicing to get a sublist.
From index
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
49
50
51
52
21 (included) to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 (excluded) with
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
49
50
51
52
23 size.
The defaults are.
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
49
50
51
52
21 is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695, n is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
611. lst[1. 3] ⇒ [9, 6]
lst[1. -2] ⇒ [9]
lst[3. ] ⇒ [2]
lst[. -2] ⇒ [8, 9]
lst[. ] ⇒ [8, 9, 6, 2]
lst[0. 4. 2] ⇒ [8, 6]
newlst = lst[. ] ⇒ Copy
lst[4. ] = [1, 2] ⇒ Extenddeldel lst[i]
del lst[m. n]
del lst[m. n. step]Delete one or more itemsdel lst[1] ⇒ [8, 6, 2]
del lst[1. ] ⇒ [8]
del lst[. ] ⇒ [] (Clear)FunctionUsageDescriptionExamples
lst = [8, 9, 6, 2]len()len(lst)Lengthlen(lst) ⇒ 4max()
min()max(lst)
min(lst)Maximum value
minimum valuemax(lst) ⇒ 9
min(lst) ⇒ 2sum()sum(lst)Sum (for number lists only)sum(lst) ⇒ 16

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, unlike string, is mutable. You can insert, remove and modify its items

Ví dụ như,

Appending Items to a list
Copying a list
list-Specific Member Functions

Lớp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 cung cấp nhiều hàm thành viên. Suppose
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
49
50
51
52
720 is a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 object

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    623. return the index of the first occurrence of
    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
    49
    50
    51
    52
    719; or error
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    625. append the given
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    626 behind the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966; same as slicing operation
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    629
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    630. append the given list
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    631 behind the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966; same as slicing operation
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    634
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    635. insert the given
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    626 before the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    637 and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966. Hence,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    639 inserts before the first item of the
    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
    49
    50
    51
    52
    720;
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    641 inserts at the end of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 which is the same as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    625
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    644. remove the first occurrence of
    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
    49
    50
    51
    52
    719 from the
    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
    49
    50
    51
    52
    720 and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966; or error
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    648. remove and return the last item of the
    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
    49
    50
    51
    52
    720
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    650. xóa và trả lại mục được lập chỉ mục của
    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
    49
    50
    51
    52
    720
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    652. remove all the items from the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966; same as operator
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    656. return the occurrences of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    626
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    658. đảo ngược
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 tại chỗ và trả về
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    661. sort the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 in place and return
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    664. trả lại một bản sao của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605;

Recall that

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 is mutable (unlike string which is immutable). These functions modify the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 directly. Ví dụ như,

Using list as a last-in-first-out Stack

To use a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 as a last-in-first-out (LIFO) stack, use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
670 to add an item to the top-of-stack (TOS) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
671 to remove the item from the TOS

Using list as a first-in-first-out Queue

To use a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 as a first-in-first-out (FIFO) queue, use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
670 to add an item to the end of the queue and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
674 to remove the first item of the queue

However,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
674 is slow. The standard library provide a class
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
676 to efficiently implement deque with fast appends and pops from both ends

Tuple (v1, v2,. )

Tuple is similar to list except that it is immutable (just like string). Hence, tuple is more efficient than list. A tuple consists of items separated by commas, enclosed in parentheses

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3945

An one-item tuple needs a comma to differentiate from parentheses

Các dấu ngoặc đơn thực sự là tùy chọn, nhưng được khuyến nghị để dễ đọc. Nevertheless, the commas are mandatory. For example,

You can operate on tuples using (supposing that

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
678 is a tuple)

  • built-in functions such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    679;
  • built-in functions for tuple of numbers such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    681 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    682;
  • operators such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62; and
  • 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
    49
    50
    51
    52
    14's member functions such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    687,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    688, etc
Chuyển đổi giữa List và Tuple

You can covert a list to a tuple using built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
689; and a tuple to a list using
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
690. For examples,

Dictionary {k1. v1, k2. v2,. }

Loại từ điển tích hợp sẵn của Python hỗ trợ các cặp khóa-giá trị (còn được gọi là cặp tên-giá trị, mảng kết hợp hoặc ánh xạ)

  • A dictionary is enclosed by a pair of curly braces
    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
    49
    50
    51
    52
    8. The key and value are separated by a colon (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    667), in the form of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    615
  • Unlike list and tuple, which index items using an integer index 0, 1, 2, 3,. , từ điển có thể được lập chỉ mục bằng bất kỳ loại khóa nào, bao gồm số, chuỗi hoặc các loại khác
  • Từ điển có thể thay đổi
Dictionary-Specific Member Functions

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
694 class has many member methods. The commonly-used are follows (suppose that
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695 is a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
694 object)

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    697
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    698,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    699,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    603
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604. merge the given dictionary
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 into
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695. Override the value if key exists, else, add new key-value
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607

For Examples,

Set {k1, k2,. }

A set is an unordered, non-duplicate collection of objects. Một tập hợp được giới hạn bởi dấu ngoặc nhọn

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
49
50
51
52
8, giống như từ điển. You can think of a set as a collection of dictionary keys without associated values. Bộ có thể thay đổi

Ví dụ,

Set-Specific Operators (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
609,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
601,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
61,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
674)

Python hỗ trợ các toán tử tập hợp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
609 (giao điểm),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
614 (hợp),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
61 (dị biệt) và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
674 (độc quyền-hoặc). For example,

Các loại trình tự. list, tuple, str

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604,
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
49
50
51
52
14, and
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
49
50
51
52
3 are parts of the sequence types.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 is mutable, while
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
49
50
51
52
14 and
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
49
50
51
52
3 are immutable. They share the common sequence's built-in operators and built-in functions, as follows

Opr / FuncUsageDescriptionin
not inx in seq
x not in seqContain? Return
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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
6+seq + seq1Concatenation*seq * countRepetition (Same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
626)[i]
[-i]seq[i]
seq[-i]Indexing to get an item.
Front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695; back index begins at
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
49
50
51
52
19 (or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
629). [m. n. step]
[m. n]
[m. ]
[. n]
[. ]seq[m. n. step]
seq[m. n]
seq[m. ]
seq[. n}
seq[. ] Cắt lát để lấy chuỗi con.
From index
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
49
50
51
52
21 (included) to
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
49
50
51
52
22 (excluded) with
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
49
50
51
52
23 size.
The defaults are.
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
49
50
51
52
24 is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695, n is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
629. len()
min()
max()len(seq)
min(seq)
max(seq)Return the Length, mimimum and maximum of the sequenceseq. index()seq. index(x)
seq. index(x, i)
seq. index(x, i, j)Return the index of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
636 in the sequence, or raise
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
637.
Search from
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
49
50
51
52
726 (included) to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
639 (excluded)seq. count()seq. count(x)Returns the count of x in the sequence

For mutable sequences (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604), the following built-in operators and built-in functions (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
641
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642) and member functions (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
638
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
644) are supported

Opr / FuncUsageDescription[]seq[i] = x
seq[m. n] = []
seq[. ] = []
seq[m. n] = seq1
seq[m. n. step] = seq1Replace one item
Remove one or more items
Remove all items
Replace more items with a sequence of the same size+=seq += seq1Extend by
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645*=seq *= countRepeat
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
646 timesdeldel seq[i]
del seq[m. n]
del seq[m. n. step]Delete one item
Delete more items, same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647seq. rõ ràng () seq. clear()Remove all items, same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
648 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
649seq. append()seq. append(x)Append x to the end of the sequence,
same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
650seq. extend()seq. entend(seq1)Extend the sequence,
same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
651 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
652seq. chèn () seq. insert(i, x)Insert
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 at index
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4015, same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655seq. remove()seq. remove(x)Remove the first occurence of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
636seq. pop() seq. pop()
seq. pop(i)Retrieve and remove the last item
Retrieve and remove the item at index
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
49
50
51
52
726seq. copy()seq. copy()Create a shallow copy of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
658, same as.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
659seq. reverse()seq. reverse()Reverse the sequence in place

Người khác

Deque

[LÀM]

Heap

[LÀM]

Flow Control Constructs

Conditional if-elif-else

The syntax is as follows. The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3971 (else-if) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3972 blocks are optional

For example

There is no

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 statement in Python (as in C/C++/Java)

Comparison and Logical Operators

Python supports these comparison (relational) operators, which return a

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
49
50
51
52
4 of either
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
49
50
51
52
5 or
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
49
50
51
52
6

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68 (less than),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69 (less than or equal to),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66 (equal to),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67 (not equal to),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 (greater than),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61 (greater than or equal to). (Điều này cũng giống như C/C++/Java. )
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63. Check if an item is. is not in a sequence (list, tuple, string, set, etc)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    675. Kiểm tra xem hai biến có cùng tham chiếu không

Python supports these logical (boolean) operators.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68. (C/C++/Java sử dụng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
600,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
601. )

So sánh chuỗi v1 < x < v2

Python supports chain comparison in the form of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
682, e. g. ,

Comparing Sequences

The comparison operators (such as

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69) are overloaded to support sequences (such as string, list and tuple)

In comparing sequences, the first items from both sequences are compared. If they differ the outcome is decided. Otherwise, the next items are compared, and so on

Shorthand if-else (or Conditional Expression)

Cú pháp là

Ví dụ,

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
49
50
51
52
79

Note. Python does not use "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
685" for shorthand
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645, as in C/C++/Java

The while loop

Cú pháp như sau

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3972 block is optional, which will be executed if the loop exits normally without encountering a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3976 statement

Ví dụ,

break, continue, pass and loop-else

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3976 statement breaks out from the innermost loop; the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3977 statement skips the remaining statements of the loop and continues the next iteration. This is the same as C/C++/Java

The

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
49
50
51
52
740 statement does nothing. It serves as a placeholder for an empty statement or empty block

The loop-

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3972 block is executed if the loop is exited normally, without encountering the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3976 statement

Examples. [TODO]

Using Assignment in while-loop's Test?

In many programming languages, assignment can be part of an expression, which return a value. It can be used in

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647-loop's test, e. g. ,

Python issues a syntax error at the assignment operator. In Python, you cannot use assignment operator in an expression

You could do either of the followings

The for-in loop

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 loop has the following syntax

You shall read it as "for each item in the sequence. ". Again, the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3972 block is executed only if the loop exits normally, without encountering the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3976 statement

Iterating through Sequences

Iterating through a Sequence (String, List, Tuple, Dictionary, Set) using for-in Loop

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 loop is primarily used to iterate through all the items of a sequence. For example,

for(;;) Loop

Python does NOT support the C/C++/Java-like

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
699 loop, which uses a varying index for the iterations

Take note that you cannot use the "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
600" loop to modify a list. To modify the list, you need to get the list indexes by creating an index list. For example,

Manually creating the index list is not practical. You can use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
601 function to create the index list (described below)

The range() Built-in Function

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
601 function produces a series of running integers, which can be used as index list for the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 loop

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 produces integers from
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606;
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 produces integers from
    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
    49
    50
    51
    52
    24 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606;
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    610 produces integers from
    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
    49
    50
    51
    52
    24 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606 in step of
    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
    49
    50
    51
    52
    38

Ví dụ,

Using else-clause in Loop

Recall that the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3972-clause will be executed only if the loop exits without encountering a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3976

Iterating through a Sequence of Sequences

A sequence (such as list, tuple) can contain sequences. For example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
390
Lặp lại thông qua một từ điển

There are a few ways to iterate through an dictionary

The iter() and next() Built-in Functions

The built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
616 takes a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
617 (such as sequence) and returns an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
618 object. You can then use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
619 to iterate through the items. For example,

The reversed() Built-in Function

To iterate a sequence in the reverse order, apply the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
620 function which reverses the iterator over values of the sequence. For example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
391
The enumerate() Built-in Function

You can use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
621 to obtain the positional indexes, when looping through a sequence. For example,

Multiple Sequences and the zip() Built-in Function

To loop over two or more sequences concurrently, you can pair the entries with the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
622 built-in function. For examples,

Comprehension for Generating Mutable List, Dictionary and Set

List comprehension provides concise way to generate a new list. The syntax is

Ví dụ như,

Similarly, you can create dictionary and set (mutable sequences) via comprehension. For example,

Comprehension cannot be used to generate

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
49
50
51
52
12 and
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
49
50
51
52
14, as they are immutable and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
625 cannot be applied

Naming Conventions and Coding Styles (PEP 8 & PEP 257)

Naming Conventions

These are the recommended naming conventions in Python

  • Variable names. use a noun in lowercase words (optionally joined with underscore if it improves readability), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    626
  • Function names. use a verb in lowercase words (optionally joined with underscore if it improves readability), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    627 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    628
  • Class names. use a noun in camel-case (initial-cap all words), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    629,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    630,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    631
  • Constant names. use a noun in uppercase words joined with underscore, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    632,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    633
Coding Styles

Read

The recommended styles are

  • Use 4 spaces for indentation. Don't use tab
  • Lines shall not exceed 79 characters
  • Use blank lines to separate functions and classes
  • Use a space before and after an operator
  • [TODO] more

Functions

Syntax

In Python, you define a function via the keyword

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3955 followed by the function name, the parameter list, the doc-string and the function body. Inside the function body, you can use a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3981 statement to return a value to the caller. There is no need for type declaration like C/C++/Java

Cú pháp là

Example 1

Take note that you need to define the function before using it, because Python is interpretative

Example 2
ví dụ 3. Function doc-string

This example elaborates on the function's doc-string

  • The first line "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    636" specifies the type of the argument and return value. Python does not perform type check on function, and this line merely serves as documentation
  • The second line gives a description
  • Examples of function invocation follow. You can use the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    637 module to perform unit test for this function based on these examples (to be described in the "Unit Test" section
The pass statement

The

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
49
50
51
52
740 statement does nothing. It is sometimes needed as a dummy statement placeholder to ensure correct syntax, e. g. ,

Function Parameters and Arguments

Passing Arguments by Value vs. by Reference

In Python

  • Immutable arguments (such as integers, floats, strings and tuples) are passed by value. That is, a copy is cloned and passed into the function. The original cannot be modified inside the function
  • Mutable arguments (such as lists, dictionaries, sets and instances of classes) are passed by reference. That is, they can be modified inside the function

Ví dụ như,

Function Parameters with Default Values

You can assign a default value to the "trailing" function parameters. These trailing parameters having default values are optional during invocation. For example,

Another example,

In stead of hard-coding the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
639, it is more flexible to use a parameter with a default value, as follows

Positional and Keyword Arguments

Python functions support both positional and keyword (or named) arguments

Normally, Python passes the arguments by position from left to right, i. e. , positional, just like C/C++/Java. Python also allows you to pass arguments by keyword (or name) in the form of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
640. For example,

You can also mix the positional arguments and keyword arguments, but you need to place the positional arguments first, as shown in the above examples

Variable Number of Positional Parameters (*args)

Python supports variable (arbitrary) number of arguments. In the function definition, you can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
62 to pack all the remaining positional arguments into a tuple. For example,

Python supports placing

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642 in the middle of the parameter list. However, all the arguments after
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642 must be passed by keyword to avoid ambiguity. For example

Unpacking List/Tuple into Positional Arguments (*lst, *tuple)

In the reverse situation when the arguments are already in a list/tuple, you can also use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
62 to unpack the list/tuple as separate positional arguments. For example,

Variable Number of Keyword Parameters (**kwargs)

For keyword parameters, you can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
65 to pack them into a dictionary. For example,

Unpacking Dictionary into Keyword Arguments (**dict)

Similarly, you can also use ** to unpack a dictionary into individual keyword arguments

Using both *args and **kwargs

You can use both

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647 in your function definition. Place
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642 before
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647. For example,

Function Overloading

Python does NOT support Function Overloading like Java/C++ (where the same function name can have different versions differentiated by their parameters)

Giá trị trả về của hàm

You can return multiple values from a Python function, e. g. ,

It seems that Python function can return multiple values. In fact, a tuple that packs all the return values is returned

Recall that a tuple is actually formed through the commas, not the parentheses, e. g. ,

Types Hints via Function Annotations

From Python 3. 5, you can provide type hints via function annotations in the form of

The type hints annotations are usually ignored, and merely serves as documentation. But there are external library that can perform the type check

Read. "PEP 484 -- Type Hints"

Modules, Import-Statement and Packages

Modules

A Python module is a file containing Python codes - including statements, variables, functions and classes. It shall be saved with file extension of "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3938". The module name is the filename, i. e. , a module shall be saved as "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3938"

By convention, modules names shall be short and all-lowercase (optionally joined with underscores if it improves readability)

A module typically begins with a triple-double-quoted documentation string (doc-string) (available in

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
652), followed by variable, function and class definitions

Example. The greet Module

Create a module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
653 and save as "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
654" as follows

This

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
653 module defines a variable
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
656 and a function
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
657

The import statement

To use an external module in your script, use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693 statement

Once

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693ed, you can reference the module's attributes as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
660. You can use the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
661 to assign a new module name to avoid module name conflict

For example, to use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
653 module created earlier

The import statements should be grouped in this order

  1. Python's standard library
  2. Third party libraries
  3. Local application libraries

The from-import Statement

Cú pháp là

With the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
663 statement, you can reference the imported attributes using
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 directly, without qualifying with the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664

Ví dụ,

import vs. from-import

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
663 statement actually loads the entire module (like
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693 statement); and NOT just the imported attributes. But it exposes ONLY the imported attributes to the namespace. Furthermore, you can reference them directly without qualifying with the module name

For example, let create the following module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
668 for testing
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693 vs.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
663

Let's try out

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
392

Now, try the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
663 and note that the entire module is loaded, just like the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
693 statement

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
393

Conditional Import

Python supports conditional import too. For example,

sys. path and PYTHONPATH/PATH environment variables

The environment variable

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3937 shall include the path to Python Interpreter "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
675"

The Python module search path is maintained in a Python variable

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
676 of the
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
49
50
51
52
714 module, i. e.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
678. The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
678 is initialized from the environment variable
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680, plus an installation-dependent default. Theo mặc định, biến môi trường
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680 trống

Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
394

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
678 default includes the current working directory (denoted by an empty string), the standard Python directories, plus the extension directories in
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
683

The imported modules must be available in one of the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
678 entries

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
395

To show the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3937 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680 environment variables, use one of these commands

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
396

Reloading Module using imp. reload() or importlib. reload()

If you modify a module, you can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
687 function of the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
688 (for import) module to reload the module, for example,

NOTE. Since Python 3. 4, the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
688 package is pending deprecation in favor of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
690

Template for Python Standalone Module

The following is a template of standalone module for performing a specific task

When you execute a Python module (via the Python Interpreter), the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
683 is set to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
684. On the other hand, when a module is imported, its
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
683 is set to the module name. Hence, the above module will be executed if it is loaded by the Python interpreter, but not imported by another module

Example. [TODO]

Packages

A module contains attributes (such as variables, functions and classes). Relevant modules (kept in the same directory) can be grouped into a package. Python also supports sub-packages (in sub-directories). Packages and sub-packages are a way of organizing Python's module namespace by using "dotted names" notation, in the form of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
694

To create a Python package

  1. Create a directory and named it your package's name
  2. Put your modules in it
  3. Create a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 file in the directory

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695 marks the directory as a package. For example, suppose that you have this directory/file structure

If

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
697 is in your
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
698, you can import
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
699 as

Without the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695, Python will NOT search the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6001 directory for
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
699. Moreover, you cannot reference modules in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6001 directory directly (e. g. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6004) as it is not in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
698

Attributes in '__init__. py'

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695 file is usually empty, but it can be used to initialize the package such as exporting selected portions of the package under more convenient name, hold convenience functions, etc

The attributes of the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695 module can be accessed via the package name directly (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6008 instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6009). For example,

Sub-Packages

A package can contain sub-packages too. For example,

Clearly, the package's dot structure corresponds to the directory structure

Relative from-import

In the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
663 statement, you can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
660 to refer to the current package and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6012 to refer to the parent package. For example, inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6013, you can write

Take note that in Python, you write

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6014,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6015 by omitting the separating dot (instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6016,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6017)

Circular Import Problem

[LÀM]

Advanced Functions and Namespaces

Local Variables vs. Global Variables

Names created inside a function (i. e. within

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3955 statement) are local to the function and are available inside the function only

Names created outside all functions are global to that particular module (or file), but not available to the other modules. Global variables are available inside all the functions defined in the module. Global-scope in Python is equivalent to module-scope or file-scope. KHÔNG có phạm vi toàn mô-đun trong Python

Ví dụ,

Function Variables (Variables of Function Object)

In Python, a variable takes a value or object (such as

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
49
50
51
52
1,
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
49
50
51
52
3). It can also take a function. For example,

A variable in Python can hold anything, a value, a function or an object

In Python, you can also assign a specific invocation of a function to a variable. For example,

Nested Functions

Python supports nested functions, i. e. , defining a function inside a function. For example,

Sản lượng dự kiến ​​là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
397

Take note that the inner function has read-access to all the attributes of the enclosing outer function, and the global variable of this module

Lambda Function (Anonymous Function)

Lambda functions are anonymous function or un-named function. They are used to inline a function definition, or to defer execution of certain codes. The syntax is

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
398

Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6021 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6022 do the same thing. Take note that
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3981 keyword is NOT needed inside the lambda function. Instead, it is similar to evaluating an expression to obtain a value

Lambda function, like ordinary function, can have default values for its parameters

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
399

More usages for lambda function will be shown later

Multiple Statements?

Take note that the body of a lambda function is an one-liner

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6024. In other words, you cannot place multiple statements inside the body of a lambda function. You need to use a regular function for multiple statements

Functions are Objects

In Python, functions are objects (like instances of a class). Like any object,

  1. a function can be assigned to a variable;
  2. a function can be passed into a function as an argument; and
  3. a function can be the return value of a function, i. e. , a function can return a function
Example. Passing a Function Object as a Function Argument

A function name is a variable name that can be passed into another function as argument

Example. Returning an Inner Function object from an Outer Function
Example. Returning a Lambda Function

Function Closure

In the above example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 is not local to the lambda function. Instead,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 is obtained from the outer function

When we assign

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6027 to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6028,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 takes on the value of 8 during the invocation. But we expect
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 to go out of scope after the outer function terminates. If this is the case, calling
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6031 would encounter an non-existent
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009?

This problem is resolved via so called Function Closure. A closure is an inner function that is passed outside the enclosing function, to be used elsewhere. In brief, the inner function creates a closure (enclosure) for its enclosing namespaces at definition time. Hence, in

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6028, an enclosure with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6034 is created; while in
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6035, an enclosure with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6036 is created. Take note that Python only allows the read access to the outer scope, but not write access. You can inspect the enclosure via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6037, e. g. ,

Functional Programming. Using Lambda Function in filter(), map(), reduce() and Comprehension

Thay vì sử dụng vòng lặp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 để lặp qua tất cả các mục trong một
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6039 (dãy), bạn có thể sử dụng các hàm sau để áp dụng thao tác cho tất cả các mục. This is known as functional programming or expression-oriented programming. Filter-map-reduce is popular in big data analysis (or data science)

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6040. Return an
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6041 yielding those
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    626s of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    617 for which
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6044 is
    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
    49
    50
    51
    52
    5. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6046. Apply (or Map or Transform) the function func on each item of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    617. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6048 (in module
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6049). Apply the function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value, also known as aggregation. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6050. used frequently in big data analysis to obtain an aggregate value
  • List comprehension. a one-liner to generate a list as discussed in the earlier section. e. g. ,

These mechanism replace the traditional for-loop, and express their functionality in simple function calls. It is called functional programming, i. e. , applying a series of functions (filter-map-reduce) over a collection

Decorators

In Python, a decorator is a callable (function) that takes a function as an argument and returns a replacement function. Recall that functions are objects in Python, i. e. , you can pass a function as argument, and a function can return an inner function. A decorator is a transformation of a function. It can be used to pre-process the function arguments before passing them into the actual function; or extending the behavior of functions that you don't want to modify, such as ascertain that the user has login and has the necessary permissions

Example. Decorating an 1-argument Function

Notes

  1. The decorator
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6051 takes a 1-argument function as its argument, and returns an replacement 1-argument function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6052, with its argument
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010 clamped to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6054, before applying the original function
  2. In
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6055, we decorate the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6056 function and assign the decorated (replacement) function to the same function name (confusing?. ). After the decoration, the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6056 takes on a new decorated life
Example. Using the @ symbol

Using

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6055 to decorate a function is messy?. Instead, Python uses the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3963 symbol to denote the replacement. For example,

For Java programmers, do not confuse the Python decorator

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3963 with Java's annotation like
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6061

Example. Decorator with an Arbitrary Number of Function Arguments

The above example only work for one-argument function. You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
642 and/or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647 to handle variable number of arguments. For example, the following decorator log all the arguments before the actual processing

We can also modify our earlier

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6051 to handle an arbitrary number of arguments

The @wraps Decorator

Decorator can be hard to debug. This is because it wraps around and replaces the original function and hides variables like

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
683 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6066. This can be solved by using the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6067 of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6049, which modifies the signature of the replacement functions so they look more like the decorated function. For example,

Example. Passing Arguments into Decorators

Let's modify the earlier

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6069 decorator to take two arguments -
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6070 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6071 of the range

The decorator

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6069 takes the desired arguments and returns a wrapper function which takes a function argument (for the function to be decorated)

Confused?. Python có nhiều tính năng ưa thích không có trong các ngôn ngữ truyền thống như C/C++/Java

Namespace

Names, Namespaces and Scope

In Python, a name is roughly analogous to a variable in other languages but with some extras. Because of the dynamic nature of Python, a name is applicable to almost everything, including variable, function, class/instance, module/package

Names defined inside a function are local. Names defined outside all functions are global for that module, and are accessible by all functions inside the module (i. e. , module-global scope). There is no all-module-global scope in Python

A namespace is a collection of names (i. e. , a space of names)

A scope refers to the portion of a program from where a names can be accessed without a qualifying prefix. For example, a local variable defined inside a function has local scope (i. e. , it is available within the function, and NOT available outside the function)

Each Module has a Global Namespace

A module is a file containing attributes (such as variables, functions and classes). Each module has its own global namespace. Hence, you cannot define two functions or classes of the same name within a module. But you can define functions of the same name in different modules, as the namespaces are isolated

When you launch the interactive shell, Python creates a module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073, with its associated global namespace. All subsequent names are added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073's namespace

When you import a module via

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6075 under the interactive shell, only the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 is added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073's namespace. You need to access the names (attributes) inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
660. In other words, the imported module retains its own namespace and must be prefixed with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
660 inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073. (Recall that the scope of a name is the portion of codes that can access it without prefix. )

However, if you import an attribute via

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6082 under the interactive shell, the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 is added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073's namespace, and you can access the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 directly without prefixing with the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664

On the other hand, when you import a module inside another module (instead of interactive shell), the imported

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
664 is added into the target module's namespace (instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073 for the interactive shell)

The built-in functions are kept in a module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6089, which is imported into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073 automatically

The globals(), locals() and dir() Built-in Functions

You can list the names of the current scope via these built-in functions

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6091. return a dictionary (name-value pairs) containing the current scope's global variables
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6092. return a dictionary (name-value pairs) containing the current scope's local variables. If
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6092 is issued in global scope, it returns the same outputs as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6091
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6095. return a list of local names in the current scope, which is equivalent to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6096
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6097. return a list of the local names for the given object

Ví dụ,

To show the difference between locals and globals, we need to define a function to create a local scope. For example,

More on Module's Global Namespace

Let's create two modules.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099, where
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 imports
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099, as follows

Let's import

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 (which in turn import
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099) under the interpreter shell, and check the namespaces

Take note that the interpreter's current scope

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
683 is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6073. It's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 (imported). The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099 (imported) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6109. To refer to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099, you need to go thru
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098, in the form of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6112. The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6112's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6114

Now, let run

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 instead, under IDLE3, and check the namespaces

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
400

Take note that the current scope's

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6116 is again __main__, which is the executing module
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098. Không gian tên của nó chứa
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099 (đã nhập) và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6109

Name Resolution

When you ask for a name (variable), says

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010, Python searches the LEGB namespaces, in this order, of the current scope

  1. L. Local namespace which is specific to the current function
  2. E. for nested function, the Enclosing function's namespace
  3. G. Global namespace for the current module
  4. B. Built-in namespace for all the modules

If

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 cannot be found, Python raises a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6122

Modifying Global Variables inside a Function

Recall that names created inside a function are local, while names created outside all functions are global for that module. You can "read" the global variables inside all functions defined in that module. For example,

If you assign a value to a name inside a function, a local name is created, which hides the global name. For example,

To modify a global variable inside a function, you need to use a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3983 statement to declare the name global; otherwise, the modification (assignment) will create a local variable (see above). For example,

For nested functions, you need to use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3984 statement in the inner function to modify names in the enclosing outer function. For example,

To modify a global variable inside a nested function, declare it via

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3983 statement too. For example,

In summary,

  1. The order for name resolution (for names inside a function) is. local, enclosing function for nested
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3955, global, and then the built-in namespaces (i. e. , LEGB)
  2. However, if you assign a new value to a name, a local name is created, which hides the global name
  3. You need to declare via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3983 statement to modify globals inside the function. Similarly, you need to declare via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3984 statement to modify enclosing local names inside the nested function
More on global Statement

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3983 statement is necessary if you are changing the reference to an object (e. g. with an assignment). It is not needed if you are just mutating or modifying the object. For example,

In the above example, we modify the contents of the array. The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3983 statement is not needed

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
401

In the above example, we are modifying the reference to the variable.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3983 is needed, otherwise, a local variable will be created inside the function

Built-in Namespace

The built-in namespace is defined in the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6132 module, which contains built-in functions such as
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
49
50
51
52
04,
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
49
50
51
52
89,
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
49
50
51
52
88,
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
49
50
51
52
738,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
690,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
689 and etc. You can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6141 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6142 to list the attributes of the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6132 module

[LÀM]

del Statement

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
635 statement to remove names from the namespace, for example,

If you override a built-in function, you could also use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
635 to remove it from the namespace to recover the function from the built-in space

Xác nhận và xử lý ngoại lệ

assert Statement

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3995 statement to test a certain assertion (or constraint). For example, if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 is supposed to be
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695 in a certain part of the program, you can use the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3995 statement to test this constraint. An
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6150 will be raised if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 is not zero

Ví dụ,

The assertions are always executed in Python

Syntax

The syntax for

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3995 is

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
402

If the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6153 if
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
49
50
51
52
5, nothing happens; otherwise, an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6150 will be raised with the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6156

Exceptions

In Python, errors detected during execution are called exceptions. For example,

Whenever an exception is raised, the program terminates abruptly

try-except-else-finally

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6157 exception handling facility to prevent the program from terminating abruptly

Example 1. Handling Index out-of-range for List Access

Các kết quả đầu ra dự kiến ​​là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
403

The exception handling process for

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6157 is

  1. Python runs the statements in the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991-block
  2. If no exception is raised in all the statements of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991-block, all the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992-blocks are skipped, and the program continues to the next statement after the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6162 statement
  3. However, if an exception is raised in one of the statement in the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991-block, the rest of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991-block will be skipped. The exception is matched with the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992-blocks. The first matched
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992-block will be executed. The program then continues to the next statement after the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6162 statement, instead of terminates abruptly. Nevertheless, if none of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992-blocks is matched, the program terminates abruptly
  4. The
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3972-block will be executable if no exception is raised
  5. The
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3993-block is always executed for doing house-keeping tasks such as closing the file and releasing the resources, regardless of whether an exception has been raised
Syntax

The syntax for

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6157 is

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3991-block (mandatory) must follow by at least one
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3992 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3993 block. The rests are optional

CAUTION. Python 2 uses older syntax of "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6175", which should be re-written as "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6176" for portability

Example 2. Input Validation

raise Statement

You can manually raise an exception via the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3994 statement, for example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
404

Cú pháp là

A

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3994 without argument in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3992 block re-raise the exception to the outer block, e. g. ,

Built-in Exceptions

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6180,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6181,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6182. base classes
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6183. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6184,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6185,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6186
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6187
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6188. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    630,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6190
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6191. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6192,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6193
  • [TODO] more

User-defined Exception

You can defined your own exception by sub-classing the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6181 class

Example

with-as Statement and Context Managers

The syntax of the

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
49
50
51
52
780 statement is as follows

Python’s

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3979 statement supports the concept of a runtime context defined by a context manager. Trong lập trình, bối cảnh có thể được xem như một thùng để truyền thông tin xung quanh, tôi. e. , the state at a point in time. Context Managers are a way of allocating and releasing resources in the context

Example 1

This is equivalent to

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
405

The with-statement's context manager acquires, uses, and releases the context (of the file) cleanly, and eliminate a bit of boilerplate

However, the

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
49
50
51
52
780 statement is applicable to certain objects only, such as file; while
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6198 can be applied to all

Example 2

Frequently-Used Python Standard Library Modules

Python provides a set of standard library. (Many non-standard libraries are provided by third party. )

To use a module, use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6075 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6082 to import the entire module or a selected attribute. You can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6201 to list all the attributes of the module,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6202 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6202 to read the documentation page. For example,

math and cmath Modules

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6204 module provides access to the mathematical functions defined by the C language standard. The commonly-used attributes are

  • Constants.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6205,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4087
  • Power and exponent.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6207,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6208,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6209,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6210,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6211,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6212
  • Converting
    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
    49
    50
    51
    52
    2 to
    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
    49
    50
    51
    52
    1.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6215,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6216,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6217
  • 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
    49
    50
    51
    52
    2 operations.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6219,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6220
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6221 (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6222)
  • Conversion between degrees and radians.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6223,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6224
  • Trigonometric functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6225,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6226,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6227,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6228,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6229,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6230,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6231
  • Hyperbolic functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6232,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6233,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6234,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6235,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6236,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6237

Ví dụ như,

In addition, the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6238 module provides mathematical functions for complex numbers. See Python documentation for details

statistics Module

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6239 module computes the basic statistical properties such as mean, median, variance, and etc. (Many third-party vendors provide advanced statistics packages. ) For examples,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
406

random Module

The module

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
689 can be used to generate various pseudo-random numbers

Ví dụ như,

sys Module

The module

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
49
50
51
52
714 (for system) provides system-specific parameters and functions. The commonly-used are

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6242. exit the program by raising the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6243 exception. If used inside a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991, the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3993 clause is honored. The optional argument
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6246 can be an integer (default to 0 for normal termination, or non-zero for abnormal termination); or any object (e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6247)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    678. A
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 of module search-paths. Initialized from the environment variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680, plus installation-dependent default entries. See earlier example
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6251,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6252,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6253. standard input, output and error stream
  • 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
    49
    50
    51
    52
    771. Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 đối số dòng lệnh được chuyển vào tập lệnh Python.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6256 is the script name. See example below
Example. Command-Line Arguments

The command-line arguments are kept in

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
49
50
51
52
771 as a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604. For example, create the following script called "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6259"

Run the script

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
407

logging Module

The logging module

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 module supports a flexible event logging system for your applications and libraries

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 supports five levels

  1. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6262. Detailed information meant for debugging
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6263. Confirmation that an event takes place as expected
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6264. Something unexpected happened, but the application is still working
  4. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6265. The application does not work as expected
  5. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6266. Serious error, the application may not be able to continue

The logging functions are

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6267. Perform basic configuration of the logging system. The keyword arguments are.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6268,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6269 (default to append
    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
    49
    50
    51
    52
    751),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6271 (log this level and above), and etc
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6272,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6273,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6274,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6275,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6276. Log the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6277 at the specific level. The
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6278 are merged into
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6277 using formatting specifier
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6280. General logging function, at the given log
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6281
Basic Logging via logging. basicConfig()

Ví dụ,

The logging functions support

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6282-like format specifiers such as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6283,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6284, with values as function arguments (instead of via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66 operator in Python)

Run the script. A log file

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6286 would be created, with these records

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
408

By default, the log records include the log-level and logger-name (default of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6287) before the message

Getting the Log Level from a Configuration File

Log levels, such as

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6262 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6263, are stored as certain integers in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 module. For example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
409

The log level is typically read from a configuration file, in the form of a descriptive string. The following example shows how to convert a string log-level (e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6291) sang cấp nhật ký số (e. g. , 10) được sử dụng bởi mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260

Log Record Format

To set the log message format, use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6293 keyword

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0

where

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6294 for date/time,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6295 for log level,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6116 for logger name,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6297 for full-path filename (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6268 for filename only),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6299 (
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
49
50
51
52
1) for the line number, and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6301 for the log message

Advanced Logging. Logger, Handler, Filter and Formatter

So far, we presented the basic logging facilities. The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 library is extensive and organized into these components

  • Loggers. expose the methods to application for logging
  • Handlers. send the log records created by the loggers to the appropriate destination, such as file, console (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6253), email via SMTP, or network via HTTP/FTP
  • Filters. quyết định bản ghi nhật ký nào sẽ xuất ra
  • Formatters. specify the layout format of log records
Loggers

Để tạo một phiên bản

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6304, hãy gọi
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6305, trong đó tùy chọn
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6306 chỉ định tên trình ghi nhật ký (mặc định là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6287)

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6304's methods falls into two categories. configuration and logging

The commonly-used logging methods are.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6309,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6310,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6311,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6312,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6313 and the general
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6314

The commonly-used configuration methods are

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6315
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6316 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6317
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6318 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6319
xử lý

Thư viện ghi nhật ký cung cấp các trình xử lý như

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6320 (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6253,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6252),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6323,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6324 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6325 (email)

Các phương pháp thường được sử dụng là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6315. The logger's
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6315 determines which message levels to be passed to the handler; while the handler's
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6315 determines which message level to be sent to the destination
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6329. để định dạng tin nhắn được gửi đến đích
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6318 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6319

Bạn có thể thêm nhiều trình xử lý vào trình ghi nhật ký, có thể xử lý các cấp độ nhật ký khác nhau. Ví dụ: bạn có thể thêm một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6325 để nhận email cho cấp độ
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6333;

Trình định dạng

Đính kèm vào trình xử lý (thông qua

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6336) để định dạng thông điệp tường trình

Ví dụ. Sử dụng Logger với Console Handler và Formatter
  1. Có lẽ không có tiêu chuẩn cho định dạng bản ghi nhật ký (trừ khi bạn có một công cụ phân tích)?. Nhưng tôi khuyên bạn nên chọn một dấu phân cách trường không xuất hiện trong thông báo tường trình để dễ xử lý các bản ghi nhật ký (e. g. , xuất sang bảng tính)

Các kết quả đầu ra dự kiến ​​là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
1
Ví dụ. Sử dụng tệp nhật ký xoay vòng với RotatingFileHandler
  1. Chúng tôi giữ tất cả các tham số ghi nhật ký trong từ điển, thường được truy xuất từ ​​tệp cấu hình
  2. Trong hàm tạo của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6324,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6338 đặt giới hạn kích thước tệp nhật ký; . Cả
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6338 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6339 đều mặc định là 0. Nếu một trong hai bằng 0, cuộn qua không bao giờ xảy ra
  3. Ví dụ trên tạo ra 4 tệp nhật ký.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6345,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6346 đến
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6347. Tệp được ghi vào luôn là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6345. Khi tệp này được lấp đầy, nó được đổi tên thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6346;
Ví dụ. Sử dụng nhật ký email cho cấp độ quan trọng và luân phiên các tệp nhật ký cho cấp độ INFO
Ví dụ. Tách Nhật ký LỖI và Nhật ký INFO với Định dạng khác nhau

Mô-đun ConfigParser (Python 2) hoặc configparser (Python 3)

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
631 triển khai trình phân tích cú pháp tệp cấu hình cơ bản cho
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6356

Tệp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6356 chứa các cặp khóa-giá trị được sắp xếp theo từng phần và trông giống như

  1. Tệp cấu hình bao gồm các phần (được đánh dấu bằng tiêu đề
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6358). Một phần chứa các cặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6359 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6360. Các khoảng trắng ở đầu và cuối được cắt bớt từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6361. Dòng bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6363 là chú thích

Bạn có thể sử dụng

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
631 để phân tích cú pháp tệp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6356, e. g. ,

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6366. đọc và phân tích từ danh sách tên tệp. Nó ghi đè các khóa với mỗi tệp liên tiếp, nếu có
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6367. lấy giá trị của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6368 từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6369
Interpolation with SafeConfigParser

A

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6361 may contain formatting string in the form of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6371, which refers to another
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6116 in the SAME section, or a special
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6373 (in uppercase) section. This interpolation feature is, however, supported only in
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6374. For example, suppose we have the following configuration file called
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6375

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
2

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
656 will be interpolated as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6377, interpolated from the SAME section and DEFAULT section

datetime Module

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6378 module supplies classes for manipulating dates and time in both simple and complex ways

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6379. Return the current local date

smtplib and email Modules

The SMTP (Simple Mail Transfer Protocol) is a protocol, which handles sending email and routing email between mail servers. Python provides a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6380 module, which defines an SMTP client session object that can be used to send email to any Internet machine with an SMTP listener daemon

To use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6380

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6382 module can be used to construct an email message

[TODO] more

json Module

JSON (JavaScript Object Notation) is a lightweight data interchange format inspired by JavaScript object literal syntax. The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6383 module provides implementation for JSON encoder and decoder

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6384. Serialize
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 to a JSON-encoded string ('s' for string)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6386. Create a Python object from the given JSON-encoded string
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6387. Serialize
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 to the file
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6389. Create a Python object by reading the given file

Ví dụ,

pickle and cPickle Modules

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6383 module (described earlier) handles lists and dictionaries, but serializing arbitrary class instances requires a bit of extra effort. On the other hand, the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391 module implements serialization and de-serialization of any Python object. Pickle is a protocol which allows the serialization of arbitrarily complex Python objects. It is specific to the Python languages and not applicable to other languages

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391 module provides the same functions as the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6383 module

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6394. Return the pickled representation of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 as a string
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6396. Construct a Python object from
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6397
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6398. Write a pickled representation of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6400
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6401. Construct a Python object reading from the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6400

The module

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6403 is an improved version of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391

signal module

Signals (software interrupt) are a limited form of asynchronous inter-process communication, analogous to hardware interrupts. It is generally used by the operating system to notify processes about certain issues/states/errors, like division by zero, etc

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6405 module provides mechanisms to use signal handlers in Python

signal. signal()

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6406 method takes two arguments. the signal number to handle, and the handling function. For example,

What is the formula of sum of prime numbers?

How to find the sum of prime numbers up to a prime number n, that is for example. the sum of prime numbers up to 7 is. 2+3+5+7=17 .

Có chức năng Isprime trong Python không?

Dưới đây là danh sách các chức năng này. isprime(n). It tests if n is a prime number (True) or not (False) . primerange(a, b). It generates a list of all prime numbers in the range [a, b). randprime(a, b). It returns a random prime number in the range [a, b).

Cách nhanh nhất để lấy số nguyên tố trong Python là gì?

Để tìm số nguyên tố trong Python, bạn phải lặp giá trị từ đầu đến cuối bằng vòng lặp for và với mọi số, nếu số đó lớn hơn 1, hãy kiểm tra xem . Nếu chúng tôi tìm thấy bất kỳ số nào khác chia hết, hãy in giá trị đó .

Công thức tìm số nguyên tố trong Python là gì?

from math import sqrt # Số cần kiểm tra nguyên tố n = 9 flag = 0 if(n > 1). cho k trong phạm vi (2, int (sqrt (n)) + 1). nếu (n% k == 0). cờ = 1 ngắt nếu (cờ == 0). print(n," là một số nguyên tố. ") khác. print(n," không phải là số nguyên tố. ") khác. print(n," không phải là số nguyên tố