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 <= lớp <= 100 (Dòng 68). Python hỗ trợ cú pháp này để so sánh
  8. Có một số cách in
    1. chức năng tích hợp print() (Dòng 75-80). Theo mặc định,
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      62 in một dòng mới ở cuối. Bạn cần bao gồm đối 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
      63 để chặn dòng mới
    2. in (str. định dạng()) (Dòng 51, 53). Kiểu mới của Python 3 cho chuỗi được định dạng thông qua hàm thành viên lớp
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      3
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      65. Chuỗi mà phương thức này được gọi có thể chứa văn bản bằng chữ hoặc các trường thay thế được phân tách bằng dấu ngoặc nhọn
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      8. Mỗi trường thay thế chứa chỉ mục số của đối số vị trí hoặc tên của đối số từ khóa, với các chỉ định định dạng giống như C bắt đầu bằ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
      67 (thay 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
      6 trong C), 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
      69 cho số nguyê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
      70 cho số dấu phẩy động,
    3. print('chuỗi định dạng' % args) (Dòng 81). Kiểu cũ của Python 2 cho chuỗi được định dạng bằng toán tử
      Enter a hex string: 1abcd
      The decimal equivalent for hex "1abcd" is: 109517
      The decimal equivalent for hex "1abcd" using built-in function is: 109517
      6.
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      76 có thể chứa các bộ xác định định dạng giống như C, 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
      77 cho số nguyê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
      78 cho số dấu phẩy độ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
      79 cho chuỗi. This line is included in case you need to read old programs. Tôi khuyên bạn nên sử dụng kiểu định dạng mới của Python 3
  9. lớp = int(đầu vào('Nhập. ')) (Dòng 66, 72). Bạn có thể đọc đầu vào từ thiết bị đầu vào tiêu chuẩn (mặc định là bàn phím) thông qua chức năng
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    80 tích hợp. Khi 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
    80 trả về một chuỗi, chúng ta cần ép kiểu nó thành
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1
  10. if __name__ == '__main__'. (Dòng 87). Khi bạn thực thi một mô-đun Python thông qua Trình thông dịch Python, biến toàn cục
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 được đặ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
    84. Mặt khác, khi một mô-đun được nhập vào một mô-đun khá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
    83 của nó được đặt thành tên mô-đun. Do đó, mô-đun trên sẽ được thực thi nếu nó được tải bởi trình thông dịch Python, nhưng không được nhập bởi mô-đun khác. Đây là một thực hành tốt để thử nghiệm một mô-đun

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

This is a number guessing game. Nó minh họa vòng lặp lồng nhau nếu (

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

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
86), 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 với cờ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 và mô-đun
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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. 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Làm thế nào nó hoạt động
  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
    90 (Dòng 12). Chúng ta sẽ 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
    92 của 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
    89 để tạo một số bí mật. Trong Python, bạn cầ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
    93 mô-đun (thư viện bên ngoài) trước khi sử dụng nó
  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
    94 (Line 15). Tạo một số nguyên ngẫu nhiên giữ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
    95 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
    96 (bao gồm cả hai)
  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
    97 (Line 17). Python hỗ trợ loại
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 cho các giá trị boolean của
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 hoặc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6. Chúng tôi sử dụng cờ boolean này để kiểm soát vòng lặp
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 của chúng tôi
  4. Cú pháp của 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 (Dòng 19) là
  5. Cú pháp cho lồng nhau-
    $ 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: ***
    03 (Dòng 22) là

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

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

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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức chương trình thành các chức năng
  2. Chúng tôi triển khai hai phiên bản hàm để kiểm tra số ma thuật - phiên bản
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1 (Dòng 10) và phiên bản
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 (Dòng 25) - cho mục đích học tập
  3. def isMagic(số. int, ma thuậtDigit. int = 8) -> bool. (Dòng 10). The hightlight parts are known as type hint annotations. They are ignored by Python Interpreter, and merely serves as documentation
  4. nếu __name__ == '__main__'. (Dòng 51). Khi bạn thực thi một mô-đun Python thông qua Trình thông dịch Python, biến toàn cục
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 được đặ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
    84. On the other hand, when a module is imported into another module, 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. Do đó, mô-đun trên sẽ được thực thi nếu nó được tải bởi trình thông dịch Python, nhưng không được nhập bởi mô-đun khác. Đây là một thực hành tốt để thử nghiệm một mô-đun

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

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

