Cách thêm số đầu vào trong python

Sử dụng hàm input() tích hợp sẵn của Python để lấy đầu vào số nguyên từ người dùng. Hàm input() này trả về dữ liệu chuỗi và nó có thể được lưu trữ trong một biến chuỗi. Sau đó sử dụng hàm int() để phân tích thành một giá trị số nguyên

Ví dụ lấy đầu vào số nguyên trong Python 3

Mã ví dụ đơn giản lấy đầu vào số nguyên, chúng ta phải đánh máy các đầu vào đó thành số nguyên bằng cách sử dụng hàm int() tích hợp sẵn của Python

age = int(input("Enter age: "))

print(age)
print(type(age))

đầu ra

Cách thêm số đầu vào trong python

Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về mã đầu vào Python này

Ghi chú. IDE. PyCharm 2021. 3. 3 (Phiên bản cộng đồng)

cửa sổ 10

Trăn 3. 10. 1

Tất cả các Ví dụ về Python đều nằm trong Python 3, vì vậy có thể nó khác với python 2 hoặc các phiên bản nâng cấp

Cách thêm số đầu vào trong python

Rohit

Bằng cấp về Khoa học Máy tính và Kỹ sư. Nhà phát triển ứng dụng và có kinh nghiệm về nhiều ngôn ngữ lập trình. Đam mê công nghệ & thích học hỏi kỹ thuật

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 (_______1_______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,
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

5
7
    
a = input()
b = input()
s = a + b
print(s)
    

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 lên. 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
first = 5
second = 7
print(first * second)

# you can use single or double quotes to define a string
first = '5'
second = "7"
print(first * second)
    

Để 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