Putar d kali dalam ahli penugasan python

I shall assume that you are familiar with some programming languages such as C/C++/Java. This article is NOT meant to be an introduction to programming

I personally recommend that you learn a traditional general-purpose programming language (such as C/C++/Java) before learning scripting language like Python/JavaScript/Perl/PHP because they are less structure than the traditional languages with many fancy features

Python By Examples

This section is for experienced programmers to look at Python's syntaxes and those who need to refresh their memory. For novices, go to the next section

Syntax Summary and Comparison

  • Comment. Python's comment begins with a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 and lasts until the end-of-line. Python does not support multi-line comments
    (C/C++/C#/Java end-of-line comment begins with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. They support multi-line comments via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. )
  • String. Python's string can be delimited by either single quotes (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7) or double quotes (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8). Python also supports multi-line string, delimited by either triple-single (
    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). Strings are immutable in Python
    (C/C++/C#/Java use double quotes for string and single quotes for character. They do not support multi-line string. )
  • Variable Type Declaration. Like most of the scripting interpreted languages (such as JavaScript/Perl), Python is dynamically typed. You do NOT need to declare variables (name and type) before using them. A variables is created via the initial assignment. Python associates types with the objects, not the variables, i. e. , a variable can hold object of any types
    (In C/C++/C#/Java, you need to declare the name and type of a variable before using it. )
  • Data Types. Python support these data types.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1 (integers),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2 (floating-point numbers),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 (String),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 (boolean of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6), and more
  • Statements. Python's statement ends with a newline
    (C/C++/C#/Java's statement ends with a semi-colon (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    7))
  • Compound Statements and Indentation. Python uses indentation to indicate body-block. (C/C++/C#/Java use braces
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8. ) This syntax forces you to indent the program correctly which is crucial for reading your program. You can use space or tab for indentation (but not mixture of both). Each body level must be indented at the same distance. It is recommended to use 4 spaces for each level of indentation
  • Assignment Operator.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    9
  • Arithmetic Operators.
    Enter a 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)
  • Conditional
  • Loop. Python TIDAK mendukung 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 tradisional dengan 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. membership test
    • 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
    • # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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. pengindeksan
    • # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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. mengiris
    • # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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
    Hanya untuk urutan (daftar) yang dapat diubah
    • Penugasan melalui
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 (mengindeks) dan
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 (mengiris)
    • Penugasan melalui
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 (gabungan gabungan),
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      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 (pengulangan gabungan)
    • # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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. menghapus
    • # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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, ________46______44
  • Definisi Fungsi

Contoh grade_statistics. py - Sintaks dan Konstruksi Dasar

Contoh ini berulang kali meminta pengguna untuk memberi nilai (antara 0 dan 100 dengan validasi input). Kemudian menghitung jumlah, rata-rata, minimum, dan mencetak histogram horizontal

Contoh ini mengilustrasikan sintaks dan konstruksi Python dasar, seperti komentar, pernyataan, indentasi blok, bersyarat

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

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

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

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

# (Unix/Mac OS/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 dan fungsi

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

Untuk menjalankan skrip 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

Keluaran yang diharapkan adalah

$ 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: ***
Bagaimana itu bekerja
  1. #. /usr/bin/env python3 (Baris 1) hanya berlaku untuk lingkungan Unix. Ini dikenal sebagai Hash-Bang (atau She-Bang) untuk menentukan lokasi Python Interpreter, sehingga skrip dapat dijalankan secara langsung sebagai program mandiri
  2. # -*- pengkodean. UTF-8 -*- (Baris 2, opsional) menentukan skema pengkodean sumber untuk menyimpan file sumber. Kami memilih dan merekomendasikan UTF-8 untuk internasionalisasi. Format khusus ini dikenali oleh banyak editor populer karena menyimpan kode sumber dalam format penyandian yang ditentukan
  3. Doc-String. Skrip dimulai dengan apa yang disebut doc-string (string dokumentasi )(Baris 3-12) untuk menyediakan dokumentasi untuk modul Python ini. Doc-string adalah string multi-baris (dibatasi dengan kutip triple-single atau triple-double), yang dapat diekstraksi dari file sumber untuk membuat dokumentasi
  4. def my_sum(lst). (Baris 15-20). Kami mendefinisikan fungsi yang disebut
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 yang mengambil
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 dan mengembalikan jumlah item. Ini menggunakan perulangan untuk setiap perulangan untuk mengulangi semua item dari
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 yang diberikan. Karena Python bersifat interpretatif, Anda perlu mendefinisikan fungsi terlebih dahulu, sebelum menggunakannya. Kami memilih nama fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 untuk membedakan dari fungsi bawaan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. tempat sampah = [0]*10 (Baris 38). Python mendukung operator pengulangan (
    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). Pernyataan ini membuat daftar sepuluh angka nol. Demikian pula, operator pengulangan (*) dapat diterapkan pada string (Baris 59)
  6. untuk baris dalam rentang(len(bins)). (Baris 48, 56). Python hanya mendukung
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Itu TIDAK mendukung C-seperti
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 tradisional dengan index. Oleh karena itu, kita perlu menggunakan fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 bawaan untuk membuat
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 dari indeks
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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, lalu terapkan loop
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 pada indeks
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 <= nilai <= 100 (Baris 68). Python mendukung sintaks ini untuk perbandingan
  8. Ada beberapa cara mencetak
    1. print() fungsi bawaan (Baris 75-80). Secara default,
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 mencetak baris baru di bagian akhir. Anda perlu menyertakan argumen
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk menekan baris baru
    2. cetak (str. format()) (Baris 51, 53). Gaya baru Python 3 untuk string terformat melalui
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 fungsi anggota kelas
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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. The string on which this method is called can contain literal text or replacement fields delimited by 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. Setiap bidang pengganti berisi indeks numerik dari argumen posisi, atau nama argumen kata kunci, dengan penentu format seperti C yang dimulai dengan
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 (bukan
      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 dalam C) seperti
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk bilangan bulat,
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk angka titik-mengambang,
    3. print('formatting-string' % args) (Baris 81). Gaya lama Python 2 untuk string terformat menggunakan 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
      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 dapat berisi penentu format seperti C, seperti
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk bilangan bulat,
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk angka floating-point,
      # (All platforms) Invoke Python Interpreter to run the script
      $ cd /path/to/project_directory
      $ python3 grade_statistics.py
      
      # (Unix/Mac OS/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 untuk string. Baris ini disertakan jika Anda perlu membaca program lama. Saya sarankan Anda menggunakan gaya pemformatan Python 3 yang baru
  9. nilai = int(input('Masukkan. ')) (Baris 66, 72). Anda dapat membaca input dari perangkat input standar (default pada keyboard) melalui fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 bawaan. Karena fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 mengembalikan sebuah string, kita perlu mentransmisikannya ke
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. jika __nama__ == '__main__'. (Baris 87). Saat Anda menjalankan modul Python melalui Python Interpreter, variabel global ________46______83 diatur ke
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Di sisi lain, ketika modul diimpor ke modul lain,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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-nya disetel ke nama modul. Oleh karena itu, modul di atas akan dieksekusi jika dimuat oleh juru bahasa Python, tetapi tidak diimpor oleh modul lain. Ini adalah praktik yang baik untuk menguji modul

Contoh angka_tebakan. py - Tebak Angka

Ini adalah permainan tebak angka. Ini menggambarkan nested-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
86),
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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 dengan flag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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, dan modul
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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. Sebagai contoh,

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
Bagaimana itu bekerja
  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 (Baris 12). Kita akan menggunakan fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 modul ________46______92 untuk menghasilkan nomor rahasia. Dalam Python, Anda perlu
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 modul (perpustakaan eksternal) sebelum menggunakannya
  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 (Baris 15). Hasilkan bilangan bulat acak antara
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 dan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 (keduanya inklusif)
  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 (Baris 17). Python mendukung tipe
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 untuk nilai boolean
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 atau
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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. Kami menggunakan bendera boolean ini untuk mengontrol
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 kami
  4. Sintaks untuk
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 (Baris 19) adalah
  5. Sintaks untuk nested-
    $ 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 (Baris 22) adalah

Contoh magic_number. py - Periksa apakah Angka Berisi Angka Ajaib

Contoh ini meminta pengguna untuk memasukkan nomor, dan memeriksa apakah nomor tersebut berisi angka ajaib. Contoh ini menggambarkan fungsi,

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

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
Bagaimana itu bekerja
  1. Kami mengatur program menjadi fungsi
  2. Kami menerapkan dua versi fungsi untuk memeriksa angka ajaib - versi
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (Baris 10) dan versi
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 (Baris 25) - untuk tujuan akademik
  3. def isMagic(nomor. int, magicDigit. int = 8) -> bool. (Baris 10). Bagian hightlight dikenal sebagai anotasi petunjuk jenis. Mereka diabaikan oleh Python Interpreter, dan hanya berfungsi sebagai dokumentasi
  4. jika __nama__ == '__main__'. (Baris 51). Saat Anda menjalankan modul Python melalui Python Interpreter, variabel global ________46______83 diatur ke
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Di sisi lain, ketika modul diimpor ke modul lain,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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-nya disetel ke nama modul. Oleh karena itu, modul di atas akan dieksekusi jika dimuat oleh juru bahasa Python, tetapi tidak diimpor oleh modul lain. Ini adalah praktik yang baik untuk menguji modul

Contoh hex2dec. py - Konversi Heksadesimal Ke Desimal

Contoh ini meminta pengguna untuk string heksadesimal (hex), dan mencetak padanan desimalnya. Ini mengilustrasikan for-loop dengan indeks, nested-if, operasi string dan kamus (array asosiatif). Sebagai contoh,

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
Bagaimana itu bekerja
  1. Rumus konversinya adalah.
    $ 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, di mana
    $ 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 (Baris 12). Kita akan menggunakan fungsi
    $ 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 modul
    $ 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 untuk menghentikan program karena input yang tidak valid. Di Python, kita perlu mengimpor modul (perpustakaan eksternal) sebelum menggunakannya
  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 (Baris 21). Python tidak mendukung 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 tradisional dengan index. Ini hanya mendukung
    $ 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 loop untuk mengulangi setiap
    $ 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 di
    $ 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. Kami menggunakan fungsi bawaan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 untuk menghasilkan daftar
    $ 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, lalu mengulangi setiap item dalam daftar yang dihasilkan
  4. Dalam Python, kita dapat mengulangi setiap karakter string melalui
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. , Untuk contoh ini, kami tidak dapat menggunakan yang di atas karena kami memerlukan indeks karakter untuk melakukan konversi
  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 (Baris 22). Dengan Python, Anda dapat menggunakan operator pengindeksan
    $ 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 untuk mengekstrak
    $ 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: ***
    karakter ke-26. Perhatikan bahwa Python tidak mendukung karakter, tetapi perlakukan karakter sebagai string 1 karakter
  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 (Line 23). Python mendukung operator eksponen (atau pangkat) dalam bentuk
    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. Perhatikan bahwa indeks string dimulai dari 0, dan bertambah dari kiri ke kanan. Di sisi lain, eksponen digit hex dimulai dari 0, tetapi meningkat dari kanan ke kiri
  7. Ada
    $ 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 kasus string 1 karakter untuk
    $ 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, dan lainnya, yang dapat ditangani oleh 5 kasus nested-if sebagai berikut
    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 (Baris 24). kami mengonversi string
      $ 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 menjadi
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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 melalui
      $ 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 fungsi bawaan
    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 (Baris 26). tidak apa-apa. Dalam Python, Anda perlu menyertakan pernyataan tiruan bernama
      $ 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 (Baris 28) di blok badan
    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 (Baris 28). Untuk mengonversi string 1 karakter
      $ 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 menjadi
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      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, kami menggunakan fungsi bawaan
      $ 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: ***
      45 untuk mendapatkan Unicode
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1 dari
      $ 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: ***
      47, kurangi dengan basis
      $ 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: ***
      48 dan tambahkan 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 (Baris 30). Python mendukung struktur data yang disebut kamus (array asosiatif), yang berisi pasangan kunci-nilai. Kami membuat kamus
      $ 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 (Baris 15) untuk memetakan
      $ 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 ke
      $ 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 ke
      $ 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, dan seterusnya. Kami kemudian dapat merujuk kamus melalui
      $ 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 untuk mengambil nilainya (Baris 31)
    5. lainnya (Baris 32). kami menggunakan
      $ 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 untuk menghentikan program. Kami mengembalikan kode bukan nol untuk menunjukkan penghentian yang tidak normal

Contoh bin2dec. py - Konversi Biner ke Desimal

Contoh ini meminta pengguna untuk string biner (dengan validasi input), dan mencetak padanan desimalnya. Sebagai contoh,

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
Bagaimana itu bekerja
  1. Kami mengatur kode dalam fungsi
  2. Rumus konversinya adalah.
    $ 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, di mana
    $ 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. Anda dapat menggunakan fungsi bawaan
    $ 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 untuk mengubah string angka dari
    $ 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 yang diberikan menjadi desimal (Baris 38)

Contoh dec2hex. py - Konversi Desimal ke Heksadesimal

Program ini meminta pengguna untuk angka desimal, dan mencetak ekuivalen heksadesimalnya. Sebagai contoh,

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

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

# (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
$ cd /path/to/project_directory
$ chmod u+x grade_statistics.py
$ ./grade_statistics.py
2
Bagaimana itu bekerja
  1. Kami menggunakan modulus/pembagian berulang kali untuk mendapatkan digit hex dalam urutan terbalik
  2. Kami menggunakan daftar pencarian (Baris 11) untuk mengonversi
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 menjadi digit 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. Anda dapat menggunakan fungsi bawaan
    $ 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 untuk mengonversi desimal menjadi heksadesimal, oktal, dan biner; . e. g. ,

Contoh wc. py - Jumlah Kata

Contoh ini membaca nama file dari baris perintah dan mencetak jumlah baris, kata, dan karakter (mirip dengan utilitas

$ 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 di Unix). Ini menggambarkan input file teks dan pemrosesan string teks

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

# (Unix/Mac OS/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
Bagaimana itu bekerja
  1. impor sys (Baris 14). Kami menggunakan modul
    $ 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. //dokumen. python. org/3/library/sys. html) dari pustaka standar Python untuk mengambil argumen baris perintah yang disimpan di
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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, dan untuk mengakhiri program melalui
    $ 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. Dalam Python, Anda perlu
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 modul sebelum menggunakannya
  2. Argumen baris perintah disimpan dalam variabel
    $ 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, yang merupakan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 (array dinamis Python). Item pertama dari
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 adalah nama skrip, diikuti oleh argumen baris perintah lainnya
  3. jika len(sys. argumen). = 2. (Baris 15). Kami menggunakan fungsi bawaan
    $ 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 untuk memverifikasi bahwa panjang argumen baris perintah
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 adalah 2
  4. dengan terbuka (sys. argv[1]) sebagai infile. (Baris 25). Kami membuka file melalui pernyataan
    $ 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, yang menutup file secara otomatis saat keluar
  5. untuk baris dalam file. (Baris 26). Kami menggunakan
    $ 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 loop (Baris 29) untuk memproses setiap
    $ 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 dari
    $ 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, di mana
    $ 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 milik kelas bawaan "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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" (dimaksudkan untuk dukungan string @ ). Kami menggunakan fungsi anggota kelas
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 ________97______87 untuk menghapus spasi putih di depan dan di belakang;
  6. Kami juga menjalankan utilitas 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" melalui perintah shell eksternal dalam 2 cara. melalui
    $ 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 dan
    $ 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

Contoh htmlescape. py - Keluar dari Karakter HTML yang Dicadangkan

Contoh ini membaca nama file input dan output dari baris perintah dan mengganti karakter HTML yang dicadangkan dengan entitas HTML yang sesuai. Ini menggambarkan input/output file dan substitusi string

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

# (Unix/Mac OS/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
Bagaimana itu bekerja
  1. impor sys (Baris 14). We
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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
    $ 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 module (@ https. //dokumen. python. org/3/library/sys. html). Kami mengambil argumen baris perintah dari
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 ________97______71, di mana
    $ 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 adalah nama skrip;
  2. dengan terbuka (sys. argv[1]) sebagai infile, open(sys. argv[2], 'w') sebagai outfile. (Baris 21). Kami menggunakan pernyataan
    $ 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, yang menutup file secara otomatis saat keluar, untuk membuka
    $ 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 untuk membaca (default) dan
    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 untuk menulis (
    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. untuk baris dalam file. (Baris 22). Kami menggunakan
    $ 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 loop untuk memproses setiap
    $ 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 dari
    $ 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, di mana
    $ 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 milik kelas bawaan "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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" (dimaksudkan untuk dukungan string @ ). Kami menggunakan fungsi anggota kelas
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 untuk menghapus spasi putih (kanan) di belakang;
  4. Piton 3. 2 memperkenalkan modul
    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 baru, dengan fungsi
    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 untuk keluar dari karakter khusus 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

Contoh files_rename. py - Ganti Nama File

Contoh ini mengganti nama semua file dalam direktori yang diberikan menggunakan ekspresi reguler (regex). Ini menggambarkan pemrosesan direktori/file (menggunakan modul

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) dan ekspresi reguler (menggunakan modul
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
Bagaimana itu bekerja

pengantar

Python diciptakan oleh Belanda Guido van Rossum sekitar tahun 1991. Python adalah proyek sumber terbuka. Situs induknya adalah www. python. org

Fitur utama Python adalah

  • Python adalah bahasa yang mudah dan intuitif. Skrip Python mudah dibaca dan dipahami
  • Python (seperti Perl) bersifat ekspresif. Satu baris kode Python dapat melakukan banyak baris kode dalam bahasa tujuan umum tradisional (seperti C/C++/Java)
  • Python gratis dan bersumber terbuka. Ini lintas platform dan berjalan di Windows, Linux/UNIX, dan Mac OS X
  • Python sangat cocok untuk pengembangan aplikasi cepat (RAD). Anda dapat membuat kode aplikasi dengan Python dalam waktu yang jauh lebih singkat daripada bahasa tujuan umum lainnya (seperti C/C++/Java). Python dapat digunakan untuk menulis aplikasi kecil dan prototipe cepat, tetapi juga memiliki skala yang baik untuk mengembangkan proyek berskala besar
  • Python adalah bahasa scripting dan diketik secara dinamis. Seperti kebanyakan bahasa skrip (mis. g. , Perl, JavaScript), Python mengasosiasikan tipe dengan objek, bukan variabel. Artinya, variabel dapat diberi nilai jenis apa pun, daftar (array) dapat berisi objek dari jenis yang berbeda
  • Python menyediakan manajemen memori otomatis. Anda tidak perlu mengalokasikan dan membebaskan memori dalam program Anda
  • Python menyediakan tipe data tingkat tinggi seperti array dinamis dan kamus (atau array asosiatif)
  • Python berorientasi objek
  • Python bukan bahasa yang sepenuhnya dikompilasi. Itu dikompilasi menjadi kode byte internal, yang kemudian ditafsirkan. Oleh karena itu, Python tidak secepat bahasa yang dikompilasi penuh seperti C/C++
  • Python hadir dengan sekumpulan besar perpustakaan termasuk toolkit antarmuka pengguna grafis (GUI), perpustakaan pemrograman web, jaringan, dan lain-lain

Python memiliki 3 versi

  • Piton 1. versi awal
  • Piton 2. dirilis pada tahun 2000, dengan banyak fitur baru seperti pengumpul sampah dan dukungan untuk Unicode
  • Python 3 (Python 3000 atau py3k). Upgrade besar dirilis pada tahun 2008. Python 3 TIDAK kompatibel dengan Python 2
Python 2 or Python 3?

Saat ini, dua versi Python didukung secara paralel, versi 2. 7 dan versi 3. 5. Ada sayangnya tidak kompatibel. Situasi ini muncul karena ketika Guido Van Rossum (pencipta Python) memutuskan untuk membawa perubahan signifikan pada Python 2, ia menemukan bahwa perubahan baru tersebut tidak sesuai dengan kode yang ada. Dia memutuskan untuk memulai versi baru yang disebut Python 3, tetapi terus mempertahankan Python 2 tanpa memperkenalkan fitur baru. Piton 3. 0 dirilis pada tahun 2008, sedangkan Python 2. 7 tahun 2010

LAGI, PERHATIKAN BAHWA PYTHON 2 DAN PYTHON 3 TIDAK KOMPATIBEL. Anda perlu memutuskan apakah akan menggunakan Python 2 atau Python 3. Mulai proyek baru Anda menggunakan Python 3. Gunakan Python 2 hanya untuk memelihara proyek lawas

Untuk memeriksa versi Python Anda, keluarkan perintah ini

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

# (Unix/Mac OS/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

Instalasi dan Memulai

Instalasi

Untuk Pendatang Baru di Python (Windows, Mac OSX, Ubuntu)

Saya sarankan Anda menginstal "distribusi Anaconda" dari Python 3, yang mencakup Command Prompt, IDE (Jupyter Notebook dan Spyder), dan dibundel dengan paket yang umum digunakan (seperti NumPy, Matplotlib, dan Panda yang digunakan untuk analitik data)

Situs ibu Goto Anaconda (@ https. // www. anaconda. com/) ⇒ Pilih "Distribusi Anaconda" Unduh ⇒ Pilih "Python 3. x" ⇒ Ikuti petunjuk untuk menginstal

Periksa Apakah Python Sudah Terinstal dan Versinya

Untuk memeriksa apakah Python sudah diinstal dan versinya, keluarkan perintah berikut. ,

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

# (Unix/Mac OS/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)

Baik Python 3 dan Python 2 seharusnya sudah terinstal secara default. Jika tidak, Anda dapat menginstal Python melalui

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

# (Unix/Mac OS/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

Untuk memverifikasi instalasi 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
Windows

Anda dapat menginstal keduanya

  1. "Distribusi Anaconda" (Lihat bagian sebelumnya)
  2. Python Biasa dari Python Software Foundation @ https. // www. python. org/download/, unduh penginstal MSI 32-bit atau 64-bit, dan jalankan penginstal yang diunduh
  3. Di bawah Cygwin (lingkungan Unix untuk Windows) dan instal Python (di bawah kategori "devel")
Mac OS X

[MELAKUKAN]

Dokumentasi

Dokumentasi Python dan referensi bahasa disediakan secara online @ https. //dokumen. python. org

Memulai dengan Python Interpreter

Mulai Interpreter Python Interaktif

Anda dapat menjalankan "Python Interpreter" dalam mode interaktif di bawah "Command-Line Shell" (seperti Anaconda Prompt, CMD Windows, Terminal Mac OS X, Bash Shell 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

Prompt perintah Python dilambangkan sebagai

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. Anda dapat memasukkan pernyataan Python di command prompt Python, mis. 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

Untuk keluar dari Python Interpreter

  • $ 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 dan Ubuntu) ________139______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 diikuti dengan Enter

Menulis dan Menjalankan Skrip Python

Skrip Python Pertama - halo. py

Gunakan editor teks pemrograman untuk menulis skrip Python berikut dan simpan sebagai "

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" di direktori pilihan Anda

$ 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
Bagaimana itu bekerja
  1. Secara konvensi, nama file skrip (modul) Python menggunakan huruf kecil semua (mis. 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. Komentar EOL. Pernyataan yang dimulai dengan
    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 hingga akhir baris (EOL) adalah komentar
  3. #. /usr/bin/env python3 (Baris 1) hanya berlaku untuk lingkungan Unix. Ini dikenal sebagai Hash-Bang (atau She-Bang) untuk menentukan lokasi Python Interpreter, sehingga skrip dapat dijalankan secara langsung sebagai program mandiri
  4. # -*- pengkodean. UTF-8 -*- (Baris 2, opsional) menentukan skema pengkodean sumber untuk menyimpan file sumber. Kami memilih dan merekomendasikan UTF-8 untuk internasionalisasi. Format khusus ini dikenali oleh banyak editor populer karena menyimpan kode sumber dalam format penyandian yang ditentukan
  5. """ halo. """ (Baris 3-5). Skrip dimulai dengan apa yang disebut doc-string untuk menyediakan dokumentasi untuk modul Python ini. Doc-string biasanya berupa string multi-baris (dibatasi dengan kutip triple-single atau triple-double), yang dapat diekstraksi dari file sumber untuk membuat dokumentasi
  6. Variables. Kami membuat variabel
    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 (Baris 6, 8, 10, 12, 14) dengan menugaskan nilai ke dalamnya
  7. String Python dapat diapit dengan tanda kutip tunggal
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7 (Baris 6) atau tanda kutip ganda
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8
  8. Bilangan bulat Python tidak terbatas ukurannya (Baris 8)
  9. Python mendukung angka floating-point (Baris 10)
  10. Python mendukung bilangan kompleks (Baris 12) dan tipe data tingkat tinggi lainnya
  11. Python mendukung array dinamis yang disebut daftar (Baris 14), diwakili oleh
    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. Elemen dapat diambil melalui indeks
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 (Baris 15)
  12. cetak(aVar). Fungsi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 dapat digunakan untuk mencetak nilai variabel ke konsol
Keluaran yang diharapkan

Keluaran yang diharapkan adalah

$ 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
Menjalankan Skrip Python

Anda dapat mengembangkan/menjalankan skrip Python dalam banyak cara - dijelaskan di bagian berikut

Menjalankan Skrip Python di Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)

Anda dapat menjalankan skrip python melalui Python Interpreter di bawah Command-Line Shell

Skrip Shell yang Dapat Dieksekusi Unix

Di Linux/Mac OS X, Anda dapat mengubah skrip Python menjadi program yang dapat dieksekusi (disebut Skrip Shell atau Skrip yang Dapat Dieksekusi) dengan

  1. Mulailah dengan baris yang dimulai dengan
    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 (disebut "hash-bang" atau "she-bang"), diikuti dengan nama path lengkap ke Python Interpreter, e. g. , Untuk menemukan Juru Bahasa Python, gunakan perintah "
    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" atau "
    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. Jadikan file dapat dieksekusi melalui perintah ________139______35 (ubah mode file).
  3. Anda kemudian dapat menjalankan skrip Python seperti program yang dapat dieksekusi lainnya. Sistem akan mencari Penerjemah Python dari baris 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

Kelemahannya adalah Anda harus membuat kode keras jalur ke Python Interpreter, yang dapat mencegah program menjadi portabel di mesin yang berbeda

Alternatifnya, Anda dapat menggunakan yang berikut ini untuk mengambil Python Interpreter dari lingkungan

Utilitas

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 akan menemukan Python Interpreter (dari entri
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). Pendekatan ini direkomendasikan karena tidak membuat kode keras pada jalur Python

Program yang Dapat Dijalankan Windows

Di Windows, Anda dapat mengaitkan ekstensi file "

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" dengan Python Interpretable, untuk membuat skrip Python dapat dieksekusi

Menjalankan Skrip Python di dalam Interpreter Python

Untuk menjalankan skrip "

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" di dalam Penerjemah Python

  • Anda dapat menggunakan jalur absolut atau relatif untuk nama file. Tapi,
    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 (untuk direktori home) tidak bekerja?
  • Fungsi bawaan
    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 membuka file, dalam mode hanya-baca default;

Lingkungan Pengembangan Interaktif (IDE)

Menggunakan IDE dengan debugging grafis dapat sangat meningkatkan produktivitas Anda

Untuk pemula, saya sarankan

  1. Penerjemah Python (seperti dijelaskan di atas)
  2. IDLE Python
  3. Jupyter Notebook (khusus untuk Analisis Data)

Untuk pengembang Webapp, saya sarankan

  1. Gerhana dengan PyDev
  2. PyCharm

Lihat "" untuk detailnya

Sintaks Dasar Python

Komentar

Komentar Python dimulai dengan tanda hash (

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) dan terakhir hingga akhir baris saat ini. Komentar diabaikan oleh Interpreter Python, tetapi sangat penting dalam memberikan penjelasan dan dokumentasi untuk orang lain (dan Anda sendiri tiga hari kemudian) untuk membaca program Anda. Gunakan komentar secara bebas

Tidak ada komentar multi-baris dengan Python?. (C/C++/Java mendukung komentar multi-baris melalui

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

Pernyataan

Pernyataan Python dibatasi oleh baris baru. Suatu pernyataan tidak dapat melewati batas garis, kecuali

  1. Ekspresi dalam tanda kurung
    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, tanda kurung siku
    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, dan tanda kurung kurawal
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 dapat merentang beberapa baris
  2. Garis miring terbalik (
    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) di akhir baris menunjukkan kelanjutan ke baris berikutnya. Ini adalah aturan lama dan TIDAK direkomendasikan karena rawan kesalahan

Tidak seperti C/C++/C#/Java, Anda tidak menempatkan titik koma (

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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) di akhir pernyataan Python. Tetapi Anda dapat menempatkan banyak pernyataan dalam satu baris, dipisahkan dengan titik koma (
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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). Sebagai contoh,

Blokir, Indentasi dan Pernyataan Majemuk

Blok adalah sekelompok pernyataan yang dieksekusi sebagai satu unit. Tidak seperti C/C++/C#/Java, yang menggunakan kurung kurawal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 untuk mengelompokkan pernyataan dalam sebuah blok tubuh, Python menggunakan lekukan untuk blok tubuh. Dengan kata lain, indentasi signifikan secara sintaksis di Python - blok badan harus diindentasi dengan benar. Ini adalah sintaks yang bagus untuk memaksa Anda membuat indentasi blok dengan benar agar mudah dipahami

Pernyataan majemuk, seperti kondisional (

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

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

# (Unix/Mac OS/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) dan definisi fungsi (
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), dimulai dengan baris header diakhiri dengan titik dua (
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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);

Sebagai contoh,

Python tidak menentukan berapa banyak lekukan yang akan digunakan, tetapi semua pernyataan dari blok tubuh SAMA harus dimulai pada jarak SAMA dari margin kanan. Anda dapat menggunakan spasi atau tab untuk lekukan tetapi Anda tidak dapat mencampurnya dalam blok badan SAMA. Disarankan untuk menggunakan 4 spasi untuk setiap tingkat lekukan

Tanda titik dua (

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

# (Unix/Mac OS/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) dan lekukan badan mungkin merupakan fitur paling aneh di Python, jika Anda berasal dari C/C++/C#/Java. Python memberlakukan aturan lekukan yang ketat untuk memaksa pemrogram menulis kode yang dapat dibaca

Variabel, Pengidentifikasi dan Konstanta

Seperti semua bahasa pemrograman, variabel adalah lokasi penyimpanan bernama. Variabel memiliki nama (atau pengidentifikasi) dan menyimpan nilai

Seperti kebanyakan bahasa interpretasi skrip (seperti JavaScript/Perl), Python diketik secara dinamis. Anda TIDAK perlu mendeklarasikan variabel sebelum menggunakannya. Variabel dibuat melalui penugasan awal. (Tidak seperti bahasa pengetikan statis tujuan umum tradisional seperti C/C++/Java/C#, di mana Anda perlu mendeklarasikan nama dan jenis variabel sebelum menggunakan variabel. )

Sebagai contoh,

Putar d kali dalam ahli penugasan python

Seperti disebutkan, Python diketik dinamis. Python mengasosiasikan tipe dengan objek, bukan variabel, mis. e. , variabel dapat menyimpan objek jenis apa pun, seperti yang ditunjukkan pada contoh di atas

Aturan Pengenal (Nama)

Pengidentifikasi dimulai dengan huruf (

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) atau garis bawah (
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), diikuti dengan nol atau lebih huruf, garis bawah, dan angka (
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 tidak mengizinkan karakter khusus seperti
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 dan
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

Kata kunci

Python 3 memiliki 35 kata yang dicadangkan, atau kata kunci, yang tidak dapat digunakan sebagai pengidentifikasi

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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 dan literal khusus)
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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, ________97,_______39, _______4)
  • 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 (fungsi)
  • 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 (operator)
  • 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 (penanganan kesalahan)
  • 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
Konvensi Penamaan Variabel

Nama variabel adalah kata benda, atau frase kata benda yang terdiri dari beberapa kata. Ada dua konvensi

  1. Dalam kata-kata huruf kecil dan secara opsional digabungkan dengan garis bawah jika itu meningkatkan keterbacaan, mis. g. , jumlah_siswa,
    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, dll
  2. Dalam apa yang disebut huruf unta di mana kata pertama dalam huruf kecil, dan kata-kata yang tersisa dikapitalisasi awal, 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, dan
    1
    2
    3
    4
    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. )
Rekomendasi
  1. Penting untuk memilih nama yang menggambarkan diri sendiri dan mencerminkan arti variabel, 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. Boleh menggunakan singkatan, mis. 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 untuk indeks
  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. Hindari nama satu huruf seperti
    1
    2
    3
    4
    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, yang lebih mudah diketik tetapi seringkali tidak bermakna. 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, to indicate that it should not be modified in the program. 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

Sebagai contoh,

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. Sebagai contoh,

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. That is, a variable does not have a fixed type and can be assigned an object of any type. A variable simply provides a reference to an object

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

You can use built-in function

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

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

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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
14,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
15,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
16,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
17, etc. For example,

In summary, a variable does not associate with a type. Instead, a type is associated with an object. A variable provides a reference to an object (of a certain type)

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

You can also use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
18 to check if the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
19 belong to the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
20. For example,

$ 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
The Assignment Operator (=)

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

Pair-wise Assignment and Chain Assignment

Sebagai contoh,

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

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

del Operator

You can use

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

# (Unix/Mac OS/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 operator to delete a variable. For example,

Number Operations

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
(Mengembalikan bilangan bulat lantai)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 (+=, -=, *=, /=, //=, **=, %=)

Masing-masing operator aritmatika memiliki mitra penugasan steno yang sesuai, yaitu. 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
  • Fungsi konversi radix dasar.
    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

Sebagai contoh,

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. Triple-quoted strings are useful for multi-line documentation, HTML and other codes

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

Escape Sequences for Characters (\code)

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

  • Special non-printable characters, such as tab (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    84), newline (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    85), carriage return (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    86)
  • Resolve ambiguity, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    83 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    82 inside double-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    81 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    80 inside single-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    91 (for ________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'. ' or r". ")

You can prefix a string by

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
97 to disable the interpretation of escape sequences (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
98), i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
99 is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
00 (two characters) instead of newline (one character). Raw strings are used extensively in regex (to be discussed in module
Enter 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
Dari indeks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 (termasuk) hingga
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 (tidak termasuk) dengan ukuran
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
23
The defaults are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
25,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
26,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
27. s[1. 3] ⇒ 'el'
s[1. -2] ⇒ 'el'
s[3. ] ⇒ 'lo'
s[. -2] ⇒ 'Hel'
s[. ] ⇒ 'Hello'
s[0. 5. 2] ⇒ 'Hlo'

Sebagai contoh,

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. hapus spasi putih di depan dan di belakang, spasi putih di kanan (di belakang);
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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
Pemformatan String 1 (Gaya Baru). Menggunakan str. format() fungsi

Ada beberapa cara untuk menghasilkan string yang diformat untuk keluaran. Python 3 memperkenalkan gaya baru dalam fungsi anggota

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
$ 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 dengan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 sebagai placeholder (disebut bidang format). Sebagai contoh,

Saat Anda meneruskan daftar, tupel, atau kamus (akan dibahas nanti) sebagai argumen ke dalam fungsi

$ 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, Anda dapat mereferensikan elemen urutan dalam bidang format dengan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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. Sebagai contoh,

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). Instead, you need to use the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69 function to convert the number to a string. 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: ***
8

The None Value

Python memberikan nilai khusus yang disebut

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 (perhatikan ejaan dalam huruf kapital awal), yang dapat digunakan untuk menginisialisasi objek (akan dibahas di OOP nanti). 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]*
*=pertama * hitung
pertama *= hitung Pengulangan pertama * 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 pertama[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

Sebagai contoh,

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. append 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 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
    29
  • 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. mengembalikan kejadian
    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

Untuk menggunakan

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

# (Unix/Mac OS/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 sebagai tumpukan last-in-first-out (LIFO), gunakan
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 untuk menambahkan item ke top-of-stack (TOS) dan
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 untuk menghapus item dari TOS

Menggunakan daftar sebagai Antrian masuk pertama keluar pertama

Untuk menggunakan

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

# (Unix/Mac OS/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 sebagai antrean masuk pertama keluar pertama (FIFO), gunakan
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 untuk menambahkan item ke akhir antrean dan
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 untuk menghapus item pertama antrean

Namun,

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 lambat. Pustaka standar menyediakan kelas
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 untuk mengimplementasikan deque secara efisien dengan penambahan dan pop cepat dari kedua ujungnya

Tupel (v1, v2,. )

Tuple mirip dengan daftar kecuali tidak dapat diubah (seperti 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

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
94 class has many member methods. 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

Sebagai contoh,

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
Dari indeks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 (termasuk) hingga
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 (tidak termasuk) dengan ukuran
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
23
The defaults are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
24 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] = seq1Ganti satu 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
Delete more 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
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. extend()seq. entend(seq1)Extend 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
51 atau
1
2
3
4
5
6
7
8
9
10
11
12
13
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

[MELAKUKAN]

Tumpukan

[MELAKUKAN]

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 (kurang dari),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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 (kurang dari atau sama dengan),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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 (sama dengan),
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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 (tidak sama dengan),
    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 (lebih besar dari),
    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 (lebih besar dari atau sama dengan). (Ini sama dengan 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. tidak berurutan (list, tuple, string, set, dll)
  • 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

Sebagai contoh,

$ 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

Sebagai contoh,

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

Blok 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 dijalankan jika loop keluar secara normal, tanpa menemui pernyataan
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

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

Iterasi melalui Urutan

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

Sebagai contoh,

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

Untuk mengulangi urutan dalam urutan terbalik, terapkan fungsi

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 yang membalikkan iterator di atas nilai urutan. 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
Fungsi Bawaan enumerate()

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. Sebagai contoh,

Pemahaman untuk Menghasilkan Daftar, Kamus, dan Set yang Dapat Diubah

Pemahaman daftar menyediakan cara ringkas untuk menghasilkan daftar baru. Sintaksnya adalah

Sebagai contoh,

Demikian pula, Anda dapat membuat kamus dan mengatur (urutan yang dapat diubah) melalui pemahaman. For example,

Pemahaman tidak dapat digunakan untuk menghasilkan

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

Konvensi Penamaan dan Gaya Pengkodean (PEP8 vs PEP 257)

Naming Conventions

These are the recommended naming conventions in Python

  • Variable names. use a noun in lowercase words (optionally joined with underscore if it improves readability), e. g. ,
    Enter a 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. Jangan gunakan 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. Tidak perlu deklarasi tipe seperti C/C++/Java

The syntax is

Contoh 1

Perhatikan bahwa Anda perlu mendefinisikan fungsi sebelum menggunakannya, karena Python bersifat interpretatif

Contoh 2
Contoh 3. Fungsi doc-string

Contoh ini menguraikan string-doc fungsi

  • Baris pertama "
    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" menentukan jenis argumen dan nilai pengembalian. Python tidak melakukan pemeriksaan tipe pada fungsi, dan baris ini hanya berfungsi sebagai dokumentasi
  • Baris kedua memberikan deskripsi
  • Contoh pemanggilan fungsi ikuti. Anda dapat menggunakan modul
    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 untuk melakukan pengujian unit untuk fungsi ini berdasarkan contoh-contoh ini (untuk dijelaskan di bagian "Uji Unit"
Pernyataan lulus

Pernyataan

$ 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 tidak melakukan apa-apa. Terkadang diperlukan sebagai placeholder pernyataan dummy untuk memastikan sintaks yang benar, mis. g. ,

Parameter dan Argumen Fungsi

Melewati Argumen berdasarkan Nilai vs. oleh Referensi

Dengan Python

  • Argumen yang tidak dapat diubah (seperti bilangan bulat, pelampung, string, dan tupel) diteruskan oleh nilai. Artinya, salinan dikloning dan diteruskan ke fungsi. Asli tidak dapat dimodifikasi di dalam fungsi
  • Argumen yang dapat diubah (seperti daftar, kamus, set, dan instance kelas) diteruskan dengan referensi. Artinya, mereka dapat dimodifikasi di dalam fungsi

Sebagai contoh,

Parameter Fungsi dengan Nilai Default

Anda dapat menetapkan nilai default ke parameter fungsi "tertinggal". Parameter tambahan yang memiliki nilai default ini bersifat opsional selama pemanggilan. Sebagai contoh,

Contoh lain,

Alih-alih melakukan hard-coding pada

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, akan lebih fleksibel untuk menggunakan parameter dengan nilai default, sebagai berikut

Argumen Posisi dan Kata Kunci

Fungsi Python mendukung argumen posisi dan kata kunci (atau bernama).

Biasanya, Python meneruskan argumen berdasarkan posisi dari kiri ke kanan, i. e. , posisional, seperti C/C++/Java. Python juga memungkinkan Anda untuk menyampaikan argumen dengan kata kunci (atau nama) dalam bentuk

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. Sebagai contoh,

Membongkar Kamus ke Argumen Kata Kunci (**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. In fact, a tuple that packs all the return values is returned

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

Types Hints via Function Annotations

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

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

Membaca. "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

The from-import Statement

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

Sebagai contoh,

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. But it exposes ONLY the imported attributes to the namespace. Furthermore, you can reference them directly without qualifying with the module name

For example, let create the following module called

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

# (Unix/Mac OS/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 vs.
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

Let's try out

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

# (Unix/Mac OS/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

Now, try 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 and note that the entire module is loaded, just like 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

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

Conditional Import

Python supports conditional import too. For example,

sys. path and PYTHONPATH/PATH environment variables

The environment 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.
37 shall include the path to Python Interpreter "
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"

The Python module search path is maintained in a Python 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
76 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: ***
14 module, 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. 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
78 is 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 an installation-dependent default. 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 is empty by default

Sebagai contoh,

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 default includes the current working directory (denoted by an empty string), the standard Python directories, plus the extension directories 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
83

The imported modules must be available in one 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
78 entries

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

To show 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.
37 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
80 environment variables, use one of these commands

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

Reloading Module using imp. reload() or importlib. 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

Atribut di '__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

[MELAKUKAN]

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. Global-scope in Python is equivalent to module-scope or file-scope. There is NO all-module-scope in Python

Sebagai contoh,

Function Variables (Variables of Function Object)

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

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

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

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

Nested Functions

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

Keluaran yang diharapkan adalah

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

Sebagai contoh,

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

# (Unix/Mac OS/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

Fungsi Lambda, seperti fungsi biasa, dapat memiliki nilai default untuk parameternya

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

Pemrograman Fungsional. 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. 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
    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)

Each Module has a Global Namespace

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

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

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

# (Unix/Mac OS/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, with its associated global namespace. All subsequent names are added into
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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's namespace

Saat Anda mengimpor modul melalui

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

# (Unix/Mac OS/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 di bawah shell interaktif, hanya
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 yang ditambahkan ke namespace
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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. Anda perlu mengakses nama (atribut) di dalam
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 melalui
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. Dengan kata lain, modul yang diimpor mempertahankan ruang namanya sendiri dan harus diawali dengan
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 di dalam
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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. (Ingat bahwa ruang lingkup nama adalah bagian dari kode yang dapat mengaksesnya tanpa awalan. )

Namun, jika Anda mengimpor atribut melalui

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

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

# (Unix/Mac OS/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, dan Anda dapat mengakses
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 secara langsung tanpa diawali dengan
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

Di sisi lain, ketika Anda mengimpor modul di dalam modul lain (bukan shell interaktif),

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 yang diimpor ditambahkan ke namespace modul target (bukan
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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 untuk shell interaktif)

Fungsi bawaan disimpan dalam modul yang disebut

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

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

# (Unix/Mac OS/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 secara otomatis

Fungsi bawaan globals(), locals() dan dir()

Anda dapat mencantumkan nama lingkup saat ini melalui fungsi bawaan ini

  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. mengembalikan kamus (pasangan nama-nilai) yang berisi variabel global lingkup saat ini
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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

Sebagai contoh,

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

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

[MELAKUKAN]

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

menegaskan Pernyataan

Anda dapat menggunakan pernyataan

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 untuk menguji pernyataan (atau kendala) tertentu. Misalnya, jika
1
2
3
4
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 seharusnya
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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 di bagian tertentu dari program, Anda dapat menggunakan pernyataan
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 untuk menguji kendala ini.
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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 akan dinaikkan jika
1
2
3
4
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 bukan nol

Sebagai contoh,

Pernyataan selalu dieksekusi dengan Python

Syntax

Sintaks untuk

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 adalah

1
2
3
4
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

Jika

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

# (Unix/Mac OS/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 jika
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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, tidak ada yang terjadi;

Pengecualian

Di Python, kesalahan yang terdeteksi selama eksekusi disebut pengecualian. Sebagai contoh,

Setiap kali pengecualian dimunculkan, program berhenti secara tiba-tiba

coba-kecuali-lain-akhirnya

Anda dapat menggunakan

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

# (Unix/Mac OS/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 fasilitas penanganan pengecualian untuk mencegah program berhenti secara tiba-tiba

Contoh 1. Menangani Indeks di luar jangkauan untuk Akses Daftar

Keluaran yang diharapkan adalah

1
2
3
4
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

Proses penanganan pengecualian untuk

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

# (Unix/Mac OS/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 adalah

  1. Python menjalankan pernyataan di blok ________139______91
  2. Jika tidak ada pengecualian yang muncul di semua pernyataan blok
    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, semua blok
    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 dilewati, dan program berlanjut ke pernyataan berikutnya setelah pernyataan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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
  3. Namun, jika pengecualian muncul di salah satu pernyataan di blok
    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, blok
    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 lainnya akan dilewati. Pengecualian cocok dengan
    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-blok. Blok
    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 pertama yang cocok akan dieksekusi. Program kemudian melanjutkan ke pernyataan berikutnya setelah pernyataan
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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, bukannya berhenti secara tiba-tiba. Namun demikian, jika tidak ada
    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-blok yang cocok, program berhenti tiba-tiba
  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

raise Statement

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

Contoh 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)
  • Konversi antara derajat dan radian.
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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

Sebagai contoh,

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

random Module

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

Sebagai contoh,

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 adalah nama skrip. 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. For example, create the following script 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
259"

Run the script

1
2
3
4
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

logging Module

The logging 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
260 module supports a flexible event logging system for your applications and libraries

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
260 supports five levels

  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. Detailed information meant for debugging
  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. Konfirmasi bahwa suatu peristiwa berlangsung seperti yang diharapkan
  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. Something unexpected happened, but the application is still working
  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. The application does not work as expected
  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

The logging functions 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
    267. Perform basic configuration of the logging system. The keyword arguments 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
    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 (default to append
    $ 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 (log this level and above), and etc
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Log 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
    277 at the specific level.
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 digabungkan menjadi
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 menggunakan penentu pemformatan
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. General logging function, at the given log
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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
Basic Logging via logging. basicConfig()

Sebagai contoh,

The logging functions support

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

# (Unix/Mac OS/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-like format specifiers 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
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, with values as function arguments (instead of via
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 in Python)

Run the script. A log file

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

# (Unix/Mac OS/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 would be created, with these records

1
2
3
4
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

By default, the log records include the log-level and logger-name (default 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
287) before the message

Getting the Log Level from a Configuration File

Log levels, 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
262 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
263, are stored as certain integers 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
260 module. 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
9

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

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

# (Unix/Mac OS/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) to the numeric log-level (e. g. , 10) used 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
260 module

Log Record Format

To set the log message format, 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
293 keyword

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

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
294 for date/time,
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

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

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

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

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

# (Unix/Mac OS/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) for the line number, 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
301 for the log message

Advanced Logging. Logger, Handler, Filter and Formatter

So far, we presented the basic logging facilities. 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
260 library is extensive and organized into these components

  • Loggers. expose the methods to application for logging
  • Handlers. send the log records created by the loggers to the appropriate destination, such as file, console (
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 via SMTP, or network via HTTP/FTP
  • Filters. decide which log records to output
  • Formatters. specify the layout format of log records
Loggers

To create 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 instance, invoke 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
305, where the optional
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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 specifies the logger name (default 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
287)

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
304's methods falls into two categories. configuration and logging

The commonly-used logging methods 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
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 and the general
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

# (Unix/Mac OS/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

The commonly-used configuration methods 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
    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 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
    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 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
    319
Handlers

The logging library provides handlers 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
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, 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
325 (emails)

The commonly-used methods 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
    315. The logger'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
    315 determines which message levels to be passed to the handler; while the handler'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
    315 determines which message level to be sent to the destination
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. for formatting the message sent to the destination
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 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
    319

You can add more than one handlers to a logger, possibly handling different log levels. For example, you can add 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
325 to receive emails 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
333 level; and 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 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
335 level

Formatters

Attach to a handler (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
336) to format the log messages

Example. 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)?. But I recommend that you choose a field delimiter which does not appear in the log messages, for ease of processing of log records (e. g. , export to spreadsheet)

Keluaran yang diharapkan adalah

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
1
Example. Using Rotating Log Files with RotatingFileHandler
  1. We keep all the logging parameters in a dictionary, which are usually retrieved from a configuration file
  2. In the constructor 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
    324, 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
    338 sets the log file size-limit; 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
    339 appends
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    340,
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script
    $ cd /path/to/project_directory
    $ chmod u+x grade_statistics.py
    $ ./grade_statistics.py
    341, etc to the old log files, such 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
    340 is always the newer backup of the log file. Both
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 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
    339 default to 0. If either one is zero, roll-over never occurs
  3. The above example produces 4 log files.
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 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
    347. The file being written to is always
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. When this file is filled, it is renamed 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
    346; and 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
    346 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
    351 exist, they will be renamed 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
    351 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
    347 respectively, with the old
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 deleted
Example. Using an Email Log for CRITICAL Level and Rotating Log Files for INFO Level
Example. Separating ERROR Log and INFO Log with Different Format

ConfigParser (Python 2) or configparser (Python 3) Module

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
31 module implements a basic configuration file parser 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
356

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
356 file contains key-value pairs organized in sections and looks like

  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). A section 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
    359 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
    360 pairs. The leading and trailing whitespaces are trimmed 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
    361. Lines beginning with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 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
    363 are comments

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
31 to parse 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
356 file, 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. read and parse from the list of filenames. It overrides the keys with each successive file, if present
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. get the value 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
    368 from
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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
Interpolation with SafeConfigParser

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
361 may contain formatting string 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
371, which refers to another
# (All platforms) Invoke Python Interpreter to run the script
$ cd /path/to/project_directory
$ python3 grade_statistics.py

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

# (Unix/Mac OS/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 (in uppercase) section. This interpolation feature is, however, supported only 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
374. For example, suppose we have the following configuration file 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
375

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

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
56 will be interpolated 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
377, interpolated from the SAME section and DEFAULT section

datetime 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
378 module supplies classes for manipulating dates and time in both simple and complex ways

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

smtplib and email Modules

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

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

# (Unix/Mac OS/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 module, which defines an SMTP client session object that can be used to send email to any Internet machine with an SMTP listener daemon

To use

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

# (Unix/Mac OS/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

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
382 module can be used to construct an email message

[TODO] more

json Module

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

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

# (Unix/Mac OS/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 module provides implementation for JSON encoder and decoder

  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Serialize
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 to a JSON-encoded string ('s' for string)
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Create a Python object from the given JSON-encoded string
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Serialize
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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 to the file
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Create a Python object by reading the given file

Sebagai contoh,

pickle and cPickle 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
383 module (described earlier) handles lists and dictionaries, but serializing arbitrary class instances requires a bit of extra effort. On the other hand, 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
391 module implements serialization and de-serialization of any Python object. Pickle is a protocol which allows the serialization of arbitrarily complex Python objects. It is specific to the Python languages and not applicable to other languages

The

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

# (Unix/Mac OS/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 module provides the same functions as 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
383 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
    394. Return the pickled representation 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
    385 as a string
  • # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Construct a Python object from
    # (All platforms) Invoke Python Interpreter to run the script
    $ cd /path/to/project_directory
    $ python3 grade_statistics.py
    
    # (Unix/Mac OS/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. Write a pickled representation 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
    385 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
    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. Construct a Python object reading 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
    400

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
403 is an improved version 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
391

signal module

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

The

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

# (Unix/Mac OS/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 module provides mechanisms to use signal handlers in Python

signal. signal()

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
406 method takes two arguments. nomor sinyal yang akan ditangani, dan fungsi penanganan. Sebagai contoh,

How do you rotate words in a sentence in Python?

Approach is very simple, .
Separate string in two parts first & second, for Left rotation Lfirst = str[0 . d] and Lsecond = str[d . ]. For Right rotation Rfirst = str[0 . len(str)-d] and Rsecond = str[len(str)-d . ]
Now concatenate these two parts second + first accordingly

Bagaimana Anda memutar sesuatu dengan Python?

rotate() function digunakan untuk memutar gambar dengan sudut di Python.

How to rotate numbers in Python?

Approach. Follow the steps below to solve the problem. .
Initialize a variable, say X, to store the count of digits in N
Update K = (K + X) % X to reduce it to a case of left rotation
Remove the first K digits of N and append all the removed digits to the right of the digits of N
Finally, print the value of N

Apakah ada fungsi putar di Python?

Modul Numpy Python menyediakan fungsi roll() bawaan untuk melakukan rotasi pada larik.