Enter a hex string: 1abcd
The decimal equivalent for hex "1abcd" is: 109517
The decimal equivalent for hex "1abcd" using built-in function is: 109517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Làm thế nào nó hoạt động
  1. Công thức chuyển đổi 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: ***
    11, trong đó
    $ 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: ***
    12
  2. $ 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: ***
    13 (Dòng 12). Chúng tôi sẽ sử dụng chức năng
    $ 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: ***
    15 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 để kết thúc chương trình cho đầu vào không hợp lệ. Trong Python, chúng ta cần import module (thư viện bên ngoài) trước khi sử dụng
  3. $ 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: ***
    16 (Dòng 21). 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. Nó chỉ hỗ trợ vòng lặp
    $ 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: ***
    18 để lặp qua từng
    $ 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 trong
    $ 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. Chúng tôi sử dụng hàm tích hợp 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
    57 để tạo danh sách
    $ 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: ***
    22, sau đó lặp qua từng mục trong danh sách đã tạo
  4. In Python, we can iterate through each character of a string via 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, e. g. , Đối với ví dụ này, chúng ta không thể sử dụng ở trên vì chúng ta cần chỉ mục của ký tự để thực hiện chuyển đổi
  5. $ 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: ***
    24 (Dòng 22). Trong Python, bạn có thể sử dụng toán tử lập chỉ mục
    $ 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: ***
    25 để trích xuất ký tự thứ 26 của
    $ 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ưu ý rằng Python không hỗ trợ ký tự, nhưng coi ký tự là chuỗi 1 ký tự
  6. $ 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: ***
    27 (Dòng 23). Python hỗ trợ toán tử lũy thừa (hoặc lũy thừa) ở 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
    5. Lưu ý rằng chỉ mục chuỗi bắt đầu từ 0 và tăng dần từ trái sang phải. Mặt khác, số mũ của chữ số hex bắt đầu từ 0, nhưng tăng dần từ phải sang trái
  7. $ 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: ***
    29 trường hợp chuỗi 1 ký tự cho
    $ 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: ***
    30,
    $ 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: ***
    31,
    $ 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: ***
    32,
    $ 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: ***
    33 và các chuỗi khác, có thể được xử lý bằng 5 trường hợp lồng nhau nếu như sau
    1. $ 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: ***
      34 (Line 24). chúng tôi chuyển đổi chuỗi
      $ 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: ***
      34 thành
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1
      $ 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: ***
      37 thông qua chức năng tích hợp sẵn
      $ 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
    2. $ 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: ***
      39 (Dòng 26). không có gì. In Python, you need to include a dummy statement called
      $ 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 (Line 28) in the body block
    3. $ 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: ***
      32 (Dòng 28). Để chuyển đổi chuỗi 1 ký tự
      $ 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: ***
      32 thành
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1
      $ 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: ***
      44, chúng ta sử dụng hàm có sẵn ___97_______45 để lấy mã Unicode _______6_______1 của ______97_______47, trừ cơ số _______97_______48 và cộng 10
    4. $ 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: ***
      49 (Dòng 30). Python supports a data structure called dictionary (associative array), which contains key-value pairs. Chúng tôi đã tạo một từ điển
      $ 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: ***
      50 (Dòng 15) để ánh xạ
      $ 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 tới
      $ 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: ***
      52,
      $ 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: ***
      53 tới
      $ 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: ***
      54, v.v. We can then reference the dictionary via
      $ 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: ***
      55 to retrieve its value (Line 31)
    5. khác (Dòng 32). chúng tôi sử dụng
      $ 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: ***
      56 để chấm dứt chương trình. Chúng tôi trả về một mã khác không để biểu thị việc chấm dứt bất thường

Example bin2dec. py - Binary to Decimal Conversion

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

Enter a binary string: 1011001110
The decimal equivalent for binary "1011001110" is: 718
The decimal equivalent for binary "1011001110" using built-in function is: 718
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
0
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức mã trong các chức năng
  2. The conversion formula is.
    $ 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: ***
    57, trong đó
    $ 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: ***
    58
  3. Bạn có thể sử dụng hàm có sẵn
    $ 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: ***
    59 để chuyển đổi một chuỗi số từ số đã cho
    $ 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: ***
    60 sang số thập phân (Dòng 38)

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

