[Nhà tài trợ] Bắt đầu học Python với hướng dẫn Giới thiệu về Python miễn phí của DataCamp. Tìm hiểu Khoa học dữ liệu bằng cách hoàn thành các thử thách mã hóa tương tác và xem video của các chuyên gia hướng dẫn. Bắt đầu bây giờ
Cập nhật ngày 07 tháng 1 năm 2020
Bạn có thể chạy các chương trình python theo hai cách, thứ nhất bằng cách gõ lệnh trực tiếp trong trình bao python hoặc chạy chương trình được lưu trữ trong một tệp. Nhưng hầu hết thời gian bạn muốn chạy các chương trình được lưu trữ trong một tệp
Hãy tạo một tệp có tên hello.py
trong thư mục tài liệu của bạn i. e C:\Users\YourUserName\Documents
bằng notepad [hoặc bất kỳ trình soạn thảo văn bản nào khác mà bạn chọn], hãy nhớ rằng tệp python có phần mở rộng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 270, sau đó viết đoạn mã sau vào tệp
Trong python, chúng tôi sử dụng chức năng in để hiển thị chuỗi ra bàn điều khiển. Nó có thể chấp nhận nhiều hơn một đối số. Khi hai hoặc nhiều đối số được truyền vào, hàm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271 sẽ hiển thị mỗi đối số được phân tách bằng dấu cách
Sản lượng dự kiến
Bây giờ hãy mở terminal và thay đổi thư mục làm việc hiện tại thành C:\Users\YourUserName\Documents
bằng lệnh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 273
Để chạy chương trình gõ lệnh sau
Nếu mọi thứ suôn sẻ, bạn sẽ nhận được đầu ra sau
Tìm sự giúp đỡ
Sớm hay muộn khi sử dụng python, bạn sẽ gặp tình huống muốn biết thêm về một số phương thức hoặc chức năng. Để giúp bạn, Python có hàm help[], đây là cách sử dụng hàm này
cú pháp
Để tìm thông tin về lớp học.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 274
Để tìm hiểu thêm về phương pháp thuộc về lớp.
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.0
Giả sử bạn muốn biết thêm về lớp int, hãy vào Python shell và gõ lệnh sau
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.
Như bạn có thể thấy hàm
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.1 tạo ra toàn bộ lớp
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.2 với tất cả các phương thức, nó cũng chứa mô tả khi cần
Bây giờ, giả sử bạn muốn biết các đối số cần thiết cho phương thức
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.3 của lớp
>>> help[int] Help on class int in module builtins: class int[object] | int[x=0] -> integer | int[x, base=10] -> integer | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is a number, return x.__int__[]. For floating point | numbers, this truncates towards zero. | | If x is not a number or if base is given, then x must be a string, | bytes, or bytearray instance representing an integer literal in the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int['0b100', base=0] | 4 | | Methods defined here: | | __abs__[self, /] | abs[self] | | __add__[self, value, /] | Return self+value.4, để tìm hiểu bạn cần gõ lệnh sau vào trình bao python
>>> help[str.index] Help on method_descriptor: index[...] S.index[sub[, start[, end]]] -> int Like S.find[] but raise ValueError when the substring is not found.
Trong bài đăng tiếp theo, chúng ta sẽ tìm hiểu về các loại dữ liệu và biến trong python
Hướng dẫn khác [Nhà tài trợ]
Trang web này được hỗ trợ rộng rãi bởi DataCamp. DataCamp cung cấp Hướng dẫn Python tương tác trực tuyến cho Khoa học dữ liệu. Tham gia cùng hơn một triệu người học khác và bắt đầu học Python cho khoa học dữ liệu ngay hôm nay