Các dấu ngoặc đơn xung quanh đối số để in có gây ra lỗi cú pháp không?

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

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

Python By Examples

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

Syntax Summary and Comparison

  • Comment. Python's comment begins with a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 and lasts until the end-of-line. Python không hỗ trợ bình luận nhiều dòng.
    [Nhận xét cuối dòng C/C++/C#/Java bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. They support multi-line comments via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. ]
  • String. Python's string can be delimited by either single quotes [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7] or double quotes [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8]. Python also supports multi-line string, delimited by either triple-single [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    9] or triple-double quotes [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    0]. Strings are immutable in Python.
    [C/C++/C#/Java use double quotes for string and single quotes for character. They do not support multi-line string. ]
  • Variable Type Declaration. Like most of the scripting interpreted languages [such as JavaScript/Perl], Python is dynamically typed. You do NOT need to declare variables [name and type] before using them. A variables is created via the initial assignment. Python 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.
    [Trong C/C++/C#/Java, bạn cần khai báo tên và kiểu biến trước khi sử dụng. ]
  • Loại dữ liệu. Python support these data types.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1 [integers],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2 [floating-point numbers],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 [String],
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4 [boolean of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6], and more
  • Câu lệnh. Python's statement ends with a newline.
    [C/C++/C#/Java's statement ends with a semi-colon [
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    7]]
  • Câu lệnh ghép và thụt đầu dòng. Python sử dụng thụt đầu dòng để biểu thị khối cơ thể. [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
    52
    8. ] Cú pháp này buộc bạn phải thụt lề chương trình một cách chính xác, điều này rất quan trọng để đọc chương trình của bạn. Bạn có thể sử dụng dấu cách hoặc tab để thụt đầu dòng [nhưng không được kết hợp cả hai]. Mỗi cấp độ cơ thể phải được thụt vào ở cùng một khoảng cách. Nên sử dụng 4 dấu cách cho mỗi mức thụt đầu dòng
  • Toán tử chuyển nhượ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
    52
    9
  • toán tử số học.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60 [cộng],
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61 [trừ],
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62 [nhân],
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63 [chia], ________ 164 [chia số nguyên], ________ 165 [số mũ], ________ 166 [mô đun]. [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68 không được hỗ trợ]
  • Toán tử gán hợp chất.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65
  • Toán tử so sánh.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    60,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    61,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65
  • Toán tử logic.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68. [C/C++/C#/Java sử dụng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601]
  • có điều kiện
  • Vòng. Python KHÔNG hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 giống C truyền thống với chỉ mục.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    603
  • Danh sách. Python hỗ trợ mảng động có kích thước thay đổi thông qua cấu trúc dữ liệu tích hợp có tên là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604, được ký hiệu là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    605
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    606. Danh sách tương tự như mảng của C/C++/C#/Java nhưng KHÔNG có kích thước cố định. Bạn có thể tham khảo một phần tử qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    608 hoặc danh sách phụ qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    609. Bạn có thể sử dụng các hàm có sẵn như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    610,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    611,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    612
  • Cấu trúc dữ liệu
    • Danh sách.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      613 [mảng động có thể thay đổi]
    • Tuple.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      614 [Mảng có kích thước cố định không thay đổi]
    • Từ điển.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      615 [cặp khóa-giá trị có thể thay đổi, mảng kết hợp, bản đồ]
    • Bộ.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      616 [with unique key and mutable]
  • Sequence [String, Tuple, List] Operators and Functions.
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      62,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      63. membership test.
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      60. nối
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      62. lặp đi lặp lại
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      621,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      622. lập chỉ mục
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      623. cắt lát
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      624,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      625,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      626
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      627,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      628
    Chỉ dành cho các chuỗi có thể thay đổi [danh sách]
    • Assignment via
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      621,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      630 [indexing] and
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      623 [slicing]
    • Chuyển nhượng qua
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      9,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      69 [ghép từ ghép],
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      61 [lặp từ ghép]
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      635. delete
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      636,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      637,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      638
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      639,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      640,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      641,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      642,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      643,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      644
  • Function Definition

Ví dụ grade_statistic. py - Cấu trúc và cú pháp cơ bản

This example repeatably prompts user for grade [between 0 and 100 with input validation]. It then compute the sum, average, minimum, and print the horizontal histogram

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 number
645, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
602, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647, đầu vào/đầu ra, chuỗi,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 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
5

To run the Python script

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

The expected output is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
7How it Works
  1. #. /usr/bin/env python3 [Line 1] is applicable to the Unix environment only. It is known as the Hash-Bang [or She-Bang] for specifying the location of Python Interpreter, so that the script can be executed directly as a standalone program
  2. # -*- coding. UTF-8 -*- [Line 2, optional] specifies the source encoding scheme for saving the source file. We choose and recommend UTF-8 for internationalization. This special format is recognized by many popular editors for saving the source code in the specified encoding format
  3. Doc-String. The script begins by the so-called doc-string [documentation string ][Line 3-12] to provide the documentation for this Python module. Doc-string is a multi-line string [delimited by triple-single or triple-double quoted], which can be extracted from the source file to create documentation
  4. def my_sum[lst]. [Line 15-20]. We define a function called
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    649 which takes a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 and return the sum of the items. It uses a for-each-in loop to iterate through all the items of the given
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604. As Python is interpretative, you need to define the function first, before using it. We choose the function name
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    652 to differentiate from the built-in function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    653
  5. bins = [0]*10 [Line 38]. Python supports repetition operator [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62]. This statement creates a list of ten zeros. Similarly, repetition operator [*] can be apply on string [Line 59]
  6. for row in range[len[bins]]. [Line 48, 56]. Python supports only
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655 loop. It does NOT support the traditional C-like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602-loop with index. Hence, we need to use the built-in
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    657 function to create a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 of indexes
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    659, then apply the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655 loop on the index
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
  7. 0 bool. [Dòng 10]. Các phần nổi bật được gọi là chú thích gợi ý loại. Chúng bị Trình thông dịch Python bỏ qua và chỉ đóng vai trò là tài liệu
  8. nếu __name__ == '__main__'. [Dòng 51]. Khi bạn thực thi một mô-đun Python thông qua Trình thông dịch Python, biến toàn cục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 được đặt thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    684. Mặt khác, khi một mô-đun được nhập vào một mô-đun khác,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    683 của nó được đặt thành tên mô-đun. Do đó, mô-đun trên sẽ được thực thi nếu nó được tải bởi trình thông dịch Python, nhưng không được nhập bởi mô-đun khác. Đây là một thực hành tốt để thử nghiệm một mô-đun

Ví dụ hex2dec. py - Chuyển đổi thập lục phân sang thập phân

Ví 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 number: 123456789
123456789 is a magic number
123456789 is a magic number
6
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
6How it Works
  1. Công thức chuyển đổi 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
    52
    711, trong đó
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    712
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    713 [Dòng 12]. Chúng tôi sẽ sử dụng chức nă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
    52
    715 của mô-đun
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    714 để chấm dứt chương trình cho đầu vào không hợp lệ. Trong Python, chúng ta cần import module [thư viện bên ngoài] trước khi sử dụng
  3. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    716 [Dòng 21]. Python không hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602 giống C truyền thống với chỉ mục. Nó chỉ hỗ trợ vò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
    52
    718 để lặp qua 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
    52
    719 trong
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    720. We use the built-in function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    657 to generate a list
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    722, and then iterate through each item in the generated list
  4. Trong Python, chúng ta có thể lặp qua từng ký tự của một chuỗi thông qua vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    655, e. g. , Đối với ví dụ này, chúng ta không thể sử dụng ở trên vì chúng ta cần chỉ mục của ký tự để thực hiện chuyển đổi
  5. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    724 [Dòng 22]. Trong Python, bạn có thể sử dụng toán tử lập 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
    52
    725 để trích xuất ký tự thứ
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    726. Lưu ý rằng Python không hỗ trợ ký tự, nhưng coi ký tự là chuỗi 1 ký tự
  6. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    727 [Line 23]. Python supports exponent [or power] operator in the form of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65. Take note that string index begins from 0, and increases from left-to-right. On the other hand, the hex digit's exponent begins from 0, but increases from right-to-left
  7. There are
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    729 cases of 1-character strings for
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    730,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    731,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    732,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    733, and other, which can be handled by 5 cases of nested-if as follows
    1. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      734 [Line 24]. we convert the string
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      734 to
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      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
      737 via
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      738 built-in function
    2. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      739 [Line 26]. no nothing. In Python, you need to include a dummy statement called
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      740 [Line 28] in the body block
    3. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      732 [Line 28]. To convert 1-character string
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      732 to
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      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
      744, we use the
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      745 built-in function to get the Unicode
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      1 of
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      747, subtract by the base
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      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
      748 and add 10
    4. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      749 [Line 30]. Python supports a data structure called dictionary [associative array], which contains key-value pairs. We created a dictionary
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      750 [Line 15] to map
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      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
      751 to
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      752,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      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
      753 to
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      754, and so on. We can then reference the dictionary via
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      755 to retrieve its value [Line 31]
    5. other [Line 32]. we use
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      756 to terminate the program. We return a non-zero code to indicate abnormal termination

Example bin2dec. py - Binary to Decimal Conversion

This 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 number
6
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60How it Works
  1. We organize the code in functions
  2. The conversion formula is.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    757, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    758
  3. You can use built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    759 to convert a number string from the given
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    760 to decimal [Line 38]

Ví dụ dec2hex. py - Decimal to Hexadecimal Conversion

This 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 number
61
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
62How it Works
  1. We use the modulus/division repeatedly to get the hex digits in reverse order
  2. We use a look-up list [Line 11] to convert
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    762 to hex digit
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    763
  3. You can use built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    764,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    765,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    766 to convert decimal to hexadecimal, octal and binary, respectively; or use the more general
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    767 function. E. g. ,

Example wc. py - Word Count

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
52
768 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 number
60How it works
  1. import sys [Line 14]. We use the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    714 module [@ https. //docs. python. org/3/library/sys. html] from the Python's standard library to retrieve the command-line arguments kept in
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    771, and to terminate the program via
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    772. In Python, you need to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 the module before using it
  2. The command-line arguments are stored in a variable
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    771, which is a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 [Python's dynamic array]. The first item of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    777 is the script name, followed by the other command-line arguments
  3. if len[sys. argv] . = 2. [Line 15]. We use the built-in function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    778 to verify that the length of the command-line-argument
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 is 2
  4. with open[sys. argv[1]] as infile. [Line 25]. We open the file via a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    780 statement, which closes the file automatically upon exit
  5. for line in infile. [Line 26]. We use a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    781 loop [Line 29] to process each
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    782 of the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    783, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    782 belong to the built-in class "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3" [meant for string support @ https. //docs. python. org/3/library/stdtypes. html#str]. We use the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 class' member functions
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    787 to strip the leading and trailing white spaces; and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    788 to split the string into a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604 of words
  6. We also invoke the Unix utility "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    768" via external shell command in 2 ways. via
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    791 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    792

Example htmlescape. py - Escape Reserved HTML Characters

Ví dụ này đọc tên tệp đầu vào và đầu ra từ dòng lệnh và thay thế các ký tự HTML dành riêng bằng các thực thể HTML tương ứng của chúng. It illustrates file input/output and string substitution

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
64How it works
  1. import sys [Line 14]. We
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693 the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    714 module [@ https. //docs. python. org/3/library/sys. html]. We retrieve the command-line arguments from the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    604
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    771, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    777 is the script name; and use
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    772 [Line 18] to terminate the program
  2. with open[sys. argv[1]] as infile, open[sys. argv[2], 'w'] as outfile. [Line 21]. We use the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    780 statement, which closes the files automatically at exit, to open the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    783 for read [default] and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3901 for write [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3902]
  3. for line in infile. [Line 22]. We use a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    781 loop to process each
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    782 of the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    783, where
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    782 belongs to the built-in class "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3" [meant for string support @ https. //docs. python. org/3/library/stdtypes. html#str]. We use
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    3 class' member function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3909 to strip the trailing [right] white spaces; and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3910 for substitution
  4. Python 3. 2 introduces a new
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3911 module, with a function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3912 to escape HTML reserved characters.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65

Example files_rename. py - Rename Files

This 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 number
3913] and regular expression [using module
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3914]

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
66How it works

Introduction

Python 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 is an easy and intuitive language. Python scripts are easy to read and understand
  • Python [like Perl] is expressive. A single line of Python code can do many lines of code in traditional general-purpose languages [such as C/C++/Java]
  • Python is free and open-source. It is cross-platform and runs on Windows, Linux/UNIX, and Mac OS X
  • Python is well suited for rapid application development [RAD]. You can code an application in Python in much shorter time than other general-purpose languages [such as C/C++/Java]. Python can be used to write small applications and rapid prototypes, but it also scales well for developing large-scale project
  • Python is a scripting language and dynamically typed. Like most of the scripting languages [e. g. , Perl, JavaScript], Python associates types with objects, instead of variables. That is, a variable can be assigned a value of any type, a list [array] can contain objects of different types
  • Python cung cấp quản lý bộ nhớ tự động. You do not need to allocate and free memory in your programs
  • Python provides high-level data types such as dynamic array and dictionary [or associative array]
  • Python is object-oriented
  • Python is not a fully compiled language. It is compiled into internal byte-codes, which is then interpreted. Hence, Python is not as fast as fully-compiled languages such as C/C++
  • Python comes with a huge set of libraries including graphical user interface [GUI] toolkit, web programming library, networking, and etc

Python has 3 versions

  • Python 1. the initial version
  • Python 2. released in 2000, with many new features such as garbage collector and support for Unicode
  • Python 3 [Python 3000 or py3k]. A major upgrade released in 2008. Python 3 is NOT backward compatible with Python 2
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. 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 đưa ra lệnh này

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67

Cài đặt và Bắt đầu

Cài đặt

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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68Ubuntu [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

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69

Để xác minh cài đặt Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
70các cửa sổ

Bạn có thể cài đặt một trong hai

  1. "Phân phối Anaconda" [Xem phần trước]
  2. Python đơn giản từ Python Software Foundation @ https. //www. con trăn. org/download/, tải xuống trình cài đặt MSI 32-bit hoặc 64-bit và chạy trình cài đặt đã tải xuống
  3. Trong Cygwin [môi trường Unix cho Windows] và cài đặt Python [trong danh mục "phát triển"]
hệ điều hành Mac

[LÀM]

Tài liệu

Tà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 Python

Bắt đầu Trình thông dịch Python tương tác

Bạ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]

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

Dấu nhắc lệnh Python được ký hiệu là _______ 13915. Bạn có thể nhập câu lệnh Python tại dấu nhắc lệnh của Python, e. g. ,

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

Để thoát khỏi Trình thông dịch Python

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    715
  • [Mac OS X và Ubuntu]
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3917
  • [Windows]
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3918 theo sau là Enter

Viết và chạy tập lệnh Python

Tập lệnh Python đầu tiên - xin chào. py

Sử 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 a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" trong thư mục bạn chọ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
52
73How it Works
  1. Theo quy ước, tên tập lệnh Python [mô-đun] ở dạng chữ thường [e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3920]
  2. Nhận xét EOL. Các câu lệnh bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3921 cho đến cuối dòng [EOL] là các nhận xét
  3. #. /usr/bin/env python3 [Line 1] is applicable to the Unix environment only. It is known as the Hash-Bang [or She-Bang] for specifying the location of Python Interpreter, so that the script can be executed directly as a standalone program
  4. # -*- coding. UTF-8 -*- [Line 2, optional] specifies the source encoding scheme for saving the source file. We choose and recommend UTF-8 for internationalization. This special format is recognized by many popular editors for saving the source code in the specified encoding format
  5. """ xin chào. """ [Dòng 3-5]. Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu thường là một chuỗi nhiều dòng [được phân định bằng dấu nháy đơn ba hoặc dấu ba kép], có thể được trích xuất từ ​​​​tệp nguồn để tạo tài liệu
  6. Biến. Ta tạo các biến ________ 13922, ________ 13923, ________ 13924, ________ 13925,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3926 [Dòng 6, 8, 10, 12, 14] bằng cách gán giá trị cho chúng
  7. Các chuỗi của Python có thể được đặt trong dấu nháy đơn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7 [Dòng 6] hoặc dấu nháy kép
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    8
  8. Số nguyên của Python có kích thước không giới hạn [Dòng 8]
  9. Python hỗ trợ số dấu phẩy động [Dòng 10]
  10. Python supports complex numbers [Line 12] and other high-level data types
  11. Python hỗ trợ một mảng động gọi là danh sách [Dòng 14], được đại diện bởi
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3929. Phần tử có thể được truy xuất thông qua chỉ mục
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    607 [Dòng 15]
  12. in[aVar]. Hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    662 có thể được sử dụng để in giá trị của một biến ra bàn điều khiển
Sản lượng dự kiến

Các kết quả đầu ra 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
52
74Chạy tập lệnh Python

Bạ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

Unix's Executable Shell Script

In Linux/Mac OS X, you can turn a Python script into an executable program [called Shell Script or Executable Script] by

  1. Start with a line beginning with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3932 [called "hash-bang" or "she-bang"], followed by the full-path name to the Python Interpreter, e. g. , To locate the Python Interpreter, use command "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3933" or "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3934"
  2. Make the file executable via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3935 [change file mode] command
  3. You can then run the Python script just like any executable programs. The system will look for the Python Interpreter from the she-bang line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    75

Hạn chế là bạn phải mã hóa đường dẫn đến Trình thông dịch Python, điều này có thể khiến chương trình không thể di động trên các máy khác nhau

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 a number: 123456789
123456789 is a magic number
123456789 is a magic number
3936 sẽ định vị Trình thông dịch Python [từ các mục nhập của
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3937]. 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 Windows

Trong Windows, bạn có thể liên kết phần mở rộng tệp "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3938" với Python Có thể hiểu được, để 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 a number: 123456789
123456789 is a magic number
123456789 is a magic number
3919" bên trong Trình thông dịch của Python

  • Bạn có thể sử dụng đường dẫn tuyệt đối hoặc tương đối cho tên tệp. Nhưng,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3940 [đối với thư mục gốc] không hoạt động?
  • Hàm tích hợp sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3941 sẽ mở tệp ở chế độ chỉ đọc mặc định;

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

  1. Trình thông dịch Python [như mô tả ở trên]
  2. Python IDLE
  3. Jupyter Notebook [đặc biệt dành cho Phân tích dữ liệu]

Đối với các nhà phát triển Webapp, tôi khuyên bạn nên

  1. Nhật thực với PyDev
  2. PyCharm

Xem "Python IDE và trình gỡ lỗi" để biết chi tiết

Các cú pháp cơ bản của Python

Bình luận

Nhận xét Python bắt đầu bằng dấu thăng [

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3921] và kéo dài đến cuối dòng hiện tại. 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 number
6. ]

Các câu lệnh

Mộ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ừ

  1. Một biểu thức trong dấu ngoặc đơn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3945, dấu ngoặc vuông
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946 và 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
    52
    8 có thể trải rộng trên nhiều dòng
  2. Dấu gạch chéo ngược [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3948] ở cuối dòng biểu thị sự tiếp tục của dòng tiếp theo. Đây là một quy tắc cũ và KHÔNG được khuyến nghị vì nó dễ bị lỗi

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
52
7] ở 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
52
7]. Ví dụ như,

Câu lệnh khối, thụt đầu dòng và hợp chất

Mộ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
52
8 để 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 [

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
645], vòng lặp [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
647,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
602] và định nghĩa hàm [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3955], bắt đầu bằng một dòng tiêu đề kết thúc bằng dấu hai chấm [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667];

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 [

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
667] và thụt lề nội dung 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 a number: 123456789
123456789 is a magic number
123456789 is a magic number
3958,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3959] hoặc dấu gạch dưới [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3960], theo sau là 0 hoặc nhiều chữ cái, dấu gạch dưới và chữ số [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3961]. Python không cho phép các ký tự đặc biệt như
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3962 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
3963

từ khóa

Python 3 có 35 từ hoặc từ khóa dành riêng, không thể được sử dụng làm định danh

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966 [boolean và chữ đặc biệt]
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    693,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3968,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3969
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    703,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3971,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3972,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    602,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    647,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3976,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3977,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    740,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3979 [điều khiển luồng]
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3955,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3981,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3982,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3983,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3984 [chức năng]
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3985
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    67,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    635 [nhà điều hành]
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3991,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3992,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3993,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3994,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3995 [xử lý lỗi]
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3996,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3997,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3998
Quy ước đặt tên biến

Tê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ừ. Có hai quy ước

  1. Trong các từ viết thường và tùy ý nối với dấu gạch dưới nếu nó cải thiện khả năng đọc, e. g. , num_students,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3999,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4000,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4001, v.v.
  2. Trong cái gọi là trường hợp lạc đà trong đó từ đầu tiên được viết thường và các từ còn lại được viết hoa ban đầu, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4002,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4003,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4004,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4005,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4006 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4007. [Đây là quy ước đặt tên của Java. ]
khuyến nghị
  1. Điều quan trọng là chọn một tên tự mô tả và phản ánh chặt chẽ ý nghĩa của biến, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4002, nhưng không phải là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4009 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010, để lưu trữ số học sinh. Viết tắt cũng được mà e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4011 cho chỉ mục
  2. Không sử dụng những tên vô nghĩa như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4012,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4013,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4014,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4017,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4009,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4019,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4020,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4021,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4022,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4023 [mục đích của bài tập này là gì?] và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4024 [Ví dụ này nói về cái gì?]
  3. Tránh các tên có một chữ cái như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4017,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4012,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4013,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4014, dễ gõ hơn nhưng thường vô nghĩa. Ngoại lệ là các tên phổ biến như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4010,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4032,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4033 cho tọa độ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4015 cho chỉ mục. Tên dài khó gõ hơn, nhưng hãy tự ghi lại chương trình của bạn. [I suggest you spend sometimes practicing your typing. ]
  4. Sử dụng danh từ số ít và số nhiều một cách thận trọng để phân biệt giữa biến số ít và số nhiều.   For example, you may use the variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4035 to refer to a single row number and the variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4036 to refer to many rows [such as a list of rows - to be discussed later]
Constants

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 number
4037, Java qua
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4038. ]

It is a convention to name a variable in uppercase [joined with underscore], e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4039,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4040, để chỉ ra rằng nó không nên được sửa đổi trong chương trình. Tuy nhiên, không có gì ngăn cản nó được sửa đổi

Loại dữ liệu. Number, String and List

Python 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
52
1 [đối với số nguyên như
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4042,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4043],
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2 [đối với số dấu phẩy động như
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4045,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4046,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4047] 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
52
4 [đố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
52
5 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
52
6]

Python supports text string [a sequence of characters]. In Python, strings can be delimited with single-quotes or double-quotes, e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4051,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4052,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4053 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4054 [empty string]

Python supports a dynamic-array structure called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604, denoted as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4056. You can reference the i-th element as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
607. Python's
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
604 is similar to C/C++/Java's array, but it is NOT fixed size, and can be expanded dynamically during runtime

I will describe these data types in details in the later section

Console Input/Output. input[] and print[] Built-in Functions

You can use built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
680 to read input from the console [as a string] and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 to print output to the console. For example,

print[]

The built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 has the following signature

Ví dụ như,

print[]'s separator [sep] and ending [end]

You can use the optional keyword-argument

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4062 to set the separator string [default is space], and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4063 for ending string [default is newline]. For examples,

print in Python 2 vs Python 3

Recall that Python 2 and Python 3 are NOT compatible. In Python 2, you can use "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4064", without the parentheses [because
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4065 is a keyword in Python 2]. In Python 3, parentheses are required as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 is a function. For example,

Important. Always use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
662 function with parentheses, for portability

Data Types and Dynamic Typing

Python has a large number of built-in data types, such as Numbers [Integer, Float, Boolean, Complex Number], String, List, Tuple, Set, Dictionary and File. More high-level data types, such as Decimal and Fraction, are supported by external modules

You can use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4068 to check the type of a variable or literal

Number Types

Python supports these built-in number types

  1. Integers [type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1]. e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4042,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4043. Unlike C/C++/Java, integers are of unlimited size in Python. For example, You can also express integers in hexadecimal with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4072 [or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4073]; in octal with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4074 [or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4075]; and in binary with prefix
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4076 [or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4077]. For examples,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4078,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4079,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4080,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4081
  2. Floating-point numbers [type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2]. e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4083,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4084,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4085,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4086, with a decimal point and an optional exponent [denoted by
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4087 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4088].
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    2s are 64-bit double precision floating-point numbers. For example,
  3. Booleans [type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    4]. takes a value of either
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6. Take note of the spelling in initial-capitalized. In Python, integer
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695, an empty value [such as empty string
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4053,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4054, empty list
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3946, empty tuple
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3945, empty dictionary
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    8], and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    3966 are treated as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6; anything else are treated as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5. Booleans can also act as integers in arithmetic operations with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 for
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    5 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    695 for
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    6. For example,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    76
  4. Complex Numbers [type
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    06]. e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    07,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    08. Complex numbers have a real part and an imaginary part denoted with suffix of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4016 [or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    10]. For example,
  5. Other number types are provided by external modules, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    11 module for decimal fixed-point numbers,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    12 module for rational numbers

Dynamic Typing and Assignment Operator

Nhớ lại rằng Python được gõ động [thay vì gõ tĩnh]

Python associates types with objects, instead of variables. That is, a variable does not have a fixed type and can be assigned an object of any type. A variable simply provides a reference to an object

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

You can use built-in function

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

Type Casting. int[x], float[x], str[x]

You can perform type conversion [or type casting] via built-in functions

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

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

Check Instance's Type. isinstance[instance, type]

You can also use the built-in function

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

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

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

Pair-wise Assignment and Chain Assignment

Ví dụ,

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

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

del Operator

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
635 operator to delete a variable. For example,

Number Operations

Arithmetic Operators [+, -, *, /, //, **, %]

Python supports these arithmetic operators

OperatorModeUsageDescriptionExample+Binary
Unaryx + y
+xAddition
Positive-Binary
Unaryx - y
-xSubtraction
Negate*Binaryx * yMultiplication/Binaryx / yFloat Division
[Returns a float]1 / 2 ⇒ 0. 5
-1 / 2 ⇒ -0. 5//Binaryx // yInteger Division
[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. 6000000000000001Compound Assignment Operators [+=, -=, *=, /=, //=, **=, %=]

Each of the arithmetic operators has a corresponding shorthand assignment counterpart, i. e. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
60,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
61,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
62,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
63,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
64 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
65. For example
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 is the same as
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32

Increment/Decrement [++, --]?

Python does not support increment [

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
67] and decrement [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68] operators [as in C/C++/Java]. You need to use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
32 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
31 for increment

Python accepts

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
37, and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
38. Don't get trap into this. But Python flags a syntax error for
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
39 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
40

Mixed-Type Operations

For mixed-type operations, e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
41 [
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
42], the value of the "smaller" type is first promoted to the "bigger" type. It then performs the operation in the "bigger" type and returns the result in the "bigger" type. In Python,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
1 is "smaller" than
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2, which is "smaller" than
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
06

Relational [Comparison] Operators [==, !=, =, in, not in, is, is not]

Python supports these relational [comparison] operators that return a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 value of either
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5 or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
6

OperatorModeUsageDescriptionExample==
. =
=Binaryx == y
x . = y
x < y
x
x > y
x >= yComparison
Return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 of either
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5 or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
6in
not inBinaryx in seq
x not in seqCheck if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 is contained in the sequence
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53
Return bool of either True or Falselst = [1, 2, 3]
x = 1
x in lst ⇒ Falseis
is notBinaryx is y
x is not yCheck if
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
4010 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53 are referencing the same object
Return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
4 of either
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
5 or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
6

Example. [TODO]

Logical Operators [and, or, not]

Python supports these logical [boolean] operators, that operate on boolean values

OperatorModeUsageDescriptionExampleandBinaryx and yLogical ANDorBinaryx or yLogical ORnotUnarynot xLogical NOT

Notes

  • Python's logical operators are typed out in word, unlike C/C++/Java which uses symbols
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    600 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    601
  • Python does not have an exclusive-or [
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    62] boolean operator

Example. [TODO]

Built-in Functions

Python provides many built-in functions for numbers, including

  • Mathematical functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    63,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    64,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    65, etc
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    66 to get the type
  • Type conversion functions.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    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
    738,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    68,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    69,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    70, etc
  • Base radix conversion functions.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    71,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    72,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    73

Ví dụ như,

Bitwise Operators [Advanced]

Python supports these bitwise operators

OperatorModeUsageDescriptionExample
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
74
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
75&binaryx & ybitwise ANDx & y ⇒ 0b10000001. binaryx . ybitwise ORx . y ⇒ 0b10001111~Unary~xbitwise NOT [or negate]~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110

Chủ Đề