Chương trình này nhắc người dùng nhập số thập phân và in số thập lục phân tương đương của nó. 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
1____46_______2
Làm thế nào nó hoạt động
  1. Chúng tôi sử dụng mô đun/phép chia nhiều lần để lấy các chữ số hex theo thứ tự ngược lại
  2. Chúng tôi sử dụng danh sách tra cứu (Dòng 11) để chuyển đổi
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1
    $ 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: ***
    62 thành chữ số hex
    $ 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: ***
    63
  3. You can use built-in function
    $ 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: ***
    64,
    $ 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: ***
    65,
    $ 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: ***
    66 to convert decimal to hexadecimal, octal and binary, respectively; or use the more general
    $ 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. e. g. ,

wc ví dụ. py - Word Count

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

$ 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: ***
68 trong Unix). It illustrates the text file input and text string processing

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

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
0
Làm thế nào nó hoạt động
  1. nhập sys (Dòng 14). Chúng tôi sử dụng mô-đun
    $ 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 (@ https. // tài liệu. con trăn. org/3/library/sys. html) from the Python's standard library to retrieve the command-line arguments kept 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
    04
    $ 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, and to terminate the program via
    $ 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: ***
    72. In Python, you need 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
    93 the module before using it
  2. Các đối số dòng lệnh được lưu trữ trong một biến
    $ 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, là 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 (mảng động của Python). The first item 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
    04
    $ 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: ***
    77 is the script name, followed by the other command-line arguments
  3. nếu len(sys. argv). = 2. (Dòng 15). Chúng tôi sử dụng chức năng tích hợp sẵn
    $ 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: ***
    78 để xác minh rằng độ dài của đối số dòng 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
    04 là 2
  4. với mở (sys. argv[1]) dưới dạng tệp tin. (Line 25). Chúng tôi mở tệp thông qua câu lệnh
    $ 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, câu lệnh này sẽ tự động đóng tệp khi thoát
  5. for line in infile. (Dòng 26). Chúng tôi sử dụng vòng lặp
    $ 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: ***
    81 (Dòng 29) để xử lý từng
    $ 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: ***
    82 của
    $ 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: ***
    83, trong đó
    $ 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: ***
    82 thuộc về lớp dựng sẵn "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3" (có nghĩa là hỗ trợ chuỗi @ ). We use the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 class' member functions
    $ 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: ***
    87 to strip the leading and trailing white spaces; and
    $ 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: ***
    88 to split the string into 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 words
  6. Chúng tôi cũng gọi tiện ích Unix "
    $ 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: ***
    68" thông qua lệnh shell bên ngoài theo 2 cách. thông qua
    $ 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: ***
    91 và
    $ 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: ***
    92

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

This example reads the input and output filenames from the command-line and replaces the reserved HTML characters by their corresponding HTML entities. Nó minh họa đầu vào/đầu ra tập tin và thay thế 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
4
Làm thế nào nó hoạt động
  1. nhập sys (Dòng 14). Chúng 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
    93 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 (@ https. // tài liệu. con trăn. org/3/library/sys. html). Chúng tôi truy xuất các đối số dòng lệnh từ
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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
    $ 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, trong đó
    $ 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: ***
    77 là tên tập lệnh;
  2. với mở (sys. argv[1]) dưới dạng tệp tin, mở(sys. argv[2], 'w') dưới dạng tệp ngoài. (Dòng 21). Chúng tôi sử dụng câu lệnh
    $ 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, tự động đóng các tệp khi thoát, để 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: ***
    83 để đọc (mặc định) 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.
    01 để ghi (
    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.
    02)
  3. cho dòng trong infile. (Dòng 22). Chúng tôi sử dụng một vòng lặp
    $ 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: ***
    81 để xử lý từng
    $ 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: ***
    82 của
    $ 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: ***
    83, trong đó
    $ 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: ***
    82 thuộc về lớp dựng sẵn "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3" (có nghĩa là hỗ trợ chuỗi @ ). Chúng tôi sử dụng hàm thành viên của lớp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3
    Enter 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.
    09 để loại bỏ các khoảng trắng ở cuối (bên phải);
  4. Trăn 3. 2 giới thiệu một mô-đun
    Enter 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.
    11 mới, với chức nă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.
    12 để thoát khỏi các ký tự dành riêng cho HTML.
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    5

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

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

Enter 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.
13) và biểu thức chính quy (sử dụng mô-đun
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)

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

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
6
Làm thế nào nó hoạt động

Giới thiệu

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

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

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

Python có 3 phiên bản

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

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

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

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

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

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

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

Cài đặt

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

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

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

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

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

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

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
8
Ubuntu (16. 04LTS)

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

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

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

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

$ 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: ***
0
các cửa sổ

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

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

[LÀM]

Tài liệu

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

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

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

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

$ 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: ***
1

Dấu nhắc lệnh của Python được ký hiệu 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.
15. Bạn có thể nhập câu lệnh Python tại dấu nhắc lệnh của Python, e. g. ,

$ 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: ***
2

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

  • $ 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: ***
    15
  • (Mac OS X và Ubuntu)
    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.
    17
  • (Windows)
    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.
    18 theo sau là Enter

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

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

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

Enter 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.
19" trong thư mục bạn chọn

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

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

$ 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: ***
4
Chạy tập lệnh Python

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

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

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

Executable Shell Script của Unix

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

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

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

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

Tiện ích

Enter 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.
36 sẽ định vị Trình thông dịch Python (từ mục nhập
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). Cách tiếp cận này được khuyến nghị vì nó không mã hóa cứng đường dẫn của Python

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

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

Enter 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" với phần có thể phiên dịch Python để làm cho tập lệnh Python có thể thực thi được

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

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

Enter 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.
19" bên trong Trình thông dịch của Python

  • Bạn có thể sử dụng đường dẫn tuyệt đối hoặc tương đối cho tên tệp. Nhưng,
    Enter 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.
    40 (đối với thư mục chính) không hoạt động?
  • Hàm tích hợp sẵn
    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.
    41 sẽ mở tệp ở chế độ chỉ đọc mặc định;

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

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

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

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

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

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

Xem "" để biết chi tiết

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

Bình luận

Một nhận xét Python bắt đầu bằng dấu thă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.
21) và kéo dài đến cuối dòng hiện tại. Các nhận xét bị Trình thông dịch Python bỏ qua, nhưng chúng rất quan trọng trong việc cung cấp giải thích và tài liệu cho người khác (và chính bạn ba ngày sau) để đọc chương trình của bạn. Sử dụng bình luận một cách tự do

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

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

