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 number4 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 number5. They support multi-line comments via Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6. )
  • String. Python's string can be delimited by either single quotes (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number7) or double quotes (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number8). Python also supports multi-line string, delimited by either triple-single (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number9) 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 520). 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 521 (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 522 (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 523 (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 524 (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 525 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 526), 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 527))
  • 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 528. ) 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 529
  • 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: 1095170 (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: 1095171 (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: 1095172 (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: 1095173 (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: 1095174 (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: 1095175 (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: 1095176 (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: 1095177 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: 1095178 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: 1095179, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 373, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 374, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 375
  • 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 376, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 377, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 378, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 379, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7180, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7181, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7182, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7183, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7184, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7185
  • 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: 7186, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7187, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7188. (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: 7189, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py00 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.py01)
  • 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.py02-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.py04, 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.py05# (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py06. 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.py07 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.py08, 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.py09. 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.py10, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py11, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py12
  • 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.py13 (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.py14 (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.py15 (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.py16 (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: 7182, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7183. 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: 1095170. 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: 1095172. 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.py21, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py22. 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.py25, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py26
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/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.py28
    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.py21, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py30 (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.py23 (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 529, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 1095179 (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 371 (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.py37, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py38# (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py39, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py40, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py41, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py42, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py43, ________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.py45, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py02-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.py47-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.py04 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.py49 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.py04 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.py04 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.py53
  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: 1095172). 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.py55 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.py02-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.py57 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.py04 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.py59, 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.py55 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.py04
  7. 0 = 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 524 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 525 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 526in
    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 2810 is contained in the sequence Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number53
    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 2810 and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number53 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 524 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 525 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 526

    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: 7189, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py00 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.py01
    • Python does not have an exclusive-or (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number62) 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 number63, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number64, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number65, etc
    • Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number66 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 number68, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number70, etc
    • Fungsi konversi radix dasar. Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number71, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number72, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number73

    Sebagai contoh,

    Bitwise Operators (Advanced)

    Python supports these bitwise operators

    OperatorModeUsageDescriptionExample
    Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number74
    Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number75&binaryx & ybitwise ANDx & y ⇒ 0b10000001|binaryx ! ybitwise ORx | y ⇒ 0b10001111~Unary~xbitwise NOT (or negate)~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110 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 number7) or double-quotes (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number8). Python also supports multi-line strings via triple-single-quotes (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number9) 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 520)

    To place a single-quote (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number80) inside a single-quoted string, you need to use escape sequence Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number81. Similarly, to place a double-quote (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number82) inside a double-quoted string, use Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number83. 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 number84), newline (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number85), carriage return (Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number86)
    • Resolve ambiguity, such as Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number83 (for Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number82 inside double-quoted string), Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number81 (for Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number80 inside single-quoted string), Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number91 (for ________139_______48)
    • Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number93 for character in hex value and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number94 for octal value
    • Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number95 for 4-hex-digit (16-bit) Unicode character and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number96 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 number97 to disable the interpretation of escape sequences (i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number98), i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number99 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 5200 (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 5202, Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter 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 5204;
    • 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: 7182 (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: 1095170 (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: 1095172 (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.py21 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.py22, 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.py23

    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 5211 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 5212, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04, 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 5214 (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 524 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 525 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 526'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.py95;
    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 5219 (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5220). 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 5222 (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 5223
    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 52241 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5225, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5226, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5227. 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.py55 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 5229 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 5230 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 2813, 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 5232

    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 5233

    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 523 (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 523 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 5236 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 523 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 52381 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5239, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5240, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5241. 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 52381 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5243, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5244. 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 52381 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5246, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5247. 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 52381 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5249
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5250
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5251
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5252
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 52541 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5255
    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 523 $ 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 528 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 5260. 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 523'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 5262 (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 2809 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 5264, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5265, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5231 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5267 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: 1095176 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 5269 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 number68 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 number69 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 5273). Instead, you need to use the Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69 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.py04

    • 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.py04 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.py04 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.py04 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.py04 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.py04, 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 5211. 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 5211 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 5204
    • 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 5211 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.py04 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 5288, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5289, 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 5290
    • 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: 7182 (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: 1095170 (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: 1095172 (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.py35, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5295 (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 5296 (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.py05 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 5298 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 5299 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: 10951700 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: 10951701 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: 10951702 (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 5221 (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 5222 (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 5223 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 524 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 525 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 5269 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.py95;
    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 5219 (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: 10951711). 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 5221 (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 2809 (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 5223 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 5221 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.py95, 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: 10951711. 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.py04, unlike string, is mutable. You can insert, remove and modify its items

    Sebagai contoh,

    Appending Items to a listCopying a listlist-Specific Member Functions

    The # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04 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.py04 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.py05Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951723. 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: 10951725. 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: 10951726 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.py05 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: 10951729
    • Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951730. 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: 10951731 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.py05 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: 10951734
    • Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951735. 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: 10951726 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: 10951737 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: 10951739 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: 10951741 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.py05 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: 10951725
    • Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951744. 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: 10951748. 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: 10951750. 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: 10951752. 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.py05 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: 10951755
    • Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951756. 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: 10951758. 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.py05 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: 10951761. 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.py05 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.py05; 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: 10951766

    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.py04 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.py04 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.py04 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: 10951770 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: 10951771 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: 10951770 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: 10951774 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: 10951778 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: 10951779;
    • 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: 10951780, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951781 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: 10951782;
    • 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: 7182, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 1095170 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: 1095172; 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 5214'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: 10951787, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951788, 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: 10951789; 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: 10951790. 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 528. 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.py67), 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.py15
    • 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: 10951794 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: 10951795 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: 10951794 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: 10951797
    • Enter 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: 10951799, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3700
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3701
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3702
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3703
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3704. 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 3705 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: 10951795. 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 3707

    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 528, 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 3709, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py01, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 1095171, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py74)

    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 3709 (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 3714 (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: 1095171 (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.py74 (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.py04, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5214, 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 523 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.py04 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 5214 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 523 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 524 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 525 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 526+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 3726)[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.py95; 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 5219 (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 3729). [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 5222 (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 5223
    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 5224 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.py95, 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 3729. 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 3736 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 3737
    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 3739 (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.py04), 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 37411 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3742) 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.py381 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3744) 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 3745*=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 3746 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 3747seq. 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 3748 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 3749seq. 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 3750seq. 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 3751 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 3752seq. 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 2810 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 2815, 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 3755seq. 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 3736seq. 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 3758, 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 3759seq. 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 3762 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 524 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 525 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 526

    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 378 (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 379 (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 376 (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 377 (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: 7180 (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: 7181 (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: 7182, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7183. 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: 7184, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3775. 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: 7186, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7187, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 7188. (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: 7189, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py00, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py01. )

    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 3782, 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 376, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 379) 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 3785" 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.py45, 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.py47-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.py55 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.py55 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-like1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3799 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: 71800" 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: 71801 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: 71801 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.py55 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: 71804 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.py95 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: 71806;
    • Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71807 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 5224 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: 71806;
    • Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71810 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 5224 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: 71806 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 5238

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

    There are a few ways to iterate through an dictionary

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

    The built-in function Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71816 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: 71817 (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: 71818 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: 71819 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: 71820 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.1Fungsi 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: 71821 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: 71822 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 5214, 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: 71825 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: 71826
    • 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: 71827 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: 71828
    • 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: 71829, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71830, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71831
    • 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: 71832, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71833
    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 2Contoh 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: 71839, 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: 71840. 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: 1095172 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: 71842 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: 71842 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: 1095172 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: 1095175 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: 71842 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: 71847 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: 71842 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: 71847. 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: 71852), 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: 71853 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: 71854" 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: 71853 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: 71856 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: 71857

    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.py93 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.py93ed, 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: 71860. 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: 71861 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: 71853 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: 71863 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: 71864 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: 71864

    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: 71863 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.py93 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: 71868 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.py93 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: 71863

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

    Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter 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: 71863 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.py93 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: 71875"

    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: 71876 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: 71878. 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: 71878 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: 71880, 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: 71880 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: 71878 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: 71883

    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: 71878 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: 71880 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: 71887 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: 71888 (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: 71888 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: 71890

    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.py83 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.py84. 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.py83 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: 71894

    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: 71895 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: 71895 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: 71897 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: 71898, 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: 71899 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: 71895, 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.py001 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: 71899. 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.py001 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.py004) 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: 71898

    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: 71895 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: 71895 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.py008 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.py009). 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: 71863 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: 71860 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.py012 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.py013, 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.py014, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py015 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.py016, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py017)

    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 521, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523). 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.py021 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.py022 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.py024. In other words, you cannot place multiple statements inside the body of a lambda function. You need to use a regular function for multiple statements

    Functions are Objects

    In Python, functions are objects (like instances of a class). Like any object,

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

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

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

    Function Closure

    In the above example, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2809 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 2809 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.py027 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.py028, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2809 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 2809 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.py031 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 2809?

    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.py028, 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.py034 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.py035, 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.py036 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.py037, 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.py55 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.py039 (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.py040. 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.py041 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: 10951726s 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: 71817 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.py044 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 525. 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.py046. 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: 71817. 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.py048 (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.py049). 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.py050. 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.py051 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.py052, 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 2810 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.py054, 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.py055, 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.py056 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.py056 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.py055 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.py061

    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: 71842 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: 71847 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.py051 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.py83 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.py066. 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.py067 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.py049, 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.py069 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.py070 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.py071 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.py069 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.py073, 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.py073'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: 71864 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.py073. 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: 71864 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: 71860. 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: 71860 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.py073. (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: 71864 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.py073, 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: 71864 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: 71864

    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: 71864 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.py073 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.py089, 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.py073 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.py092 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.py091
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py095. 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.py096
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py097. 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.py098 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.py099, 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.py098 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.py099, 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.py098 (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.py099) 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.py83 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.py073. 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.py098 (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.py098'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.py099 (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.py109. 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.py099, 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.py098, 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.py112. 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.py112'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.py114

    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.py098 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 280

    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.py116 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.py098. 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.py099 (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.py109

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

    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 281

    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.py132 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 5204, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5289, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5288, $ 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 number68, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951790, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951789 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.py141 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.py142 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.py132 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.py35 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.py35 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 2810 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.py95 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 2810 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.py153 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 525, 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.py157 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.py162
    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.py162, 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.py157 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.py175", 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.py176" 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 284

    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.py180, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py181, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py182. 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.py183. 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.py184, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py185, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py186
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py187
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py188. 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: 71830, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py190
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py191. 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.py192, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py193
    • [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.py181 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 285

    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.py198 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.py075 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.py082 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.py201 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.py202 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.py202 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.py204 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.py205, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2887
    • 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.py207, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py208, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py209, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py210, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py211, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py212
    • 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 522 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 521. # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py215, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py216, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py217
    • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 522 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.py219, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py220
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py221 (# (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py222)
    • 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.py224
    • 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.py225, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py226, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py227, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py228, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py229, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py230, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py231
    • 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.py232, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py233, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py234, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py235, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py236, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py237

    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.py238 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.py239 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 286

    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.py89 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.py242. 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.py243 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.py246 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.py247)
    • Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 71878. 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.py04 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: 71880, 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.py251, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py252, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py253. 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.py04 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.py04. 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.py259"

    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 287

    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.py260 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.py260 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.py262. 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.py263. 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.py266. 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.py267. 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.py268, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py269 (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.py271 (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.py272, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py273, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py274, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py275, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py276. 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.py277 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.py277 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.py280. 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.py281
    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.py282-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.py283, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py284, 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: 1095176 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.py286 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 288

    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.py287) 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.py262 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.py263, 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.py260 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 289

    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.py291) 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.py260 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.py293 keyword

    Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number0

    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.py294 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.py295 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.py116 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.py297 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.py268 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.py299 (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521) 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.py301 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.py260 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.py253), 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.py304 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.py305, 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.py306 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.py287)

    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.py304'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.py309, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py310, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py311, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py312, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py313 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.py314

    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.py315
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py316 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.py317
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py318 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.py319
    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.py320 (# (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py253, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py252), # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py323, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py324, 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.py325 (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.py315. 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.py315 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.py315 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.py329. 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.py318 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.py319

    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.py325 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.py333 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.py324 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.py335 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.py336) 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 number1Example. 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.py324, 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.py338 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.py339 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.py340, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py341, 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.py340 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.py338 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.py339 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.py345, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py346 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.py347. 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.py345. 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.py346; 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.py346 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.py351 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.py351 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.py347 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.py347 deleted
    Example. Using an Email Log for CRITICAL Level and Rotating Log Files for INFO LevelExample. 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: 71831 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.py356

    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.py356 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.py358 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.py359 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.py360 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.py361. Lines beginning with Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4 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.py363 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: 71831 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.py356 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.py366. 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.py367. 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.py368 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.py369
    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.py361 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.py371, 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.py116 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.py373 (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.py374. 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.py375

    Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number2

    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: 71856 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.py377, 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.py378 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.py379. 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.py380 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.py380

    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.py382 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.py383 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.py384. 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.py385 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.py386. 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.py387. 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.py385 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.py389. 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.py383 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.py391 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.py391 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.py383 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.py394. 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.py385 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.py396. 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.py397
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py398. 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.py385 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.py400
    • # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py401. 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.py400

    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.py403 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.py391

    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.py405 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.py406 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.

Postingan terbaru

LIHAT SEMUA