Làm cách nào để nối myVar với một chuỗi bằng JavaScript?
Tôi cho rằng bạn đã quen thuộc với một số ngôn ngữ lập trình như C/C++/Java. Bài viết này KHÔNG nhằm mục đích giới thiệu về lập trình Show
Cá nhân tôi khuyên bạn nên học ngôn ngữ lập trình đa năng truyền thống (chẳng hạn như C/C++/Java) trước khi học ngôn ngữ kịch bản như Python/JavaScript/Perl/PHP vì chúng ít cấu trúc hơn các ngôn ngữ truyền thống với nhiều tính năng ưa thích Python bằng ví dụPhần này dành cho các lập trình viên có kinh nghiệm xem xét các cú pháp của Python và những người cần làm mới bộ nhớ của họ. Đối với người mới, hãy chuyển sang phần tiếp theo Tóm tắt và so sánh cú pháp
Ví dụ grade_statistic. py - Cấu trúc và cú pháp cơ bảnVí dụ này lặp đi lặp lại nhắc người dùng cho điểm (từ 0 đến 100 với xác thực đầu vào). Sau đó, nó tính tổng, trung bình, tối thiểu và in biểu đồ ngang Ví dụ này minh họa các cấu trúc và cú pháp cơ bản của Python, chẳng hạn như nhận xét, câu lệnh, thụt lề khối, điều kiện # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py45, vòng lặp # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py02, vòng lặp # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py47, đầu vào/đầu ra, chuỗi, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04 và hàm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 Để chạy tập lệnh Python # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py Sản lượng dự kiến là $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: *** Làm thế nào nó hoạt động
Ví dụ số_đoán. py - Đoán một sốThis is a number guessing game. Nó minh họa vòng lặp lồng nhau nếu ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py86), vòng lặp # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py47 với cờ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 524 và mô-đun # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py89. For example, Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Làm thế nào nó hoạt động
Ví dụ magic_number. py - Kiểm tra xem Số có chứa Chữ số ma thuật khôngVí dụ này nhắc người dùng nhập một số và kiểm tra xem số đó có chứa chữ số ma thuật không. Ví dụ này minh họa chức năng, hoạt động của 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521 và 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523. Ví dụ, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 Làm thế nào nó hoạt động
Ví dụ hex2dec. py - Chuyển đổi thập lục phân sang thập phânVí dụ này nhắc người dùng nhập chuỗi thập lục phân (hex) và in số thập phân tương đương của nó. Nó minh họa vòng lặp for với chỉ mục, lệnh lồng nhau, phép toán chuỗi và từ điển (mảng kết hợp). Ví dụ, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 109517 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 Làm thế nào nó hoạt động
Example bin2dec. py - Binary to Decimal ConversionVí dụ này nhắc người dùng về một chuỗi nhị phân (có xác thực đầu vào) và in số thập phân tương đương của nó. Ví dụ, Enter a binary string: 1011001110 The decimal equivalent for binary "1011001110" is: 718 The decimal equivalent for binary "1011001110" using built-in function is: 718 # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py0 Làm thế nào nó hoạt động
Ví dụ dec2hex. py - Chuyển đổi thập phân sang thập lục phânChương trình này nhắc người dùng nhập số thập phân và in số thập lục phân tương đương của nó. For example, # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py1____46_______2 Làm thế nào nó hoạt động
wc ví dụ. py - Word CountVí dụ này đọc tên tệp từ dòng lệnh và in số lượng dòng, từ và ký tự (tương tự như tiện ích $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***68 trong Unix). It illustrates the text file input and text string processing # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py0 Làm thế nào nó hoạt động
Ví dụ htmlescape. py - Thoát các ký tự HTML dành riêngThis example reads the input and output filenames from the command-line and replaces the reserved HTML characters by their corresponding HTML entities. Nó minh họa đầu vào/đầu ra tập tin và thay thế chuỗi # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py4 Làm thế nào nó hoạt động
Ví dụ files_rename. py - Đổi tên tập tinVí dụ này đổi tên tất cả các tệp trong thư mục đã cho bằng biểu thức chính quy (regex). Nó minh họa quá trình xử lý thư mục/tệp (sử dụng mô-đun Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.13) và biểu thức chính quy (sử dụng mô-đun Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.14) # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py6 Làm thế nào nó hoạt độngGiới thiệuPython được tạo ra bởi Dutch Guido van Rossum vào khoảng năm 1991. Python là một dự án mã nguồn mở. Trang mẹ là www. con trăn. org Các tính năng chính của Python là
Python có 3 phiên bản
Python 2 or Python 3?Hiện tại, hai phiên bản Python được hỗ trợ song song, phiên bản 2. 7 và phiên bản 3. 5. Rất tiếc là không tương thích. Tình huống này phát sinh vì khi Guido Van Rossum (người tạo ra Python) quyết định mang lại những thay đổi quan trọng cho Python 2, anh ấy nhận thấy rằng những thay đổi mới sẽ không tương thích với các mã hiện có. Anh ấy quyết định bắt đầu một phiên bản mới có tên Python 3, nhưng tiếp tục duy trì Python 2 mà không giới thiệu các tính năng mới. Trăn 3. 0 được phát hành vào năm 2008, trong khi Python 2. 7 năm 2010 MỘT LẦN NỮA, HÃY LƯU Ý RẰNG PYTHON 2 VÀ PYTHON 3 KHÔNG TƯƠNG THÍCH. Bạn cần quyết định nên sử dụng Python 2 hay Python 3. Bắt đầu các dự án mới của bạn bằng Python 3. Chỉ sử dụng Python 2 để duy trì các dự án cũ Để kiểm tra phiên bản Python của bạn, hãy ra lệnh này # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py7 Cài đặt và Bắt đầuCài đặtDành cho người mới sử dụng Python (Windows, Mac OSX, Ubuntu)Tôi khuyên bạn nên cài đặt "bản phân phối Anaconda" của Python 3, bao gồm Dấu nhắc lệnh, IDE (Jupyter Notebook và Spyder) và đi kèm với các gói thường được sử dụng (chẳng hạn như NumPy, Matplotlib và Pandas được sử dụng để phân tích dữ liệu) Goto trang mẹ Anaconda (@ https. //www. trăn anaconda. com/) ⇒ Chọn Tải xuống "Phân phối Anaconda" ⇒ Chọn "Python 3. x" ⇒ Làm theo hướng dẫn để cài đặt Kiểm tra xem Python đã được cài đặt chưa và phiên bản của nóĐể kiểm tra xem Python đã được cài đặt chưa và phiên bản của nó, hãy ra lệnh sau. , # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py8 Ubuntu (16. 04LTS)Cả Python 3 và Python 2 đều đã được cài đặt sẵn theo mặc định. Nếu không, bạn có thể cài đặt Python qua # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py9 Để xác minh cài đặt Python $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***0 các cửa sổBạn có thể cài đặt một trong hai
hệ điều hành Mac[LÀM] Tài liệuTài liệu tham khảo ngôn ngữ và tài liệu Python được cung cấp trực tuyến @ https. // tài liệu. con trăn. tổ chức Bắt đầu với Trình thông dịch PythonBắt đầu Trình thông dịch Python tương tácBạn có thể chạy "Trình thông dịch Python" ở chế độ tương tác trong "Command-Line Shell" (chẳng hạn như Anaconda Prompt, CMD của Windows, Terminal của Mac OS X, Bash Shell của Ubuntu) $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***1 Dấu nhắc lệnh của Python được ký hiệu là Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.15. Bạn có thể nhập câu lệnh Python tại dấu nhắc lệnh của Python, e. g. , $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***2 Để thoát khỏi Trình thông dịch Python
Viết và chạy tập lệnh PythonTập lệnh Python đầu tiên - xin chào. pySử dụng trình soạn thảo văn bản lập trình để viết tập lệnh Python sau và lưu dưới dạng " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.19" trong thư mục bạn chọn $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***3 Làm thế nào nó hoạt động
Sản lượng dự kiếnCác kết quả đầu ra dự kiến là $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***4 Chạy tập lệnh PythonBạn có thể phát triển/chạy tập lệnh Python theo nhiều cách - được giải thích trong các phần sau Chạy tập lệnh Python trong Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)Bạn có thể chạy tập lệnh python thông qua Trình thông dịch Python trong Trình bao dòng lệnh Executable Shell Script của UnixTrong Linux/Mac OS X, bạn có thể biến tập lệnh Python thành chương trình thực thi (gọi là Shell Script hoặc Executable Script) bằng cách
The drawback is that you have to hard code the path to the Python Interpreter, which may prevent the program from being portable across different machines Ngoài ra, bạn có thể sử dụng cách sau để chọn Trình thông dịch Python từ môi trường Tiện ích Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.36 sẽ định vị Trình thông dịch Python (từ mục nhập Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.37). Cách tiếp cận này được khuyến nghị vì nó không mã hóa cứng đường dẫn của Python Chương trình thực thi của WindowsTrong Windows, bạn có thể liên kết phần mở rộng tệp " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.38" với phần có thể phiên dịch Python để làm cho tập lệnh Python có thể thực thi được Chạy tập lệnh Python bên trong Trình thông dịch của PythonĐể chạy tập lệnh " Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.19" bên trong Trình thông dịch của Python
Môi trường phát triển tương tác (IDE)Sử dụng IDE có gỡ lỗi đồ họa có thể cải thiện đáng kể năng suất của bạn Đối với người mới bắt đầu, tôi khuyên bạn nên
Đối với các nhà phát triển Webapp, tôi khuyên bạn nên
Xem "" để biết chi tiết Các cú pháp cơ bản của PythonBình luậnMột nhận xét Python bắt đầu bằng dấu thăng ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.21) và kéo dài đến cuối dòng hiện tại. Các nhận xét bị Trình thông dịch Python bỏ qua, nhưng chúng rất quan trọng trong việc cung cấp giải thích và tài liệu cho người khác (và chính bạn ba ngày sau) để đọc chương trình của bạn. Sử dụng bình luận một cách tự do KHÔNG có nhận xét nhiều dòng trong Python?. (C/C++/Java hỗ trợ nhận xét nhiều dòng thông qua Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6. ) Các câu lệnhMột câu lệnh Python được phân định bởi một dòng mới. Một tuyên bố không thể vượt qua ranh giới, ngoại trừ
Không giống như C/C++/C#/Java, bạn không đặt dấu chấm phẩy ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527) ở cuối câu lệnh Python. Nhưng bạn có thể đặt nhiều câu lệnh trên một dòng, được phân tách bằng dấu chấm phẩy ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527). Ví dụ như, Câu lệnh khối, thụt đầu dòng và hợp chấtMột khối là một nhóm các câu lệnh thực thi như một đơn vị. Không giống như C/C++/C#/Java, sử dụng dấu ngoặc nhọn 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 528 để nhóm các câu lệnh trong một khối nội dung, Python sử dụng thụt đầu dòng cho khối nội dung. Nói cách khác, thụt đầu dòng có ý nghĩa về mặt cú pháp trong Python - khối cơ thể phải được thụt lề đúng cách. Đây là một cú pháp tốt để buộc bạn phải thụt lề các khối một cách chính xác để dễ hiểu Một câu lệnh ghép, chẳng hạn như điều kiện ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py45), vòng lặp ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.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) và định nghĩa hàm ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.55), bắt đầu bằng dòng tiêu đề kết thúc bằng dấu hai chấm ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py67); Ví dụ như, Python không chỉ định sử dụng bao nhiêu thụt đầu dòng, nhưng tất cả các câu lệnh của khối nội dung CÙNG phải bắt đầu ở khoảng cách CÙNG từ lề phải. Bạn có thể sử dụng dấu cách hoặc tab để thụt đầu dòng nhưng bạn không thể trộn chúng trong cùng một khối nội dung. Nên sử dụng 4 dấu cách cho mỗi mức thụt đầu dòng Dấu hai chấm ( # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py67) và thụt đầu dòng ở phần thân có lẽ là tính năng kỳ lạ nhất trong Python, nếu bạn đến từ C/C++/C#/Java. Python áp đặt các quy tắc thụt lề nghiêm ngặt để buộc các lập trình viên viết mã có thể đọc được Biến, Định danh và Hằng sốGiống như tất cả các ngôn ngữ lập trình, một biến là một vị trí lưu trữ được đặt tên. Một biến có tên (hoặc mã định danh) và giữ một giá trị Giống như hầu hết các ngôn ngữ diễn giải tập lệnh (chẳng hạn như JavaScript/Perl), Python được nhập động. Bạn KHÔNG cần phải khai báo một biến trước khi sử dụng nó. Một biến được tạo thông qua phép gán ban đầu. (Không giống như các ngôn ngữ gõ tĩnh có mục đích chung truyền thống như C/C++/Java/C#, nơi bạn cần khai báo tên và loại biến trước khi sử dụng biến. ) Ví dụ, Như đã đề cập, Python được gõ động. Python liên kết các loại với các đối tượng, không phải các biến, tôi. e. , một biến có thể chứa bất kỳ loại đối tượng nào, như được minh họa trong các ví dụ trên Quy tắc định danh (Tên)Mã định danh bắt đầu bằng một chữ cái ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.58, Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.59) hoặc dấu gạch dưới ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.60), theo sau là 0 hoặc nhiều chữ cái, dấu gạch dưới và chữ số ( Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.61). Python không cho phép các ký tự đặc biệt như Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.62 và Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.63 từ khóaPython 3 có 35 từ hoặc từ khóa dành riêng, không thể được sử dụng làm định danh
Variable Naming ConventionA variable name is a noun, or a noun phrase made up of several words. There are two convenctions
Recommendations
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, để chỉ ra rằng nó không nên được sửa đổi trong chương trình. Nevertheless, nothing prevents it from being modified Data Types. Number, String and 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 Ví dụ như, print()'s separator (sep) and ending (end)You can use the optional keyword-argument 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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. For example, Important. Always use # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.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. Nghĩa là, một biến không có kiểu cố định và có thể được gán cho một đối tượng thuộc bất kỳ kiểu nào. A variable simply provides a reference to an object You do not need to declare a variable before using a variable. A variable is created automatically when a value is first assigned, which links the assigned object to the variable You can use built-in function Enter a number: 123456789 123456789 is a magic number 123456789 is a magic 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. Ví dụ, Tóm lại, một biến không liên kết với một loại. Thay vào đó, một loại được liên kết với một đối tượng. Một biến cung cấp một tham chiếu đến một đối tượng (thuộc một loại nhất định) Kiểm tra loại Instance. isinstance(ví dụ, loại)Bạn cũng có thể sử dụng chức năng tích hợp sẵn Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number18 để kiểm tra xem Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number19 có thuộc về Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number20 không. Ví dụ, $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***7 Toán tử gán (=)Trong Python, bạn không cần khai báo biến trước khi sử dụng biến. Phép gán ban đầu tạo một biến và liên kết giá trị được gán với biến. Ví dụ, Chuyển nhượng theo cặp và Chuyển nhượng theo chuỗiVí dụ, Toán tử gán là liên kết phải, i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number21 được hiểu là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number22 toán tử delBạn có thể sử dụng toán tử # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py35 để xóa một biến. Ví dụ, Phép toán sốArithmetic 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 (Returns the floor integer)1 // 2 ⇒ 0 -1 // 2 ⇒ -1 8. 9 // 2. 5 ⇒ 3. 0 -8. 9 // 2. 5 ⇒ -4. 0 (floor. ) -8. 9 // -2. 5 ⇒ 3. 0**Binaryx ** yExponentiation2 ** 5 ⇒ 32 1. 2 ** 3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus (Remainder)9 % 2 ⇒ 1 -9 % 2 ⇒ 1 9 % -2 ⇒ -1 -9 % -2 ⇒ -1 9. 9 % 2. 1 ⇒ 1. 5 -9. 9 % 2. 1 ⇒ 0. 6000000000000001 Compound Assignment Operators (+=, -=, *=, /=, //=, **=, %=)Each of the arithmetic operators has a corresponding shorthand assignment counterpart, i. e. , Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 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
Ví dụ như, 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. Chuỗi trích dẫn ba lần rất hữu ích cho tài liệu nhiều dòng, HTML và các mã khác Python 3 uses Unicode character set to support internationalization (i18n) Escape Sequences for Characters (\code)Like C/C++/Java, you need to use escape sequences (a back-slash + a code) for
Raw Strings (r'. ' hoặc r". ")You can prefix a string by Enter a number: 123456789 123456789 is a magic number 123456789 is a magic 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 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 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 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' Ví dụ như, Character Type?Python does not have a dedicated character data type. A character is simply a string of length 1. You can use the indexing operator to extract individual character from a string, as shown in the above example; or process individual character using # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.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
String Formatting 1 (New Style). Using str. format() functionCó một số cách để tạo một chuỗi được định dạng cho đầu ra. Python 3 introduces a new style in the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523's $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***67 member function with 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 528 as place-holders (called format fields). For examples, When you pass lists, tuples, or dictionaries (to be discussed later) as arguments into the $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***67 function, you can reference the sequence's elements in the format fields with 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5260. For examples, String Formatting 2. Using str. rjust(n), str. ljust(n), str. center(n), str. zfill(n)You can also use 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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). Thay vào đó, bạn cần sử dụng hàm Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69 để chuyển số thành chuỗi. Ví dụ, $ Python3 grade_statistics.py Enter a grade between 0 and 100 (or -1 to end): 9 Enter a grade between 0 and 100 (or -1 to end): 999 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 101 invalid grade, try again... Enter a grade between 0 and 100 (or -1 to end): 8 Enter a grade between 0 and 100 (or -1 to end): 7 Enter a grade between 0 and 100 (or -1 to end): 45 Enter a grade between 0 and 100 (or -1 to end): 90 Enter a grade between 0 and 100 (or -1 to end): 100 Enter a grade between 0 and 100 (or -1 to end): 98 Enter a grade between 0 and 100 (or -1 to end): -1 --------------- The list is: [9, 8, 7, 45, 90, 100, 98] The minimum is: 7 The minimum using built-in function is: 7 The sum is: 357 The sum using built-in function is: 357 The average is: 51.00 --------------- 0-9 : *** 10-19 : 20-29 : 30-39 : 40-49 : * 50-59 : 60-69 : 70-79 : 80-89 : 90-100: ***8 Giá trị Không cóPython cung cấp một giá trị đặc biệt có tên là Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.66 (lưu ý cách viết hoa chữ cái đầu), giá trị này có thể được sử dụng để khởi tạo một đối tượng (sẽ được thảo luận trong OOP sau). For example, List, Tuple, Dictionary and 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]* *=lst * đếm lst *= countRepetitionlst * 2 ⇒ [8, 9, 6, 2, 8, 9, 6, 2][i] [-i]lst[i] lst[-i]Indexing to get an item Front index begins at # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.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 lst[1. ] ⇒ [8] del lst[. ] ⇒ [] (Clear)FunctionUsageDescriptionExamples lst = [8, 9, 6, 2]len()len(lst)Lengthlen(lst) ⇒ 4max() min()max(lst) min(lst)Maximum value minimum valuemax(lst) ⇒ 9 min(lst) ⇒ 2sum()sum(lst)Sum (for number lists only)sum(lst) ⇒ 16 # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04, unlike string, is mutable. You can insert, remove and modify its items Ví dụ như, 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 StackTo use a # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04 as a last-in-first-out (LIFO) stack, use Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951770 to add an item to the top-of-stack (TOS) and Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951771 to remove the item from the TOS Using list as a first-in-first-out QueueĐể sử dụng # (All platforms) Invoke Python Interpreter to run the script $ cd /path/to/project_directory $ python3 grade_statistics.py # (Unix/Mac OS/Cygwin) Set the script to executable, and execute the script $ cd /path/to/project_directory $ chmod u+x grade_statistics.py $ ./grade_statistics.py04 làm hàng đợi nhập trước xuất trước (FIFO), hãy sử dụng Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951770 để thêm một mục vào cuối hàng đợi và Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951774 để xóa mục đầu tiên của hàng đợi Tuy nhiên, Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951774 chậm. The standard library provide a class Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951776 to efficiently implement deque with fast appends and pops from both ends Tuple (v1, v2,. )Tuple is similar to list except that it is immutable (just like string). Hence, tuple is more efficient than list. A tuple consists of items separated by commas, enclosed in parentheses Enter your guess (between 0 and 100): 50 Try lower... Enter your guess (between 0 and 100): 25 Try higher... Enter your guess (between 0 and 100): 37 Try higher... Enter your guess (between 0 and 100): 44 Try lower... Enter your guess (between 0 and 100): 40 Try lower... Enter your guess (between 0 and 100): 38 Try higher... Enter your guess (between 0 and 100): 39 Congratulation! You got it in 7 trials.45 An one-item tuple needs a comma to differentiate from parentheses The parentheses are actually optional, but recommended for readability. Nevertheless, the commas are mandatory. For example, You can operate on tuples using (supposing that Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 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 FunctionsLớp Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 10951794 có nhiều phương thức thành viên. The commonly-used are follows (suppose that Enter a hex string: 1abcd The decimal equivalent for hex "1abcd" is: 109517 The decimal equivalent for hex "1abcd" using built-in function is: 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 Ví dụ, Set-Specific Operators ( |