Làm cách nào để nối myVar với một chuỗi bằng JavaScript?

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

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

Python bằng ví dụ

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

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

  • Nhận xét. Nhận xét của Python bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 và kéo dài cho đến cuối dòng. Python không hỗ trợ bình luận nhiều dòng
    [Nhận xét cuối dòng C/C++/C#/Java bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. Họ hỗ trợ nhận xét nhiều dòng thông qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. ]
  • Chuỗi. Chuỗi của Python có thể được phân định bằng dấu nháy đơn [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7] hoặc dấu nháy kép [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8]. Python cũng hỗ trợ chuỗi nhiều dòng, được phân định 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 ngoặc kép cho chuỗi và dấu ngoặc đơn cho ký tự. Họ không hỗ trợ chuỗi nhiều dòng. ]
  • Khai báo kiểu biến. Giống như hầu hết các ngôn ngữ diễn giải tập lệnh [chẳng hạn như JavaScript/Perl], Python được nhập động. Bạn KHÔNG cần khai báo biến [tên và kiểu] trước khi sử dụng chúng. Một biến được tạo thông qua phép gán ban đầu. Python liên kết các loại với các đối tượng, không phải các biến, tôi. e. , một biến có thể chứa bất kỳ loại đối tượng nào
    [Trong C/C++/C#/Java, bạn cần khai báo tên và kiểu biến trước khi sử dụng. ]
  • Data Types. Python hỗ trợ các kiểu dữ liệu này.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1 [số nguyên],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2 [số dấu phẩy động],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 [Chuỗi],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 [boolean của
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 hoặc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6], v.v.
  • Các câu lệnh. Câu lệnh của Python kết thúc bằng một dòng mới
    [Câu lệnh C/C++/C#/Java kết thúc bằng dấu chấm phẩy [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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]]
  • Câu lệnh ghép và thụt đầu dòng. Python sử dụng thụt đầu dòng để biểu thị khối cơ thể. [C/C++/C#/Java sử dụng dấu ngoặc nhọn
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8. ] Cú pháp này buộc bạn phải thụt lề chương trình một cách chính xác, điều này rất quan trọng để đọc chương trình của bạn. You can use space or tab for indentation [but not mixture of both]. Mỗi cấp độ cơ thể phải được thụt vào ở cùng một khoảng cách. It is recommended to use 4 spaces for each level of indentation
  • Toán tử chuyển nhượng.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    9
  • toán tử số học.
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    0 [cộng],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    1 [trừ],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    2 [nhân],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    3 [chia],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    4 [chia số nguyên],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    5 [số mũ],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    6 [mô đun]. [
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    7 và
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    8 không được hỗ trợ]
  • Toán tử gán hợp chất.
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    9,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    0,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    1,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    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
    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
    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
    5
  • Toán tử so 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
    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
    7,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    8,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    9,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    0,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    1,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    2,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    3,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    4,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    5
  • Toán tử logic.
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    6,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    7,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    8. [C/C++/C#/Java sử dụng
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    9,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    00 và
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    01]
  • có điều kiện
  • Loop. Python KHÔNG hỗ trợ vòng lặp
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    02 giống C truyền thống với chỉ mục.
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    03
  • List. Python hỗ trợ mảng động có kích thước thay đổi thông qua cấu trúc dữ liệu tích hợp có tên là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04, được ký hiệu là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    05
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    06. Danh sách tương tự như mảng của C/C++/C#/Java nhưng KHÔNG có kích thước cố định. You can refer to an element via
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    07 or
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    08, or sub-list via
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    09. Bạn có thể sử dụng các hàm có sẵn như
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    10,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    11,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    12
  • Cấu trúc dữ liệu
    • Danh sách.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      13 [mảng động có thể thay đổi]
    • Tuple. _______ 46 _______ 14 [Mảng kích thước cố định không thay đổi]
    • Từ điển.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      15 [cặp khóa-giá trị có thể thay đổi, mảng kết hợp, bản đồ]
    • Bố trí.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      16 [với khóa duy nhất và có thể thay đổi]
  • Trình tự [Chuỗi, Tuple, Danh sách] Toán tử và Hàm
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      2,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      3. kiểm tra tư cách thành viên
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      0. nối
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      2. lặp đi lặp lại
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      21,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      22. lập chỉ mục
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      23. cắt lát
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      24,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      25,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      26
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      27,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      28
    Chỉ dành cho các chuỗi có thể thay đổi [danh sách]
    • Chuyển nhượng qua
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      21,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      30 [lập chỉ mục] và
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      23 [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 hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      9 [nối từ ghé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
      1 [lặp lại từ ghép]
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      35. xóa bỏ
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      36,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      37,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      38
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      39,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      40,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      41,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      42,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      43,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      44
  • Đị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

# [All platforms] Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
45, vòng lặp
# [All platforms] Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
02, vòng lặp
# [All platforms] Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
47, đầu vào/đầu ra, chuỗi,
# [All platforms] Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
04 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

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

# [All platforms] Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py

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

$ Python3 grade_statistics.py
Enter a grade between 0 and 100 [or -1 to end]: 9
Enter a grade between 0 and 100 [or -1 to end]: 999
invalid grade, try again...
Enter a grade between 0 and 100 [or -1 to end]: 101
invalid grade, try again...
Enter a grade between 0 and 100 [or -1 to end]: 8
Enter a grade between 0 and 100 [or -1 to end]: 7
Enter a grade between 0 and 100 [or -1 to end]: 45
Enter a grade between 0 and 100 [or -1 to end]: 90
Enter a grade between 0 and 100 [or -1 to end]: 100
Enter a grade between 0 and 100 [or -1 to end]: 98
Enter a grade between 0 and 100 [or -1 to end]: -1
---------------
The list is: [9, 8, 7, 45, 90, 100, 98]
The minimum is: 7
The minimum using built-in function is: 7
The sum is: 357
The sum using built-in function is: 357
The average is: 51.00
---------------
  0-9  : ***
 10-19 :
 20-29 :
 30-39 :
 40-49 : *
 50-59 :
 60-69 :
 70-79 :
 80-89 :
 90-100: ***
Làm thế nào nó hoạt động
  1. #. /usr/bin/env python3 [Dòng 1] chỉ áp dụng cho môi trường Unix. Nó được gọi là Hash-Bang [hoặc She-Bang] để chỉ định vị trí của Trình thông dịch Python, để tập lệnh có thể được thực thi trực tiếp dưới dạng một chương trình độc lập
  2. # -*- mã hóa. UTF-8 -*- [Dòng 2, tùy chọn] chỉ định lược đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  3. Doc-String. Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu [chuỗi tài liệu] [Dòng 3-12] để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu là một chuỗi nhiều dòng [được phân định bằng dấu nháy đơn ba hoặc dấu ba kép], có thể được trích xuất từ ​​​​tệp nguồn để tạo tài liệu
  4. def my_sum[lst]. [Dòng 15-20]. Chúng tôi định nghĩa một hàm có tên là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    49 nhận vào một
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 đã 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à
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    52 để phân biệt với hàm có sẵn
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    53
  5. bins = [0]*10 [Line 38]. Python hỗ trợ toán tử lặp lại [
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    2]. 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. for row in range[len[bins]]. [Dòng 48, 56]. Python chỉ hỗ trợ vòng lặp
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55. Nó KHÔNG hỗ trợ vòng lặp
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    02 giống C truyền thống với chỉ mục. Do đó, chúng ta cần sử dụng hàm
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    57 tích hợp để tạo một
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 gồm các chỉ mục
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    59, sau đó áp dụng vòng lặp
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 trên chỉ mục
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04
  7. 0 > 2 ⇒ 0b100000

    String

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

    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7] or double-quotes [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8]. Python also supports multi-line strings via triple-single-quotes [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    9] or triple-double-quotes [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    0]

    To place a single-quote [

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

    A triple-single-quoted or triple-double-quoted string can span multiple lines. There is no need for escape sequence to place a single/double quote inside a triple-quoted string. 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 uses Unicode character set to support internationalization [i18n]

    Escape Sequences for Characters [\code]

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

    • Special non-printable characters, such as tab [
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      84], newline [
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      85], carriage return [
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      86]
    • Resolve ambiguity, such as
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      83 [for
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      82 inside double-quoted string],
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      81 [for
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      80 inside single-quoted string],
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      91 [for ________139_______48]
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      93 for character in hex value and
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      94 for octal value
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      95 for 4-hex-digit [16-bit] Unicode character and
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      96 for 8-hex-digit [32-bit] Unicode character
    Raw Strings [r'. ' hoặc r". "]

    You can prefix a string by

    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    97 to disable the interpretation of escape sequences [i. e. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    98], i. e. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    99 is
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    00 [two characters] instead of newline [one character]. Raw strings are used extensively in regex [to be discussed in module
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    14 section]

    Strings are Immutable

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    02,
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    10 returns a new string object instead of modifying the string under operation

    Built-in Functions and Operators for Strings

    You can operate on strings using

    • built-in functions such as
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      04;
    • operators such as
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      2 [contains],
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      0 [concatenation],
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      2 [repetition], indexing
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      21 and
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      22, and slicing
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      23

    Note. These functions and operators are applicable to all

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    11 data types including
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    12,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04, and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    14 [to be discussed later]

    Function /
    OperatorUsageDescriptionExamples
    s = 'Hello'len[]len[str]Lengthlen[s] ⇒ 5insubstr in strContain?
    Return
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 of either
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6'ell' in s ⇒ True
    'he' in s ⇒ False+
    +=str + str1
    str += str1Concatenations + '. ' ⇒ 'Hello. '*
    *=str * count
    str *= countRepetitions * 2 ⇒ 'HelloHello'[i]
    [-i]str[i]
    str[-i]Indexing to get a character
    The front index begins at
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95;
    back index begins at
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    19 [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    20]. s[1] ⇒ 'e'
    s[-4] ⇒ 'e'[m. n. step]
    [m. n]
    [m. ]
    [. N]
    [. ]str[m. n. step]
    str[m. n]
    str[m. ]
    str[. n]
    str[. ]Slicing to get a substring
    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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    25,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    26,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    27. s[1. 3] ⇒ 'el'
    s[1. -2] ⇒ 'el'
    s[3. ] ⇒ 'lo'
    s[. -2] ⇒ 'Hel'
    s[. ] ⇒ 'Hello'
    s[0. 5. 2] ⇒ 'Hlo'

    Ví dụ như,

    Character Type?

    Python does not have a dedicated character data type. A character is simply a string of length 1. You can use the indexing operator to extract individual character from a string, as shown in the above example; or process individual character using

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 loop [to be discussed later]

    The built-in functions

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    29 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    30 operate on character, e. g. ,

    Unicode vs ASCII

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    13, e. g. ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    32

    In Python 2, strings are defaulted to be ASCII strings [byte strings]. Unicode strings are prefixed with

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    33

    You should always use Unicode for internationalization [i18n]

    String-Specific Member Functions

    Python supports strings via a built-in class called

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 [We will describe class in the Object-Oriented Programming chapter]. The
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 class provides many member functions. Since string is immutable, most of these functions return a new string. The commonly-used member functions are as follows, supposing that
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    36 is a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 object

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      38
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      39,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      40,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      41. strip the leading and trailing whitespaces, the right [trailing] whitespaces; and the left [leading] whitespaces, respectively
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      38
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      43,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      44. Return a uppercase/lowercase counterpart, respectively
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      38
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      46,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      47. Check if the string is uppercase/lowercase, respectively
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      38
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      49
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      50
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      51
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      52
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      54
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      55
    String Formatting 1 [New Style]. Using str. format[] function

    Có một số cách để tạo một chuỗi được định dạng cho đầu ra. Python 3 introduces a new style in the

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3's
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    67 member function with
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8 as place-holders [called format fields]. For examples,

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

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    67 function, you can reference the sequence's elements in the format fields with
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    60. For examples,

    String Formatting 2. Using str. rjust[n], str. ljust[n], str. center[n], str. zfill[n]

    You can also use

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3's member functions like
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    62 [where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09 is the field-width],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    64,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    65,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    67 to format a string. For example,

    String Formatting 3 [Old Style]. Using % operator

    The old style [in Python 2] is to use the

    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    6 operator, with C-like
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    69 format specifiers. For examples,

    Avoid using old style for formatting

    Conversion between String and Number. int[], float[] and str[]

    You can use built-in functions

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    38 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68 to parse a "numeric" string to an integer or a float; and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69 to convert a number to a string. For example,

    Concatenate a String and a Number?

    You CANNOT concatenate a string and a number [which results in

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    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ụ,

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    8

    Giá trị Không có

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

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    66 [lưu ý cách viết hoa chữ cái đầu], giá trị này có thể được sử dụng để khởi tạo một đối tượng [sẽ được thảo luận trong OOP sau]. For example,

    List, Tuple, Dictionary and Set

    List [v1, v2,. ]

    Python has a powerful built-in dynamic array called

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04

    • A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 is enclosed by square brackets
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      46
    • A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 can contain items of different types. It is because Python associates types to objects, not variables
    • A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 grows and shrinks in size automatically [dynamically]. You do not have to specify its size during initialization
    • A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 is mutable. You can update its contents
    Built-in Functions and Operators for list

    A

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04, like string, is a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    11. Hence, you can operate lists using

    • built-in
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      11 functions such as
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      04
    • built-in
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      11 functions for
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 of numbers such as
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      88,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      89, and
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      90
    • built-in operators such as
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      2 [contains],
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      0 [concatenation] and
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      2 [repetition],
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      35,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      95 [indexing], and
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      96 [slicing]

    Notes

    • You can index the items from the front with positive index, or from the back with negative index. E. g. , if
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 is a list,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      98 and
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      99 refer to its first and second items;
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      00 and
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      01 refer to the last and second-to-last items
    • You can also refer to a sub-list [or slice] using slice notation
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      02 [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 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
      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]
    OperatorUsageDescriptionExamples
    lst = [8, 9, 6, 2]in
    not inx in lst
    x not in lstContain? Return
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 of either
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    69 in lst ⇒ True
    5 in lst ⇒ False+
    +=lst + lst1
    lst += lst1Concatenationlst + [5, 2]
    ⇒ [8, 9, 6, 2, 5, 2]*
    *=lst * đếm
    lst *= countRepetitionlst * 2
    ⇒ [8, 9, 6, 2, 8, 9, 6, 2][i]
    [-i]lst[i]
    lst[-i]Indexing to get an item
    Front index begins at
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95;
    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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    11]. lst[1] ⇒ 9
    lst[-2] ⇒ 6[m. n. step]
    [m. n]
    [m. ]
    [. N]
    [. ]lst[m. n. step]
    lst[m. n]
    lst[m. ]
    lst[. n]
    lst[. ]Slicing to get a sublist
    From index
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    21 [included] to
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09 [excluded] with
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    23 size
    The defaults are.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    21 is
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95, n is
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    11. lst[1. 3] ⇒ [9, 6]
    lst[1. -2] ⇒ [9]
    lst[3. ] ⇒ [2]
    lst[. -2] ⇒ [8, 9]
    lst[. ] ⇒ [8, 9, 6, 2]
    lst[0. 4. 2] ⇒ [8, 6]
    newlst = lst[. ] ⇒ Copy
    lst[4. ] = [1, 2] ⇒ Extenddeldel lst[i]
    del lst[m. n]
    del lst[m. n. step]Delete one or more itemsdel lst[1] ⇒ [8, 6, 2]
    del lst[1. ] ⇒ [8]
    del lst[. ] ⇒ [] [Clear]FunctionUsageDescriptionExamples
    lst = [8, 9, 6, 2]len[]len[lst]Lengthlen[lst] ⇒ 4max[]
    min[]max[lst]
    min[lst]Maximum value
    minimum valuemax[lst] ⇒ 9
    min[lst] ⇒ 2sum[]sum[lst]Sum [for number lists only]sum[lst] ⇒ 16

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04, unlike string, is mutable. You can insert, remove and modify its items

    Ví dụ như,

    Appending Items to a listCopying a listlist-Specific Member Functions

    The

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 class provides many member functions. Suppose
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    20 is a
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 object

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      23. return the index of the first occurrence of
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      19; or error
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      25. nối
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      26 đã cho vào sau
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 và trả về
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66;
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      30. append the given list
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      31 behind the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66; same as slicing operation
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      34
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      35. insert the given
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      26 before the
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      37 and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66. Hence,
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      39 inserts before the first item of the
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      20;
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      41 inserts at the end of the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 which is the same as
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      25
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      44. remove the first occurrence of
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      19 from the
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      20 and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66; or error
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      48. remove and return the last item of the
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      20
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      50. remove and return the indexed item of the
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      20
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      52. remove all the items from the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66; same as operator
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      55
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      56. return the occurrences of
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      26
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      58. reverse the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 in place and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      61. sort the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05 in place and return
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      66
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      64. return a copy of
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      05; same as
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      66

    Recall that

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 is mutable [unlike string which is immutable]. These functions modify the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 directly. For examples,

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

    To use a

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 as a last-in-first-out [LIFO] stack, use
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    70 to add an item to the top-of-stack [TOS] and
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    71 to remove the item from the TOS

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

    Để sử dụng

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 làm hàng đợi nhập trước xuất trước [FIFO], hãy sử dụng
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    70 để thêm một mục vào cuối hàng đợi và
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    74 để xóa mục đầu tiên của hàng đợi

    Tuy nhiên,

    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    74 chậm. The standard library provide a class
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    76 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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    45

    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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    78 is a tuple]

    • built-in functions such as
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      79;
    • built-in functions for tuple of numbers such as
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      80,
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      81 and
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      82;
    • operators such as
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      2,
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      0 and
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      2; 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 hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      87,
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      88, etc
    Conversion between List and Tuple

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

    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    89; and a tuple to a list using
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    90. 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 [
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      67], in the form of
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      15
    • 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

    Lớp

    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    94 có nhiều phương thức thành viên. The commonly-used are follows [suppose that
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    95 is a
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    94 object]

    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      97
    • Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      98,
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      99,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      00
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      01
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      02
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      03
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      04. merge the given 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
      05 into
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      95. Override the value if key exists, else, add new key-value
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      07

    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

    Ví dụ,

    Set-Specific Operators [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    09,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    01,
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    1,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    74]

    Python supports set operators

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    09 [intersection],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    14 [union],
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    1 [difference] and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    74 [exclusive-or]. For example,

    Sequence Types. list, tuple, str

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    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
    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.
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04 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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    26][i]
    [-i]seq[i]
    seq[-i]Indexing to get an item
    Front index begins at
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95; 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    29]. [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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95, n 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
    29. 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    36 in the sequence, or raise
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    37
    Search from
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    26 [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
    39 [excluded]seq. count[]seq. count[x]Returns the count of x in the sequence

    For mutable sequences [

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04], the following built-in operators and built-in functions [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    41
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    42] and member functions [
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    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
    44] are supported

    Opr / FuncUsageDescription[]seq[i] = x
    seq[m. n] = []
    seq[. ] = []
    seq[m. n] = seq1
    seq[m. n. step] = seq1Replace one item
    Remove one or more items
    Remove all items
    Replace more items with a sequence of the same size+=seq += seq1Extend by
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    45*=seq *= countRepeat
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    46 timesdeldel seq[i]
    del seq[m. n]
    del seq[m. n. step]Delete one item
    Xóa nhiều mục hơn, giống 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
    47seq. clear[]seq. clear[]Remove all items, same 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
    48 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
    49seq. append[]seq. append[x]Append x to the end of the sequence,
    same 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
    50seq. mở rộng[] seq. entend[seq1]Mở rộng trình tự,
    same 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
    51 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
    52seq. insert[]seq. insert[i, x]Insert
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    10 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
    15, same 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
    55seq. remove[]seq. remove[x]Remove the first occurence 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
    36seq. pop[]seq. pop[]
    seq. pop[i]Retrieve and remove the last item
    Retrieve and remove the item at index
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    26seq. copy[]seq. copy[]Create a shallow copy 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
    58, same 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
    59seq. reverse[]seq. reverse[]Reverse the sequence in place

    Others

    Deque

    [LÀM]

    Heap

    [LÀM]

    Flow Control Constructs

    Conditional if-elif-else

    The syntax is as follows. The

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    71 [else-if] and
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    72 blocks are optional

    For example

    There is no

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    62 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

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      8 [less than],
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      9 [less than or equal 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
      6 [equal 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
      7 [not equal to],
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      0 [greater than],
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      1 [greater than or equal to]. [This is the same as C/C++/Java. ]
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      2,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      3. Check if an item is. is not in a sequence [list, tuple, string, set, etc]
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      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
      75. Check if two variables have the same reference

    Python supports these logical [boolean] operators.

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    6,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    7,
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    8. [C/C++/Java uses
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    9,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    00,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    01. ]

    Chain Comparison v1 < x < v2

    Python supports chain comparison in the form 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
    82, e. g. ,

    Comparing Sequences

    The comparison operators [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
    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
    9] are overloaded to support sequences [such as string, list and tuple]

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

    Shorthand if-else [or Conditional Expression]

    The syntax is

    Ví dụ,

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    9

    Note. Python does not 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
    85" for shorthand
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    45, as in C/C++/Java

    The while loop

    The syntax is as follows

    The

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    72 block is optional, which will be executed if the loop exits normally without encountering a
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    76 statement

    Ví dụ,

    break, continue, pass and loop-else

    The

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    76 statement breaks out from the innermost loop; the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    77 statement skips the remaining statements of the loop and continues the next iteration. This is the same as C/C++/Java

    The

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    40 statement does nothing. It serves as a placeholder for an empty statement or empty block

    The loop-

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    72 block is executed if the loop is exited normally, without encountering the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    76 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    47-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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 loop has the following syntax

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

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    72 block is executed only if the loop exits normally, without encountering the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    76 statement

    Iterating through Sequences

    Iterating through a Sequence [String, List, Tuple, Dictionary, Set] using for-in Loop

    The

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    99 loop, which uses a varying index for the iterations

    Take note that you cannot use the "

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    00" 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    01 function to create the index list [described below]

    The range[] Built-in Function

    The

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    01 function produces a series of running integers, which can be used as index list for the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 loop

    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      04 tạo ra các số nguyên từ
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      95 đến
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      06;
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      07 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 binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      06;
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      10 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 binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      06 in step of
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      38

    Ví dụ,

    Using else-clause in Loop

    Recall that the

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    72-clause will be executed only if the loop exits without encountering a
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    76

    Iterating through a Sequence of Sequences

    A sequence [such as list, tuple] can contain sequences. For example,

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    0Iterating 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    16 takes a
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    17 [such as sequence] and returns an
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    18 object. You can then use
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    19 to iterate through the items. For example,

    The reversed[] Built-in Function

    To iterate a sequence in the reverse order, apply the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    20 function which reverses the iterator over values of the sequence. For example,

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    1The enumerate[] Built-in Function

    You can use the built-in function

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    21 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    22 built-in function. For examples,

    Comprehension for Generating Mutable List, Dictionary and Set

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

    Ví dụ như,

    Similarly, you can create dictionary and set [mutable sequences] via comprehension. For example,

    Comprehension cannot be used to generate

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    12 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    14, as they are immutable and
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    25 cannot be applied

    Quy ước đặt tên và kiểu mã hóa [PEP8 so với PEP 257]

    quy ước đặt tên

    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 binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      26
    • Function names. use a verb in lowercase words [optionally joined with underscore if it improves readability], e. g. ,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      27 or
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      28
    • Class names. use a noun in camel-case [initial-cap all words], e. g. ,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      29,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      30,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      31
    • Constant names. use a noun in uppercase words joined with underscore, e. g. ,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      32,
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      33
    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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    55 followed by the function name, the parameter list, the doc-string and the function body. Inside the function body, you can use a
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    81 statement to return a value to the caller. There is no need for type declaration like C/C++/Java

    The syntax is

    ví dụ 1

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

    Example 2Example 3. Function doc-string

    This example elaborates on the function's doc-string

    • The first line "
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      36" 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 binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      37 module to perform unit test for this function based on these examples [to be described in the "Unit Test" section
    The pass statement

    The

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    40 statement does nothing. It is sometimes needed as a dummy statement placeholder to ensure correct syntax, e. g. ,

    Function Parameters and Arguments

    Passing Arguments by Value vs. by Reference

    In Python

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

    Ví dụ như,

    Function Parameters with Default Values

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

    Another example,

    In stead of hard-coding the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    39, 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    40. 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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    2 to pack all the remaining positional arguments into a tuple. For example,

    Python supports placing

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    42 in the middle of the parameter list. However, all the arguments after
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    42 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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    2 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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    5 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    42 and
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    47 in your function definition. Place
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    42 before
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    47. 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. Trên thực tế, một bộ chứa tất cả các giá trị trả về được trả về

    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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    38". The module name is the filename, i. e. , a module shall be saved as "
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    38"

    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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    52], followed by variable, function and class definitions

    Example. The greet Module

    Create a module called

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    53 and save as "
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    54" as follows

    This

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    53 module defines a variable
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    56 and a function
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    57

    The import statement

    To use an external module in your script, use the

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93 statement

    Once

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93ed, you can reference the module's attributes as
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    60. You can use the
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    61 to assign a new module name to avoid module name conflict

    For example, to use the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    53 module created earlier

    The import statements should be grouped in this order

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

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

    The syntax is

    With the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    63 statement, you can reference the imported attributes using
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    64 directly, without qualifying with the
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    64

    Ví dụ,

    import vs. from-import

    The

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    63 statement actually loads the entire module [like
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93 statement]; and NOT just the imported attributes. Nhưng nó CHỈ hiển thị các thuộc tính đã nhập vào không gian tên. Hơn nữa, bạn có thể tham khảo chúng trực tiếp mà không cần xác định tên mô-đun

    Ví dụ: hãy tạo mô-đun sau có tên là

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    68 để thử nghiệm
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93 so với.
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    63

    Hãy thử

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    2

    Bây giờ, hãy thử

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    63 và lưu ý rằng toàn bộ mô-đun đã được tải, giống như câu lệnh
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    93

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    3

    Nhập khẩu có điều kiện

    Python cũng hỗ trợ nhập có điều kiện. Ví dụ,

    hệ thống. đường dẫn và các biến môi trường PYTHONPATH/PATH

    Biến môi trường

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    37 sẽ bao gồm đường dẫn đến Trình thông dịch Python "
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    75"

    Đường dẫn tìm kiếm mô-đun Python được duy trì trong một biến Python

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    76 của mô-đun
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    14, i. e.
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    78.
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    78 được khởi tạo từ biến môi trường
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    80, cộng với mặc định phụ thuộc vào cài đặt. Theo mặc định, biến môi trường
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    80 trống

    Ví dụ,

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    4

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    78 mặc định bao gồm thư mục làm việc hiện tại [được biểu thị bằng một chuỗi trống], các thư mục tiêu chuẩn của Python, cộng với các thư mục mở rộng trong
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    83

    Các mô-đun đã nhập phải có sẵn ở một trong các mục nhập

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    78

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    5

    Để hiển thị các biến môi trường

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    37 và
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    80, hãy sử dụng một trong các lệnh sau

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    6

    Tải lại mô-đun bằng cách sử dụng imp. tải lại [] hoặc nhập khẩu. reload[]

    If you modify a module, you can use

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    87 function of the
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    88 [for import] module to reload the module, for example,

    NOTE. Since Python 3. 4, the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    88 package is pending deprecation in favor of
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    90

    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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    83 is set to
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    84. On the other hand, when a module is imported, its
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    83 is set to the module name. Hence, the above module will be executed if it is loaded by the Python interpreter, but not imported by another module

    Example. [TODO]

    Packages

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

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    94

    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 binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      95 file in the directory

    The

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    95 marks the directory as a package. For example, suppose that you have this directory/file structure

    If

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    97 is in your
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    98, you can import
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    99 as

    Without the

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    95, Python will NOT search the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    001 directory for
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    99. Moreover, you cannot reference modules in the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    001 directory directly [e. g. ,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    004] as it is not in the
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    98

    Attributes in '__init__. py'

    The

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    95 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    95 module can be accessed via the package name directly [i. e. ,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    008 instead of
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    009]. 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    63 statement, you can use
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    60 to refer to the current package and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    012 to refer to the parent package. For example, inside
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    013, you can write

    Take note that in Python, you write

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    014,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    015 by omitting the separating dot [instead of
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    016,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    017]

    Circular Import Problem

    [LÀM]

    Advanced Functions and Namespaces

    Local Variables vs. Global Variables

    Names created inside a function [i. e. within

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    55 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. Phạm vi toàn cầu trong Python tương đương với phạm vi mô-đun hoặc phạm vi tệp. There is NO all-module-scope in Python

    Ví dụ,

    Function Variables [Variables of Function Object]

    Trong Python, một biến nhận một giá trị hoặc đối tượng [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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. , định nghĩa một hàm bên trong một hàm. For example,

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

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    7

    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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    8

    Ví dụ,

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    021 and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    022 do the same thing. Take note that
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    81 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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    9

    More usages for lambda function will be shown later

    Multiple Statements?

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    024. In other words, you cannot place multiple statements inside the body of a lambda function. You need to use a regular function for multiple statements

    Functions are Objects

    In Python, functions are objects [like instances of a class]. Like any object,

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

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

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

    Function Closure

    In the above 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
    09 is not local to the lambda function. Instead,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09 is obtained from the outer function

    When we assign

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    027 to
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    028,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09 takes on the value of 8 during the invocation. But we expect
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09 to go out of scope after the outer function terminates. If this is the case, calling
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    031 would encounter an non-existent
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    09?

    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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    028, an enclosure with
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    034 is created; while in
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    035, an enclosure with
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    036 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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    037, e. g. ,

    Functional Programming. Using Lambda Function in filter[], map[], reduce[] and Comprehension

    Instead of using a

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    55 loop to iterate through all the items in an
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    039 [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]

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      040. Return an
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      041 yielding those
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      26s of
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      17 for which
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      044 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. Ví dụ,
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      046. Apply [or Map or Transform] the function func on each item of the
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      17. For example,
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      048 [in module
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      049]. 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,
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      050. used frequently in big data analysis to obtain an aggregate value
    • List comprehension. a one-liner to generate a list as discussed in the earlier section. e. g. ,

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

    Decorators

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

    Example. Decorating an 1-argument Function

    Notes

    1. The decorator
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      051 takes a 1-argument function as its argument, and returns an replacement 1-argument function
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      052, with its argument
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      10 clamped to
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      054, before applying the original function
    2. In
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      055, we decorate the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      056 function and assign the decorated [replacement] function to the same function name [confusing?. ]. After the decoration, the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      056 takes on a new decorated life
    Example. Using the @ symbol

    Using

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    055 to decorate a function is messy?. Instead, Python uses the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    63 symbol to denote the replacement. For example,

    For Java programmers, do not confuse the Python decorator

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    63 with Java's annotation like
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    061

    Example. Decorator with an Arbitrary Number of Function Arguments

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

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    42 and/or
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    47 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    051 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    83 and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    066. This can be solved by using the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    067 of
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    049, 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    069 decorator to take two arguments -
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    070 and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    071 of the range

    The decorator

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    069 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]

    Mỗi Mô-đun có một Không gian tên Toàn cầu

    A module is a file containing attributes [such as variables, functions and classes]. Mỗi mô-đun có không gian tên toàn cầu riêng. Do đó, bạn không thể định nghĩa hai hàm hoặc lớp cùng tên trong một mô-đun. Nhưng bạn có thể định nghĩa các hàm cùng tên trong các mô-đun khác nhau vì các không gian tên được tách biệt

    Khi bạn khởi chạy trình bao tương tác, Python sẽ tạo một mô-đun có tên là

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073, với không gian tên chung được liên kết của nó. Tất cả các tên tiếp theo được thêm vào không gian tên của
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073

    Khi bạn nhập một mô-đun qua

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    075 trong lớp vỏ tương tác, chỉ có __
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    64 được thêm vào không gian tên của
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073. Bạn cần truy cập tên [thuộc tính] bên trong
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    64 qua
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    60. In other words, the imported module retains its own namespace and must be prefixed with
    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    60 inside
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073. [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ố. ]

    Tuy nhiên, nếu bạn nhập một thuộc tính thông qua

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    082 trong lớp vỏ tương tác, thì ______36_______64 được thêm vào không gian tên của ______46_______073 và bạn có thể truy cập trực tiếp vào _____36_______64 mà không cần thêm tiền tố ________64

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

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    64 is added into the target module's namespace [instead of
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073 for the interactive shell]

    Các chức năng tích hợp được giữ trong một mô-đun có tên là

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    089, được nhập tự động vào
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073

    Các hàm tích hợp globals[],locals[] và dir[]

    Bạn có thể liệt kê tên của phạm vi hiện tại thông qua các hàm tích hợp này

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      091. trả về một từ điển [cặp tên-giá trị] chứa các biến toàn cục của phạm vi hiện tại
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      092. return a dictionary [name-value pairs] containing the current scope's local variables. If
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      092 is issued in global scope, it returns the same outputs as
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      091
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      095. return a list of local names in the current scope, which is equivalent to
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      096
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      097. return a list of the local names for the given object

    Ví dụ,

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

    More on Module's Global Namespace

    Let's create two modules.

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098 and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099, where
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098 imports
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099, as follows

    Let's import

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098 [which in turn import
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099] under the interpreter shell, and check the namespaces

    Take note that the interpreter's current scope

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    83 is
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    073. It's namespace contains
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098 [imported]. The
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098's namespace contains
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099 [imported] and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    109. To refer to
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099, you need to go thru
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098, in the form of
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    112. The
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    112's namespace contains
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    114

    Now, let run

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098 instead, under IDLE3, and check the namespaces

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    0

    Take note that the current scope's

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    116 is again __main__, which is the executing module
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    098. Its namespace contains
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    099 [imported] and
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    109

    Name Resolution

    When you ask for a name [variable], says

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    10 cannot be found, Python raises a
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    122

    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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    83 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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    84 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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    83 statement too. Ví dụ,

    In summary,

    1. The order for name resolution [for names inside a function] is. local, enclosing function for nested
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      55, 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 your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      83 statement to modify globals inside the function. Similarly, you need to declare via
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      84 statement to modify enclosing local names inside the nested function
    More on global Statement

    The

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    83 statement is necessary if you are changing the reference to an object [e. g. with an assignment]. It is not needed if you are just mutating or modifying the object. For example,

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

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    83 statement is not needed

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

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

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    83 is needed, otherwise, a local variable will be created inside the function

    Built-in Namespace

    The built-in namespace is defined in the

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    132 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,
    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    38,
    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 hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    90,
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    89 and etc. You can use
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    141 or
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    142 to list the attributes of the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    132 module

    [LÀM]

    del Statement

    You can use

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    35 statement to remove names from the namespace, for example,

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    35 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 your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    95 statement to test a certain assertion [or constraint]. For example, 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
    10 is supposed to be
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    95 in a certain part of the program, you can use the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    95 statement to test this constraint. An
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    150 will be raised 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
    10 is not zero

    Ví dụ,

    The assertions are always executed in Python

    Syntax

    The syntax for

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    95 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
    2

    If the

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    153 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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    150 will be raised with the
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    156

    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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    157 exception handling facility to prevent the program from terminating abruptly

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

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

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

    The exception handling process for

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    157 is

    1. Python runs the statements in the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      91-block
    2. If no exception is raised in all the statements of the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      91-block, all the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      92-blocks are skipped, and the program continues to the next statement after the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      162 statement
    3. However, if an exception is raised in one of the statement in the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      91-block, the rest of
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      91-block will be skipped. The exception is matched with the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      92-blocks. The first matched
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      92-block will be executed. The program then continues to the next statement after the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      162 statement, instead of terminates abruptly. Nevertheless, if none of the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      92-blocks is matched, the program terminates abruptly
    4. The
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      72-block will be executable if no exception is raised
    5. The
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      93-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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    157 is

    The

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    91-block [mandatory] must follow by at least one
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    92 or
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    93 block. The rests are optional

    CAUTION. Python 2 uses older syntax of "

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    175", which should be re-written as "
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    176" for portability

    Example 2. Input Validation

    nâng cao Tuyên bố

    You can manually raise an exception via the

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    94 statement, 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
    4

    The syntax is

    A

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    94 without argument in the
    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    92 block re-raise the exception to the outer block, e. g. ,

    Built-in Exceptions

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      180,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      181,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      182. các lớp cơ sở
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      183. for
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      184,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      185,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      186
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      187
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      188. for
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      30,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      190
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      191. for
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      192,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      193
    • [TODO] more

    User-defined Exception

    You can defined your own exception by sub-classing the

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    181 class

    Example

    with-as Statement and Context Managers

    The syntax of the

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    80 statement is as follows

    Python’s

    Enter your guess [between 0 and 100]: 50
    Try lower...
    Enter your guess [between 0 and 100]: 25
    Try higher...
    Enter your guess [between 0 and 100]: 37
    Try higher...
    Enter your guess [between 0 and 100]: 44
    Try lower...
    Enter your guess [between 0 and 100]: 40
    Try lower...
    Enter your guess [between 0 and 100]: 38
    Try higher...
    Enter your guess [between 0 and 100]: 39
    Congratulation!
    You got it in 7 trials.
    79 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

    ví dụ 1

    This is equivalent 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
    5

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

    However, the

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    80 statement is applicable to certain objects only, such as file; while
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    198 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    075 or
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    082 to import the entire module or a selected attribute. You can use
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    201 to list all the attributes of the module,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    202 or
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    202 to read the documentation page. For example,

    math and cmath Modules

    The

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    204 module provides access to the mathematical functions defined by the C language standard. The commonly-used attributes are

    • Constants.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      205,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      87
    • Power and exponent.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      207,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      208,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      209,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      210,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      211,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      212
    • 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.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      215,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      216,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      217
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      219,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      220
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      221 [
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      222]
    • Conversion between degrees and radians.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      223,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      224
    • Trigonometric functions.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      225,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      226,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      227,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      228,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      229,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      230,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      231
    • Hyperbolic functions.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      232,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      233,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      234,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      235,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      236,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      237

    Ví dụ như,

    In addition, the

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    238 module provides mathematical functions for complex numbers. See Python documentation for details

    statistics Module

    The

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    239 module computes the basic statistical properties such as mean, median, variance, and etc. [Many third-party vendors provide advanced statistics packages. ] For examples,

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

    Mô-đun ngẫu nhiên

    The module

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    89 can be used to generate various pseudo-random numbers

    Ví dụ như,

    sys Module

    The module

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    14 [for system] provides system-specific parameters and functions. The commonly-used are

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      242. exit the program by raising the
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      243 exception. If used inside a
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      91, the
      Enter your guess [between 0 and 100]: 50
      Try lower...
      Enter your guess [between 0 and 100]: 25
      Try higher...
      Enter your guess [between 0 and 100]: 37
      Try higher...
      Enter your guess [between 0 and 100]: 44
      Try lower...
      Enter your guess [between 0 and 100]: 40
      Try lower...
      Enter your guess [between 0 and 100]: 38
      Try higher...
      Enter your guess [between 0 and 100]: 39
      Congratulation!
      You got it in 7 trials.
      93 clause is honored. The optional argument
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      246 can be an integer [default to 0 for normal termination, or non-zero for abnormal termination]; or any object [e. g. ,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      247]
    • Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      78. A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 of module search-paths. Initialized from the environment variable
      Enter a binary string: 1011001110
      The decimal equivalent for binary "1011001110" is: 718
      The decimal equivalent for binary "1011001110" using built-in function is: 718
      80, plus installation-dependent default entries. See earlier example
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      251,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      252,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      253. standard input, output and error stream
    • $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      71. A
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      04 of command-line arguments passed into the Python script.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      256 is the script name. See example below
    Example. Command-Line Arguments

    The command-line arguments are kept in

    $ Python3 grade_statistics.py
    Enter a grade between 0 and 100 [or -1 to end]: 9
    Enter a grade between 0 and 100 [or -1 to end]: 999
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 101
    invalid grade, try again...
    Enter a grade between 0 and 100 [or -1 to end]: 8
    Enter a grade between 0 and 100 [or -1 to end]: 7
    Enter a grade between 0 and 100 [or -1 to end]: 45
    Enter a grade between 0 and 100 [or -1 to end]: 90
    Enter a grade between 0 and 100 [or -1 to end]: 100
    Enter a grade between 0 and 100 [or -1 to end]: 98
    Enter a grade between 0 and 100 [or -1 to end]: -1
    ---------------
    The list is: [9, 8, 7, 45, 90, 100, 98]
    The minimum is: 7
    The minimum using built-in function is: 7
    The sum is: 357
    The sum using built-in function is: 357
    The average is: 51.00
    ---------------
      0-9  : ***
     10-19 :
     20-29 :
     30-39 :
     40-49 : *
     50-59 :
     60-69 :
     70-79 :
     80-89 :
     90-100: ***
    71 as a
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    04. Ví dụ: tạo tập lệnh sau có tên "
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    259"

    Chạy tập lệnh

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

    đăng nhập mô-đun

    Mô-đun đăng nhập

    Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    260 hỗ trợ hệ thống ghi nhật ký sự kiện linh hoạt cho các ứng dụng và thư viện của bạn

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    260 hỗ trợ năm cấp độ

    1. # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      262. Thông tin chi tiết có nghĩa là để gỡ lỗi
    2. # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      263. Xác nhận rằng một sự kiện diễn ra như mong đợi
    3. # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      264. Đã xảy ra sự cố ngoài ý muốn nhưng ứng dụng vẫn hoạt động
    4. # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      265. Ứng dụng không hoạt động như mong đợi
    5. # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      266. Serious error, the application may not be able to continue

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

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      267. 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à.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      268,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      269 [mặc định nối thêm
      $ Python3 grade_statistics.py
      Enter a grade between 0 and 100 [or -1 to end]: 9
      Enter a grade between 0 and 100 [or -1 to end]: 999
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 101
      invalid grade, try again...
      Enter a grade between 0 and 100 [or -1 to end]: 8
      Enter a grade between 0 and 100 [or -1 to end]: 7
      Enter a grade between 0 and 100 [or -1 to end]: 45
      Enter a grade between 0 and 100 [or -1 to end]: 90
      Enter a grade between 0 and 100 [or -1 to end]: 100
      Enter a grade between 0 and 100 [or -1 to end]: 98
      Enter a grade between 0 and 100 [or -1 to end]: -1
      ---------------
      The list is: [9, 8, 7, 45, 90, 100, 98]
      The minimum is: 7
      The minimum using built-in function is: 7
      The sum is: 357
      The sum using built-in function is: 357
      The average is: 51.00
      ---------------
        0-9  : ***
       10-19 :
       20-29 :
       30-39 :
       40-49 : *
       50-59 :
       60-69 :
       70-79 :
       80-89 :
       90-100: ***
      51],
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      271 [ghi cấp độ này trở lên], v.v.
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      272,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      273,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      274,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      275,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      276. Ghi nhật ký
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      277 ở cấp độ cụ thể.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      278 được hợp nhất thành
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      277 bằng cách sử dụng công cụ xác định định dạng
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      280. Chức năng ghi nhật ký chung, tại nhật ký đã cho
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      281
    Ghi nhật ký cơ bản thông qua ghi nhật ký. basicConfig[]

    Ví dụ,

    Các hàm ghi nhật ký hỗ trợ các công cụ xác định định dạng giống như

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    282, chẳng hạn như
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    283,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    284, với các giá trị dưới dạng đối số của hàm [thay vì thông qua toán tử
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    6 trong Python]

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    286 sẽ được tạo, với các bản ghi này

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

    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à

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    287] 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ư

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    262 và
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    263, được lưu trữ dưới dạng một số nguyên nhất định trong mô-đun
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    260. 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
    9

    Cấp nhật ký thường được đọc từ tệp cấu hình, ở dạng chuỗi mô tả. The following example shows how to convert a string log-level [e. g. ,

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    291] sang cấp nhật ký số [e. g. , 10] được sử dụng bởi mô-đun
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    260

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    293

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

    trong đó

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    294 cho ngày/giờ,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    295 cho cấp nhật ký,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    116 cho tên nhật ký,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    297 cho tên tệp đường dẫn đầy đủ [
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    268 chỉ cho tên tệp],
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    299 [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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à
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    301 cho thông điệp tường trình

    Ghi nhật ký nâng cao. Logger, Handler, Filter và 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    260 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 [
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      253], email qua SMTP hoặc mạng qua HTTP/FTP
    • bộ lọc. quyết định bản ghi nhật ký nào sẽ xuất ra
    • Trình định dạng. specify the layout format of log records
    Người khai thác gỗ

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    304, hãy gọi
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    305, trong đó tùy chọn
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    306 chỉ định tên trình ghi nhật ký [mặc định là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    287]

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    304 đượ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à.

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    309,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    310,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    311,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    312,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    313 và chung
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    314

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

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      315
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      316 và
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      317
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      318 và
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      319
    xử lý

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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    320 [
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    253,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    252],
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    323,
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    324 và
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    325 [email]

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

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      315.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      315 của bộ ghi xác định mức thông báo nào sẽ được chuyển đến trình xử lý;
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      329. để định dạng tin nhắn được gửi đến đích
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      318 và
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      319

    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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    325 để nhận email cho cấp độ
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    333;

    Trình định dạng

    Đính kèm với trình xử lý [thông qua

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    336] để định dạng thông điệp tường trình

    Thí dụ. Using Logger with Console Handler and a Formatter
    1. There is probably no standard for log record format [unless you have an analysis tool in mind]?. Nhưng tôi khuyên bạn nên chọn một dấu phân cách trường không xuất hiện trong thông báo tường trình để dễ xử lý các bản ghi nhật ký [e. g. , xuất sang bảng tính]

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

    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    1Thí 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
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      324,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      338 đặt giới hạn kích thước tệp nhật ký; . Cả
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      338 và
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      339 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ý.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      345,
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      346 đến
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      347. Tệp được ghi vào luôn là
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      345. Khi tệp này được lấp đầy, nó được đổi tên thành
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      346;
    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 độ INFOThí 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 binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    31 triển khai trình phân tích cú pháp tệp cấu hình cơ bản cho
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    356

    Tệp

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    356 chứa các cặp khóa-giá trị được tổ chức thành các phần và trông giống như

    1. A configuration file consists of sections [marked by
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      358 header]. Một phần chứa các cặp
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      359 hoặc
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      360. Các khoảng trắng đầu và cuối được cắt bớt từ
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      361. Các dòng bắt đầu bằng
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      4 hoặc
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      363 là nhận xét

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

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    31 để phân tích cú pháp tệp
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    356, e. g. ,

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      366. đọ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ó
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      367. lấy giá trị của
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      368 từ
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      369
    Nội suy với SafeConfigParser

    Một

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    361 có thể chứa chuỗi định dạng ở dạng
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    371, đề cập đến một
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    116 khác trong phần CÙNG, hoặc một phần
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    373 [viết hoa] đặc biệt. Tuy nhiên, tính năng nội suy này chỉ được hỗ trợ trong
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    374. Ví dụ: giả sử chúng tôi có tệp cấu hình sau có tên là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    375

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

    Enter a binary string: 1011001110
    The decimal equivalent for binary "1011001110" is: 718
    The decimal equivalent for binary "1011001110" using built-in function is: 718
    56 sẽ được nội suy thành
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    377, được nội suy từ phần CÙNG và phần MẶC ĐỊNH

    Mô-đun ngày giờ

    Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    378 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

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      379. 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    380, 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 nghe trình nghe SMTP

    Để sử dụng

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    380

    Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    382 có thể được sử dụng để tạo một thông điệp email

    [TODO] more

    Mô-đun json

    JSON [Ký hiệu đối tượng JavaScript] là định dạng trao đổi dữ liệu nhẹ lấy cảm hứng từ cú pháp ký tự đối tượng JavaScript. Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    383 cung cấp triển khai cho bộ mã hóa và giải mã JSON

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      384. Tuần tự hóa
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      385 thành chuỗi được mã hóa JSON ['s' cho chuỗi]
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      386. Tạo một đối tượng Python từ chuỗi được mã hóa JSON đã cho
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      387. Nối tiếp _______46_______385 vào tệp
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      389. Tạo một đối tượng Python bằng cách đọc tệp đã cho

    Ví dụ,

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

    Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    383 [được mô tả trước đó] xử lý 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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    391 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    391 cung cấp các chức năng tương tự như mô-đun
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    383

    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      394. Trả lại biểu diễn ngâm của
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      385 dưới dạng một chuỗi
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      396. Xây dựng một đối tượng Python từ
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      397
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      398. Viết một đại diện ngâm của
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      385 đến
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      400
    • # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      401. Xây dựng một đối tượng Python đọc từ
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      400

    Mô-đun

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    403 là phiên bản cải tiến của
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    391

    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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    405 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

    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    406 có hai đối số. số tín hiệu cần xử lý và chức năng xử lý. Ví dụ,

    Làm cách nào để nối biến với chuỗi trong JavaScript?

    In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. Để nối một chuỗi, bạn thêm dấu + vào giữa các chuỗi hoặc biến chuỗi mà bạn muốn nối . let myPet = 'cá ngựa'; .

    Làm cách nào để nối số với chuỗi trong JavaScript?

    Trong javascript toán tử "+" được sử dụng để thêm số hoặc nối chuỗi . nếu một trong các toán hạng là một chuỗi "+" nối và nếu đó chỉ là các số thì nó sẽ thêm chúng.

    Cách tốt nhất để nối các chuỗi trong JavaScript là gì?

    Cách tốt nhất và nhanh nhất để nối chuỗi trong JavaScript là sử dụng toán tử + . Bạn cũng có thể sử dụng phương thức concat[].

    Cái gì được sử dụng để nối các chuỗi trong JavaScript?

    Phương thức concat[] nối hai hoặc nhiều chuỗi. Phương thức concat[] không thay đổi các chuỗi hiện có. Phương thức concat[] trả về một chuỗi mới.

Chủ Đề