Các câu lệnh

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

  1. An expression 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, square bracket
    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, and curly braces
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8 can span multiple lines
  2. Dấu gạch chéo ngược (
    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.
    48) ở cuối dòng biểu thị sự tiếp tục của dòng tiếp theo. Đây là một quy tắc cũ và KHÔNG được khuyến nghị vì nó dễ bị lỗi

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
7) ở cuối câu lệnh Python. Nhưng bạn có thể đặt nhiều câu lệnh trên một dòng, được phân tách bằng dấu chấm phẩy (
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
7). Ví dụ như,

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
8 để nhóm các câu lệnh trong một khối nội dung, Python sử dụng thụt đầu dòng cho khối nội dung. Nói cách khác, thụt đầu dòng có ý nghĩa về mặt cú pháp trong Python - khối cơ thể phải được thụt lề đúng cách. Đây là một cú pháp tốt để buộc bạn phải thụt lề các khối một cách chính xác để dễ hiểu

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

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

# (Unix/Mac OS/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
47,
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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à định nghĩa hàm (
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), bắt đầu bằng dòng tiêu đề kết thúc bằng dấu hai chấ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
67);

Ví dụ như,

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

Dấu hai chấm (

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

# (Unix/Mac OS/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) và thụt đầu dòng ở phần thân có lẽ là tính năng kỳ lạ nhất trong Python, nếu bạn đến từ C/C++/C#/Java. Python áp đặt các quy tắc thụt lề nghiêm ngặt để buộc các lập trình viên viết mã có thể đọc được

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

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

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

Ví dụ,

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

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

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

Mã định danh bắt đầu bằng một chữ cái (

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.
58,
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.
59) hoặc dấu gạch dưới (
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.
60), theo sau là 0 hoặc nhiều chữ cái, dấu gạch dưới và chữ 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.
61). Python không cho phép các ký tự đặc biệt như
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.
62 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.
63

