In tin nhắn trong chuyên gia gán python

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 một 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

Syntax Summary and Comparison

  • Comment. 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. Python's string can be delimited by either 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 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. A variables is created via the initial assignment. 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. , a variable can hold object of any types.
    (In C/C++/C#/Java, you need to declare the name and type of a variable before using it. )
  • Data Types. Python support these data types.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (integers),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (floating-point numbers),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (String),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 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
    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), and more
  • Statements. Python's statement ends with a newline.
    (C/C++/C#/Java's statement ends with a semi-colon (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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))
  • Compound Statements and Indentation. Python uses indentation to indicate body-block. (C/C++/C#/Java use 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. ) This syntax forces you to indent the program correctly which is crucial for reading your program. You can use space or tab for indentation (but not mixture of both). Each body level must be indented at the same distance. It is recommended to use 4 spaces for each level of indentation
  • Assignment Operator.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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
  • Arithmetic Operators.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 (add),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61 (subtract),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (multiply),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63 (divide),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64 (integer divide),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65 (exponent),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66 (modulus). (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68 are not supported)
  • Compound Assignment Operators.
    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
  • Comparison 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,
    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
  • Logical 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++/C#/Java use
    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)
  • Conditional
  • Loop. Python does NOT support the traditional C-like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602-loop with index.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    603
  • List. Python supports variable-size dynamic array via a built-in data 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
    605
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606. List is similar to C/C++/C#/Java's array but NOT fixed-size. You can refer to an element via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    608, or sub-list via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    609. You can use built-in functions such as
    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
  • Data Structures
    • List.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      613 (mutable dynamic array)
    • Tuple. ________ 4614 (Mảng 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 sơ đồ 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. ')) (Line 66, 72). You can read input from standard input device (default to keyboard) via the built-in
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680 function. As the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680 function returns a string, we need to cast it 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
  10. if __name__ == '__main__'. (Line 87). When you execute a Python module via the Python Interpreter, the global variable
    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 into another module, 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. This is a good practice for testing a module

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

This is a number guessing game. It illustrates nested-if (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
686),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647-loop 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
4 flag, and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
689 module. For example,

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 (Line 12). We are going to use
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    689 module's
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    692 function to generate a secret number. In Python, you need to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 the module (external library) before using it
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    694 (Line 15). Generate a random integer between
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    696 (both inclusive)
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    697 (Line 17). Python supports 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 type for boolean values 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
    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. We use this boolean flag to control our
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647-loop
  4. The syntax for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647-loop (Line 19) is
  5. The syntax for nested-
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (Line 22) is

Exmaple magic_number. py - Check if Number Contains a Magic Digit

This example prompts user for a number, and check if the number contains a magic digit. This example illustrate function,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 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 operations. For example,

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. We organize the program into functions
  2. We implement two versions of function to check for magic number - an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 version (Line 10) and 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 version (Line 25) - for academic purpose
  3. def isMagic(number. int, magicDigit. int = 8) -> bool. (Line 10). The hightlight parts are known as type hint annotations. They are ignored by Python Interpreter, and merely serves as documentation
  4. if __name__ == '__main__'. (Line 51). When you execute a Python module via the Python Interpreter, the global variable
    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 into another module, 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. Đây là một thực hành tốt để thử nghiệm một mô-đun

Example hex2dec. py - Hexadecimal To Decimal Conversion

This example prompts user for a hexadecimal (hex) string, and print its decimal equivalent. It illustrates for-loop with index, nested-if, string operation and dictionary (associative array). For example,

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. The conversion formula 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
    711, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (Line 12). We are going to 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
    714 module'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
    715 function to terminate the program for invalid input. In Python, we need to import the module (external library) before using it
  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 (Line 21). Python does not support the traditional C-like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602-loop with index. It supports only
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 loop to iterate through each
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 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
    720. We use the built-in function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    657 to generate 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
    722, and then iterate through each item in the generated list
  4. In Python, we can iterate through each character of a string via the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655 loop, e. g. , For this example, we cannot use the above as we need the index of the character to perform conversion
  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 (Line 22). In Python, you can use indexing operator
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to extract 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
    726-th character. Take note that Python does not support character, but treat character as a 1-character string
  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 (Line 23). Python supports exponent (or power) operator in the form of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65. Take note that string index begins from 0, and increases from left-to-right. On the other hand, the hex digit's exponent begins from 0, but increases from right-to-left
  7. There 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
    729 cases of 1-character strings 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
    730,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    731,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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, and other, which can be handled by 5 cases of nested-if as follows
    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 (Line 24). we convert the string
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 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
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 via
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 built-in function
    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 (Line 26). no nothing. In Python, you need to include a dummy statement 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
      740 (Line 28) in the body block
    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 (Line 28). To convert 1-character string
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 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
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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, we use 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
      745 built-in function to get the 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 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
      747, subtract by the base
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 and add 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 (Line 30). Python supports a data structure called dictionary (associative array), which contains key-value pairs. We created a dictionary
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 (Line 15) to map
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 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
      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 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
      754, and so on. We can then reference the dictionary via
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 to retrieve its value (Line 31)
    5. khác (Dòng 32). we 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
      756 to terminate the program. We return a non-zero code to indicate abnormal termination

Example bin2dec. py - Binary to Decimal Conversion

This example prompts user for a binary string (with input validation), and print its decimal equivalent. For example,

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. We organize the code in functions
  2. The conversion formula 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
    757, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. You can use built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to convert a number string from the given
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to decimal (Line 38)

Example dec2hex. py - Decimal to Hexadecimal Conversion

This program prompts user for a decimal number, and print its hexadecimal equivalent. For example,

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
Làm thế nào nó hoạt động
  1. We use the modulus/division repeatedly to get the hex digits in reverse order
  2. We use a look-up list (Line 11) to convert
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to hex digit
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. You can use built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to convert decimal to hexadecimal, octal and binary, respectively; or use the more general
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. E. g. ,

Example wc. py - Số từ

This example reads a filename from command-line and prints the line, word and character counts (similar 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
768 utility in Unix). It illustrates the text file input and text string processing

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60
How it works
  1. import sys (Line 14). We use 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 (@ https. //docs. python. org/3/library/sys. html) from the Python's standard library to retrieve the command-line arguments kept in
    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, and to terminate the program via
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. In Python, you need to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 the module before using it
  2. The command-line arguments are stored in a variable
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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, which is a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 (Python's dynamic array). The first item of the
    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 is the script name, followed by the other command-line arguments
  3. if len(sys. argv) . = 2. (Line 15). We use the built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 to verify that the length of the command-line-argument
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 is 2
  4. with open(sys. argv[1]) as infile. (Line 25). We open the file via 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
    780 statement, which closes the file automatically upon exit
  5. for line in infile. (Line 26). We use 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
    781 loop (Line 29) to process each
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 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
    783, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 belong to the built-in class "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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" (meant for string support @ https. //docs. python. org/3/library/stdtypes. html#str). We use 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' member 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
    787 to strip the leading and trailing white spaces; 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
    788 to split the string into a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 of words
  6. We also invoke the Unix utility "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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" via external shell command in 2 ways. via
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 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
    792

Example htmlescape. py - Escape Reserved HTML Characters

This example reads the input and output filenames from the command-line and replaces the reserved HTML characters by their corresponding HTML entities. It illustrates file input/output and string substitution

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
64
How it works
  1. import sys (Line 14). We
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 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 (@ https. //docs. python. org/3/library/sys. html). We retrieve the command-line arguments from the
    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, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 is the script name; and 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
    772 (Line 18) to terminate the program
  2. with open(sys. argv[1]) as infile, open(sys. argv[2], 'w') as outfile. (Line 21). We use 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, which closes the files automatically at exit, to open 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
    783 for read (default) and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3901 for write (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3902)
  3. for line in infile. (Line 22). We use 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
    781 loop to process each
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 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
    783, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 belongs to the built-in class "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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" (meant for string support @ https. //docs. python. 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. Python 3. 2 introduces a new
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3911 module, with a function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3912 to escape HTML reserved characters.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65

Example files_rename. py - Rename Files

This example renames all the files in the given directory using regular expression (regex). It illustrates directory/file processing (using module

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3913) and regular expression (using module
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
How it works

Introduction

Python is created by Dutch Guido van Rossum around 1991. Python is an open-source project. The mother site is www. python. org

The main features of Python are

  • Python is an easy and intuitive language. Python scripts are easy to read and understand
  • Python (like Perl) is expressive. A single line of Python code can do many lines of code in traditional general-purpose languages (such as C/C++/Java)
  • Python is free and open-source. It is cross-platform and runs on Windows, Linux/UNIX, and Mac OS X
  • Python is well suited for rapid application development (RAD). You can code an application in Python in much shorter time than other general-purpose languages (such as C/C++/Java). Python can be used to write small applications and rapid prototypes, but it also scales well for developing large-scale project
  • Python is a scripting language and dynamically typed. Like most of the scripting languages (e. g. , Perl, JavaScript), Python associates types with objects, instead of variables. That is, a variable can be assigned a value of any type, a list (array) can contain objects of different types
  • Python provides automatic memory management. You do not need to allocate and free memory in your programs
  • Python provides high-level data types such as dynamic array and dictionary (or associative array)
  • Python is object-oriented
  • Python is not a fully compiled language. Nó được biên dịch thành mã byte nội bộ, sau đó được diễn giải. Hence, Python is not as fast as fully-compiled languages such as 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

  • Python 1. phiên bản ban đầu
  • Python 2. released in 2000, with many new features such as garbage collector and support for Unicode
  • Python 3 (Python 3000 or py3k). A major upgrade released in 2008. Python 3 is NOT backward compatible with Python 2
Python 2 or Python 3?

Currently, two versions of Python are supported in parallel, version 2. 7 and version 3. 5. There are unfortunately incompatible. This situation arises because when Guido Van Rossum (the creator of Python) decided to bring significant changes to Python 2, he found that the new changes would be incompatible with the existing codes. He decided to start a new version called Python 3, but continue maintaining Python 2 without introducing new features. Python 3. 0 was released in 2008, while Python 2. 7 in 2010

AGAIN, TAKE NOTE THAT PYTHON 2 AND PYTHON 3 ARE NOT COMPATIBLE. You need to decide whether to use Python 2 or Python 3. Start your new projects using Python 3. Use Python 2 only for maintaining legacy projects

To check the version of your Python, issue this command

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

Installation and Getting Started

Installation

For Newcomers to Python (Windows, Mac OSX, Ubuntu)

I suggest you install "Anaconda distribution" of Python 3, which includes a Command Prompt, IDEs (Jupyter Notebook and Spyder), and bundled with commonly-used packages (such as NumPy, Matplotlib and Pandas that are used for data analytics)

Goto Anaconda mother site (@ https. //www. anaconda. com/) ⇒ Choose "Anaconda Distribution" Download ⇒ Choose "Python 3. x" ⇒ Follow the instructions to install

Check If Python Already Installed and its Version

To check if Python is already installed and its the version, issue the following command. ,

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

Both the Python 3 and Python 2 should have already installed by default. Otherwise, you can install Python via

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

To verify the Python installation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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ổ

You could install either

  1. "Anaconda Distribution" (See previous section)
  2. Plain Python from Python Software Foundation @ https. //www. con trăn. org/download/, download the 32-bit or 64-bit MSI installer, and run the downloaded installer
  3. Under the Cygwin (Unix environment for Windows) and install Python (under the "devel" category)
Mac OS X

[TODO]

Documentation

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. org

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

Start the Interactive Python Interpreter

You can run the "Python Interpreter" in interactive mode under a "Command-Line Shell" (such as Anaconda Prompt, Windows' CMD, Mac OS X's Terminal, Ubuntu's Bash Shell)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

The Python's command prompt is denoted as

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3915. You can enter Python statement at the Python's command prompt, 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

To exit Python Interpreter

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 and 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 followed by Enter

Writing and Running Python Scripts

First Python Script - hello. py

Use a programming text editor to write the following Python script and save as "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" in a directory of your choice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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. By convention, Python script (module) filenames are in all-lowercase (e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3920)
  2. EOL Comment. Statements beginning with a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3921 until the end-of-line (EOL) are comments
  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 sơ đồ 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. """ hello . """ (Line 3-5). The script begins by the so-called doc-string to provide the documentation for this Python module. Doc-string is typically a multi-line string (delimited by triple-single or triple-double quoted), which can be extracted from the source file to create documentation
  6. Variables. We create variables
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3922,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3923,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3924,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3925,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3926 (Line 6, 8, 10, 12, 14) by assignment values into them
  7. Python's strings can be enclosed with single quotes
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7 (Line 6) or double quotes
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8
  8. Python's integer is unlimited in size (Line 8)
  9. Python support floating-point numbers (Line 10)
  10. Python supports complex numbers (Line 12) and other high-level data types
  11. Python supports a dynamic array called list (Line 14), represented by
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3929. The element can be retrieved via index
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 (Line 15)
  12. print(aVar). The
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    662 function can be used to print the value of a variable to the console
Expected Output

The expected outputs 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
74
Running Python Scripts

You can develop/run a Python script in many ways - explained in the following sections

Running Python Scripts in Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)

You can run a python script via the Python Interpreter under the Command-Line Shell

Unix's Executable Shell Script

In Linux/Mac OS X, you can turn a Python script into an executable program (called Shell Script or Executable Script) by

  1. Start with a line beginning with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3932 (called "hash-bang" or "she-bang"), followed by the full-path name to the Python Interpreter, e. g. , To locate the Python Interpreter, use command "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3933" or "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3934"
  2. Make the file executable via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3935 (change file mode) command
  3. You can then run the Python script just like any executable programs. The system will look for the Python Interpreter from the she-bang line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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

The drawback is that you have to hard code the path to the Python Interpreter, which may prevent the program from being portable across different machines

Alternatively, you can use the following to pick up the Python Interpreter from the environment

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3936 utility will locate the Python Interpreter (from the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3937 entries). This approach is recommended as it does not hard code the Python's path

Windows' Exeutable Program

In Windows, you can associate "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3938" file extension with the Python Interpretable, to make the Python script executable

Running Python Scripts inside Python's Interpreter

To run a script "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" inside Python's Interpreter

  • You can use either absolute or relative path for the filename. 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?
  • The
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3941 built-in function opens the file, in default read-only mode; the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3942 function reads the entire file

Interactive Development Environment (IDE)

Using an IDE with graphic debugging can greatly improve on your productivity

For beginners, I recommend

  1. Python Interpreter (as described above)
  2. Python IDLE
  3. Jupyter Notebook (especially for Data Analytics)

For Webapp developers, I recommend

  1. Eclipse with PyDev
  2. PyCharm

See "Python IDE and Debuggers" for details

Python Basic Syntaxes

Comments

A Python comment begins with a hash sign (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3921) and last till the end of the current line. Comments are ignored by the Python Interpreter, but they are critical in providing explanation and documentation for others (and yourself three days later) to read your program. Use comments liberally

There is NO multi-line comment in Python?. (C/C++/Java supports multi-line comments via

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

Statements

A Python statement is delimited by a newline. A statement cannot cross line boundaries, except

  1. An expression in parentheses
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3945, square bracket
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946, and 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 can span multiple lines
  2. A backslash (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3948) at the end of the line denotes continuation to the next line. This is an old rule and is NOT recommended as it is error-prone

Unlike C/C++/C#/Java, you don't place a semicolon (

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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) at the end of a Python statement. But you can place multiple statements on a single line, separated by semicolon (
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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). For examples,

Block, Indentation and Compound Statements

A block is a group of statements executing as a unit. Unlike C/C++/C#/Java, which use 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 to group statements in a body block, Python uses indentation for body block. In other words, indentation is syntactically significant in Python - the body block must be properly indented. This is a good syntax to force you to indent the blocks correctly for ease of understanding

A compound statement, such as conditional (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645), loop (
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) and function definition (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3955), begins with a header line terminated with a colon (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667); followed by the indented body block, as follows

For examples,

Python does not specify how much indentation to use, but all statements of the SAME body block must start at the SAME distance from the right margin. You can use either space or tab for indentation but you cannot mix them in the SAME body block. It is recommended to use 4 spaces for each indentation level

The trailing colon (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667) and body indentation is probably the most strange feature in Python, if you come from C/C++/C#/Java. Python imposes strict indentation rules to force programmers to write readable codes

Variables, Identifiers and Constants

Like all programming languages, a variable is a named storage location. A variable has a name (or identifier) and holds a value

Like most of the scripting interpreted languages (such as JavaScript/Perl), Python is dynamically typed. You do NOT need to declare a variable before using it. A variables is created via the initial assignment. (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. )

For example,

In tin nhắn trong chuyên gia gán python

As mentioned, Python is dynamic typed. 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. , a variable can hold object of any types, as shown in the above examples

Rules of Identifier (Names)

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 has 35 reserved words, or keywords, which cannot be used as identifiers

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 and special literals)
  • 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 (flow control)
  • 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 (function)
  • 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 (error handling)
  • 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
Quy ước đặt tên biến

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ừ. There are two convenctions

  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, v.v.
  2. Trong cái gọi là trường hợp lạc đà trong đó từ đầu tiên được viết thường và các từ còn lại được viết hoa ban đầu, 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 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4007. (Đây là quy ước đặt tên của Java. )
khuyến nghị
  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, nhưng không phải là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4009 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010, để lưu trữ số học sinh. Viết tắt cũng được mà e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4011 cho chỉ mục
  2. Không sử dụng những tên vô nghĩa như
    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 (mục đích của bài tập này là gì?) và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4024 (Ví dụ này nói về cái gì?)
  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. Exceptions are common names like
    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 for coordinates,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015 for index. Tên dài khó gõ hơn, nhưng hãy tự ghi lại chương trình của bạn. (Tôi khuyên bạn nên dành thời gian luyện tập đánh máy đôi khi. )
  4. Sử dụng danh từ số ít và số nhiều một cách thận trọng để phân biệt giữa biến số ít và số nhiều. Ví dụ: bạn có thể sử dụng biến
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4035 để chỉ một số hàng và biến
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4036 để chỉ nhiều hàng (chẳng hạn như danh sách các hàng - sẽ được thảo luận sau)
hằng số

Python không hỗ trợ hằng số, nơi không thể sửa đổi nội dung của nó. (C hỗ trợ hằng qua từ khóa

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

Đó là một quy ước để đặt tên một biến bằng chữ hoa (nối với gạch dưới), 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

Loại dữ liệu. Số, Chuỗi và Danh sách

Python hỗ trợ nhiều loại số khác nhau, chẳng hạn 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 (đối với số nguyên như
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 (đối với số dấu phẩy động như
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) 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
4 (đối với 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 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
6)

Python hỗ trợ chuỗi văn bản (một chuỗi ký tự). Trong Python, các chuỗi có thể được phân tách bằng dấu nháy đơn hoặc nháy kép, 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 hỗ trợ cấu trúc mảng động có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, ký hiệu là
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.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 của Python tương tự như mảng của C/C++/Java, nhưng nó KHÔNG có kích thước cố định và có thể được mở rộng linh hoạt trong thời gian chạy

Tôi sẽ mô tả chi tiết các kiểu dữ liệu này trong phần sau

Đầ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. Ví dụ,

in()

Hàm tích hợp sẵn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 có chữ ký sau

For examples,

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

Bạn có thể sử dụng đối số từ khóa tùy chọn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4062 để đặt chuỗi phân tách (mặc định là khoảng trắng) và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4063 cho chuỗi kết thúc (mặc định là dòng mới). Ví dụ như,

in bằng Python 2 so với Python 3

Nhớ lại rằng Python 2 và Python 3 KHÔNG tương thích. Trong Python 2, bạn có thể sử dụng "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4064" mà không cần dấu ngoặc đơn (vì
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4065 là một từ khóa trong 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ụ,

Quan trọng. Luôn sử dụng hàm

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 với dấu ngoặc đơn, để có tính di động

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. Các loại dữ liệu cấp cao hơn, chẳng hạn như Số thập phân và Phân số, được hỗ trợ bởi các mô-đun bên ngoài

Bạn có thể sử dụng hàm có sẵn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4068 để kiểm tra loại biến hoặc ký tự

Loại số

Python hỗ trợ các kiểu số dựng sẵn này

  1. Số nguyên (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
    1). e. g. , ________ 14042, ________ 14043. Không giống như C/C++/Java, số nguyên có kích thước không giới hạn trong Python. Ví dụ: Bạn cũng có thể biểu thị số nguyên ở dạng thập lục phân với tiền tố
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4072 (hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4073); . 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. Số dấu phẩy động (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
    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, với dấu thập phân và số mũ tùy chọn (ký hiệu là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4087 hoặc
    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
    2 là các số dấu phẩy động có độ chính xác kép 64 bit. Ví dụ,
  3. Booleans (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). 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. Lưu ý cách viết hoa đầu dòng. 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 cũng có thể đóng vai trò là số nguyên trong các phép toán số học với
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 cho
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 cho
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. Các loại số khác được cung cấp bởi các mô-đun bên ngoài, chẳng hạn như mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    11 cho các số có dấu phẩy cố định thập phân, mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    12 cho các số hữu tỷ

Dynamic Typing and Assignment Operator

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

Python associates types with objects, instead of variables. That is, a variable does not have a fixed type and can be assigned an object of any type. A variable simply provides a reference to an object

You do not need to declare a variable before using a variable. A variable is created automatically when a value is first assigned, which links the assigned object to the variable

You can use built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
13 to get the object type referenced by a variable

Type Casting. int(x), float(x), str(x)

You can perform type conversion (or type casting) via built-in functions

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, etc. For example,

In summary, a variable does not associate with a type. Instead, a type is associated with an object. 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. 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
77
The Assignment Operator (=)

In Python, you do not need to declare variables before using the variables. Phép gán ban đầu tạo một biến và liên kết giá trị được gán với biến. For example,

Pair-wise Assignment and Chain Assignment

For example,

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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
21 được hiểu là
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,

Phép toán số

Arithmetic Operators (+, -, *, /, //, **, %)

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 (tầng. )
-8. 9 // -2. 5 ⇒ 3. 0**Nhị phân ** yLũy thừa2 ** 5 ⇒ 32
1. 2**3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus (Phần dư)9 % 2 ⇒ 1
-9 % 2 ⇒ 1
9 % -2 ⇒ -1
-9 % -2 ⇒ -1
9.9 % 2.1 ⇒ 1.5
-9. 9 % 2. 1 ⇒ 0. 6000000000000001
Toán tử gán hợp chất (+=, -=, *=, /=, //=, **=, %=)

Mỗi toán tử số học có một phép gán tốc ký tương ứng, 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 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
65. Ví dụ:
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 giống như
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32

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

Python không hỗ trợ các toán tử tăng (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67) và giảm (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68) (như trong C/C++/Java). Bạn cần sử dụng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32 hoặc
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 để tăng

Python chấp nhận

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
37 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
38. Đừng mắc bẫy này. Nhưng Python đánh dấu lỗi cú pháp cho
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
39 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
40

Hoạt động kiểu hỗn hợp

Đối với các hoạt động kiểu hỗn hợp, 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

Thí dụ. [LÀM]

Toán tử logic (and, or, not)

Python hỗ trợ các toán tử logic (boolean) này, hoạt động trên các giá trị boolean

Toán tửModeUsageDes mô tảExampleandBinaryx và yLogical ANDorBinaryx hoặc yLogical ORnotUnarynot xLogical NOT

ghi chú

  • Các toán tử logic của Python được gõ bằng chữ, không giống như C/C++/Java sử dụng các ký hiệu
    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
  • Python không có toán tử boolean dành riêng-hoặc (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62)

Thí dụ. [LÀM]

Chức năng tích hợp sẵn

Python cung cấp nhiều hàm dựng sẵn cho các số, bao gồm

  • 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, v.v.
  • 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, v.v.
  • 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

For examples,

Toán tử Bitwise (Nâng cao)

Python hỗ trợ các toán tử bitwise này

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. nhị phân. ybitwise ORx. y ⇒ 0b10001111~Unary~xbitwise NOT (hoặc phủ định)~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110<

Sợi dây

Trong Python, các chuỗi có thể được phân định bằng một cặp 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 (_______08). Python cũng hỗ trợ các chuỗi nhiều dòng thông qua ba dấu ngoặc đơn (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
9) hoặc ba dấu ngoặc ké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
0)

Để đặt một trích dẫn đơn (________ 080) bên trong một chuỗi trích dẫn đơn, bạn cần sử dụng chuỗi thoát ________ 081. Tương tự, để đặt dấu ngoặc kép (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
82) bên trong chuỗi dấu ngoặc kép, hãy sử dụng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
83. Không cần trình tự thoát để đặt một trích dẫn đơn bên trong một chuỗi trích dẫn kép;

Chuỗi trích dẫn ba lần đơn hoặc ba lần trích dẫn có thể kéo dài trên nhiều dòng. There is no need for escape sequence to place a single/double quote inside a triple-quoted string. Các chuỗi trích dẫn ba lần rất hữu ích cho tài liệu nhiều dòng, HTML và các mã khác

Python 3 sử dụng bộ ký tự Unicode để hỗ trợ quốc tế hóa (i18n)

Trình tự thoát cho ký tự (\code)

Giống như C/C++/Java, bạn cần sử dụng các chuỗi thoát (dấu gạch chéo ngược + mã) cho

  • Các ký tự đặc biệt không in được, chẳng hạn như tab (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    84), xuống dòng (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    85), xuống dòng (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    86)
  • Giải quyết sự mơ hồ, chẳng hạn như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    83 (đối với
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    82 bên trong chuỗi trích dẫn kép),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    81 (đối với
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    80 bên trong chuỗi trích dẫn đơn),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    91 (đối với
    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 cho ký tự ở giá trị hex và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    94 cho giá trị bát phân
  • 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)
Chuỗi thô (r'. ' hoặc r". ")

Bạn có thể đặt trước một chuỗi bằng

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
97 để vô hiệu hóa việc giải thích các chuỗi thoát (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
98), tôi. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
99 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
00 (hai ký tự) thay vì dòng mới (một ký tự). Các chuỗi thô được sử dụng rộng rãi trong regex (sẽ được thảo luận trong phần mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3914)

Chuỗi là bất biến

Chuỗi là bất biến, tôi. e. , nội dung của chúng không thể được sửa đổi. Các hàm chuỗi 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
02,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3910 trả về một đối tượng chuỗi mới thay vì sửa đổi chuỗi đang hoạt động

Hàm và toán tử tích hợp cho chuỗi

Bạn có thể thao tác trên các chuỗi bằng cách sử dụng

  • các chức năng tích hợp sẵn 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
    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

Ghi chú. Các hàm và toán tử này có thể áp dụng cho tất cả các loại dữ liệ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
11 bao gồ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
12,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 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
14 (sẽ được thảo luận sau)

Function /
OperatorUsageDescriptionExamples
s = 'Xin chào'len()len(str)Lengthlen(s) ⇒ 5insubstr trong 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 * đếm
str *= đếmLặp lại * 2 ⇒ 'Xin chàoXin chào'[i]
[-i .
str[-i]Indexing to get a character.
Chỉ mục phía trước bắt đầu từ
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695;
chỉ mục phía sau bắt đầu 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
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. bước]
[m. n]
[m. ]
[. n]
[. ]str[m. n. bước]
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.
Mặc định 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
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[. ] ⇒ 'Xin chào'
s[0. 5. 2] ⇒ 'Xin chào'

For examples,

Loại ký tự?

Python không có kiểu dữ liệu ký tự chuyên dụng. Một ký tự chỉ đơn giản là một chuỗi có độ dài 1. Bạn có thể sử dụng toán tử lập chỉ mục để trích xuất từng ký tự từ một chuỗi, như minh họa trong ví dụ trên;

Các 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
29 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
30 hoạt động trên ký tự, e. g. ,

Unicode so với ASCII

Trong Python 3, các chuỗi được mặc định là Unicode. Chuỗi ASCII được biểu diễn dưới dạng chuỗi byte, có tiền tố là

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

Trong Python 2, các chuỗi được mặc định là chuỗi ASCII (chuỗi byte). Các chuỗi Unicode có tiền tố 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
33

Bạn phải luôn sử dụng Unicode để quốc tế hóa (i18n)

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

Python hỗ trợ các chuỗi thông qua một lớp dựng sẵn 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
3 (Chúng tôi sẽ mô tả lớp này trong chương Lập trình hướng đối tượ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
3 cung cấp nhiều hàm thành viên. 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. Các hàm thành viên thường được sử dụng như sau, giả sử rằ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
36 là một đối 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
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
    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. Trả về một chữ hoa/chữ thường tương ứ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
    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. Kiểm tra xem chuỗi có phải là chữ hoa/chữ thường khô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
    38____649
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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). Sử dụng str. hàm định dạng ()

Có một số cách để tạo một chuỗi được định dạng cho đầu ra. Python 3 giới thiệu một phong cách mới trong hàm thành viê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
767 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
3 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
8 làm trình giữ chỗ (được gọi là trường định dạng). Ví dụ như,

Khi bạn chuyển các danh sách, bộ dữ liệu hoặc từ điển (sẽ được thảo luận sau) làm đối số vào 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
767, bạn có thể tham chiếu các phần tử của chuỗi trong các trường định dạng 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
60. Ví dụ như,

Định dạng chuỗi 2. Sử dụng str. rjust(n), str. ljust(n), str. trung tâm (n), str. zfill(n)

Bạn cũng có thể sử dụng các hàm thành viên 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
3 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
62 (trong đó
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 là độ rộng 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
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 để định dạng chuỗi. Ví dụ,

Định dạng chuỗi 3 (Kiểu cũ). Sử dụng toán tử %

Kiểu cũ (trong Python 2) là sử dụng toán tử

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66, với các chỉ định định 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
69 giống như C. Ví dụ như,

Tránh sử dụng kiểu cũ để định dạng

Chuyển đổi giữa Chuỗi và Số. int(), float() và str()

Bạn có thể sử dụng các hàm 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
738 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68 để phân tích chuỗi "số" thành số nguyên hoặc số thực; . Ví dụ,

Nối một chuỗi và một số?

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. 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
78

The None Value

Python cung cấp một giá trị đặc biệt có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3966 (lưu ý cách viết hoa đầu tiên), có thể được sử dụng để khởi tạo một đối tượng (sẽ được thảo luận trong OOP sau). Ví dụ,

Danh sách, Tuple, Từ điển và Tập hợp

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

Python có một mảng động tích hợp mạnh mẽ có tên là

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

  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 được đặt trong dấu ngoặc vuông
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946
  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 có thể chứa các mục thuộc các loại khác nhau. Đó là bởi vì Python liên kết các loại với các đối tượng, không phải biến
  • 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). Bạn không phải chỉ định kích thước của nó trong quá trình khởi tạo
  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 có thể thay đổi. Bạn có thể cập nhật nội dung của nó
Hàm và toán tử tích hợp cho danh sách

Một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, giống như chuỗi, là mộ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
11. Do đó, bạn có thể vận hành danh sách bằng cách 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
    11 tích hợp 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
    04
  • các 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
    11 tích hợp cho
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 của các số 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
    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 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
    90
  • các toán tử tích hợp sẵn 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) và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 (lặp lại),
    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 (lập chỉ mục) 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
    96 (cắt)

ghi chú

  • Bạn có thể lập chỉ mục các mục từ phía trước với chỉ mục dương hoặc từ phía sau với chỉ mục âm. e. g. , nếu
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 là một danh sách, 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
    98 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
    99 đề cập đến mục đầu tiên và mục thứ hai của nó;
  • 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)
OperatorUsageDescriptionVí dụ
lst = [8, 9, 6, 2]in
not inx in lst
x .
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.
Chỉ mục phía trước bắt đầu từ
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695;
chỉ mục phía sau bắt đầu 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
19 (hoặc
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. bước]
lst[m. n]
lst[m. ]
lst[. n]
lst[. ] Cắt lát để lấy danh sách phụ.
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
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4009 (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.
Mặc định 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
21 là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
695, n là
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[. ] ⇒ Sao chép
lst[4. ] = [1, 2] ⇒ Extenddeldel lst[i]
del lst[m. n]
del lst[m. n. bước]Xóa một hoặc nhiều mụcdel lst[1] ⇒ [8, 6, 2]
del lst[1. ] ⇒ [8]
del lst[. ] ⇒ [] (Xóa)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, không giống như chuỗi, có thể thay đổi. Bạn có thể chèn, xóa và sửa đổi các mục của nó

For examples,

Nối các mục vào danh sách
Sao chép một danh sách
chức năng thành viên danh sách cụ thể

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. Giả 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
720 là một đối tượng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604

  • 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. trả về chỉ số của lần xuất hiện đầu tiên 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
    719;
  • 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. nối danh sách đã cho
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    631 phía sau
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605 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
    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. remove and return the indexed 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
    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. reverse 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
    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. return a copy of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605; same as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    666

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. For examples,

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

The parentheses are actually optional, but recommended for readability. 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
Conversion between List and 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,. }

Python's built-in dictionary type supports key-value pairs (also known as name-value pairs, associative array, or mappings)

  • 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,. , dictionary can be indexed using any key type, including number, string or other types
  • Dictionary is mutable
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. A set is delimited by 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, just like dictionary. You can think of a set as a collection of dictionary keys without associated values. Sets are mutable

For example,

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 supports set operators

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
609 (intersection),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
614 (union),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
61 (difference) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
674 (exclusive-or). For example,

Sequence Types. 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[. ]Slicing to get a sub-sequence.
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] = seq1Thay thế một mục
Xóa một hoặc nhiều mục
Xóa tất cả mục
Thay thế nhiều mục hơn bằng một chuỗi giống nhau . n]
del seq[m:n]
del seq[m. n. bước]Xóa một mục
Xóa nhiều mục hơn, giống như.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647seq. rõ ràng () seq. clear() Xóa tất cả các mục, giống như.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
648 hoặc
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
649seq. append()seq. append(x)Nối x vào cuối dãy,
giống như.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
650seq. mở rộng() seq. entend(seq1)Mở rộng trình tự,
giống như.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
651 hoặc
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
652seq. chèn () seq. chèn(i, x)Chèn
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 vào chỉ mục
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4015, giống như.
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

Others

Deque

[TODO]

Heap

[TODO]

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). (This is the same as 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. Check if two variables have the same reference

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 uses
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. )

Chain Comparison 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)

Khi so sánh các trình tự, các mục đầu tiên từ cả hai trình tự được so sánh. If they differ the outcome is decided. Otherwise, the next items are compared, and so on

Shorthand if-else (or Conditional Expression)

The syntax is

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
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

The syntax is as follows

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

For example,

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

For example,

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
Iterating through a Dictionary

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

For examples,

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

The syntax is

Example 1

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

Example 2
Example 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

For examples,

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)

Function Return Values

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

Thí dụ. 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. Thư viện ứng dụng cục bộ

Tuyên bố từ nhập khẩu

The syntax is

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

For example,

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. The environment variable
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680 is empty by default

For example,

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

Thí dụ. [LÀM]

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

[TODO]

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. There is NO all-module-scope in Python

For example,

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

For example,

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. Bạn cần sử dụng một hàm thông thường cho nhiều câu lệnh

Chức năng là đối tượng

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

Đóng chức năng

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

Instead of using a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
655 loop to iterate through all the items in an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6039 (sequence), you can use the following functions to apply an operation to all the items. 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

ghi chú

  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 has many fancy features that is not available in traditional languages like 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. (Nhắc lại rằng phạm vi của tên là phần mã có thể truy cập nó mà không cần tiền tố. )

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

For example,

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

Hãy tạo hai mô-đun.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099, trong đó
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6098 nhập khẩu
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6099, như sau

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. Its 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

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). Không cần thiết nếu bạn chỉ thay đổi hoặc sửa đổi đối tượng. 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

[TODO]

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

Assertion and Exception Handling

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

For example,

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

The expected outputs are

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

The syntax is

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

Thí dụ

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. In programming, context can be seen as a bucket to pass information around, 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

Trình quản lý ngữ cảnh của câu lệnh with thu thập, sử dụng và giải phóng ngữ cảnh (của tệp) một cách rõ ràng và loại bỏ một chút bản soạn sẵn

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. Các thuộc tính thường được sử dụng là

  • 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

For examples,

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

For examples,

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. Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 đường dẫn tìm kiếm mô-đun. Được khởi tạo từ biến môi trường
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    680, cộng với các mục nhập mặc định phụ thuộc vào cài đặt. Xem ví dụ trước đó
  • 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 là tên kịch bản. Xem ví dụ bên dưới
Thí dụ. Đối số dòng lệnh

Các đối số dòng lệnh được giữ 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
771 dưới dạng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604. Ví dụ: tạo tập lệnh sau có tên "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6259"

Chạy tập lệnh

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

logging Module

Mô-đun đăng nhập

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 hỗ trợ hệ thống ghi sự kiện linh hoạt cho các ứng dụng và thư viện của bạn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 hỗ trợ năm cấp độ

  1. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6262. Thông tin chi tiết có nghĩa là để gỡ lỗi
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6263. Xác nhận rằng một sự kiện diễn ra như mong đợi
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6264. Đã xảy ra sự cố ngoài ý muốn nhưng ứng dụng vẫn hoạt động
  4. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6265. Ứng dụng không hoạt động như mong đợi
  5. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6266. Lỗi nghiêm trọng, ứng dụng có thể không tiếp tục được

Các chức năng ghi nhật ký là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6267. Thực hiện cấu hình cơ bản của hệ thống ghi nhật ký. Các đối số từ khóa là.
    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 (mặc định nối thê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
    751),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6271 (ghi nhật ký cấp này trở lên), v.v.
  • 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. Đăng nhập
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6277 ở cấp độ cụ thể.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6278 được hợp nhất thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6277 bằng cách sử dụng công cụ xác định định dạng
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6280. Chức năng ghi nhật ký chung, tại nhật ký đã cho
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6281
Ghi nhật ký cơ bản thông qua ghi nhật ký. basicConfig()

For example,

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)

Chạy tập lệnh. Tệp nhật ký

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6286 sẽ được tạo với các bản ghi này

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

Theo mặc định, các bản ghi nhật ký bao gồm cấp độ nhật ký và tên nhật ký (mặc định là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6287) trước thông báo

Lấy cấp nhật ký từ tệp cấu hình

Các mức nhật ký, chẳng hạn như

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6262 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6263, được lưu trữ dưới dạng một số nguyên nhất định trong mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260. Ví dụ,

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

Cấp nhật ký thường được đọc từ tệp cấu hình, ở dạng chuỗi mô tả. Ví dụ sau đây cho thấy cách chuyển đổi một chuỗi 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

Định dạng bản ghi nhật ký

Để đặt định dạng thông báo tường trình, hãy sử dụng từ khóa

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

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

trong đó

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6294 cho ngày/giờ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6295 cho cấp nhật ký,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6116 cho tên nhật ký,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6297 cho tên tệp đường dẫn đầy đủ (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6268 chỉ cho tên tệp),
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) cho số dòng và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6301 cho thông báo nhật ký

Ghi nhật ký nâng cao. Logger, Handler, Filter and Formatter

Cho đến nay, chúng tôi đã trình bày các phương tiện ghi nhật ký cơ bản. Thư viện

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6260 rất phong phú và được tổ chức thành các thành phần này

  • Người khai thác gỗ. hiển thị các phương thức cho ứng dụng để ghi nhật ký
  • xử lý. gửi các bản ghi nhật ký do trình ghi nhật ký tạo đến đích thích hợp, chẳng hạn như tệp, bảng điều khiển (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6253), email qua SMTP hoặc mạng qua HTTP/FTP
  • Filters. quyết định bản ghi nhật ký nào sẽ xuất ra
  • Trình định dạng. chỉ định định dạng bố cục của bản ghi nhật ký
Người khai thác gỗ

Để 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)

Các phương pháp của

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6304 được chia thành hai loại. cấu hình và đăng nhập

Các phương pháp ghi nhật ký thường được sử dụng là.

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 và chung
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6314

Các phương pháp cấu hình thường được sử dụng là

  • 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 và
    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
Handlers

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.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6315 của bộ ghi xác định mức thông báo nào sẽ được chuyển đến trình xử lý;
  • 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

Thí 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)

The expected outputs are

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
1
Thí 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;
Thí 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
Thí 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
Nội suy với SafeConfigParser

Một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6361 có thể chứa chuỗi định dạng ở dạng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6371, đề cập đến một
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6116 khác trong phần CÙNG, hoặc một phần
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6373 (viết hoa) đặc biệt. Tuy nhiên, tính năng nội suy này chỉ được hỗ trợ trong
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6374. Ví dụ: giả sử chúng tôi có tệp cấu hình sau có tên là
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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
656 sẽ được nội suy thành
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6377, được nội suy từ phần CÙNG và phần MẶC ĐỊNH

Mô-đun ngày giờ

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6378 cung cấp các lớp để thao tác ngày và giờ theo cả hai cách đơn giản và phức tạp

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6379. Trả về ngày địa phương hiện tại

Mô-đun smtplib và email

The SMTP (Simple Mail Transfer Protocol) is a protocol, which handles sending email and routing email between mail servers. Python cung cấp một mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6380, mô-đun này xác định đối tượng phiên máy khách SMTP có thể được sử dụng để gửi email đến bất kỳ máy Internet nào có trình nền trình nghe SMTP

To use

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

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6382 có thể được sử dụng để tạo một thông điệp email

[TODO] more

Mô-đun json

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. Tuần tự hóa
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 thành chuỗi được mã hóa JSON ('s' cho chuỗi)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6386. Tạo một đối tượng Python từ chuỗi được mã hóa JSON đã cho
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6387. Nối tiếp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 vào tệp
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6389. Tạo một đối tượng Python bằng cách đọc tệp đã cho

For example,

mô-đun dưa chua và cPickle

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6383 (được mô tả trước đó) xử lý các danh sách và từ điển, nhưng việc tuần tự hóa các cá thể lớp tùy ý đòi hỏi thêm một chút nỗ lực. Mặt khác, mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391 thực hiện tuần tự hóa và hủy tuần tự hóa bất kỳ đối tượng Python nào. Pickle là một giao thức cho phép tuần tự hóa các đối tượng Python phức tạp tùy ý. Nó dành riêng cho các ngôn ngữ Python và không áp dụng cho các ngôn ngữ khác

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391 cung cấp các chức năng tương tự như mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6383

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6394. Trả lại biểu diễn được ngâm của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 dưới dạng một chuỗi
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6396. Xây dựng một đối tượng Python từ
    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. Viết một biểu diễn ngâm của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6385 đến
    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. Xây dựng một đối tượng Python đọc từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6400

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6403 là phiên bản cải tiến của
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6391

mô-đun tín hiệu

Tín hiệu (ngắt phần mềm) là một dạng hạn chế của giao tiếp giữa các quá trình không đồng bộ, tương tự như ngắt phần cứng. Nó thường được hệ điều hành sử dụng để thông báo cho các quy trình về một số vấn đề/trạng thái/lỗi nhất định, như chia cho 0, v.v.

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6405 cung cấp các cơ chế để sử dụng trình xử lý tín hiệu trong Python

dấu hiệu. dấu hiệu()

Phương thức

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6406 có hai đối số. số tín hiệu cần xử lý và chức năng xử lý. Ví dụ,