Làm thế nào để bạn nhập và xuất trong python?

Mọi chương trình cuối cùng đều là một bộ xử lý dữ liệu, vì vậy chúng ta nên biết cách nhập và xuất dữ liệu bên trong nó. Tồn tại một hàm,

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
0, để xuất dữ liệu từ bất kỳ chương trình Python nào. Để sử dụng nó, hãy chuyển danh sách các đối số được phân tách bằng dấu phẩy mà bạn muốn in cho hàm
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
0. Hãy xem một ví dụ. Nhấn "chạy" và sau đó "tiếp theo" để xem chương trình đang được thực thi từng dòng như thế nào

None
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value

Để nhập dữ liệu vào một chương trình, chúng tôi sử dụng

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
2. Hàm này đọc một dòng văn bản, dưới dạng Chuỗi

Đây là chương trình đọc tên người dùng và chào họ

John
    
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    

Quảng cáo của Google, có thể dựa trên sở thích của bạn

Hãy thử viết một chương trình nhập vào hai số và in ra tổng của chúng. Chúng tôi đọc hai số và lưu trữ chúng trong các biến

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
3 và
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
4 bằng cách sử dụng toán tử gán
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
5. Ở phía bên trái của toán tử gán, chúng ta đặt tên của biến. Tên có thể là một chuỗi các ký tự Latinh (_______06,
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
7,
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
8,
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
9) nhưng phải bắt đầu bằng một chữ cái trong phạm vi
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
6 hoặc
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
7. Ở bên phải của toán tử gán, chúng tôi đặt bất kỳ biểu thức nào mà Python có thể đánh giá. Tên bắt đầu trỏ đến kết quả đánh giá. Đọc ví dụ này, chạy nó và nhìn vào đầu ra

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
6
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
7

Sau khi chạy ví dụ, chúng ta có thể thấy rằng nó in ra

John
    
2. Khi chúng tôi được dạy ở trường,
John
    
3 mang lại cho
John
    
4. Vì vậy, chương trình sai, và điều quan trọng là phải hiểu tại sao. Vấn đề là, ở dòng thứ ba
John
    
5 Python đã "tổng" hai chuỗi, thay vì hai số. Tổng của hai chuỗi trong Python hoạt động như sau. chúng chỉ được dán lần lượt. Nó đôi khi còn được gọi là "nối chuỗi"

Bạn có thấy trong trình kiểm tra biến, ở phía bên tay phải, rằng các giá trị được liên kết với biến

print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
3 và 
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
4 được đặt trong dấu ngoặc kép không? . Các chuỗi và số được biểu diễn bằng Python khác nhau

Tất cả các giá trị trong Python được gọi là "đối tượng". Mỗi đối tượng có một loại nhất định. Số 2 tương ứng với đối tượng "số 2" kiểu "int" (i. e. , một số nguyên). Chuỗi

John
    
8 tương ứng với một đối tượng "chuỗi 'hello'" thuộc loại "str". Mỗi số dấu phẩy động được biểu diễn dưới dạng một đối tượng kiểu "float". Loại đối tượng xác định loại hoạt động nào có thể được áp dụng cho nó. Chẳng hạn, nếu hai biến
John
    
9 và
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
0 đang trỏ đến các đối tượng kiểu
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
1, Python có thể nhân chúng. Tuy nhiên, nếu chúng đang trỏ đến các đối tượng kiểu
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
2, thì Python không thể làm điều đó

None
print(5 + 10)
print(3 * 7, (17 - 2) * 8)
print(2 ** 16)  # two stars are used for exponentiation (2 to the power of 16)
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
        # it returns only the quotient of the division (i.e. no remainder)
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value
0

Để truyền (chuyển đổi) chuỗi chữ số thành số nguyên, chúng ta có thể sử dụng hàm 

print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
3. Ví dụ: 
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
4 đưa ra một đối tượng int có giá trị
print('What is your name?')
name = input()  # read a single line and store it in the variable "name"
print('Hi ' + name + '!')
    
5

Với thông tin trên, bây giờ chúng ta có thể sửa đầu ra không chính xác và xuất ra tổng của hai số một cách chính xác