Đầu ra của 8 3 trong Python là gì?

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

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

Python bằng ví dụ

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

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

  • Nhận xét. Nhận xét của Python bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 và kéo dài cho đến cuối dòng. Python không hỗ trợ bình luận nhiều dòng
    [Nhận xét cuối dòng C/C++/C#/Java bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. Họ hỗ trợ nhận xét nhiều dòng thông qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. ]
  • Chuỗi. Chuỗi của Python có thể được phân định bằng dấu nháy đơn [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7] hoặc dấu nháy kép [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8]. Python cũng hỗ trợ chuỗi nhiều dòng, được phân tách bằng dấu nháy đơn ba lần [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    9] hoặc dấu nháy kép ba lần [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    0]. Các chuỗi là bất biến trong Python
    [C/C++/C#/Java sử dụng dấu 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. ]
  • Loại dữ liệu. Python hỗ trợ các kiểu dữ liệu này.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1 [số nguyên],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2 [số dấu phẩy động],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 [Chuỗi],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 [boolean của
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 hoặc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6], v.v.
  • Cá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 uses indentation to indicate body-block. [C/C++/C#/Java use braces
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8. ] This syntax forces you to indent the program correctly which is crucial for reading your program. You can use space or tab for indentation [but not mixture of both]. Each body level must be indented at the same distance. It is recommended to use 4 spaces for each level of indentation
  • Assignment Operator.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    9
  • Arithmetic Operators.
    Enter a hex string: 1abcd
    The decimal equivalent for hex "1abcd" is: 109517
    The decimal equivalent for hex "1abcd" using built-in function is: 109517
    0 [add],
    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 [subtract],
    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 [multiply],
    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 [divide],
    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 [integer divide],
    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 [exponent],
    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 [modulus]. [
    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
    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 are not supported]
  • Compound Assignment Operators.
    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
  • Comparison 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
    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
  • Logical 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++/C#/Java 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
    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]
  • có điều kiện
  • Loop. Python does NOT support the traditional C-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
    02-loop with index.
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/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 supports variable-size dynamic array via a built-in data 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
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/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. List is similar to C/C++/C#/Java's array but NOT fixed-size. 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. You can use built-in functions such 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
    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
  • Data Structures
    • List.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/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 [mutable dynamic array]
    • Tuple.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/Cygwin] Set the script to executable, and execute the script
      $ cd /path/to/project_directory
      $ chmod u+x grade_statistics.py
      $ ./grade_statistics.py
      14 [Immutable fix-sized array]
    • Dictionary.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/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 [mutable key-value pairs, associative array, map]
    • Set.
      # [All platforms] Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # [Unix/Mac OS/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 [with unique key and mutable]
  • Sequence [String, Tuple, List] Operators and Functions
    • 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 sơ đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  3. Doc-String. Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu [chuỗi tài liệu] [Dòng 3-12] để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu là một chuỗi nhiều dòng [được phân định bằng dấu nháy đơn ba hoặc dấu ba kép], có thể được trích xuất từ ​​​​tệp nguồn để tạo tài liệu
  4. def my_sum[lst]. [Dòng 15-20]. Chúng tôi định nghĩa một hàm có tên là
    # [All platforms] Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # [Unix/Mac OS/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. thùng = [0]*10 [Dòng 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. cho hàng trong phạm vi [len [thùng]]. [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

Chủ Đề