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 3Mã 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
đầu ra 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
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 value0, để 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 value0. 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 value2. 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 value3 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 value4 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 value5. Ở 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 value7, 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 value8, 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 value9) 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 value6 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 value7. Ở 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 John2. Khi chúng tôi được dạy ở trường, John3 mang lại cho John4. 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 John5 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 value3 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 value4 đượ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 John8 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 John9 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 |