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
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 withEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
5. They support multi-line comments viaEnter 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 [
00]. Strings are immutable in Python.# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
[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.
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Statements. Python's statement ends with a newline.
[C/C++/C#/Java's statement ends with a semi-colon [
07]]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Compound Statements and Indentation. Python uses indentation to indicate body-block. [C/C++/C#/Java use braces
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Assignment Operator.
09# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Arithmetic Operators.
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]# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
- Compound Assignment Operators.
09,# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
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++
05languages = ["Python", "Swift", "C++"] # access item at index 0 print[languages[0]] # Python # access item at index 2 print[languages[2]] # C++
- Comparison Operators.
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,languages = ["Python", "Swift", "C++"] # access item at index 0 print[languages[0]] # Python # access item at index 2 print[languages[2]] # C++
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ụngEnter 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
005Enter 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ử quaEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
007 hoặcEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
008 hoặc danh sách phụ quaEnter 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]
- Danh sách.
- 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.
00. concatenation# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
02. repetition# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
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ụcEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
023. cắt látEnter 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
- 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
038Enter 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
- ______402,
- Đị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 number045, vòng lặp
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number002, vòng lặp
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number047, đầu vào/đầu ra, chuỗi,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number004 và hàm
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number9
Để chạy tập lệnh Python
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number0
Sản lượng dự kiến là
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
01Làm thế nào nó hoạt động- #. /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
- # -*- 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
- 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
- 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ộtEnter 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ủaEnter 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ẵnEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
053 - thùng = [0]*10 [Dòng 38]. Python hỗ trợ toán tử lặp lại [
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]# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
- 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ặpEnter 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-inEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
057 function to create aEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004 of indexesEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
059, then apply theEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
055 loop on the indexEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004 - 0 bool. [Dòng 10]. Các phần nổi bật được gọi là chú thích gợi ý loại. Chúng bị Trình thông dịch Python bỏ qua và chỉ đóng vai trò là tài liệu
- 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ànhEnter 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]
0languages = ["Python", "Swift", "C++"]
# access item at index 0
print[languages[0]] # Python
# access item at index 2
print[languages[2]] # C++
0Làm thế nào nó hoạt động- The conversion formula is.
0111, trong đó# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
0112# 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# 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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
002-loop with index. It supports only
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
057 để tạo danh sách
0122, sau đó lặp qua từng mục trong danh sách đã tạo# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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
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ự# 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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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# empty list my_list = [] # list with mixed data types my_list = [1, "Hello", 3.4]
- Có
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# 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# 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# 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# 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]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- khác [Dòng 32]. chúng tôi sử dụng
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 number0
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number00Làm thế nào nó hoạt động
- Chúng tôi tổ chức mã trong các chức năng
- Công thức chuyển đổi là.
0157, trong đó# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
0158# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Bạn có thể sử dụng hàm tích hợp sẵn
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]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 number01
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number02Làm thế nào nó hoạt động
- We use the modulus/division repeatedly to get the hex digits in reverse order
- Chúng tôi sử dụng danh sách tra cứu [Dòng 11] để chuyển đổi
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Bạn có thể sử dụng hàm tích hợp sẵn
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. ,# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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ảnEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number00How it works
- nhập sys [Dòng 14]. Chúng tôi sử dụng mô-đun
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
093 mô-đun trước khi sử dụng nó - The command-line arguments are stored in a variable
0171, which is a# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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ủaEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004
0177 là tên tập lệnh, theo sau là các đối số dòng lệnh khác# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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
0178 để xác minh rằng độ dài của đối số dòng lệnh# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004 là 2 - 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
0180, câu lệnh này sẽ tự động đóng tệp khi thoát# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- cho dòng trong infile. [Dòng 26]. Chúng tôi sử dụng vòng lặp
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;# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Chúng tôi cũng gọi tiện ích Unix "
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 number04How it works
- 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
0114 [@ https. // tài liệu. con trăn. org/3/library/sys. html]. We retrieve the command-line arguments from the# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
004
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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
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]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- for line in infile. [Line 22]. We use a
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Python 3. 2 introduces a new
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.# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 number06How 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
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 number07
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 number08Ubuntu [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 number09
To verify the Python installation
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
010các cửa sổBạn có thể cài đặt một trong hai
- "Phân phối Anaconda" [Xem phần trước]
- 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
- Trong Cygwin [môi trường Unix cho Windows] và cài đặt Python [trong danh mục "phát triển"]
[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ácBạn có thể chạy "Trình thông dịch Python" ở chế độ tương tác trong "Command-Line Shell" [chẳng hạn như Anaconda Prompt, CMD của Windows, Terminal của Mac OS X, Bash Shell của Ubuntu]
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
011Dấ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
0115# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- [Mac OS X và Ubuntu]
4317# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- [Windows]
4318 theo sau là Enter# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Viết và chạy tập lệnh Python
Tập lệnh Python đầu tiên - xin chào. pyUse 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]
013Làm thế nào nó hoạt động- Theo quy ước, tên tập lệnh Python [mô-đun] ở dạng chữ thường [e. g. ,
4320]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Nhận xét EOL. Các câu lệnh bắt đầu bằng
4321 cho đến cuối dòng [EOL] là các nhận xét# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- #. /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
- # -*- 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
- """ 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
- Biến. Chúng ta tạo các biến
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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épEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number
8 - Số nguyên của Python có kích thước không giới hạn [Dòng 8]
- Python hỗ trợ số dấu phẩy động [Dòng 10]
- Python hỗ trợ số phức [Dòng 12] và các kiểu dữ liệu cấp cao khác
- Python hỗ trợ một mảng động được gọi là danh sách [Dòng 14], được đại diện bởi
4329. Phần tử có thể được truy xuất thông qua chỉ mục# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
007 [Dòng 15] - 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
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]
014Chạy tập lệnh PythonBạn có thể phát triển/chạy tập lệnh Python theo nhiều cách - được giải thích trong các phần sau
Chạy tập lệnh Python trong Command-Line Shell [Anaconda Prompt, CMD, Terminal, Bash]Bạn có thể chạy tập lệnh python thông qua Trình thông dịch Python trong Trình bao dòng lệnh
Executable Shell Script của UnixTrong Linux/Mac OS X, bạn có thể biến tập lệnh Python thành chương trình thực thi [gọi là Shell Script hoặc Executable Script] bằng cách
- Bắt đầu bằng một dòng bắt đầu bằng
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"# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Làm cho tệp có thể thực thi được thông qua lệnh
4335 [thay đổi chế độ tệp]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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.
015# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 PythonChương trình thực thi của WindowsTrong Windows, bạn có thể liên kết phần mở rộng tệp "
# 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 đượcChạ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,
4340 [đối với thư mục chính] không hoạt động?# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Hàm tích hợp sẵn
4341 sẽ mở tệp ở chế độ chỉ đọc mặc định;# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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
- Trình thông dịch Python [như mô tả ở trên]
- Python IDLE
- 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
- Nhật thực với PyDev
- 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ự doKHÔNG có nhận xét nhiều dòng trong Python?. [C/C++/Java hỗ trợ nhận xét nhiều dòng thông qua
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number6. ]
Các câu lệ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
- Một biểu thức trong dấu ngoặc đơn
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Dấu gạch chéo ngược [
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 understandingA compound statement, such as conditional [
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number045], loop [
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number047,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number002] 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 number067]; 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 number067] 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ụ,
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]
4363từ khóaPython 3 có 35 từ hoặc từ khóa dành riêng, không thể được sử dụng làm định danh
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]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
093,
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,# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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,
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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ử]
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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
- 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,
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.# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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. ,
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. ]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Đ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. ,
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Không sử dụng những tên vô nghĩa như
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ì?]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Tránh các tên có một chữ cái như
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. ]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Use singular and plural nouns prudently to differentiate between singular and plural variables. Ví dụ: bạn có thể sử dụng biến
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]# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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 đổiLoạ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 number004, 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 number007.
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number004 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 number080 để đọ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 number062 để 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 number062 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 3Nhớ lại rằng Python 2 và Python 3 KHÔNG tương thích. Trong Python 2, bạn có thể sử dụng "
# 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 number062 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 number062 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 literalNumber Types
Python supports these built-in number types
- Integers [type
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Floating-point numbers [type
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,# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- Booleans [type
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
095, an empty value [such as empty string
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# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
02 for
05 and# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
095 for
06. For example,# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
016# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
- 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
4416 [or# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number
10]. For example, - 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 number13 to get the object type referenced by a variableType Casting. int[x], float[x], str[x]
You can perform type conversion [or type casting] via built-in functions
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number14,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number15,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number16,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number17, etc. For example,
In summary, a variable does not associate with a type. Instead, a type is associated with an object. A variable provides a reference to an object [of a certain type]
Check Instance's Type. isinstance[instance, type]You can also use the built-in function
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number18 to check if the
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number19 belong to the
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number20. For example,
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
017The 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 AssignmentVí dụ,
Assignment operator is right-associative, i. e. ,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number21 is interpreted as
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number22del Operator
You can use
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number035 operator to delete a variable. For example,
Number Operations
Arithmetic Operators [+, -, *, /, //, **, %]Python supports these arithmetic operators
OperatorModeUsageDescriptionExample+BinaryUnaryx + y
+xAddition
Positive-Binary
Unaryx - y
-xSubtraction
Negate*Binaryx * yMultiplication/Binaryx / yFloat Division
[Returns a float]1 / 2 ⇒ 0. 5
-1 / 2 ⇒ -0. 5//Binaryx // yInteger Division
[Returns the floor integer]1 // 2 ⇒ 0
-1 // 2 ⇒ -1
8. 9 // 2. 5 ⇒ 3. 0
-8. 9 // 2. 5 ⇒ -4. 0 [floor. ]
-8. 9 // -2. 5 ⇒ 3. 0**Binaryx ** yExponentiation2 ** 5 ⇒ 32
1. 2 ** 3. 4 ⇒ 1. 858729691979481%Binaryx % yModulus [Remainder]9 % 2 ⇒ 1
-9 % 2 ⇒ 1
9 % -2 ⇒ -1
-9 % -2 ⇒ -1
9. 9 % 2. 1 ⇒ 1. 5
-9. 9 % 2. 1 ⇒ 0. 6000000000000001Compound 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 number31 is the same as
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number32Increment/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 number32 or
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number31 for increment
Python accepts
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number37, and
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number38. Don't get trap into this. But Python flags a syntax error for
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number39 and
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number40Mixed-Type Operations
For mixed-type operations, e. g. ,
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number41 [
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number42], the value of the "smaller" type is first promoted to the "bigger" type. It then performs the operation in the "bigger" type and returns the result in the "bigger" type. In Python,
# 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 number06Relational [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]
06OperatorModeUsageDescriptionExample==. =
=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]
06innot 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 number53
Return bool of either True or Falselst = [1, 2, 3]
x = 1
x in lst ⇒ Falseis
is notBinaryx is y
x is not yCheck if
# 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 number53 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]
06Example. [TODO]
Logical Operators [and, or, not]Python supports these logical [boolean] operators, that operate on boolean values
OperatorModeUsageDescriptionExampleandBinaryx and yLogical ANDorBinaryx or yLogical ORnotUnarynot xLogical NOTNotes
- 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 andEnter 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 FunctionsPython 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.
0138,# A list with 3 integers numbers = [1, 2, 5] print[numbers] # Output: [1, 2, 5]
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
OperatorModeUsageDescriptionExampleEnter a number: 123456789 123456789 is a magic number 123456789 is a magic number74
Enter a number: 123456789 123456789 is a magic number 123456789 is a magic number75&binaryx & ybitwise ANDx & y ⇒ 0b10000001. binaryx . ybitwise ORx . y ⇒ 0b10001111~Unary~xbitwise NOT [or negate]~x ⇒ -0b10000010^binaryx ^ ybitwise XORx ^ y ⇒ 0b00001110