In tin nhắn trong chuyên gia gán python
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 một 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 Syntax Summary and Comparison
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 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number645, vòng lặp Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number602, vòng lặp Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number647, đầu vào/đầu ra, chuỗi, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 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 525 Để chạy tập lệnh Python Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6 Sản lượng dự kiến là 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 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. It illustrates nested-if ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number686), Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number647-loop 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 524 flag, and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number689 module. For example, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number39 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number40 Làm thế nào nó hoạt động
Exmaple magic_number. py - Check if Number Contains a Magic DigitThis example prompts user for a number, and check if the number contains a magic digit. This example illustrate function, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521 and 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523 operations. For example, 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
Example hex2dec. py - Hexadecimal To Decimal ConversionThis example prompts user for a hexadecimal (hex) string, and print its decimal equivalent. It illustrates for-loop with index, nested-if, string operation and dictionary (associative array). For example, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6 Làm thế nào nó hoạt động
Example bin2dec. py - Binary to Decimal ConversionThis example prompts user for a binary string (with input validation), and print its decimal equivalent. For example, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number60 Làm thế nào nó hoạt động
Example dec2hex. py - Decimal to Hexadecimal ConversionThis program prompts user for a decimal number, and print its hexadecimal equivalent. For example, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number61 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number62 Làm thế nào nó hoạt động
Example wc. py - Số từThis example reads a filename from command-line and prints the line, word and character counts (similar 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 52768 utility in Unix). It illustrates the text file input and text string processing Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number60 How it works
Example htmlescape. py - Escape Reserved HTML CharactersThis example reads the input and output filenames from the command-line and replaces the reserved HTML characters by their corresponding HTML entities. It illustrates file input/output and string substitution Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number64 How it works
Example files_rename. py - Rename FilesThis example renames all the files in the given directory using regular expression (regex). It illustrates directory/file processing (using module Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3913) and regular expression (using module Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3914) Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number66 How it worksIntroductionPython is created by Dutch Guido van Rossum around 1991. Python is an open-source project. The mother site is www. python. org The main features of Python are
Python có 3 phiên bản
Python 2 or Python 3?Currently, two versions of Python are supported in parallel, version 2. 7 and version 3. 5. There are unfortunately incompatible. This situation arises because when Guido Van Rossum (the creator of Python) decided to bring significant changes to Python 2, he found that the new changes would be incompatible with the existing codes. He decided to start a new version called Python 3, but continue maintaining Python 2 without introducing new features. Python 3. 0 was released in 2008, while Python 2. 7 in 2010 AGAIN, TAKE NOTE THAT PYTHON 2 AND PYTHON 3 ARE NOT COMPATIBLE. You need to decide whether to use Python 2 or Python 3. Start your new projects using Python 3. Use Python 2 only for maintaining legacy projects To check the version of your Python, issue this command Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number67 Installation and Getting StartedInstallationFor Newcomers to Python (Windows, Mac OSX, Ubuntu)I suggest you install "Anaconda distribution" of Python 3, which includes a Command Prompt, IDEs (Jupyter Notebook and Spyder), and bundled with commonly-used packages (such as NumPy, Matplotlib and Pandas that are used for data analytics) Goto Anaconda mother site (@ https. //www. anaconda. com/) ⇒ Choose "Anaconda Distribution" Download ⇒ Choose "Python 3. x" ⇒ Follow the instructions to install Check If Python Already Installed and its VersionTo check if Python is already installed and its the version, issue the following command. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number68 Ubuntu (16. 04LTS)Both the Python 3 and Python 2 should have already installed by default. Otherwise, you can install Python via Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69 To verify the Python installation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5270 các cửa sổYou could install either
Mac OS X[TODO] DocumentationTà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. org Bắt đầu với Trình thông dịch PythonStart the Interactive Python InterpreterYou can run the "Python Interpreter" in interactive mode under a "Command-Line Shell" (such as Anaconda Prompt, Windows' CMD, Mac OS X's Terminal, Ubuntu's Bash Shell) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5271 The Python's command prompt is denoted as Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3915. You can enter Python statement at the Python's command prompt, 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 5272 To exit Python Interpreter
Writing and Running Python ScriptsFirst Python Script - hello. pyUse a programming text editor to write the following Python script and save as " Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3919" in a directory of your choice 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 Làm thế nào nó hoạt động
Expected OutputThe expected outputs 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 5274 Running Python ScriptsYou can develop/run a Python script in many ways - explained in the following sections Running Python Scripts in Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)You can run a python script via the Python Interpreter under the Command-Line Shell Unix's Executable Shell ScriptIn Linux/Mac OS X, you can turn a Python script into an executable program (called Shell Script or Executable Script) by
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 Alternatively, you can use the following to pick up the Python Interpreter from the environment The Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3936 utility will locate the Python Interpreter (from the Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3937 entries). This approach is recommended as it does not hard code the Python's path Windows' Exeutable ProgramIn Windows, you can associate " Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3938" file extension with the Python Interpretable, to make the Python script executable Running Python Scripts inside Python's InterpreterTo run a script " Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3919" inside Python's Interpreter
Interactive Development Environment (IDE)Using an IDE with graphic debugging can greatly improve on your productivity For beginners, I recommend
For Webapp developers, I recommend
See "Python IDE and Debuggers" for details Python Basic SyntaxesCommentsA Python comment begins with a hash sign ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3921) and last till the end of the current line. Comments are ignored by the Python Interpreter, but they are critical in providing explanation and documentation for others (and yourself three days later) to read your program. Use comments liberally There is NO multi-line comment in Python?. (C/C++/Java supports multi-line comments via Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6. ) StatementsA Python statement is delimited by a newline. A statement cannot cross line boundaries, except
Unlike C/C++/C#/Java, you don't place a semicolon ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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) at the end of a Python statement. But you can place multiple statements on a single line, separated by semicolon ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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). For examples, Block, Indentation and Compound StatementsA block is a group of statements executing as a unit. Unlike C/C++/C#/Java, which use braces 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 528 to group statements in a body block, Python uses indentation for body block. In other words, indentation is syntactically significant in Python - the body block must be properly indented. This is a good syntax to force you to indent the blocks correctly for ease of understanding A compound statement, such as conditional ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number645), loop ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number647, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number602) and function definition ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3955), begins with a header line terminated with a colon ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number667); followed by the indented body block, as follows For examples, Python does not specify how much indentation to use, but all statements of the SAME body block must start at the SAME distance from the right margin. You can use either space or tab for indentation but you cannot mix them in the SAME body block. It is recommended to use 4 spaces for each indentation level The trailing colon ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number667) and body indentation is probably the most strange feature in Python, if you come from C/C++/C#/Java. Python imposes strict indentation rules to force programmers to write readable codes Variables, Identifiers and ConstantsLike all programming languages, a variable is a named storage location. A variable has a name (or identifier) and holds a value Like most of the scripting interpreted languages (such as JavaScript/Perl), Python is dynamically typed. You do NOT need to declare a variable before using it. A variables is created via the initial assignment. (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. ) For example, As mentioned, Python is dynamic typed. 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. , a variable can hold object of any types, as shown in the above examples Rules of Identifier (Names)An identifier starts with a letter ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3958, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3959) or an underscore ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3960), followed by zero or more letters, underscores and digits ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3961). Python does not allow special characters such as Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3962 and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3963 KeywordsPython 3 has 35 reserved words, or keywords, which cannot be used as identifiers
Quy ước đặt tên biếnTên biến là một danh từ hoặc một cụm danh từ được tạo thành từ nhiều từ. There are two convenctions
khuyến nghị
hằng sốPython không hỗ trợ hằng số, nơi không thể sửa đổi nội dung của nó. (C hỗ trợ hằng qua từ khóa Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4037, Java qua Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4038. ) Đó là một quy ước để đặt tên một biến bằng chữ hoa (nối với gạch dưới), e. g. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4039, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4040, to indicate that it should not be modified in the program. Tuy nhiên, không có gì ngăn cản nó được sửa đổi Loại dữ liệu. Số, Chuỗi và Danh sáchPython hỗ trợ nhiều loại số khác nhau, chẳng hạn như 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (đối với số nguyên như Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4042, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4043), 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (đối với số dấu phẩy động như Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4045, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4046, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4047) 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 524 (đối với boolean của 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 525 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 526) Python hỗ trợ chuỗi văn bản (một chuỗi ký tự). Trong Python, các chuỗi có thể được phân tách bằng dấu nháy đơn hoặc nháy kép, e. g. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4051, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4052, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4053 hoặc Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4054 (chuỗi trống) Python hỗ trợ cấu trúc mảng động có tên là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604, ký hiệu là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4056. Bạn có thể tham chiếu phần tử thứ i là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number607. Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 của Python tương tự như mảng của C/C++/Java, nhưng nó KHÔNG có kích thước cố định và có thể được mở rộng linh hoạt trong thời gian chạy Tôi sẽ mô tả chi tiết các kiểu dữ liệu này trong phần sau Đầu vào/đầu ra của bảng điều khiển. Các chức năng tích hợp sẵn input() và print()You can use built-in function Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number680 to read input from the console (as a string) and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number662 to print output to the console. Ví dụ, in()Hàm tích hợp sẵn Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number662 có chữ ký sau For examples, dấu phân cách của print() (sep) và kết thúc (end)Bạn có thể sử dụng đối số từ khóa tùy chọn Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4062 để đặt chuỗi phân tách (mặc định là khoảng trắng) và Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4063 cho chuỗi kết thúc (mặc định là dòng mới). Ví dụ như, in bằng Python 2 so với Python 3Nhớ lại rằng Python 2 và Python 3 KHÔNG tương thích. Trong Python 2, bạn có thể sử dụng " Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4064" mà không cần dấu ngoặc đơn (vì Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4065 là một từ khóa trong Python 2). Trong Python 3, bắt buộc phải có dấu ngoặc đơn vì Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number662 là một hàm. Ví dụ, Quan trọng. Luôn sử dụng hàm Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number662 với dấu ngoặc đơn, để có tính di động Kiểu dữ liệu và gõ độngPython có một số lượng lớn các kiểu dữ liệu dựng sẵn, chẳng hạn như Số (Số nguyên, Số thực, Boolean, Số phức), Chuỗi, Danh sách, Tuple, Bộ, Từ điển và Tệp. Các loại dữ liệu cấp cao hơn, chẳng hạn như Số thập phân và Phân số, được hỗ trợ bởi các mô-đun bên ngoài Bạn có thể sử dụng hàm có sẵn Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4068 để kiểm tra loại biến hoặc ký tự Loại sốPython hỗ trợ các kiểu số dựng sẵn này
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, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5277 The Assignment Operator (=)In Python, you do not need to declare variables before using the variables. 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. For example, Pair-wise Assignment and Chain AssignmentFor example, Assignment operator is right-associative, 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 del OperatorYou can use Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number635 operator to delete a variable. For example, 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 (tầng. ) -8. 9 // -2. 5 ⇒ 3. 0**Nhị phân ** yLũy thừa2 ** 5 ⇒ 32 1. 2**3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus (Phần dư)9 % 2 ⇒ 1 -9 % 2 ⇒ 1 9 % -2 ⇒ -1 -9 % -2 ⇒ -1 9.9 % 2.1 ⇒ 1.5 -9. 9 % 2. 1 ⇒ 0. 6000000000000001 Toán tử gán hợp chất (+=, -=, *=, /=, //=, **=, %=)Mỗi toán tử số học có một phép gán tốc ký tương ứng, i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number69, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number60, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number61, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number62, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number63, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number64 và Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number65. Ví dụ: Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number31 giống như Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number32 Tăng/Giảm (++, --)?Python không hỗ trợ các toán tử tăng ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number67) và giảm ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number68) (như trong C/C++/Java). Bạn cần sử dụng Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number32 hoặc Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number31 để tăng Python chấp nhận Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number37 và Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number38. Đừng mắc bẫy này. Nhưng Python đánh dấu lỗi cú pháp cho Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number39 và Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number40 Hoạt động kiểu hỗn hợpĐối với các hoạt động kiểu hỗn hợp, 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. Sau đó, nó thực hiện thao tác ở kiểu "lớn hơn" và trả về kết quả ở kiểu "lớn hơn". Trong 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 "nhỏ hơn" so với 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 522, "nhỏ hơn" so với Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number06 Relational (Comparison) Operators (==, !=, <, <=, >, >=, in, not in, is, is not)Python hỗ trợ các toán tử quan hệ (so sánh) này trả về giá trị 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 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 525 hoặc 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 526OperatorModeUsageDescriptionExample== . = < <= > >=Binaryx == y x != y x < y x x > y x > 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 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4010 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 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4010 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 Thí dụ. [LÀM] Toán tử logic (and, or, not)Python hỗ trợ các toán tử logic (boolean) này, hoạt động trên các giá trị boolean Toán tửModeUsageDes mô tảExampleandBinaryx và yLogical ANDorBinaryx hoặc yLogical ORnotUnarynot xLogical NOTghi chú
Thí dụ. [LÀM] Chức năng tích hợp sẵnPython cung cấp nhiều hàm dựng sẵn cho các số, bao gồm
For examples, Toán tử Bitwise (Nâng cao)Python hỗ trợ các toán tử bitwise này 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. nhị phân. ybitwise ORx. y ⇒ 0b10001111~Unary~xbitwise NOT (hoặc phủ định)~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110< Sợi dâyTrong Python, các chuỗi có thể được phân định bằng một cặp dấu nháy đơn ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number7) hoặc dấu nháy kép (_______08). Python cũng hỗ trợ các chuỗi nhiều dòng thông qua ba dấu ngoặc đơn ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number9) hoặc ba dấu ngoặc kép ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 520) Để đặt một trích dẫn đơn (________ 080) bên trong một chuỗi trích dẫn đơn, bạn cần sử dụng chuỗi thoát ________ 081. Tương tự, để đặt dấu ngoặc kép ( Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number82) bên trong chuỗi dấu ngoặc kép, hãy sử dụng Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number83. Không cần trình tự thoát để đặt một trích dẫn đơn bên trong một chuỗi trích dẫn kép; Chuỗi trích dẫn ba lần đơn hoặc ba lần trích dẫn có thể kéo dài trên nhiều dòng. There is no need for escape sequence to place a single/double quote inside a triple-quoted string. Các 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 sử dụng bộ ký tự Unicode để hỗ trợ quốc tế hóa (i18n) Trình tự thoát cho ký tự (\code)Giống như C/C++/Java, bạn cần sử dụng các chuỗi thoát (dấu gạch chéo ngược + mã) cho
Chuỗi thô (r'. ' hoặc r". ")Bạn có thể đặt trước một chuỗi bằng Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number97 để vô hiệu hóa việc giải thích các chuỗi thoát (i. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number98), tôi. e. , Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number99 là 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (hai ký tự) thay vì dòng mới (một ký tự). Các chuỗi thô được sử dụng rộng rãi trong regex (sẽ được thảo luận trong phần mô-đun Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3914) Chuỗi là bất biếnChuỗi là bất biến, tôi. e. , nội dung của chúng không thể được sửa đổi. Các hàm chuỗi như 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 a number: 123456789 123456789 is a magic number 123456789 is a magic number3910 trả về một đối tượng chuỗi mới thay vì sửa đổi chuỗi đang hoạt động Hàm và toán tử tích hợp cho chuỗiBạn có thể thao tác trên các chuỗi bằng cách sử dụng
Ghi chú. Các hàm và toán tử này có thể áp dụng cho tất cả các loại dữ liệu 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 bao gồ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 5212, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 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 5214 (sẽ được thảo luận sau)Function / OperatorUsageDescriptionExamples s = 'Xin chào'len()len(str)Lengthlen(s) ⇒ 5insubstr trong 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 * đếm str *= đếmLặp lại * 2 ⇒ 'Xin chàoXin chào'[i] [-i . str[-i]Indexing to get a character. Chỉ mục phía trước bắt đầu từ Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number695; chỉ mục phía sau bắt đầu từ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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. bước] [m. n] [m. ] [. n] [. ]str[m. n. bước] str[m. n] str[m. ] str[. n] str[. ] Cắt để lấy một chuỗi con. Từ chỉ mụ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 5221 (bao gồm) đế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 5222 (không bao gồm) với kích thướ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 5223. Mặc định là. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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[. ] ⇒ 'Xin chào' s[0. 5. 2] ⇒ 'Xin chào' For examples, Loại ký tự?Python không có kiểu dữ liệu ký tự chuyên dụng. Một ký tự chỉ đơn giản là một chuỗi có độ dài 1. Bạn có thể sử dụng toán tử lập chỉ mục để trích xuất từng ký tự từ một chuỗi, như minh họa trong ví dụ trên; Các chức năng tích hợp sẵ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 5229 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 5230 hoạt động trên ký tự, e. g. , Unicode so với ASCIITrong Python 3, các chuỗi được mặc định là Unicode. Chuỗi ASCII được biểu diễn dưới dạng chuỗi byte, có tiền tố là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4013, 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 Trong Python 2, các chuỗi được mặc định là chuỗi ASCII (chuỗi byte). Các chuỗi Unicode có tiền tố là 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 Bạn phải luôn sử dụng Unicode để quốc tế hóa (i18n) Các hàm thành viên dành riêng cho chuỗiPython hỗ trợ các chuỗi thông qua một lớp dựng sẵn có tên là 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (Chúng tôi sẽ mô tả lớp này trong chương Lập trình hướng đối tượng). Lớp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523 cung cấp nhiều hàm thành viên. Vì chuỗi là bất biến nên hầu hết các hàm này đều trả về một chuỗi mới. Các hàm thành viên thường được sử dụng như sau, giả sử rằng 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5236 là một đối tượng 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 523
Định dạng chuỗi 1 (Kiểu mới). Sử dụng str. hàm định dạng ()Có một số cách để tạo một chuỗi được định dạng cho đầu ra. Python 3 giới thiệu một phong cách mới trong hàm thành viê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 52767 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 523 với 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 528 làm trình giữ chỗ (được gọi là trường định dạng). Ví dụ như, Khi bạn chuyển các danh sách, bộ dữ liệu hoặc từ điển (sẽ được thảo luận sau) làm đối số vào 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 52767, bạn có thể tham chiếu các phần tử của chuỗi trong các trường định dạng với 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5260. Ví dụ như, Định dạng chuỗi 2. Sử dụng str. rjust(n), str. ljust(n), str. trung tâm (n), str. zfill(n)Bạn cũng có thể sử dụng các hàm thành viên 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 523 như 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (trong đó Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4009 là độ rộng trường), 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 để định dạng chuỗi. Ví dụ, Định dạng chuỗi 3 (Kiểu cũ). Sử dụng toán tử %Kiểu cũ (trong Python 2) là sử dụng toán tử Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number66, với các chỉ định định dạng 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5269 giống như C. Ví dụ như, Tránh sử dụng kiểu cũ để định dạng Chuyển đổi giữa Chuỗi và Số. int(), float() và str()Bạn có thể sử dụng các hàm dựng sẵ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 52738 và Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number68 để phân tích chuỗi "số" thành số nguyên hoặc số thực; . Ví dụ, Nối một chuỗi và một số?Bạn KHÔNG THỂ nối một chuỗi và một số (kết quả là 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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ụ, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5278 The None ValuePython cung cấp một giá trị đặc biệt có tên là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number3966 (lưu ý cách viết hoa đầu tiên), có thể được sử dụng để khởi tạo một đối tượng (sẽ được thảo luận trong OOP sau). Ví dụ, Danh sách, Tuple, Từ điển và Tập hợpLiệt kê [v1, v2,. ]Python có một mảng động tích hợp mạnh mẽ có tên là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604
Hàm và toán tử tích hợp cho danh sáchMột Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604, giống như chuỗi, là một 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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. Do đó, bạn có thể vận hành danh sách bằng cách sử dụng
ghi chú
lst = [8, 9, 6, 2]in not inx in lst x . 5 in lst ⇒ False+ +=lst + lst1 lst += lst1Concatenationlst + [5, 2] ⇒ [8, 9, 6, 2, 5, 2]* *=lst * count lst *= countRepetitionlst * 2 ⇒ [8, 9, 6, 2, 8, 9, 6, 2][i] [-i]lst[i] lst[-i]Indexing to get an item. Chỉ mục phía trước bắt đầu từ Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number695; chỉ mục phía sau bắt đầu từ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 (hoặc Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number611). lst[1] ⇒ 9 lst[-2] ⇒ 6[m. n. bước] [m. n] [m. ] [. n] [. ]lst[m. n. bước] lst[m. n] lst[m. ] lst[. n] lst[. ] Cắt lát để lấy danh sách phụ. Từ chỉ mụ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 5221 (bao gồm) đến Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number4009 (không bao gồm) với kích thướ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 5223. Mặc định là. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number695, n là Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number611. 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[. ] ⇒ Sao chép lst[4. ] = [1, 2] ⇒ Extenddeldel lst[i] del lst[m. n] del lst[m. n. bước]Xóa một hoặc nhiều mụcdel lst[1] ⇒ [8, 6, 2] del lst[1. ] ⇒ [8] del lst[. ] ⇒ [] (Xóa)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 Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604, không giống như chuỗi, có thể thay đổi. Bạn có thể chèn, xóa và sửa đổi các mục của nó For examples, Nối các mục vào danh sáchSao chép một danh sáchchức năng thành viên danh sách cụ thểLớp Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 cung cấp nhiều hàm thành viên. Giả sử 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52720 là một đối tượng Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604
Recall that Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 is mutable (unlike string which is immutable). These functions modify the Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 directly. For examples, Using list as a last-in-first-out StackTo use a Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 as a last-in-first-out (LIFO) stack, use Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number670 to add an item to the top-of-stack (TOS) and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number671 to remove the item from the TOS Using list as a first-in-first-out QueueTo use a Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number604 as a first-in-first-out (FIFO) queue, use Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number670 to add an item to the end of the queue and Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number674 to remove the first item of the queue However, Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number674 is slow. The standard library provide a class Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number676 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 a number: 123456789 123456789 is a magic number 123456789 is a magic number3945 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 number: 123456789 123456789 is a magic number 123456789 is a magic number678 is a tuple)
Conversion between List and TupleYou can covert a list to a tuple using built-in function Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number689; and a tuple to a list using Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number690. 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 number: 123456789 123456789 is a magic number 123456789 is a magic number694 class has many member methods. The commonly-used are follows (suppose that Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number695 is a Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number694 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 For example, Set-Specific Operators ( |