B xin chào chúc b thêm python print b

Giả sử chúng ta cần ghi tuổi của 5 học sinh. Instead of creating 5 separate variables, we can simply create a list

B xin chào chúc b thêm python print b
Elements of a list

Tạo một danh sách Python

Một danh sách được tạo bằng Python bằng cách đặt các mục bên trong

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
5, được phân tách bằng dấu phẩy. For example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]

Here, we have created a list named numbers with 3 integer items

Một danh sách có thể có bất kỳ số lượng mục nào và chúng có thể thuộc các loại khác nhau (số nguyên, số float, chuỗi, v.v. ). For example,

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]

Access Python List Elements

In Python, each item in a list is associated with a number. The number is known as a list index

We can access elements of an array using the index number (0, 1, 2 …). For example,

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++

In the above example, we have used the list comprehension to make a list with each item being increased by power of 2. Notice the code,

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 does not support multi-line comments.
    (C/C++/C#/Java end-of-line comment begins with
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    5. They support multi-line comments via
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    6. )
  • String. Chuỗi của Python có thể được phân định bằng dấu nháy đơn (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    7) hoặc dấu nháy kép (
    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 (
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    00). Strings are immutable in Python.
    (C/C++/C#/Java use double quotes for string and single quotes for character. They do not support multi-line string. )
  • Variable Type Declaration. Like most of the scripting interpreted languages (such as JavaScript/Perl), Python is dynamically typed. You do NOT need to declare variables (name and type) before using them. A variables is created via the initial assignment. Python associates types with the objects, not the variables, i. e. , a variable can hold object of any types.
    (In C/C++/C#/Java, you need to declare the name and type of a variable before using it. )
  • Data Types. Python support these data types.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01 (integers),
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    02 (floating-point numbers),
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03 (String),
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    04 (boolean of
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05 or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06), and more
  • Statements. Python's statement ends with a newline.
    (C/C++/C#/Java's statement ends with a semi-colon (
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    07))
  • Compound Statements and Indentation. Python uses indentation to indicate body-block. (C/C++/C#/Java use braces
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    08. ) This syntax forces you to indent the program correctly which is crucial for reading your program. You can use space or tab for indentation (but not mixture of both). Each body level must be indented at the same distance. It is recommended to use 4 spaces for each level of indentation
  • Assignment Operator.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    09
  • Arithmetic Operators.
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    00 (add),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    01 (subtract),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    02 (multiply),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    03 (divide),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    04 (integer divide),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    05 (exponent),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    06 (modulus). (
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    07 and
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    08 are not supported)
  • Compound Assignment Operators.
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    09,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    00,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    01,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    02,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    03,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    04,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    05
  • Comparison Operators.
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    06,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    07,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    08,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    09,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    00,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    01,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    03,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    04,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    05
  • Logical Operators.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    06,
    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. (C/C++/C#/Java sử dụng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    09,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    000 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    001)
  • 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
    002 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
    003
  • 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
    004, ký hiệu là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    006. 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
    007 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    008 hoặc danh sách phụ qua
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    009. 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
    010,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    011,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    012
  • Cấu trúc dữ liệu
    • Danh sách.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      013 (mảng động có thể thay đổi)
    • Tuple. ________ 5014 (Mảng 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
      015 (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
      016 (với khóa duy nhất và có thể thay đổi)
  • Sequence (String, Tuple, List) Operators and Functions.
    • ______402,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      03. kiểm tra tư cách thành viên.
    • # empty list
      my_list = []
      
      # list with mixed data types
      my_list = [1, "Hello", 3.4]
      00. concatenation
    • # empty list
      my_list = []
      
      # list with mixed data types
      my_list = [1, "Hello", 3.4]
      02. repetition
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      021,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      022. lập chỉ mục
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      023. cắt lát
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      024,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      025,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      026
    • ________ 5027, ________ 5028
    Chỉ dành cho các chuỗi có thể thay đổi (danh sách)
    • Chuyển nhượng qua
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      021,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      030 (lập chỉ mục) và
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      023 (cắt lát)
    • Chuyển nhượng qua ________ 109, ________ 209 (nối ghép), ________ 301 (lặp lại ghép)
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      035. xóa bỏ
    • Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      036,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      037,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      038
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      039,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      040,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      041,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      042,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      043,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      044
  • Định nghĩa hàm

Ví dụ grade_statistic. py - Basic Syntaxes and Constructs

Ví dụ này lặp đi lặp lại nhắc người dùng cho điểm (từ 0 đến 100 với xác thực đầu vào). Sau đó, nó tính tổng, trung bình, tối thiểu và in biểu đồ ngang

Ví dụ này minh họa các cấu trúc và cú pháp cơ bản của Python, chẳng hạn như nhận xét, câu lệnh, thụt lề khối, điều kiện

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
045, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
002, vòng lặp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047, đầu vào/đầu ra, chuỗi,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 và hàm

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
9

Để chạy tập lệnh Python

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0

Sản lượng dự kiến ​​là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01
Làm thế nào nó hoạt động
  1. #. /usr/bin/env python3 (Dòng 1) chỉ áp dụng cho môi trường Unix. Nó được gọi là Hash-Bang (hoặc She-Bang) để chỉ định vị trí của Trình thông dịch Python, để tập lệnh có thể được thực thi trực tiếp dưới dạng một chương trình độc lập
  2. # -*- mã hóa. UTF-8 -*- (Dòng 2, tùy chọn) chỉ định lược đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  3. Doc-String. Tập lệnh bắt đầu bằng cái gọi là chuỗi tài liệu (chuỗi tài liệu) (Dòng 3-12) để cung cấp tài liệu cho mô-đun Python này. Chuỗi tài liệu 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
  4. def my_sum(lst). (Dòng 15-20). Chúng tôi định nghĩa một hàm có tên là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    049 nhận vào một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 và trả về tổng của các mục. Nó sử dụng vòng lặp for-each-in để lặp qua tất cả các mục của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 đã cho. Vì Python là diễn giải, trước tiên bạn cần xác định hàm trước khi sử dụng nó. Ta chọn tên hàm là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    052 để phân biệt với hàm có sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    053
  5. thùng = [0]*10 (Dòng 38). Python hỗ trợ toán tử lặp lại (
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    02). Câu lệnh này tạo ra một danh sách mười số không. Tương tự, toán tử lặp (*) có thể áp dụng cho chuỗi (Dòng 59)
  6. cho hàng trong phạm vi (len (thùng)). (Dòng 48, 56). Python chỉ hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    055. Nó KHÔNG hỗ trợ vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    002 giống C truyền thống với chỉ mục. Hence, we need to use the built-in
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    057 function to create a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 of indexes
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    059, then apply the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    055 loop on the index
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004
  7. 0 <= lớp <= 100 (Dòng 68). Python hỗ trợ cú pháp này để so sánh
  8. There are a few ways of printing
    1. print() built-in function (Line 75-80). Theo mặc định,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      062 in một dòng mới ở cuối. You need to include argument
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      063 to suppress the newline
    2. in (str. format()) (Line 51, 53). Python 3's new style for formatted string via
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      03 class member function
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      065. The string on which this method is called can contain literal text or replacement fields delimited by braces
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      08. Mỗi trường thay thế chứa chỉ mục số của đối số vị trí hoặc tên của đối số từ khóa, với các chỉ định định dạng giống C bắt đầu bằng
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      067 (thay vì
      # empty list
      my_list = []
      
      # list with mixed data types
      my_list = [1, "Hello", 3.4]
      06 trong C), chẳng hạn như
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      069 cho số nguyên,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      070 cho số dấu phẩy động,
    3. print('chuỗi định dạng' % args) (Dòng 81). Kiểu cũ của Python 2 cho chuỗi được định dạng bằng toán tử
      # empty list
      my_list = []
      
      # list with mixed data types
      my_list = [1, "Hello", 3.4]
      06.
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      076 có thể chứa các bộ xác định định dạng giống như C, chẳng hạn như
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      077 cho số nguyên,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      078 cho số dấu phẩy động,
      Enter a number: 123456789
      123456789 is a magic number
      123456789 is a magic number
      079 cho chuỗi. This line is included in case you need to read old programs. I suggest you do use the new Python 3's formatting style
  9. lớp = int(đầu vào('Nhập. ')) (Dòng 66, 72). Bạn có thể đọc đầu vào từ thiết bị đầu vào tiêu chuẩn (mặc định là bàn phím) thông qua chức năng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    080 tích hợp. Khi hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    080 trả về một chuỗi, chúng ta cần chuyển nó sang
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01
  10. nếu __name__ == '__main__'. (Dòng 87). When you execute a Python module via the Python Interpreter, the global variable
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    083 is set to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    084. On the other hand, when a module is imported into another module, its
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    083 is set to the module name. 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ụ số_đoán. py - Guess a Number

Đây là một trò chơi đoán số. It illustrates nested-if (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
086),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047-loop with
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 flag, and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
089 module. For example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
43
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
44
Làm thế nào nó hoạt động
  1. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    090 (Dòng 12). Chúng tôi sẽ sử dụng chức năng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    092 của mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    089 để tạo một số bí mật. Trong Python, bạn cần
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    093 mô-đun (thư viện bên ngoài) trước khi sử dụng nó
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    094 (Dòng 15). Tạo một số nguyên ngẫu nhiên giữa
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    095 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    096 (bao gồm cả hai)
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    097 (Dòng 17). Python hỗ trợ loại
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    04 cho các giá trị boolean của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05 hoặc
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06. We use this boolean flag to control our
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    047-loop
  4. Cú pháp cho vòng lặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    047 (Dòng 19) là
  5. Cú pháp cho lồng nhau-
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0103 (Dòng 22) là

Ví dụ magic_number. py - Check if Number Contains a Magic Digit

Ví dụ này nhắc người dùng nhập một số và kiểm tra xem số đó có chứa chữ số ma thuật không. This example illustrate function,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01 and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 operations. Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức chương trình thành các chức năng
  2. Chúng tôi triển khai hai phiên bản hàm để kiểm tra số ma thuật - phiên bản
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01 (Dòng 10) và phiên bản
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03 (Dòng 25) - cho mục đích học thuật
  3. def isMagic(số. int, ma thuậtDigit. int = 8) -> 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
  4. if __name__ == '__main__'. (Line 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
    083 được đặt thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    084. Mặt khác, khi một mô-đun được nhập vào một mô-đun khác, thì
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    083 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ụ,

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
0
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
0
Làm thế nào nó hoạt động
  1. The conversion formula is.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0111, trong đó
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0112
  2. # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0113 (Dòng 12). Chúng tôi sẽ sử dụng chức năng
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0115 của mô-đun
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0114 để kết thúc 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. # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0116 (Dòng 21). Python does not support the traditional C-like
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    002-loop with index. It supports only
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0118 loop to iterate through each
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0119 in the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0120. Chúng tôi sử dụng hàm tích hợp sẵn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    057 để tạo danh sách
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0122, sau đó lặp qua từng mục trong danh sách đã tạo
  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
    055, 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. # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0124 (Line 22). Trong Python, bạn có thể sử dụng toán tử lập chỉ mục
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0125 để trích xuất ký tự thứ
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0126. Lưu ý rằng Python không hỗ trợ ký tự, nhưng coi ký tự là chuỗi 1 ký tự
  6. # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0127 (Dòng 23). Python supports exponent (or power) operator in the form of
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    05. Lưu ý rằng chỉ mục chuỗi bắt đầu từ 0 và tăng dần từ trái sang phải. Mặt khác, số mũ của chữ số hex bắt đầu từ 0, nhưng tăng dần từ phải sang trái
  7. # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0129 trường hợp chuỗi 1 ký tự cho ________ 10130, ________ 10131, ______ 10132, ________ 10133 và các chuỗi khác, có thể được xử lý bằng 5 trường hợp lồng nhau nếu như sau
    1. # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0134 (Dòng 24). chúng tôi chuyển đổi chuỗi
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0134 thành
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      01
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0137 thông qua chức năng tích hợp sẵn
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0138
    2. # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0139 (Dòng 26). không có gì. Trong Python, bạn cần đưa một câu lệnh giả có tên là
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0140 (Dòng 28) vào khối nội dung
    3. # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0132 (Dòng 28). Để chuyển đổi chuỗi 1 ký tự
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0132 thành
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      01
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0144, chúng ta sử dụng hàm có sẵn
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0145 để lấy mã Unicode
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      01 của
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0147, trừ cơ số
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0148 và cộng 10
    4. # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0149 (Dòng 30). Python hỗ trợ cấu trúc dữ liệu được gọi là từ điển (mảng kết hợp), chứa các cặp khóa-giá trị. Chúng tôi đã tạo một từ điển
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0150 (Dòng 15) để ánh xạ
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0151 đến
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0152,
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0153 đến
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0154, v.v. Sau đó, chúng tôi có thể tham khảo từ điển qua
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0155 để lấy giá trị của nó (Dòng 31)
    5. khác (Dòng 32). chúng tôi sử dụng
      # A list with 3 integers
      numbers = [1, 2, 5]
      
      print(numbers)
      
      # Output: [1, 2, 5]
      0156 để chấm dứt chương trình. Chúng tôi trả về một mã khác không để biểu thị việc chấm dứt bất thường

Ví dụ bin2dec. py - Chuyển đổi nhị phân sang thập phân

Ví dụ này nhắc người dùng về một chuỗi nhị phân (có xác thực đầu vào) và in số thập phân tương đương của nó. Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
00
Làm thế nào nó hoạt động
  1. Chúng tôi tổ chức mã trong các chức năng
  2. Công thức chuyển đổi là.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0157, trong đó
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0158
  3. Bạn có thể sử dụng hàm tích hợp sẵn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0159 để chuyển đổi một chuỗi số từ
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0160 đã cho thành số thập phân (Dòng 38)

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

Chương trình này nhắc người dùng nhập số thập phân và in số thập lục phân tương đương của nó. For example,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
01
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
02
Làm thế nào nó hoạt động
  1. We use the modulus/division repeatedly to get the hex digits in reverse order
  2. Chúng tôi sử dụng danh sách tra cứu (Dòng 11) để chuyển đổi
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0162 sang chữ số hex
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0163
  3. Bạn có thể sử dụng hàm tích hợp sẵn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0164,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0165,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0166 để lần lượt chuyển đổi số thập phân thành thập lục phân, bát phân và nhị phân; . e. g. ,

wc ví dụ. py - Số từ

Ví dụ này đọc tên tệp từ dòng lệnh và in số lượng dòng, từ và ký tự (tương tự như tiện ích

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0168 trong Unix). Nó minh họa việc nhập tệp văn bản và xử lý chuỗi văn bản

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
00
How it works
  1. nhập sys (Dòng 14). Chúng tôi sử dụng mô-đun
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0114 (@ https. // tài liệu. con trăn. org/3/library/sys. html) từ thư viện chuẩn của Python để truy xuất các đối số dòng lệnh được lưu trong
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0171 và để kết thúc chương trình qua
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0172. Trong Python, bạn cần
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    093 mô-đun trước khi sử dụng nó
  2. The command-line arguments are stored in a variable
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0171, which is a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 (Python's dynamic array). Mục đầu tiên của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0177 là tên tập lệnh, theo sau là các đối số dòng lệnh khác
  3. if len(sys. argv). = 2. (Dòng 15). Chúng tôi sử dụng chức năng tích hợp sẵn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0178 để xác minh rằng độ dài của đối số dòng lệnh
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 là 2
  4. with open(sys. argv[1]) dưới dạng tệp tin. (Line 25). Chúng tôi mở tệp thông qua câu lệnh
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0180, câu lệnh này sẽ tự động đóng tệp khi thoát
  5. cho dòng trong infile. (Dòng 26). Chúng tôi sử dụng vòng lặp
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0181 (Dòng 29) để xử lý từng
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0182 của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0183, trong đó
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0182 thuộc về lớp dựng sẵn "
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03" (có nghĩa là hỗ trợ chuỗi @ https. // tài liệu. con trăn. org/3/library/stdtypes. html#str). Chúng tôi sử dụng các hàm thành viên của lớp
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0187 để loại bỏ các khoảng trắng ở đầu và cuối;
  6. Chúng tôi cũng gọi tiện ích Unix "
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0168" thông qua lệnh shell bên ngoài theo 2 cách. thông qua
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0191 và
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0192

Ví dụ htmlescape. py - Thoát các ký tự HTML dành riêng

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. Nó minh họa đầu vào/đầu ra tập tin và thay thế chuỗi

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
04
How it works
  1. nhập sys (Dòng 14). Chúng tôi
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    093 mô-đun
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0114 (@ https. // tài liệu. con trăn. 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
    004
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0171, where
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0177 is the script name; and use
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0172 (Line 18) to terminate the program
  2. với mở (sys. argv[1]) dưới dạng tệp tin, mở(sys. argv[2], 'w') as outfile. (Line 21). We use the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0180 statement, which closes the files automatically at exit, to open the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0183 for read (default) and
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4301 for write (
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4302)
  3. for line in infile. (Line 22). We use a
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0181 loop to process each
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0182 of the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0183, where
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0182 belongs to the built-in class "
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03" (meant for string support @ https. //docs. python. org/3/library/stdtypes. html#str). We use
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    03 class' member function
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4309 to strip the trailing (right) white spaces; and
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4310 for substitution
  4. Python 3. 2 introduces a new
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4311 module, with a function
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4312 to escape HTML reserved characters.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    05

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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4313) and regular expression (using module
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4314)

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
06
How 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. Nó đa nền tảng và chạy trên Windows, Linux/UNIX và 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 provides automatic memory management. 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. phiên bản ban đầu
  • Python 2. phát hành năm 2000, với nhiều tính năng mới như bộ thu gom rác và hỗ trợ Unicode
  • Python 3 (Python 3000 hoặc py3k). Một bản nâng cấp lớn được phát hành vào năm 2008. Python 3 is NOT backward compatible with Python 2
Python 2 hay Python 3?

Hiện tại, hai phiên bản Python được hỗ trợ song song, phiên bản 2. 7 và phiên bản 3. 5. Rất tiếc là không tương thích. Tình huống này phát sinh vì khi Guido Van Rossum (người tạo ra Python) quyết định mang lại những thay đổi quan trọng cho Python 2, anh ấy nhận thấy rằng những thay đổi mới sẽ không tương thích với các mã hiện có. Anh ấy quyết định bắt đầu một phiên bản mới có tên Python 3, nhưng tiếp tục duy trì Python 2 mà không giới thiệu các tính năng mới. Trăn 3. 0 được phát hành vào năm 2008, trong khi Python 2. 7 năm 2010

MỘT LẦN NỮA, HÃY LƯU Ý RẰNG PYTHON 2 VÀ PYTHON 3 KHÔNG TƯƠNG THÍCH. Bạn cần quyết định nên sử dụng Python 2 hay Python 3. Bắt đầu các dự án mới của bạn bằng Python 3. Use Python 2 only for maintaining legacy projects

Để kiểm tra phiên bản Python của bạn, hãy ra lệnh này

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
07

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
08
Ubuntu (16. 04LTS)

Cả Python 3 và Python 2 đều đã được cài đặt sẵn theo mặc định. Nếu không, bạn có thể cài đặt Python qua

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
09

To verify the Python installation

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
010
cá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. python. org

Getting Started with Python Interpreter

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)

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
011

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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
012

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

  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0115
  • (Mac OS X và Ubuntu)
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4317
  • (Windows)
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4318 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

Use a programming text editor to write the following Python script and save as "

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4319" in a directory of your choice

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
013
Làm thế nào nó hoạt động
  1. Theo quy ước, tên tập lệnh Python (mô-đun) ở dạng chữ thường (e. g. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4320)
  2. Nhận xét EOL. Các câu lệnh bắt đầu bằng
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4321 cho đến cuối dòng (EOL) là các nhận xét
  3. #. /usr/bin/env python3 (Dòng 1) chỉ áp dụng cho môi trường Unix. Nó được gọi là Hash-Bang (hoặc She-Bang) để chỉ định vị trí của Trình thông dịch Python, để tập lệnh có thể được thực thi trực tiếp dưới dạng một chương trình độc lập
  4. # -*- mã hóa. UTF-8 -*- (Dòng 2, tùy chọn) chỉ định lược đồ mã hóa nguồn để lưu tệp nguồn. Chúng tôi chọn và đề xuất UTF-8 để quốc tế hóa. Định dạng đặc biệt này được nhiều trình soạn thảo phổ biến công nhận để lưu mã nguồn ở định dạng mã hóa được chỉ định
  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. Chúng ta tạo các biến
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4322,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4323,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4324,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4325,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4326 (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 hỗ trợ số phức (Dòng 12) và các kiểu dữ liệu cấp cao khác
  11. Python hỗ trợ một mảng động được gọi là danh sách (Dòng 14), được đại diện bởi
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4329. 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
    007 (Dòng 15)
  12. in(aVar). Hàm
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    062 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à

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
014
Chạ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

Executable Shell Script của Unix

Trong Linux/Mac OS X, bạn có thể biến tập lệnh Python thành chương trình thực thi (gọi là Shell Script hoặc Executable Script) bằng cách

  1. Bắt đầu bằng một dòng bắt đầu bằng
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4332 (được gọi là "hash-bang" hoặc "she-bang"), theo sau là tên đường dẫn đầy đủ đến Trình thông dịch Python, e. g. , Để định vị Trình thông dịch Python, hãy sử dụng lệnh "
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4333" hoặc "
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4334"
  2. Làm cho tệp có thể thực thi được thông qua lệnh
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4335 (thay đổi chế độ tệp)
  3. Sau đó, bạn có thể chạy tập lệnh Python giống như bất kỳ chương trình thực thi nào. Hệ thống sẽ tìm kiếm Trình thông dịch Python từ dòng she-bang.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    015

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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4336 sẽ định vị Trình thông dịch Python (từ các mục nhập của
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4337). 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 "

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4338" với phần có thể thông dịch Python, để làm cho tập lệnh Python có thể thực thi được

Chạy tập lệnh Python bên trong Trình thông dịch của Python

Để chạy tập lệnh "

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4319" 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,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4340 (đối với thư mục chính) không hoạt động?
  • Hàm tích hợp sẵn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4341 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

For beginners, I recommend

  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 (

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4321) và kéo dài đến cuối dòng hiện tại. Các nhận xét bị Trình thông dịch Python bỏ qua, nhưng chúng rất quan trọng trong việc cung cấp giải thích và tài liệu cho người khác (và chính bạn ba ngày sau) để đọc chương trình của bạn. Sử dụng bình luận một cách tự do

KHÔNG có nhận xét nhiều dòng trong Python?. (C/C++/Java hỗ trợ nhận xét nhiều dòng thông qua

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

Các câu lệnh

Một câu lệnh Python được phân định bằng một dòng mới. A statement cannot cross line boundaries, except

  1. Một biểu thức trong dấu ngoặc đơn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4345, dấu ngoặc vuông
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4346 và dấu ngoặc nhọn
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    08 có thể trải rộng trên nhiều dòng
  2. Dấu gạch chéo ngược (
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4348) ở 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 (

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
07) ở 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 (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
07). 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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
08 để 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. This is a good syntax to force you to indent the blocks correctly for ease of understanding

A compound statement, such as conditional (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
045), loop (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
002) and function definition (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4355), begins with a header line terminated with a colon (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
067); followed by the indented body block, as follows

For examples,

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
067) và thụt đầu dòng vào 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ụ,

B xin chào chúc b thêm python print b

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 (

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4358,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4359) hoặc dấu gạch dưới (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4360), theo sau là 0 hoặc nhiều chữ cái, dấu gạch dưới và chữ số (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4361). Python không cho phép các ký tự đặc biệt như
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4362 và
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4363

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

  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366 (boolean và chữ đặc biệt)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    093,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4368,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4369
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0103,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4371,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4372,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    002,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    047,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4376,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4377,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0140,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4379 (điều khiển luồng)
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4355,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4381,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4382,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4383,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4384 (chức năng)
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4385
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    06,
    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,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    04,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    035 (toán tử)
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4392,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4393,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4394,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4395 (xử lý lỗi)
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4396,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4397,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4398
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ừ. There are two convenctions

  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,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4399,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4400,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4401, 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. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4402,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4403,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4404,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4405,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4406, and
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4407. (Đâ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. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4402, nhưng không phải là
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4409 hoặc
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4410, để lưu trữ số học sinh. It is alright to use abbreviations, e. g. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4411 cho chỉ mục
  2. Không sử dụng những tên vô nghĩa như
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4412,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4413,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4414,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4415,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4416,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4417,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4409,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4419,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4420,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4421,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4422,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4423 (mục đích của bài tập này là gì?) và
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4424 (Ví dụ này nói về cái gì?)
  3. Tránh các tên có một chữ cái như
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4415,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4416,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4417,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4412,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4413,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4414, dễ gõ hơn nhưng thường vô nghĩa. Exceptions are common names like
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4410,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4432,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4433 for coordinates,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4415 for index. Tên dài khó gõ hơn, nhưng hãy tự ghi lại chương trình của bạn. (Tôi khuyên bạn nên dành thời gian luyện tập đánh máy đôi khi. )
  4. Use singular and plural nouns prudently to differentiate between singular and plural variables. Ví dụ: bạn có thể sử dụng biến
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4435 để chỉ một số hàng và biến
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4436 để chỉ nhiều hàng (chẳng hạn như danh sách các hàng - sẽ được thảo luận sau)
hằng số

Python không hỗ trợ hằng số, nơi không thể sửa đổi nội dung của nó. (C supports constants via keyword

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4437, Java via
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4438. )

Đó là một quy ước để đặt tên một biến bằng chữ hoa (nối với gạch dưới), e. g. ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4439,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4440, to indicate that it should not be modified in the program. Tuy nhiên, không có gì ngăn cản nó được sửa đổi

Loại dữ liệu. Số, Chuỗi và Danh sách

Python hỗ trợ nhiều loại số khác nhau, chẳng hạn như

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01 (đối với số nguyên như
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4442,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4443),
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
02 (đối với số dấu phẩy động như
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4445,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4446,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4447) và
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 (đối với boolean của
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 và
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06)

Python supports text string (a sequence of characters). Trong Python, các chuỗi có thể được phân tách bằng dấu nháy đơn hoặc nháy kép, e. g. ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4451,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4452,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4453 hoặc
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4454 (chuỗi trống)

Python hỗ trợ cấu trúc mảng động có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004, ký hiệu là
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4456. Bạn có thể tham chiếu phần tử thứ i là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
007.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 của Python tương tự như mảng của C/C++/Java, nhưng nó KHÔNG có kích thước cố định và có thể được mở rộng linh hoạt trong thời gian chạy

Tôi sẽ mô tả chi tiết các kiểu dữ liệu này trong phần sau

Đầu vào/đầu ra của bảng điều khiển. Các chức năng tích hợp sẵn input() và print()

Bạn có thể sử dụng chức năng tích hợp sẵn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
080 để đọc đầu vào từ bảng điều khiển (dưới dạng chuỗi) và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
062 để in đầu ra ra bảng điều khiển. Ví dụ,

print()

The built-in function

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

For examples,

dấu phân cách của print() (sep) và kết thúc (end)

Bạn có thể sử dụng đối số từ khóa tùy chọn

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4462 để đặt chuỗi phân tách (mặc định là khoảng trắng) và
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4463 cho chuỗi kết thúc (mặc định là dòng mới). Ví dụ như,

in bằng Python 2 so với Python 3

Nhớ lại rằng Python 2 và Python 3 KHÔNG tương thích. Trong Python 2, bạn có thể sử dụng "

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4464" mà không cần dấu ngoặc đơn (vì
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4465 là một từ khóa trong Python 2). Trong Python 3, bắt buộc phải có dấu ngoặc đơn vì
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
062 là một hàm. For example,

Quan trọng. Always use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
062 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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4468 to check the type of a variable or literal

Number Types

Python supports these built-in number types

  1. Integers (type
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01). e. g. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4442,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4443. Unlike C/C++/Java, integers are of unlimited size in Python. For example, You can also express integers in hexadecimal with prefix
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4472 (or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4473); in octal with prefix
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4474 (or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4475); and in binary with prefix
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4476 (or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4477). Ví dụ: ________ 14478, ________ 14479, ________ 14480, ________ 14481
  2. Floating-point numbers (type
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    02). e. g. ,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4483,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4484,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4485,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4486, with a decimal point and an optional exponent (denoted by
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4487 or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4488).
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    02s are 64-bit double precision floating-point numbers. For example,
  3. Booleans (type
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    04). takes a value of either
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05 or
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06. 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
    095, an empty value (such as empty string
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4453,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4454, empty list
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4346, empty tuple
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4345, empty dictionary
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    08), and
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366 are treated as
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06; anything else are treated as
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05. 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
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    095 for
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    06. For example,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    016
  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
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4416 (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

Recall that Python is dynamic typed (instead of static typed)

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

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

You can use built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic 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,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
017
The Assignment Operator (=)

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

Pair-wise Assignment and Chain 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
035 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. 6000000000000001
Compound Assignment Operators (+=, -=, *=, /=, //=, **=, %=)

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

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
09,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
00,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
01,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
02,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
03,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
04 and
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
05. 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 không hỗ trợ các toán tử tăng (________ 207) và giảm (________ 208) (như trong 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,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01 is "smaller" than
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
02, 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

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 value of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06

OperatorModeUsageDescriptionExample==
. =
<
<=
>
>=Binaryx == y
x . = y
x < y
x
x > y
x >= yComparison
Return
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06in
not inBinaryx in seq
x not in seqCheck if
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 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
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
53 are referencing the same object
Return
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06

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
    09,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    000 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    001
  • 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.
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0138,
    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

For examples,

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<

String

In Python, strings can be delimited by a pair of single-quotes (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic 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 strings via triple-single-quotes (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
9) or triple-double-quotes (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
00)

To place a single-quote (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
80) inside a single-quoted string, you need to use escape sequence
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
81. Similarly, to place a double-quote (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
82) inside a double-quoted string, use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
83. Không cần trình tự thoát để đặt một trích dẫn đơn bên trong một chuỗi trích dẫn kép;

A triple-single-quoted or triple-double-quoted string can span multiple lines. There is no need for escape sequence to place a single/double quote inside a triple-quoted string. Triple-quoted strings are useful for multi-line documentation, HTML and other codes

Python 3 uses Unicode character set to support internationalization (i18n)

Escape Sequences for Characters (\code)

Like C/C++/Java, you need to use escape sequences (a back-slash + a code) for

  • Special non-printable characters, such as tab (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    84), newline (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    85), carriage return (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    86)
  • Resolve ambiguity, such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    83 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    82 inside double-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    81 (for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    80 inside single-quoted string),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    91 (for
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    410)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    93 for character in hex value and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    94 for octal value
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    95 for 4-hex-digit (16-bit) Unicode character and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    96 for 8-hex-digit (32-bit) Unicode character
Raw Strings (r'. ' or r". ")

You can prefix a string by

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
97 to disable the interpretation of escape sequences (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
98), i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
99 is
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
000 (two characters) instead of newline (one character). Raw strings are used extensively in regex (to be discussed in module
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4314 section)

Strings are Immutable

Strings are immutable, i. e. , their contents cannot be modified. String functions such as

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
002,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4310 returns a new string object instead of modifying the string under operation

Built-in Functions and Operators for Strings

You can operate on strings using

  • built-in functions such as
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    004;
  • operators such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 (contains),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    00 (concatenation),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    02 (repetition), indexing
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    021 and
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    022, and slicing
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    023

Note. These functions and operators are applicable to all

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
011 data types including
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
012,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004, and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
014 (to be discussed later)

Function /
OperatorUsageDescriptionExamples
s = 'Hello'len()len(str)Lengthlen(s) ⇒ 5insubstr in strContain?
Return
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06'ell' in s ⇒ True
'he' in s ⇒ False+
+=str + str1
str += str1Concatenations + '. ' ⇒ 'Hello. '*
*=str * count
str *= countRepetitions * 2 ⇒ 'HelloHello'[i]
[-i]str[i]
str[-i]Indexing to get a character.
The front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095;
back index begins at
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
019 (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
020). s[1] ⇒ 'e'
s[-4] ⇒ 'e'[m. n. step]
[m. n]
[m. ]
[. n]
[. ]str[m. n. step]
str[m. n]
str[m. ]
str[. n]
str[. ]Slicing to get a substring.
Từ chỉ mục
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
021 (bao gồm) đến
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
022 (không bao gồm) với kích thước
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
023.
Mặc định là.
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
024
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
025,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
026,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
027. s[1. 3] ⇒ 'el'
s[1. -2] ⇒ 'el'
s[3. ] ⇒ 'lo'
s[. -2] ⇒ 'Hel'
s[. ] ⇒ 'Hello'
s[0. 5. 2] ⇒ 'Hlo'

For examples,

Character Type?

Python does not have a dedicated character data type. A character is simply a string of length 1. You can use the indexing operator to extract individual character from a string, as shown in the above example; or process individual character using

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
055 loop (to be discussed later)

The built-in functions

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
029 and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
030 operate on character, e. g. ,

Unicode vs ASCII

In Python 3, strings are defaulted to be Unicode. ASCII strings are represented as byte strings, prefixed with

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4413, e. g. ,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
032

In Python 2, strings are defaulted to be ASCII strings (byte strings). Các chuỗi Unicode có tiền tố là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
033

Bạn phải luôn sử dụng Unicode để quốc tế hóa (i18n)

Các hàm thành viên dành riêng cho chuỗi

Python hỗ trợ các chuỗi thông qua một lớp dựng sẵn có tên là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 (Chúng tôi sẽ mô tả lớp này trong chương Lập trình hướng đối tượng). Lớp
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 cung cấp nhiều hàm thành viên. Vì chuỗi là bất biến nên hầu hết các hàm này đều trả về một chuỗi mới. Các hàm thành viên thường được sử dụng như sau, giả sử rằng
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
036 là một đối tượng
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03

  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    038
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    039,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    040,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    041. loại bỏ các khoảng trắng đầu và cuối, khoảng trắng bên phải (dấu);
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    038
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    043,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    044. Trả về một chữ hoa/chữ thường tương ứng
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    038
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    046,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    047. Kiểm tra xem chuỗi có phải là chữ hoa/chữ thường không
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    038
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    049
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    050
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    051
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    052
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    053,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    054
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    055
Định dạng chuỗi 1 (Kiểu mới). Sử dụng str. format() function

Có một số cách để tạo một chuỗi được định dạng cho đầu ra. Python 3 giới thiệu một phong cách mới trong hàm thành viên

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0167 của
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 với
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
08 làm trình giữ chỗ (được gọi là trường định dạng). Ví dụ như,

Khi bạn chuyển các danh sách, bộ dữ liệu hoặc từ điển (sẽ được thảo luận sau) làm đối số vào hàm

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0167, bạn có thể tham chiếu các phần tử của chuỗi trong các trường định dạng với
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
060. Ví dụ như,

Định dạng chuỗi 2. Sử dụng str. rjust(n), str. ljust(n), str. trung tâm (n), str. zfill(n)

Bạn cũng có thể sử dụng các hàm thành viên của

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 như
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
062 (trong đó
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 là độ rộng trường),
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
064,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
065,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
067 để định dạng chuỗi. Ví dụ,

Định dạng chuỗi 3 (Kiểu cũ). Sử dụng toán tử %

The old style (in Python 2) is to use the

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
06 operator, with C-like
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
069 format specifiers. Ví dụ như,

Tránh sử dụng kiểu cũ để định dạng

Chuyển đổi giữa Chuỗi và Số. int(), float() và str()

Bạn có thể sử dụng các hàm dựng sẵn

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0138 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
68 để phân tích chuỗi "số" thành số nguyên hoặc số thực; . For example,

Nối một chuỗi và một số?

Bạn KHÔNG THỂ nối một chuỗi và một số (kết quả là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
073). Thay vào đó, bạn cần sử dụng hàm
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
69 để chuyển số thành chuỗi. Ví dụ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
018

Giá trị Không có

Python provides a special value called

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4366 (take note of the spelling in initial-capitalized), which can be used to initialize an object (to be discussed in OOP later). Ví dụ,

Danh sách, Tuple, Từ điển và Tập hợp

Liệt kê [v1, v2,. ]

Python có một mảng động tích hợp mạnh mẽ có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004

  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 được đặt trong dấu ngoặc vuông
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4346
  • A
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 can contain items of different types. Đó là bởi vì Python liên kết các loại với các đối tượng, không phải biến
  • Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 tự động tăng và giảm kích thước (động). Bạn không phải chỉ định kích thước của nó trong quá trình khởi tạo
  • A
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 is mutable. You can update its contents
Hàm và toán tử tích hợp cho danh sách

Một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004, giống như chuỗi, là một
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
011. Do đó, bạn có thể vận hành danh sách bằng cách sử dụng

  • chức năng
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    011 tích hợp như
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    004
  • các hàm
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    011 tích hợp cho
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 của các số như
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    088,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    089 và
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    090
  • built-in operators such as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02 (contains),
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    00 (concatenation) and
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    02 (repetition),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    035,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    095 (indexing), and
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    096 (slicing)

Notes

  • Bạn có thể lập chỉ mục các mục từ phía trước với chỉ mục dương hoặc từ phía sau với chỉ mục âm. E. g. , nếu
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 là một danh sách, thì
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    098 và
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    099 đề cập đến mục đầu tiên và mục thứ hai của nó;
  • Bạn cũng có thể tham khảo danh sách con (hoặc lát cắt) bằng cách sử dụng ký hiệu lát cắt
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    002 (từ chỉ mục
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    021 (bao gồm) đến chỉ mục
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    022 (không bao gồm) với kích thước
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    023)
OperatorUsageDescriptionVí dụ
lst = [8, 9, 6, 2]in
not inx in lst
x .
5 in lst ⇒ False+
+=lst + lst1
lst += lst1Concatenationlst + [5, 2]
⇒ [8, 9, 6, 2, 5, 2]*
*=lst * count
lst *= countRepetitionlst * 2
⇒ [8, 9, 6, 2, 8, 9, 6, 2][i]
[-i]lst[i]
lst[-i]Indexing to get an item.
Front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095;
back index begins at
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
019 (or
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
011). lst[1] ⇒ 9
lst[-2] ⇒ 6[m. n. bước]
[m. n]
[m. ]
[. n]
[. ]lst[m. n. bước]
lst[m. n]
lst[m. ]
lst[. n]
lst[. ] Cắt lát để lấy danh sách phụ.
Từ chỉ mục
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
021 (bao gồm) đến
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 (không bao gồm) với kích thước
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
023.
Mặc định là.
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
021 là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095, n là
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
011. lst[1. 3] ⇒ [9, 6]
lst[1. -2] ⇒ [9]
lst[3. ] ⇒ [2]
lst[. -2] ⇒ [8, 9]
lst[. ] ⇒ [8, 9, 6, 2]
lst[0. 4. 2] ⇒ [8, 6]
newlst = lst[. ] ⇒ Sao chép
lst[4. ] = [1, 2] ⇒ Extenddeldel lst[i]
del lst[m. n]
del lst[m. n. bước]Xóa một hoặc nhiều mụcdel lst[1] ⇒ [8, 6, 2]
del lst[1. ] ⇒ [8]
del lst[. ] ⇒ [] (Xóa)FunctionUsageDescriptionExamples
lst = [8, 9, 6, 2]len()len(lst)Lengthlen(lst) ⇒ 4max()
min()max(lst)
min(lst)Maximum value
minimum valuemax(lst) ⇒ 9
min(lst) ⇒ 2sum()sum(lst)Sum (for number lists only)sum(lst) ⇒ 16

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004, không giống như chuỗi, có thể thay đổi. Bạn có thể chèn, xóa và sửa đổi các mục của nó

For examples,

Nối các mục vào danh sách
Sao chép một danh sách
chức năng thành viên danh sách cụ thể

Lớp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 cung cấp nhiều hàm thành viên. Suppose
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0120 is a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 object

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    023. trả về chỉ số của lần xuất hiện đầu tiên của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0119;
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    025. append the given
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    026 behind the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 and return
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366; same as slicing operation
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    029
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    030. append the given list
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    031 behind the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 and return
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366; same as slicing operation
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    034
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    035. chèn
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    026 đã cho trước
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    037 và trả về
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366. Do đó,
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    039 chèn vào trước mục đầu tiên của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0120;
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    044. loại bỏ lần xuất hiện đầu tiên của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0119 khỏi
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0120 và trả về
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366;
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    048. remove and return the last item of the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0120
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    050. xóa và trả lại mục được lập chỉ mục của
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0120
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    052. xóa tất cả các mục khỏi
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 và trả lại
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366;
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    056. trả lại các lần xuất hiện của
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    026
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    058. đảo ngược
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 tại chỗ và trả về
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    061. sort the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005 in place and return
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4366
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    064. return a copy of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    005; same as
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    066

Recall that

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 is mutable (unlike string which is immutable). These functions modify the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 directly. For examples,

Using list as a last-in-first-out Stack

To use a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 as a last-in-first-out (LIFO) stack, use
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
070 to add an item to the top-of-stack (TOS) and
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
071 to remove the item from the TOS

Using list as a first-in-first-out Queue

To use a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 as a first-in-first-out (FIFO) queue, use
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
070 to add an item to the end of the queue and
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
074 to remove the first item of the queue

However,

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
074 is slow. The standard library provide a class
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
076 to efficiently implement deque with fast appends and pops from both ends

Tuple (v1, v2,. )

Tuple is similar to list except that it is immutable (just like string). Hence, tuple is more efficient than list. A tuple consists of items separated by commas, enclosed in parentheses

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4345

An one-item tuple needs a comma to differentiate from parentheses

The parentheses are actually optional, but recommended for readability. Nevertheless, the commas are mandatory. For example,

You can operate on tuples using (supposing that

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
078 is a tuple)

  • built-in functions such as
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    079;
  • built-in functions for tuple of numbers such as
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    080,
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    081 and
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    082;
  • các toán tử như
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02,
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    00 và
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    02;
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    014's member functions such as
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    087,
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    088, etc
Conversion between List and Tuple

You can covert a list to a tuple using built-in function

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
089; and a tuple to a list using
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
090. For examples,

từ điển {k1. v1, k2. v2,. }

Python's built-in dictionary type supports key-value pairs (also known as name-value pairs, associative array, or mappings)

  • A dictionary is enclosed by a pair of curly braces
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    08. The key and value are separated by a colon (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    067), in the form of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    015
  • Unlike list and tuple, which index items using an integer index 0, 1, 2, 3,. , dictionary can be indexed using any key type, including number, string or other types
  • Dictionary is mutable
Dictionary-Specific Member Functions

The

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
094 class has many member methods. thường được sử dụng như sau (giả sử rằng
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
095 là một đối tượng
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
094)

  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    097
  • # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    098,
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    099,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    000
  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    001
  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    002
  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    003
  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    004. merge the given dictionary
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    005 into
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    095. Override the value if key exists, else, add new key-value
  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    007

For Examples,

Set {k1, k2,. }

A set is an unordered, non-duplicate collection of objects. A set is delimited by curly braces

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
08, just like dictionary. You can think of a set as a collection of dictionary keys without associated values. Sets are mutable

Ví dụ,

Set-Specific Operators (
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
009,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
001,
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
01,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
074)

Python supports set operators

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
009 (intersection),
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
014 (union),
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
01 (difference) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
074 (exclusive-or). For example,

Sequence Types. list, tuple, str

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
014, and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 are parts of the sequence types.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004 is mutable, while
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
014 and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03 are immutable. They share the common sequence's built-in operators and built-in functions, as follows

Opr / FuncUsageDescriptionin
not inx in seq
x not in seqContain? Return
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06+seq + seq1Concatenation*seq * countRepetition (Same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
026)[i]
[-i]seq[i]
seq[-i]Indexing to get an item.
Front index begins at
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095; back index begins at
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
019 (or
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
029). [m. n. step]
[m. n]
[m. ]
[. n]
[. ]seq[m. n. step]
seq[m. n]
seq[m. ]
seq[. n}
seq[. ]Slicing to get a sub-sequence.
From index
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
021 (included) to
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
022 (excluded) with
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
023 size.
The defaults are.
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
024 is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095, n is
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
029. len()
min()
max()len(seq)
min(seq)
max(seq)Return the Length, mimimum and maximum of the sequenceseq. index()seq. index(x)
seq. index(x, i)
seq. index(x, i, j)Return the index of
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
036 in the sequence, or raise
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
037.
Search from
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0126 (included) to
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
039 (excluded)seq. count()seq. count(x)Returns the count of x in the sequence

For mutable sequences (

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004), the following built-in operators and built-in functions (
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
041
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
042) and member functions (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
038
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
044) are supported

Opr / FuncUsageDescription[]seq[i] = x
seq[m. n] = []
seq[. ] = []
seq[m. n] = seq1
seq[m. n. step] = seq1Replace one item
Remove one or more items
Remove all items
Replace more items with a sequence of the same size+=seq += seq1Extend by
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
045*=seq *= countRepeat
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
046 timesdeldel seq[i]
del seq[m. n]
del seq[m. n. step]Delete one item
Delete more items, same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
047seq. clear()seq. clear()Remove all items, same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
048 or
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
049seq. append()seq. append(x)Append x to the end of the sequence,
same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
050seq. extend()seq. entend(seq1)Extend the sequence,
same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
051 or
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
052seq. insert()seq. insert(i, x)Insert
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 at index
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4415, same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
055seq. remove()seq. xóa (x) Xóa lần xuất hiện đầu tiên của
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
036seq. pop()seq. pop()
seq. pop(i)Retrieve and remove the last item
Retrieve and remove the item at index
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0126seq. copy()seq. copy()Create a shallow copy of
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
058, same as.
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
059seq. reverse()seq. reverse()Reverse the sequence in place

Others

Deque

[LÀM]

Heap

[LÀM]

Flow Control Constructs

Conditional if-elif-else

The syntax is as follows. The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4371 (else-if) and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4372 blocks are optional

For example

There is no

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
062 statement in Python (as in C/C++/Java)

Comparison and Logical Operators

Python supports these comparison (relational) operators, which return a

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
04 of either
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
06

  • languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    08 (less than),
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    09 (less than or equal to),
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    06 (equal to),
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    07 (not equal to),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    00 (greater than),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    01 (greater than or equal to). (This is the same as C/C++/Java. )
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    02,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    03. Check if an item is. is not in a sequence (list, tuple, string, set, etc)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    04,
    languages = ["Python", "Swift", "C++"]
    
    # access item at index 0
    print(languages[0])   # Python
    
    # access item at index 2
    print(languages[2])   # C++
    075. Check if two variables have the same reference

Python supports these logical (boolean) operators.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
06,
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. (C/C++/Java uses
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
09,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
000,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
001. )

Chain Comparison v1 < x < v2

Python supports chain comparison in the form of

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
082, e. g. ,

Comparing Sequences

The comparison operators (such as

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
06,
languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
09) are overloaded to support sequences (such as string, list and tuple)

In comparing sequences, the first items from both sequences are compared. If they differ the outcome is decided. Otherwise, the next items are compared, and so on

Shorthand if-else (or Conditional Expression)

The syntax is

Ví dụ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
019

Note. Python does not use "

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
085" for shorthand
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
045, as in C/C++/Java

The while loop

The syntax is as follows

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4372 block is optional, which will be executed if the loop exits normally without encountering a
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4376 statement

Ví dụ,

break, continue, pass and loop-else

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4376 statement breaks out from the innermost loop; the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4377 statement skips the remaining statements of the loop and continues the next iteration. This is the same as C/C++/Java

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0140 statement does nothing. Nó phục vụ như một trình giữ chỗ cho một câu lệnh trống hoặc khối trống

The loop-

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4372 block is executed if the loop is exited normally, without encountering the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4376 statement

Examples. [TODO]

Using Assignment in while-loop's Test?

In many programming languages, assignment can be part of an expression, which return a value. It can be used in

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047-loop's test, e. g. ,

Python issues a syntax error at the assignment operator. In Python, you cannot use assignment operator in an expression

You could do either of the followings

The for-in loop

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
055 loop has the following syntax

You shall read it as "for each item in the sequence. ". Again, the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4372 block is executed only if the loop exits normally, without encountering the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4376 statement

Iterating through Sequences

Iterating through a Sequence (String, List, Tuple, Dictionary, Set) using for-in Loop

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
055 loop is primarily used to iterate through all the items of a sequence. For example,

for(;;) Loop

Python does NOT support the C/C++/Java-like

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
099 loop, which uses a varying index for the iterations

Take note that you cannot use the "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
000" loop to modify a list. To modify the list, you need to get the list indexes by creating an index list. For example,

Manually creating the index list is not practical. You can use the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
001 function to create the index list (described below)

The range() Built-in Function

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
001 function produces a series of running integers, which can be used as index list for the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
055 loop

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 produces integers from
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    095 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    006;
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    007 produces integers from
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    024 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    006;
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    010 produces integers from
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    024 to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    006 in step of
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    038

Ví dụ,

Using else-clause in Loop

Recall that the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4372-clause will be executed only if the loop exits without encountering a
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4376

Iterating through a Sequence of Sequences

A sequence (such as list, tuple) can contain sequences. For example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
430
Iterating through a Dictionary

There are a few ways to iterate through an dictionary

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

The built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
016 takes a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
017 (such as sequence) and returns an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
018 object. You can then use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
019 to iterate through the items. For example,

The reversed() Built-in Function

Để lặp lại một chuỗi theo thứ tự ngược lại, hãy áp dụng hàm

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
020 để đảo ngược trình lặp qua các giá trị của chuỗi. For example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
431
The enumerate() Built-in Function

You can use the built-in function

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
021 to obtain the positional indexes, when looping through a sequence. For example,

Multiple Sequences and the zip() Built-in Function

To loop over two or more sequences concurrently, you can pair the entries with the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
022 built-in function. For examples,

Comprehension for Generating Mutable List, Dictionary and Set

List comprehension provides concise way to generate a new list. The syntax is

For examples,

Similarly, you can create dictionary and set (mutable sequences) via comprehension. For example,

Comprehension cannot be used to generate

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
012 and
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
014, as they are immutable and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
025 cannot be applied

Naming Conventions and Coding Styles (PEP 8 & PEP 257)

Naming Conventions

These are the recommended naming conventions in Python

  • Variable names. use a noun in lowercase words (optionally joined with underscore if it improves readability), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    026
  • Function names. use a verb in lowercase words (optionally joined with underscore if it improves readability), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    027 or
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    028
  • Class names. use a noun in camel-case (initial-cap all words), e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    029,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    030,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    031
  • Constant names. use a noun in uppercase words joined with underscore, e. g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    032,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    033
Coding Styles

Read

The recommended styles are

  • Use 4 spaces for indentation. Don't use tab
  • Lines shall not exceed 79 characters
  • Use blank lines to separate functions and classes
  • Use a space before and after an operator
  • [TODO] more

Functions

Syntax

In Python, you define a function via the keyword

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4355 followed by the function name, the parameter list, the doc-string and the function body. Inside the function body, you can use a
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4381 statement to return a value to the caller. There is no need for type declaration like C/C++/Java

The syntax is

Example 1

Take note that you need to define the function before using it, because Python is interpretative

Example 2
Example 3. Function doc-string

This example elaborates on the function's doc-string

  • The first line "
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    036" specifies the type of the argument and return value. Python does not perform type check on function, and this line merely serves as documentation
  • The second line gives a description
  • Examples of function invocation follow. Bạn có thể sử dụng mô-đun
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    037 để thực hiện kiểm tra đơn vị cho chức năng này dựa trên các ví dụ này (sẽ được mô tả trong phần "Kiểm tra đơn vị"
The pass statement

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0140 statement does nothing. It is sometimes needed as a dummy statement placeholder to ensure correct syntax, e. g. ,

Function Parameters and Arguments

Passing Arguments by Value vs. by Reference

In Python

  • Immutable arguments (such as integers, floats, strings and tuples) are passed by value. That is, a copy is cloned and passed into the function. The original cannot be modified inside the function
  • Mutable arguments (such as lists, dictionaries, sets and instances of classes) are passed by reference. That is, they can be modified inside the function

For examples,

Function Parameters with Default Values

You can assign a default value to the "trailing" function parameters. These trailing parameters having default values are optional during invocation. For example,

Another example,

In stead of hard-coding the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
039, it is more flexible to use a parameter with a default value, as follows

Positional and Keyword Arguments

Python functions support both positional and keyword (or named) arguments

Normally, Python passes the arguments by position from left to right, i. e. , positional, just like C/C++/Java. Python also allows you to pass arguments by keyword (or name) in the form of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
040. For example,

You can also mix the positional arguments and keyword arguments, but you need to place the positional arguments first, as shown in the above examples

Variable Number of Positional Parameters (*args)

Python supports variable (arbitrary) number of arguments. Trong định nghĩa hàm, bạn có thể sử dụng

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
02 để đóng gói tất cả các đối số vị trí còn lại vào một bộ. Ví dụ,

Python hỗ trợ đặt

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
042 ở giữa danh sách tham số. Tuy nhiên, tất cả các đối số sau
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
042 phải được chuyển theo từ khóa để tránh sự mơ hồ. Ví dụ

Giải nén Danh sách/Tuple thành Đối số Vị trí (*lst, *tuple)

Trong tình huống ngược lại khi các đối số đã có trong danh sách/bộ, bạn cũng có thể sử dụng

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
02 để giải nén danh sách/bộ dưới dạng các đối số vị trí riêng biệt. Ví dụ,

Số lượng tham số từ khóa có thể thay đổi (**kwargs)

Đối với các tham số từ khóa, bạn có thể sử dụng

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
05 để đóng gói chúng vào từ điển. Ví dụ,

Giải nén Từ điển thành Đối số Từ khóa (**dict)

Tương tự, bạn cũng có thể sử dụng ** để giải nén từ điển thành các đối số từ khóa riêng lẻ

Sử dụng cả *args và **kwargs

Bạn có thể sử dụng cả

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
042 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047 trong định nghĩa hàm của mình. Đặt
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
042 trước
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047. Ví dụ,

Quá tải chức năng

Python KHÔNG hỗ trợ Nạp chồng hàm như Java/C++ (trong đó cùng một tên hàm có thể có các phiên bản khác nhau được phân biệt bởi các tham số của chúng)

Giá trị trả về của hàm

Bạn có thể trả về nhiều giá trị từ một hàm Python, e. g. ,

Có vẻ như hàm Python có thể trả về nhiều giá trị. Trên thực tế, một bộ chứa tất cả các giá trị trả về được trả về

Nhớ lại rằng một bộ thực sự được hình thành thông qua dấu phẩy, không phải dấu ngoặc đơn, e. g. ,

Các loại gợi ý thông qua chú thích chức năng

Từ Python 3. 5, bạn có thể cung cấp gợi ý loại thông qua chú thích chức năng ở dạng

Các chú thích gợi ý loại thường bị bỏ qua và chỉ đóng vai trò là tài liệu. Nhưng có thư viện bên ngoài có thể thực hiện kiểm tra loại

Đọc. "PEP 484 -- Gợi ý nhập"

Mô-đun, Báo cáo nhập khẩu và Gói

mô-đun

Mô-đun Python là một tệp chứa mã Python - bao gồm các câu lệnh, biến, hàm và lớp. Nó sẽ được lưu với phần mở rộng tệp là "

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4338". Tên mô-đun là tên tệp, tôi. e. , một mô-đun sẽ được lưu dưới dạng "______14338"

Theo quy ước, tên mô-đun phải ngắn và toàn chữ thường (tùy chọn được nối với dấu gạch dưới nếu nó cải thiện khả năng đọc)

Một mô-đun thường bắt đầu bằng một chuỗi tài liệu được trích dẫn ba lần (chuỗi tài liệu) (có sẵn trong

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
052), theo sau là các định nghĩa về biến, hàm và lớp

Ví dụ. Mô-đun chào hỏi

Tạo một mô-đun có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
053 và lưu thành "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
054" như sau

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
053 này định nghĩa một biến
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
056 và một hàm
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
057

Báo cáo nhập khẩu

Để sử dụng mô-đun bên ngoài trong tập lệnh của bạn, hãy sử dụng câu lệnh

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093

Sau khi

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093ed, bạn có thể tham chiếu các thuộc tính của mô-đun là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
060. Bạn có thể sử dụng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
061 để gán tên mô-đun mới để tránh xung đột tên mô-đun

Ví dụ: để sử dụng mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
053 đã tạo trước đó

Các câu lệnh nhập nên được nhóm theo thứ tự này

  1. Thư viện chuẩn của Python
  2. Thư viện bên thứ ba
  3. Thư viện ứng dụng cục bộ

Tuyên bố từ nhập khẩu

The syntax is

Với câu lệnh

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
063, bạn có thể tham chiếu trực tiếp các thuộc tính đã nhập bằng cách sử dụng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 mà không cần xác định điều kiện với
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064

Ví dụ,

nhập vs. từ nhập khẩu

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
063 statement actually loads the entire module (like
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093 statement); and NOT just the imported attributes. Nhưng nó CHỈ hiển thị các thuộc tính đã nhập vào không gian tên. Hơn nữa, bạn có thể tham khảo chúng trực tiếp mà không cần xác định tên mô-đun

Ví dụ: hãy tạo mô-đun sau có tên là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
068 để thử nghiệm
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093 so với.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
063

Hãy thử

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
432

Bây giờ, hãy thử

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
063 và lưu ý rằng toàn bộ mô-đun đã được tải, giống như câu lệnh
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
093

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
433

Nhập khẩu có điều kiện

Python cũng hỗ trợ nhập có điều kiện. For example,

sys. path and PYTHONPATH/PATH environment variables

The environment variable

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4337 shall include the path to Python Interpreter "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
075"

The Python module search path is maintained in a Python variable

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
076 of the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0114 module, i. e.
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
078. The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
078 is initialized from the environment variable
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
080, plus an installation-dependent default. The environment variable
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
080 is empty by default

Ví dụ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
434

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
078 default includes the current working directory (denoted by an empty string), the standard Python directories, plus the extension directories in
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
083

The imported modules must be available in one of the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
078 entries

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
435

To show the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4337 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
080 environment variables, use one of these commands

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
436

Reloading Module using imp. reload() or importlib. reload()

If you modify a module, you can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
087 function of the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
088 (for import) module to reload the module, for example,

NOTE. Since Python 3. 4, the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
088 package is pending deprecation in favor of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
090

Template for Python Standalone Module

The following is a template of standalone module for performing a specific task

When you execute a Python module (via the Python Interpreter), the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
083 is set to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
084. On the other hand, when a module is imported, its
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
083 is set to the module name. Hence, the above module will be executed if it is loaded by the Python interpreter, but not imported by another module

Example. [TODO]

Packages

A module contains attributes (such as variables, functions and classes). Relevant modules (kept in the same directory) can be grouped into a package. Python also supports sub-packages (in sub-directories). Packages and sub-packages are a way of organizing Python's module namespace by using "dotted names" notation, in the form of

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
094

To create a Python package

  1. Create a directory and named it your package's name
  2. Put your modules in it
  3. Create a
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    095 file in the directory

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095 marks the directory as a package. For example, suppose that you have this directory/file structure

If

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
097 is in your
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
098, you can import
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
099 as

Without the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095, Python will NOT search the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0001 directory for
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
099. Moreover, you cannot reference modules in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0001 directory directly (e. g. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0004) as it is not in the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
098

Attributes in '__init__. py'

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095 file is usually empty, but it can be used to initialize the package such as exporting selected portions of the package under more convenient name, hold convenience functions, etc

The attributes of the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095 module can be accessed via the package name directly (i. e. ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0008 instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0009). For example,

Sub-Packages

A package can contain sub-packages too. For example,

Clearly, the package's dot structure corresponds to the directory structure

Relative from-import

In the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
063 statement, you can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
060 to refer to the current package and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0012 to refer to the parent package. For example, inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0013, you can write

Take note that in Python, you write

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0014,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0015 by omitting the separating dot (instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0016,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0017)

Circular Import Problem

[LÀM]

Advanced Functions and Namespaces

Local Variables vs. Global Variables

Names created inside a function (i. e. within

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4355 statement) are local to the function and are available inside the function only

Names created outside all functions are global to that particular module (or file), but not available to the other modules. Các biến toàn cục có sẵn bên trong tất cả các chức năng được xác định trong mô-đun. Phạm vi toàn cầu trong Python tương đương với phạm vi mô-đun hoặc phạm vi tệp. There is NO all-module-scope in Python

Ví dụ,

Function Variables (Variables of Function Object)

In Python, a variable takes a value or object (such as

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
03). It can also take a function. For example,

A variable in Python can hold anything, a value, a function or an object

In Python, you can also assign a specific invocation of a function to a variable. For example,

Nested Functions

Python supports nested functions, i. e. , defining a function inside a function. For example,

Sản lượng dự kiến ​​là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
437

Take note that the inner function has read-access to all the attributes of the enclosing outer function, and the global variable of this module

Lambda Function (Anonymous Function)

Lambda functions are anonymous function or un-named function. They are used to inline a function definition, or to defer execution of certain codes. The syntax is

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
438

Ví dụ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0021 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0022 do the same thing. Take note that
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4381 keyword is NOT needed inside the lambda function. Instead, it is similar to evaluating an expression to obtain a value

Lambda function, like ordinary function, can have default values for its parameters

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
439

More usages for lambda function will be shown later

Multiple Statements?

Take note that the body of a lambda function is an one-liner

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0024. In other words, you cannot place multiple statements inside the body of a lambda function. You need to use a regular function for multiple statements

Functions are Objects

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

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

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

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

Function Closure

In the above example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 is not local to the lambda function. Instead,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 is obtained from the outer function

When we assign

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0027 to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0028,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 takes on the value of 8 during the invocation. But we expect
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409 to go out of scope after the outer function terminates. If this is the case, calling
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0031 would encounter an non-existent
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4409?

This problem is resolved via so called Function Closure. Bao đóng là một chức năng bên trong được truyền bên ngoài chức năng kèm theo, được sử dụng ở nơi khác. In brief, the inner function creates a closure (enclosure) for its enclosing namespaces at definition time. Hence, in

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0028, an enclosure with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0034 is created; while in
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0035, an enclosure with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0036 is created. Take note that Python only allows the read access to the outer scope, but not write access. You can inspect the enclosure via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0037, e. g. ,

Functional Programming. Using Lambda Function in filter(), map(), reduce() and Comprehension

Instead of using a

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
055 loop to iterate through all the items in an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0039 (sequence), you can use the following functions to apply an operation to all the items. This is known as functional programming or expression-oriented programming. Filter-map-reduce is popular in big data analysis (or data science)

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0040. Return an
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0041 yielding those
    # empty list
    my_list = []
    
    # list with mixed data types
    my_list = [1, "Hello", 3.4]
    026s of
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    017 for which
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0044 is
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    05. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0046. Apply (or Map or Transform) the function func on each item of the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    017. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0048 (in module
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0049). Apply the function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value, also known as aggregation. For example,
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0050. used frequently in big data analysis to obtain an aggregate value
  • List comprehension. a one-liner to generate a list as discussed in the earlier section. e. g. ,

These mechanism replace the traditional for-loop, and express their functionality in simple function calls. It is called functional programming, i. e. , applying a series of functions (filter-map-reduce) over a collection

Decorators

In Python, a decorator is a callable (function) that takes a function as an argument and returns a replacement function. Recall that functions are objects in Python, i. e. , you can pass a function as argument, and a function can return an inner function. A decorator is a transformation of a function. It can be used to pre-process the function arguments before passing them into the actual function; or extending the behavior of functions that you don't want to modify, such as ascertain that the user has login and has the necessary permissions

Example. Decorating an 1-argument Function

Notes

  1. The decorator
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0051 takes a 1-argument function as its argument, and returns an replacement 1-argument function
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0052, with its argument
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4410 clamped to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0054, before applying the original function
  2. In
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0055, we decorate the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0056 function and assign the decorated (replacement) function to the same function name (confusing?. ). After the decoration, the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0056 takes on a new decorated life
Ví dụ. Using the @ symbol

Using

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0055 to decorate a function is messy?. Thay vào đó, Python sử dụng ký hiệu
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4363 để biểu thị sự thay thế. For example,

For Java programmers, do not confuse the Python decorator

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4363 with Java's annotation like
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0061

Example. Decorator with an Arbitrary Number of Function Arguments

The above example only work for one-argument function. You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
042 and/or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
047 to handle variable number of arguments. For example, the following decorator log all the arguments before the actual processing

We can also modify our earlier

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0051 to handle an arbitrary number of arguments

The @wraps Decorator

Decorator can be hard to debug. This is because it wraps around and replaces the original function and hides variables like

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
083 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0066. This can be solved by using the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0067 of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0049, which modifies the signature of the replacement functions so they look more like the decorated function. For example,

Example. Passing Arguments into Decorators

Let's modify the earlier

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0069 decorator to take two arguments -
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0070 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0071 of the range

The decorator

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0069 takes the desired arguments and returns a wrapper function which takes a function argument (for the function to be decorated)

Confused?. Python has many fancy features that is not available in traditional languages like C/C++/Java

Namespace

Names, Namespaces and Scope

In Python, a name is roughly analogous to a variable in other languages but with some extras. Because of the dynamic nature of Python, a name is applicable to almost everything, including variable, function, class/instance, module/package

Names defined inside a function are local. Names defined outside all functions are global for that module, and are accessible by all functions inside the module (i. e. , module-global scope). There is no all-module-global scope in Python

A namespace is a collection of names (i. e. , a space of names)

A scope refers to the portion of a program from where a names can be accessed without a qualifying prefix. For example, a local variable defined inside a function has local scope (i. e. , it is available within the function, and NOT available outside the function)

Each Module has a Global Namespace

A module is a file containing attributes (such as variables, functions and classes). Each module has its own global namespace. Hence, you cannot define two functions or classes of the same name within a module. But you can define functions of the same name in different modules, as the namespaces are isolated

When you launch the interactive shell, Python creates a module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073, with its associated global namespace. All subsequent names are added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073's namespace

When you import a module via

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0075 under the interactive shell, only the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 is added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073's namespace. You need to access the names (attributes) inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 via
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
060. In other words, the imported module retains its own namespace and must be prefixed with
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
060 inside
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073. (Recall that the scope of a name is the portion of codes that can access it without prefix. )

However, if you import an attribute via

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0082 under the interactive shell, the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 is added into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073's namespace, and you can access the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 directly without prefixing with the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064

On the other hand, when you import a module inside another module (instead of interactive shell), the imported

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
064 is added into the target module's namespace (instead of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073 for the interactive shell)

The built-in functions are kept in a module called

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0089, which is imported into
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073 automatically

The globals(), locals() and dir() Built-in Functions

You can list the names of the current scope via these built-in functions

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0091. return a dictionary (name-value pairs) containing the current scope's global variables
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0092. return a dictionary (name-value pairs) containing the current scope's local variables. If
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0092 is issued in global scope, it returns the same outputs as
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0091
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0095. return a list of local names in the current scope, which is equivalent to
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0096
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0097. return a list of the local names for the given object

Ví dụ,

To show the difference between locals and globals, we need to define a function to create a local scope. For example,

More on Module's Global Namespace

Let's create two modules.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098 and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099, where
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098 imports
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099, as follows

Let's import

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098 (which in turn import
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099) under the interpreter shell, and check the namespaces

Take note that the interpreter's current scope

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
083 is
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0073. It's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098 (imported). The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099 (imported) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0109. To refer to
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099, you need to go thru
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098, in the form of
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0112. The
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0112's namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0114

Now, let run

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098 instead, under IDLE3, and check the namespaces

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
440

Take note that the current scope's

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0116 is again __main__, which is the executing module
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0098. Its namespace contains
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0099 (imported) and
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0109

Name Resolution

When you ask for a name (variable), says

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410, Python searches the LEGB namespaces, in this order, of the current scope

  1. L. Local namespace which is specific to the current function
  2. E. for nested function, the Enclosing function's namespace
  3. G. Global namespace for the current module
  4. B. Built-in namespace for all the modules

If

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 cannot be found, Python raises a
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0122

Modifying Global Variables inside a Function

Recall that names created inside a function are local, while names created outside all functions are global for that module. You can "read" the global variables inside all functions defined in that module. For example,

If you assign a value to a name inside a function, a local name is created, which hides the global name. For example,

To modify a global variable inside a function, you need to use a

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4383 statement to declare the name global; otherwise, the modification (assignment) will create a local variable (see above). For example,

For nested functions, you need to use the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4384 statement in the inner function to modify names in the enclosing outer function. For example,

To modify a global variable inside a nested function, declare it via

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4383 statement too. For example,

In summary,

  1. The order for name resolution (for names inside a function) is. local, enclosing function for nested
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4355, global, and then the built-in namespaces (i. e. , LEGB)
  2. However, if you assign a new value to a name, a local name is created, which hides the global name
  3. You need to declare via
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4383 statement to modify globals inside the function. Similarly, you need to declare via
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4384 statement to modify enclosing local names inside the nested function
More on global Statement

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4383 statement is necessary if you are changing the reference to an object (e. g. with an assignment). It is not needed if you are just mutating or modifying the object. For example,

In the above example, we modify the contents of the array. The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4383 statement is not needed

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
441

Trong ví dụ trên, chúng tôi đang sửa đổi tham chiếu đến biến.

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4383 is needed, otherwise, a local variable will be created inside the function

Built-in Namespace

The built-in namespace is defined in the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0132 module, which contains built-in functions such as
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
004,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
089,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
088,
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0138,
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,
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
090,
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
089 and etc. You can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0141 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0142 to list the attributes of the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0132 module

[LÀM]

del Statement

Bạn có thể sử dụng câu lệnh

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
035 để xóa tên khỏi không gian tên, ví dụ:

Nếu bạn ghi đè một chức năng tích hợp sẵn, bạn cũng có thể sử dụng

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
035 để xóa nó khỏi không gian tên nhằm khôi phục chức năng từ không gian tích hợp

Assertion and Exception Handling

assert Statement

You can use

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4395 statement to test a certain assertion (or constraint). For example, if
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 is supposed to be
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
095 in a certain part of the program, you can use the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4395 statement to test this constraint. An
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0150 will be raised if
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4410 is not zero

Ví dụ,

The assertions are always executed in Python

Syntax

The syntax for

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4395 is

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
442

If the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0153 if
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
05, nothing happens; otherwise, an
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0150 will be raised with the
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0156

Exceptions

In Python, errors detected during execution are called exceptions. For example,

Whenever an exception is raised, the program terminates abruptly

try-except-else-finally

You can use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0157 exception handling facility to prevent the program from terminating abruptly

Example 1. Handling Index out-of-range for List Access

Các kết quả đầu ra dự kiến ​​là

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
443

The exception handling process for

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0157 is

  1. Python chạy các câu lệnh trong khối
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391
  2. If no exception is raised in all the statements of the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391-block, all the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4392-blocks are skipped, and the program continues to the next statement after the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0162 statement
  3. However, if an exception is raised in one of the statement in the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391-block, the rest of
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391-block will be skipped. The exception is matched with the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4392-blocks. The first matched
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4392-block will be executed. The program then continues to the next statement after the
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0162 statement, instead of terminates abruptly. Nevertheless, if none of the
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4392-blocks is matched, the program terminates abruptly
  4. The
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4372-block will be executable if no exception is raised
  5. The
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4393-block is always executed for doing house-keeping tasks such as closing the file and releasing the resources, regardless of whether an exception has been raised
Syntax

The syntax for

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0157 is

The

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4391-block (mandatory) must follow by at least one
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4392 or
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4393 block. The rests are optional

CAUTION. Python 2 uses older syntax of "

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0175", which should be re-written as "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0176" for portability

Example 2. Input Validation

raise Statement

You can manually raise an exception via the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4394 statement, for example,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
444

The syntax is

A

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4394 without argument in the
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4392 block re-raise the exception to the outer block, e. g. ,

Built-in Exceptions

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0180,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0181,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0182. base classes
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0183. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0184,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0185,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0186
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0187
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0188. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    030,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0190
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0191. for
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0192,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0193
  • [TODO] more

User-defined Exception

You can defined your own exception by sub-classing the

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0181 class

Example

with-as Statement and Context Managers

The syntax of the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0180 statement is as follows

Python’s

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
4379 statement supports the concept of a runtime context defined by a context manager. In programming, context can be seen as a bucket to pass information around, i. e. , the state at a point in time. Context Managers are a way of allocating and releasing resources in the context

Example 1

This is equivalent to

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
445

The with-statement's context manager acquires, uses, and releases the context (of the file) cleanly, and eliminate a bit of boilerplate

However, the

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0180 statement is applicable to certain objects only, such as file; while
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0198 can be applied to all

Example 2

Frequently-Used Python Standard Library Modules

Python provides a set of standard library. (Many non-standard libraries are provided by third party. )

To use a module, use

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0075 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0082 to import the entire module or a selected attribute. You can use
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0201 to list all the attributes of the module,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0202 or
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0202 to read the documentation page. For example,

math and cmath Modules

The

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0204 module provides access to the mathematical functions defined by the C language standard. The commonly-used attributes are

  • Constants.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0205,
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4487
  • Power and exponent.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0207,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0208,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0209,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0210,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0211,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0212
  • Converting
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    02 to
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    01.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0215,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0216,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0217
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    02 operations. ________ 50219, ________ 50220
  • ________ 50221 (________ 50222)
  • Chuyển đổi giữa độ và radian. ________ 50223, ________ 50224
  • Hàm lượng giác.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0225,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0226,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0227,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0228,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0229,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0230,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0231
  • hàm hypebol.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0232,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0233,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0234,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0235,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0236,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0237

For examples,

Ngoài ra, mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0238 cung cấp các hàm toán học cho số phức. Xem tài liệu Python để biết chi tiết

mô-đun thống kê

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0239 tính toán các thuộc tính thống kê cơ bản như giá trị trung bình, trung vị, phương sai, v.v. (Nhiều nhà cung cấp bên thứ ba cung cấp các gói thống kê nâng cao. ) Ví dụ như,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
446

Mô-đun ngẫu nhiên

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
089 có thể được sử dụng để tạo các số giả ngẫu nhiên khác nhau

For examples,

mô-đun hệ thống

Mô-đun

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0114 (dành cho hệ thống) cung cấp các tham số và chức năng dành riêng cho hệ thống. thường được sử dụng là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0242. thoát khỏi chương trình bằng cách tăng ngoại lệ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0243. Nếu được sử dụng bên trong một
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4391, mệnh đề
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    4393 được tôn trọng. Đối số tùy chọn
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0246 có thể là một số nguyên (mặc định là 0 đối với kết thúc thông thường hoặc khác 0 đối với kết thúc bất thường); . g. ,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0247)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    078. Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 đường dẫn tìm kiếm mô-đun. Được khởi tạo từ biến môi trường
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    080, cộng với các mục nhập mặc định phụ thuộc vào cài đặt. Xem ví dụ trước đó
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0251,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0252,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0253. luồng đầu vào, đầu ra và lỗi tiêu chuẩn
  • # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0171. Một
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    004 đối số dòng lệnh được chuyển vào tập lệnh Python.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0256 là tên kịch bản. Xem ví dụ bên dưới
Ví dụ. Đối số dòng lệnh

Các đối số dòng lệnh được giữ trong

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
0171 dưới dạng một
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
004. Ví dụ: tạo tập lệnh sau có tên "
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0259"

Chạy tập lệnh

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
447

đăng nhập mô-đun

Mô-đun đăng nhập

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0260 hỗ trợ hệ thống ghi sự kiện linh hoạt cho các ứng dụng và thư viện của bạn

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0260 hỗ trợ năm cấp độ

  1. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0262. Thông tin chi tiết có nghĩa là để gỡ lỗi
  2. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0263. Xác nhận rằng một sự kiện diễn ra như mong đợi
  3. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0264. Đã xảy ra sự cố ngoài ý muốn nhưng ứng dụng vẫn hoạt động
  4. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0265. Ứng dụng không hoạt động như mong đợi
  5. Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0266. Lỗi nghiêm trọng, ứng dụng có thể không tiếp tục được

Các chức năng ghi nhật ký là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0267. Thực hiện cấu hình cơ bản của hệ thống ghi nhật ký. Các đối số từ khóa là.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0268,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0269 (mặc định nối thêm
    # A list with 3 integers
    numbers = [1, 2, 5]
    
    print(numbers)
    
    # Output: [1, 2, 5]
    0151),
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0271 (ghi nhật ký cấp này trở lên), v.v.
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0272,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0273,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0274,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0275,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0276. Đăng nhập
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0277 ở cấp độ cụ thể.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0278 được hợp nhất thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0277 bằng cách sử dụng công cụ xác định định dạng
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0280. Chức năng ghi nhật ký chung, tại nhật ký đã cho
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0281
Ghi nhật ký cơ bản thông qua ghi nhật ký. basicConfig()

Ví dụ,

Các hàm ghi nhật ký hỗ trợ các trình xác định định dạng giống như

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0282, chẳng hạn như
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0283,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0284, với các giá trị dưới dạng đối số của hàm (thay vì thông qua toán tử
# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]
06 trong Python)

Chạy tập lệnh. Tệp nhật ký

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0286 sẽ được tạo với các bản ghi này

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
448

Theo mặc định, các bản ghi nhật ký bao gồm cấp độ nhật ký và tên nhật ký (mặc định là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0287) trước thông báo

Lấy cấp nhật ký từ tệp cấu hình

Các mức nhật ký, chẳng hạn như

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0262 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0263, được lưu trữ dưới dạng số nguyên nhất định trong mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0260. Ví dụ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
449

Cấp nhật ký thường được đọc từ tệp cấu hình, ở dạng chuỗi mô tả. Ví dụ sau đây cho thấy cách chuyển đổi một chuỗi log-level (e. g. ,

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0291) sang cấp nhật ký số (e. g. , 10) được sử dụng bởi mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0260

Định dạng bản ghi nhật ký

Để đặt định dạng thông báo tường trình, hãy sử dụng từ khóa

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0293

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0

trong đó

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0294 cho ngày/giờ,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0295 cho cấp độ nhật ký,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0116 cho tên nhật ký,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0297 cho tên tệp đường dẫn đầy đủ (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0268 chỉ cho tên tệp),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0299 (
# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]
01) cho số dòng và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0301 cho thông báo tường trình

Ghi nhật ký nâng cao. Logger, Handler, Filter và Formatter

Cho đến nay, chúng tôi đã trình bày các phương tiện ghi nhật ký cơ bản. Thư viện

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0260 rất phong phú và được tổ chức thành các thành phần này

  • Người khai thác gỗ. hiển thị các phương thức cho ứng dụng để ghi nhật ký
  • xử lý. gửi các bản ghi nhật ký do trình ghi nhật ký tạo đến đích thích hợp, chẳng hạn như tệp, bảng điều khiển (
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0253), email qua SMTP hoặc mạng qua HTTP/FTP
  • bộ lọc. quyết định bản ghi nhật ký nào sẽ xuất ra
  • Trình định dạng. chỉ định định dạng bố cục của bản ghi nhật ký
Người khai thác gỗ

Để tạo một phiên bản

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0304, hãy gọi
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0305, trong đó tùy chọn
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0306 chỉ định tên trình ghi nhật ký (mặc định là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0287)

Các phương pháp của

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0304 được chia thành hai loại. cấu hình và đăng nhập

Các phương pháp ghi nhật ký thường được sử dụng là.

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0309,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0310,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0311,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0312,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0313 và chung
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0314

Các phương pháp cấu hình thường được sử dụng là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0315
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0316 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0317
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0318 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0319
xử lý

Thư viện ghi nhật ký cung cấp các trình xử lý như

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0320 (
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0253,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0252),
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0323,
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0324 và
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0325 (email)

Các phương pháp thường được sử dụng là

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0315.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0315 của bộ ghi xác định mức thông báo nào sẽ được chuyển đến trình xử lý;
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0329. để định dạng tin nhắn được gửi đến đích
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0318 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0319

Bạn có thể thêm nhiều trình xử lý vào trình ghi nhật ký, có thể xử lý các cấp độ nhật ký khác nhau. Ví dụ: bạn có thể thêm một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0325 để nhận email cho cấp độ
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0333;

Trình định dạng

Đính kèm vào trình xử lý (thông qua

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0336) để định dạng thông điệp tường trình

Ví dụ. Sử dụng Logger với Console Handler và Formatter
  1. Có lẽ không có tiêu chuẩn cho định dạng bản ghi nhật ký (trừ khi bạn có một công cụ phân tích)?. Nhưng tôi khuyên bạn nên chọn một dấu phân cách trường không xuất hiện trong thông báo tường trình để dễ xử lý các bản ghi nhật ký (e. g. , xuất sang bảng tính)

Các kết quả đầu ra dự kiến ​​là

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
1
Ví dụ. Sử dụng tệp nhật ký xoay vòng với RotatingFileHandler
  1. Chúng tôi giữ tất cả các tham số ghi nhật ký trong từ điển, thường được truy xuất từ ​​tệp cấu hình
  2. Trong hàm tạo của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0324,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0338 đặt giới hạn kích thước tệp nhật ký; . Cả
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0338 và
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0339 đều mặc định là 0. Nếu một trong hai bằng 0, cuộn qua không bao giờ xảy ra
  3. Ví dụ trên tạo ra 4 tệp nhật ký.
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0345,
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0346 đến
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0347. Tệp được ghi vào luôn là
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0345. Khi tệp này được lấp đầy, nó được đổi tên thành
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0346;
Ví dụ. Sử dụng nhật ký email cho cấp độ quan trọng và luân phiên các tệp nhật ký cho cấp độ INFO
Ví dụ. Tách Nhật ký LỖI và Nhật ký INFO với Định dạng khác nhau

Mô-đun ConfigParser (Python 2) hoặc configparser (Python 3)

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
031 triển khai trình phân tích cú pháp tệp cấu hình cơ bản cho
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0356

Tệp

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0356 chứa các cặp khóa-giá trị được sắp xếp theo từng phần và trông giống như

  1. Tệp cấu hình bao gồm các phần (được đánh dấu bằng tiêu đề
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0358). Một phần chứa các cặp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0359 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0360. Các khoảng trắng ở đầu và cuối được cắt bớt từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0361. Các dòng bắt đầu bằng
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    4 hoặc
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0363 là nhận xét

Bạn có thể sử dụng

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
031 để phân tích tệp
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0356, e. g. ,

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0366. đọc và phân tích từ danh sách tên tệp. Nó ghi đè các khóa với mỗi tệp liên tiếp, nếu có
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0367. lấy giá trị của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0368 từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0369
Nội suy với SafeConfigParser

Một

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0361 có thể chứa chuỗi định dạng ở dạng
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0371, đề cập đến một
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0116 khác trong phần CÙNG, hoặc một phần
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0373 (viết hoa) đặc biệt. Tuy nhiên, tính năng nội suy này chỉ được hỗ trợ trong
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0374. Ví dụ: giả sử chúng tôi có tệp cấu hình sau có tên là
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0375

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
2

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
056 sẽ được nội suy thành
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0377, được nội suy từ phần CÙNG và phần MẶC ĐỊNH

Mô-đun ngày giờ

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0378 cung cấp các lớp để thao tác ngày và giờ theo cả hai cách đơn giản và phức tạp

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0379. Trả về ngày địa phương hiện tại

Mô-đun smtplib và email

SMTP (Simple Mail Transfer Protocol) là một giao thức xử lý việc gửi email và định tuyến email giữa các máy chủ thư. Python cung cấp mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0380, mô-đun này xác định đối tượng phiên máy khách SMTP có thể được sử dụng để gửi email đến bất kỳ máy Internet nào có trình nghe trình nghe SMTP

Để sử dụng

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0380

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0382 có thể được sử dụng để tạo một thông điệp email

[TODO] more

Mô-đun json

JSON (Ký hiệu đối tượng JavaScript) là định dạng trao đổi dữ liệu nhẹ lấy cảm hứng từ cú pháp ký tự đối tượng JavaScript. Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0383 cung cấp triển khai cho bộ mã hóa và giải mã JSON

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0384. Nối tiếp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0385 thành chuỗi được mã hóa JSON ('s' cho chuỗi)
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0386. Tạo một đối tượng Python từ chuỗi được mã hóa JSON đã cho
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0387. Nối tiếp
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0385 vào tệp
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0389. Tạo một đối tượng Python bằng cách đọc tệp đã cho

Ví dụ,

mô-đun dưa chua và cPickle

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0383 (được mô tả trước đó) xử lý các danh sách và từ điển, nhưng việc tuần tự hóa các cá thể lớp tùy ý đòi hỏi thêm một chút nỗ lực. Mặt khác, mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0391 thực hiện tuần tự hóa và hủy tuần tự hóa bất kỳ đối tượng Python nào. Pickle là một giao thức cho phép tuần tự hóa các đối tượng Python phức tạp tùy ý. Nó dành riêng cho các ngôn ngữ Python và không áp dụng cho các ngôn ngữ khác

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0391 cung cấp các chức năng tương tự như mô-đun
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0383

  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0394. Trả lại biểu diễn ngâm của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0385 dưới dạng một chuỗi
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0396. Xây dựng một đối tượng Python từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0397
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0398. Viết một đại diện ngâm của
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0385 đến
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0400
  • Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0401. Xây dựng một đối tượng Python đọc từ
    Enter a number: 123456789
    123456789 is a magic number
    123456789 is a magic number
    0400

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0403 là phiên bản cải tiến của
Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0391

mô-đun tín hiệu

Tín hiệu (ngắt phần mềm) là một dạng hạn chế của giao tiếp giữa các quá trình không đồng bộ, tương tự như ngắt phần cứng. Nó thường được hệ điều hành sử dụng để thông báo cho các quy trình về một số vấn đề/trạng thái/lỗi nhất định, như chia cho 0, v.v.

Mô-đun

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0405 cung cấp các cơ chế để sử dụng trình xử lý tín hiệu trong Python

dấu hiệu. dấu hiệu()

Phương thức

Enter a number: 123456789
123456789 is a magic number
123456789 is a magic number
0406 có hai đối số. số tín hiệu cần xử lý và chức năng xử lý. Ví dụ,

Tôi có thể sử dụng += để thêm vào danh sách trong Python không?

Đối với danh sách, += giống phương thức mở rộng hơn là phương thức chắp thêm . Với một danh sách ở bên trái toán tử +=, cần một danh sách khác ở bên phải toán tử. Tất cả các mục trong danh sách ở bên phải của toán tử được thêm vào cuối danh sách được tham chiếu ở bên trái của toán tử.

Là gì [. ] trong Python?

[. ] là cú pháp lát mảng cho mọi phần tử trong mảng .

Việc sử dụng phương thức append() và expand() trong Python là gì?

append() thêm một phần tử vào cuối danh sách trong khi. extend() có thể thêm nhiều phần tử riêng lẻ vào cuối danh sách .

Có chức năng chắp thêm trong Python không?

Append trong Python là một phương thức được xác định trước được sử dụng để thêm một mục vào các loại bộ sưu tập nhất định . Nếu không có phương thức chắp thêm, nhà phát triển sẽ phải thay đổi toàn bộ mã của bộ sưu tập để thêm một giá trị hoặc mục.