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 Show
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 ExamplesThis 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
Contoh grade_statistics. py - Sintaks dan Konstruksi DasarContoh 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
Contoh angka_tebakan. py - Tebak AngkaIni adalah permainan tebak angka. Ini menggambarkan nested-if ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py86), # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac 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 dengan flag 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 524, dan modul # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py89. Sebagai contoh, Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Bagaimana itu bekerja
Contoh magic_number. py - Periksa apakah Angka Berisi Angka AjaibContoh ini meminta pengguna untuk memasukkan nomor, dan memeriksa apakah nomor tersebut berisi angka ajaib. Contoh ini menggambarkan fungsi, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521 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 523 operasi. Sebagai contoh, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 Bagaimana itu bekerja
Contoh hex2dec. py - Konversi Heksadesimal Ke DesimalContoh ini meminta pengguna untuk string heksadesimal (hex), dan mencetak padanan desimalnya. Ini mengilustrasikan for-loop dengan indeks, nested-if, operasi string dan kamus (array asosiatif). Sebagai contoh, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 109517 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 Bagaimana itu bekerja
Contoh bin2dec. py - Konversi Biner ke DesimalContoh ini meminta pengguna untuk string biner (dengan validasi input), dan mencetak padanan desimalnya. Sebagai contoh, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 718 # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py0 Bagaimana itu bekerja
Contoh dec2hex. py - Konversi Desimal ke HeksadesimalProgram ini meminta pengguna untuk angka desimal, dan mencetak ekuivalen heksadesimalnya. Sebagai contoh, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py1 # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py2 Bagaimana itu bekerja
Contoh wc. py - Jumlah KataContoh ini membaca nama file dari baris perintah dan mencetak jumlah baris, kata, dan karakter (mirip dengan utilitas $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***68 di Unix). Ini menggambarkan input file teks dan pemrosesan string teks # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_0 Bagaimana itu bekerja
Contoh htmlescape. py - Keluar dari Karakter HTML yang DicadangkanContoh ini membaca nama file input dan output dari baris perintah dan mengganti karakter HTML yang dicadangkan dengan entitas HTML yang sesuai. Ini menggambarkan input/output file dan substitusi string # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_4 Bagaimana itu bekerja
Contoh files_rename. py - Ganti Nama FileContoh ini mengganti nama semua file dalam direktori yang diberikan menggunakan ekspresi reguler (regex). Ini menggambarkan pemrosesan direktori/file (menggunakan modul Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.13) dan ekspresi reguler (menggunakan modul Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.14) # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_6 Bagaimana itu bekerjapengantarPython diciptakan oleh Belanda Guido van Rossum sekitar tahun 1991. Python adalah proyek sumber terbuka. Situs induknya adalah www. python. org Fitur utama Python adalah
Python memiliki 3 versi
Python 2 or Python 3?Saat ini, dua versi Python didukung secara paralel, versi 2. 7 dan versi 3. 5. Ada sayangnya tidak kompatibel. Situasi ini muncul karena ketika Guido Van Rossum (pencipta Python) memutuskan untuk membawa perubahan signifikan pada Python 2, ia menemukan bahwa perubahan baru tersebut tidak sesuai dengan kode yang ada. Dia memutuskan untuk memulai versi baru yang disebut Python 3, tetapi terus mempertahankan Python 2 tanpa memperkenalkan fitur baru. Piton 3. 0 dirilis pada tahun 2008, sedangkan Python 2. 7 tahun 2010 LAGI, PERHATIKAN BAHWA PYTHON 2 DAN PYTHON 3 TIDAK KOMPATIBEL. Anda perlu memutuskan apakah akan menggunakan Python 2 atau Python 3. Mulai proyek baru Anda menggunakan Python 3. Gunakan Python 2 hanya untuk memelihara proyek lawas Untuk memeriksa versi Python Anda, keluarkan perintah ini # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_7 Instalasi dan MemulaiInstalasiUntuk Pendatang Baru di Python (Windows, Mac OSX, Ubuntu)Saya sarankan Anda menginstal "distribusi Anaconda" dari Python 3, yang mencakup Command Prompt, IDE (Jupyter Notebook dan Spyder), dan dibundel dengan paket yang umum digunakan (seperti NumPy, Matplotlib, dan Panda yang digunakan untuk analitik data) Situs ibu Goto Anaconda (@ https. // www. anaconda. com/) ⇒ Pilih "Distribusi Anaconda" Unduh ⇒ Pilih "Python 3. x" ⇒ Ikuti petunjuk untuk menginstal Periksa Apakah Python Sudah Terinstal dan VersinyaUntuk memeriksa apakah Python sudah diinstal dan versinya, keluarkan perintah berikut. , # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_8 Ubuntu (16. 04LTS)Baik Python 3 dan Python 2 seharusnya sudah terinstal secara default. Jika tidak, Anda dapat menginstal Python melalui # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py_9 Untuk memverifikasi instalasi Python $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***0 WindowsAnda dapat menginstal keduanya
Mac OS X[MELAKUKAN] DokumentasiDokumentasi Python dan referensi bahasa disediakan secara online @ https. //dokumen. python. org Memulai dengan Python InterpreterMulai Interpreter Python InteraktifAnda dapat menjalankan "Python Interpreter" dalam mode interaktif di bawah "Command-Line Shell" (seperti Anaconda Prompt, CMD Windows, Terminal Mac OS X, Bash Shell Ubuntu) $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***1 Prompt perintah Python dilambangkan sebagai Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.15. Anda dapat memasukkan pernyataan Python di command prompt Python, mis. g. , $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***2 Untuk keluar dari Python Interpreter
Menulis dan Menjalankan Skrip PythonSkrip Python Pertama - halo. pyGunakan editor teks pemrograman untuk menulis skrip Python berikut dan simpan sebagai " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.19" di direktori pilihan Anda $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***3 Bagaimana itu bekerja
Keluaran yang diharapkanKeluaran yang diharapkan adalah $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***4 Menjalankan Skrip PythonAnda dapat mengembangkan/menjalankan skrip Python dalam banyak cara - dijelaskan di bagian berikut Menjalankan Skrip Python di Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)Anda dapat menjalankan skrip python melalui Python Interpreter di bawah Command-Line Shell Skrip Shell yang Dapat Dieksekusi UnixDi Linux/Mac OS X, Anda dapat mengubah skrip Python menjadi program yang dapat dieksekusi (disebut Skrip Shell atau Skrip yang Dapat Dieksekusi) dengan
Kelemahannya adalah Anda harus membuat kode keras jalur ke Python Interpreter, yang dapat mencegah program menjadi portabel di mesin yang berbeda Alternatifnya, Anda dapat menggunakan yang berikut ini untuk mengambil Python Interpreter dari lingkungan Utilitas Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials._36 akan menemukan Python Interpreter (dari entri Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.37). Pendekatan ini direkomendasikan karena tidak membuat kode keras pada jalur Python Program yang Dapat Dijalankan WindowsDi Windows, Anda dapat mengaitkan ekstensi file " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials._38" dengan Python Interpretable, untuk membuat skrip Python dapat dieksekusi Menjalankan Skrip Python di dalam Interpreter PythonUntuk menjalankan skrip " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials._19" di dalam Penerjemah Python
Lingkungan Pengembangan Interaktif (IDE)Menggunakan IDE dengan debugging grafis dapat sangat meningkatkan produktivitas Anda Untuk pemula, saya sarankan
Untuk pengembang Webapp, saya sarankan
Lihat "" untuk detailnya Sintaks Dasar PythonKomentarKomentar Python dimulai dengan tanda hash ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.21) dan terakhir hingga akhir baris saat ini. Komentar diabaikan oleh Interpreter Python, tetapi sangat penting dalam memberikan penjelasan dan dokumentasi untuk orang lain (dan Anda sendiri tiga hari kemudian) untuk membaca program Anda. Gunakan komentar secara bebas Tidak ada komentar multi-baris dengan Python?. (C/C++/Java mendukung komentar multi-baris melalui Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6. ) PernyataanPernyataan Python dibatasi oleh baris baru. Suatu pernyataan tidak dapat melewati batas garis, kecuali
Tidak seperti C/C++/C#/Java, Anda tidak menempatkan titik koma ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527) di akhir pernyataan Python. Tetapi Anda dapat menempatkan banyak pernyataan dalam satu baris, dipisahkan dengan titik koma ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527). Sebagai contoh, Blokir, Indentasi dan Pernyataan MajemukBlok adalah sekelompok pernyataan yang dieksekusi sebagai satu unit. Tidak seperti C/C++/C#/Java, yang menggunakan kurung kurawal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 528 untuk mengelompokkan pernyataan dalam sebuah blok tubuh, Python menggunakan lekukan untuk blok tubuh. Dengan kata lain, indentasi signifikan secara sintaksis di Python - blok badan harus diindentasi dengan benar. Ini adalah sintaks yang bagus untuk memaksa Anda membuat indentasi blok dengan benar agar mudah dipahami Pernyataan majemuk, seperti kondisional ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py45), 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, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac 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) dan definisi fungsi ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.55), dimulai dengan baris header diakhiri dengan titik dua ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py67); Sebagai contoh, Python tidak menentukan berapa banyak lekukan yang akan digunakan, tetapi semua pernyataan dari blok tubuh SAMA harus dimulai pada jarak SAMA dari margin kanan. Anda dapat menggunakan spasi atau tab untuk lekukan tetapi Anda tidak dapat mencampurnya dalam blok badan SAMA. Disarankan untuk menggunakan 4 spasi untuk setiap tingkat lekukan Tanda titik dua ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py67) dan lekukan badan mungkin merupakan fitur paling aneh di Python, jika Anda berasal dari C/C++/C#/Java. Python memberlakukan aturan lekukan yang ketat untuk memaksa pemrogram menulis kode yang dapat dibaca Variabel, Pengidentifikasi dan KonstantaSeperti semua bahasa pemrograman, variabel adalah lokasi penyimpanan bernama. Variabel memiliki nama (atau pengidentifikasi) dan menyimpan nilai Seperti kebanyakan bahasa interpretasi skrip (seperti JavaScript/Perl), Python diketik secara dinamis. Anda TIDAK perlu mendeklarasikan variabel sebelum menggunakannya. Variabel dibuat melalui penugasan awal. (Tidak seperti bahasa pengetikan statis tujuan umum tradisional seperti C/C++/Java/C#, di mana Anda perlu mendeklarasikan nama dan jenis variabel sebelum menggunakan variabel. ) Sebagai contoh, Seperti disebutkan, Python diketik dinamis. Python mengasosiasikan tipe dengan objek, bukan variabel, mis. e. , variabel dapat menyimpan objek jenis apa pun, seperti yang ditunjukkan pada contoh di atas Aturan Pengenal (Nama)Pengidentifikasi dimulai dengan huruf ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.58, Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.59) atau garis bawah ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.60), diikuti dengan nol atau lebih huruf, garis bawah, dan angka ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.61). Python tidak mengizinkan karakter khusus seperti Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.62 dan Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.63 Kata kunciPython 3 memiliki 35 kata yang dicadangkan, atau kata kunci, yang tidak dapat digunakan sebagai pengidentifikasi
Konvensi Penamaan VariabelNama variabel adalah kata benda, atau frase kata benda yang terdiri dari beberapa kata. Ada dua konvensi
Rekomendasi
ConstantsPython does not support constants, where its contents cannot be modified. (C supports constants via keyword 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2837, Java via 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2838. ) It is a convention to name a variable in uppercase (joined with underscore), e. g. , 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2839, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2840, to indicate that it should not be modified in the program. Nevertheless, nothing prevents it from being modified Data Types. Number, String and ListPython supports various number type such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521 (for integers such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2842, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2843), 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (for floating-point number such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2845, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2846, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2847), 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 524 (for boolean of either 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 525 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 526) Python supports text string (a sequence of characters). In Python, strings can be delimited with single-quotes or double-quotes, e. g. , 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2851, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2852, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2853 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 2854 (empty string) Python supports a dynamic-array structure called # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04, denoted as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2856. You can reference the i-th element as # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py07. Python's # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04 is similar to C/C++/Java's array, but it is NOT fixed size, and can be expanded dynamically during runtime I will describe these data types in details in the later section Console Input/Output. input() and print() Built-in FunctionsYou can use built-in function # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py80 to read input from the console (as a string) and # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py62 to print output to the console. For example, print()The built-in function # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py62 has the following signature Sebagai contoh, print()'s separator (sep) and ending (end)You can use the optional keyword-argument 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2862 to set the separator string (default is space), and 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2863 for ending string (default is newline). For examples, print in Python 2 vs Python 3Recall that Python 2 and Python 3 are NOT compatible. In Python 2, you can use " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2864", without the parentheses (because 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2865 is a keyword in Python 2). In Python 3, parentheses are required as # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py62 is a function. Sebagai contoh, Important. Always use # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py62 function with parentheses, for portability Data Types and Dynamic TypingPython has a large number of built-in data types, such as Numbers (Integer, Float, Boolean, Complex Number), String, List, Tuple, Set, Dictionary and File. More high-level data types, such as Decimal and Fraction, are supported by external modules You can use the built-in function 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2868 to check the type of a variable or literal Number TypesPython supports these built-in number types
Dynamic Typing and Assignment OperatorRecall that Python is dynamic typed (instead of static typed) Python associates types with objects, instead of variables. That is, a variable does not have a fixed type and can be assigned an object of any type. A variable simply provides a reference to an object You do not need to declare a variable before using a variable. A variable is created automatically when a value is first assigned, which links the assigned object to the variable You can use built-in function Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number13 to get the object type referenced by a variable Type Casting. int(x), float(x), str(x)You can perform type conversion (or type casting) via built-in functions Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number14, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number15, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number16, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number17, etc. For example, In summary, a variable does not associate with a type. Instead, a type is associated with an object. A variable provides a reference to an object (of a certain type) Check Instance's Type. isinstance(instance, type)You can also use the built-in function Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number18 to check if the Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number19 belong to the Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number20. For example, $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***7 The Assignment Operator (=)In Python, you do not need to declare variables before using the variables. The initial assignment creates a variable and links the assigned value to the variable. For example, Pair-wise Assignment and Chain AssignmentSebagai contoh, Assignment operator is right-associative, i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number21 is interpreted as Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number22 del OperatorYou 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 operator to delete a variable. For example, Number OperationsArithmetic Operators (+, -, *, /, //, **, %)Python supports these arithmetic operators OperatorModeUsageDescriptionExample+BinaryUnaryx + y +xAddition Positive-Binary Unaryx - y -xSubtraction Negate*Binaryx * yMultiplication/Binaryx / yFloat Division (Returns a float)1 / 2 ⇒ 0. 5 -1 / 2 ⇒ -0. 5//Binaryx // yInteger Division (Mengembalikan bilangan bulat lantai)1 // 2 ⇒ 0 -1 // 2 ⇒ -1 8. 9 // 2. 5 ⇒ 3. 0 -8. 9 // 2. 5 ⇒ -4. 0 (floor. ) -8. 9 // -2. 5 ⇒ 3. 0**Binaryx ** yExponentiation2 ** 5 ⇒ 32 1. 2 ** 3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus (Remainder)9 % 2 ⇒ 1 -9 % 2 ⇒ 1 9 % -2 ⇒ -1 -9 % -2 ⇒ -1 9. 9 % 2. 1 ⇒ 1. 5 -9. 9 % 2. 1 ⇒ 0. 6000000000000001 Compound Assignment Operators (+=, -=, *=, /=, //=, **=, %=)Masing-masing operator aritmatika memiliki mitra penugasan steno yang sesuai, yaitu. e. , Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 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 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 375. For example Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number31 is the same as Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number32 Increment/Decrement (++, --)?Python does not support increment ( Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 1095177) and decrement ( Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 1095178) operators (as in C/C++/Java). You need to use Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number32 or Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number31 for increment Python accepts Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number37, and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number38. Don't get trap into this. But Python flags a syntax error for Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number39 and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number40 Mixed-Type OperationsFor mixed-type operations, e. g. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number41 ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number42), the value of the "smaller" type is first promoted to the "bigger" type. It then performs the operation in the "bigger" type and returns the result in the "bigger" type. In Python, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521 is "smaller" than 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 522, which is "smaller" than Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number06 Relational (Comparison) Operators (==, !=, <, <=, >, >=, in, not in, is, is not)Python supports these relational (comparison) operators that return a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 524 value of either 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 526OperatorModeUsageDescriptionExample== = < <= > >=Binaryx == y x . = y x < y x <= y x > y x >= yComparison Return 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 NOTNotes
Example. [TODO] Built-in FunctionsPython provides many built-in functions for numbers, including
Sebagai contoh, Bitwise Operators (Advanced)Python supports these bitwise operators OperatorModeUsageDescriptionExampleEnter 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< StringIn 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
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 ImmutableStrings 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 StringsYou can operate on strings using
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 5224 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 ASCIIIn 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 FunctionsPython 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
Pemformatan String 1 (Gaya Baru). Menggunakan str. format() fungsiAda 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 523 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 % operatorThe 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 ValuePython 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 SetList [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
Built-in Functions and Operators for listA # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac 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
Notes
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 FunctionsThe # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac 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
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 StackUntuk 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 pertamaUntuk 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)
Conversion between List and TupleYou 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)
Dictionary-Specific Member FunctionsThe 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)
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 ( |