từ khóa

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

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6,
    Enter 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 (boolean và chữ đặc biệ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
    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.
    68,
    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.
    69
  • $ 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: ***
    03,
    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,
    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,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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,
    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,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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,
    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,
    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,
    $ 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,
    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 (flow control)
  • 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,
    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,
    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.
    82,
    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,
    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 (chức nă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.
    85
  • 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,
    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,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 (operators)
  • 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,
    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,
    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,
    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,
    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 (error handling)
  • 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.
    96,
    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.
    97,
    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.
    98
Variable Naming Convention

A variable name is a noun, or a noun phrase made up of several words. There are two convenctions

  1. In lowercase words and optionally joined with underscore if it improves readability, e. g. , num_students,
    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.
    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
    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
    01, etc
  2. In the so-called camel-case where the first word is in lowercase, and the remaining words are initial-capitalized, 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
    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
    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
    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
    05,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    06, 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
    07. (This is the Java's naming convention. )
Recommendations
  1. It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable, 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
    02, but not
    1
    2
    3
    4
    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 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
    10, to store the number of students. It is alright to use abbreviations, 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
    11 for index
  2. Do not use meaningless names 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
    12,
    1
    2
    3
    4
    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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    14,
    1
    2
    3
    4
    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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    16,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    17,
    1
    2
    3
    4
    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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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
    20,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    21,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    22,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    23 (what is the purpose of this exercise?), 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
    24 (What is this example about?)
  3. Avoid single-letter names 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
    15,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    16,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    17,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    12,
    1
    2
    3
    4
    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,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    14, which are easier to type but often meaningless. Exceptions are common names 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
    10,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    32,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    33 for coordinates,
    1
    2
    3
    4
    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 for index. Long names are harder to type, but self-document your program. (I suggest you spend sometimes practicing your typing. )
  4. Use singular and plural nouns prudently to differentiate between singular and plural variables.   For example, you may use the variable
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    35 to refer to a single row number and the variable
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    36 to refer to many rows (such as a list of rows - to be discussed later)
Constants

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

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

It is a convention to name a variable in uppercase (joined with underscore), 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
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
40, để chỉ ra rằng nó không nên được sửa đổi trong chương trình. Nevertheless, nothing prevents it from being modified

Data Types. Number, String and List

Python supports various number type such as

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

Python supports text string (a sequence of characters). In Python, strings can be delimited with single-quotes or double-quotes, 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
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
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
53 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
54 (empty string)

Python supports a dynamic-array structure 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, denoted 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
56. You can reference the i-th element 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
07. Python'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
04 is similar to C/C++/Java's array, but it is NOT fixed size, and can be expanded dynamically during runtime

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

Console Input/Output. input() and print() Built-in Functions

You can use built-in 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
80 to read input from the console (as a string) 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
62 to print output to the console. For example,

print()

The built-in 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
62 has the following signature

Ví dụ như,

print()'s separator (sep) and ending (end)

You can use the optional keyword-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
62 to set the separator string (default is space), 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
63 for ending string (default is newline). For examples,

print in Python 2 vs Python 3

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
64", without the parentheses (because
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
65 is a keyword in Python 2). In Python 3, parentheses are required 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
62 is a function. For example,

Important. Always 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
62 function with parentheses, for portability

Data Types and Dynamic Typing

Python has a large number of built-in data types, such as Numbers (Integer, Float, Boolean, Complex Number), String, List, Tuple, Set, Dictionary and File. More high-level data types, such as Decimal and Fraction, are supported by external modules

You can use the built-in function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
68 to check the type of a variable or literal

Number Types

Python supports these built-in number types

  1. Integers (type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1). e. g. ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    42,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    43. Unlike C/C++/Java, integers are of unlimited size in Python. For example, You can also express integers in hexadecimal with prefix
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    72 (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
    73); in octal with prefix
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    74 (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
    75); and in binary with prefix
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    76 (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
    77). 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
    78,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    79,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    80,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    81
  2. Floating-point numbers (type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2). e. g. ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    83,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    84,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    85,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    86, with a decimal point and an optional exponent (denoted 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
    87 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
    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
    2s are 64-bit double precision floating-point numbers. For example,
  3. Booleans (type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4). takes a value 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. Take note of the spelling in initial-capitalized. In Python, integer
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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, an empty value (such as empty string
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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
    54, empty list
    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, empty tuple
    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, empty dictionary
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8), 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.
    66 are treated 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
    6; anything else are treated 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
    5. Booleans can also act as integers in arithmetic operations with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 for
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 and
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 for
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6. For example,
    $ 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: ***
    6
  4. Complex Numbers (type
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    06). e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    07,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    08. Complex numbers have a real part and an imaginary part denoted with suffix of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    16 (or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    10). For example,
  5. Other number types are provided by external modules, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    11 module for decimal fixed-point numbers,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    12 module for rational numbers

Dynamic Typing and Assignment Operator

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

Python associates types with objects, instead of variables. Nghĩa là, một biến không có kiểu cố định và có thể được gán cho một đối tượng thuộc bất kỳ kiểu nào. A variable simply provides a reference to an object

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

You can use built-in function

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

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

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

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

Tóm lại, một biến không liên kết với một loại. Thay vào đó, một loại được liên kết với một đối tượng. Một biến cung cấp một tham chiếu đến một đối tượng (thuộc một loại nhất định)

Kiểm tra loại Instance. isinstance(ví dụ, loại)

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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
18 để kiểm tra xem
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
19 có thuộc về
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
20 không. 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: ***
7
Toán tử gán (=)

Trong Python, bạn không cần khai báo biến trước khi sử dụng biến. Phép gán ban đầu tạo một biến và liên kết giá trị được gán với biến. Ví dụ,

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

Ví dụ,

Toán tử gán là liên kết phải, i. e. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
21 được hiểu là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
22

toán tử del

Bạn có thể sử dụng toá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
35 để xóa một biến. Ví dụ,

Phép toán số

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

Python supports these arithmetic operators

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

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

Enter a 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 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
5. For example
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 is the same as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32

Increment/Decrement (++, --)?

Python does not support increment (

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) and decrement (
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) operators (as in C/C++/Java). You need to use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 for increment

Python accepts

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

Mixed-Type Operations

For mixed-type operations, e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
41 (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
42), the value of the "smaller" type is first promoted to the "bigger" type. It then performs the operation in the "bigger" type and returns the result in the "bigger" type. In Python,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
1 is "smaller" 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2, which is "smaller" than
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
06

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

Python supports these relational (comparison) operators that 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 value 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

OperatorModeUsageDescriptionExample==
=
<
<=
>
>=Binaryx == y
x . = y
x < y
x <= y
x > y
x >= yComparison
Return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 of either
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5 or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
6in
not inBinaryx in seq
x not in seqCheck if
1
2
3
4
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 contained in the sequence
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53
Return bool of either True or Falselst = [1, 2, 3]
x = 1
x in lst ⇒ Falseis
is notBinaryx is y
x is not yCheck if
1
2
3
4
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 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53 are referencing the same object
Return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 of either
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5 or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
6

Example. [TODO]

Logical Operators (and, or, not)

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

OperatorModeUsageDescriptionExampleandBinaryx and yLogical ANDorBinaryx or yLogical ORnotUnarynot xLogical NOT

Notes

  • Python's logical operators are typed out in word, unlike C/C++/Java which uses symbols
    Enter a 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 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
    01
  • Python does not have an exclusive-or (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62) boolean operator

Example. [TODO]

Built-in Functions

Python provides many built-in functions for numbers, including

  • Mathematical functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65, etc
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66 to get the type
  • Type conversion 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,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    70, etc
  • Base radix conversion functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    71,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    72,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    73

Ví dụ như,

Bitwise Operators (Advanced)

Python supports these bitwise operators

OperatorModeUsageDescriptionExample
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
74
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
75&binaryx & ybitwise ANDx & y ⇒ 0b10000001|binaryx ! ybitwise ORx | y ⇒ 0b10001111~Unary~xbitwise NOT (or negate)~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110<>binaryx >> countbitwise Right-Shift (padded with zeros)x >> 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 list
Copying a list
list-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.
0
Iterating through a Dictionary

There are a few ways to iterate through an dictionary

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

The built-in function

Enter a 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.
1
The 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 2
Example 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 Function
Example. 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
1
Thí dụ. Sử dụng tệp nhật ký xoay vòng với RotatingFileHandler
  1. Chúng tôi giữ tất cả các tham số ghi nhật ký trong từ điển, thường được truy xuất từ ​​tệp cấu hình
  2. Trong hàm tạo của
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 độ INFO
Thí dụ. Tách Nhật ký LỖI và Nhật ký INFO với Định dạng khác nhau

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

Mô-đun

Enter a 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.