mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
translated
This commit is contained in:
parent
f63fb68a59
commit
f129d141ba
@ -1,127 +0,0 @@
|
||||
[#]: subject: "Input Function in Python: Concepts and Examples"
|
||||
[#]: via: "https://www.debugpoint.com/input-function-python/"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Input Function in Python: Concepts and Examples
|
||||
======
|
||||
|
||||
**In this article, I will explain the basics of the Input function of Python with some easy-to-understand examples.**
|
||||
|
||||
![][1]
|
||||
|
||||
Python is one of the most popular programming languages in the world, and it is widely used in various applications. One of the basic concepts in Python is the `Input()` function, which allows users to interact with the program by providing input values.
|
||||
|
||||
Let’s find out the input function, how it works, and how you can use it effectively in your Python programs.
|
||||
|
||||
Before you start, I hope you have Python installed and set up a basic editor. If not, do refer to my below guides:
|
||||
|
||||
- [How to Install Python 3.11 on Ubuntu/Linux][2]
|
||||
- [How to install Python on Windows][3]
|
||||
- [How to set up the IDLE editor for Python][4]
|
||||
|
||||
### Python Input Function
|
||||
|
||||
The input function in Python is used to accept user input as a string. It prompts the user to enter a value and waits for the user to provide input. The user’s input is then stored as a string in a variable, which can be used later in the program.
|
||||
|
||||
#### Syntax
|
||||
|
||||
```
|
||||
input("your message to user")
|
||||
```
|
||||
|
||||
When you run the input function, it shows the message to the user and _waits_ for input. The cursor waits. Once the user enters input and presses enter, the input function reads the user’s input. The value is stored in the variable you specify.
|
||||
|
||||
Let’s see a few examples.
|
||||
|
||||
### Example 1: Simple Input function usage
|
||||
|
||||
The following code snippet takes input and shows the output with an additional string.
|
||||
|
||||
```
|
||||
your_name = input("Enter your name:")print("Hello " + your_name)
|
||||
```
|
||||
|
||||
**Output:**
|
||||
|
||||
```
|
||||
Enter your name:arindamHello arindam
|
||||
```
|
||||
|
||||
![Python Input function - a simple example][5]
|
||||
|
||||
Example 2: Taking input as integer and float
|
||||
|
||||
You can also convert the input as int() or float() at runtime while using input() function. Here is an example.
|
||||
|
||||
```
|
||||
no_of_books = int(input("Enter total books ordered:"))
|
||||
print ("Total number of books:", no_of_books)
|
||||
|
||||
price_of_each_book = float(input("Enter unit price:"))
|
||||
print ("Total price:", no_of_books * price_of_each_book)
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Enter total books ordered:5
|
||||
Total number of books: 5
|
||||
Enter unit price:10.1
|
||||
Total price: 50.5
|
||||
```
|
||||
|
||||
### Example 4: Joining lists
|
||||
|
||||
You can also use other functions, such as lists, to accept a set of values and convert them as [lists][6] in Python. Here’s an example which takes input and converts it to a list. Then used, another set of values and appended to the first list.
|
||||
|
||||
```
|
||||
# take input for first list
|
||||
list_1 = list(input("Enter numbers for list 1:"))
|
||||
|
||||
#take input for second list
|
||||
list_2 = list(input("Enter some letters for list 2:"))
|
||||
|
||||
#loop through the second list and add to first list
|
||||
for j in list_2:
|
||||
list_1.append(j)
|
||||
|
||||
#print the modified first list
|
||||
print(list_1)
|
||||
```
|
||||
|
||||
**Output:**
|
||||
|
||||
```
|
||||
Enter numbers for list 1:1234
|
||||
Enter some letters for list 2:ABCD
|
||||
['1', '2', '3', '4', 'A', 'B', 'C', 'D']
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
I hope this simple guide clarifies the input() function with several examples. It’s a powerful function to accept values from standard input for simple use cases.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/input-function-python/
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://www.debugpoint.com/wp-content/uploads/2023/03/pyinput.jpg
|
||||
[2]: https://www.debugpoint.com/install-python-3-11-ubuntu/
|
||||
[3]: https://www.debugpoint.com/install-python-windows/
|
||||
[4]: https://www.debugpoint.com/install-idle-ubuntu-linux/
|
||||
[5]: https://www.debugpoint.com/wp-content/uploads/2023/03/Python-Input-function-a-simple-example.jpg
|
||||
[6]: https://docs.python.org/3/library/stdtypes.html?highlight=list#lists
|
@ -0,0 +1,129 @@
|
||||
[#]: subject: "Input Function in Python: Concepts and Examples"
|
||||
[#]: via: "https://www.debugpoint.com/input-function-python/"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Python 中的输入函数:概念和示例
|
||||
======
|
||||
|
||||
**在这篇文章中,我将通过一些通俗易懂的例子来解释 Python 的 Input 函数的基础知识。**
|
||||
|
||||
![][1]
|
||||
|
||||
Python 是世界上最流行的编程语言之一,广泛应用于各种应用程序中。Python 中的一个基本概念是 `Input()` 函数,它允许用户通过提供输入值与程序进行交互。
|
||||
|
||||
让我们看看 input 函数它是如何工作的,以及如何在 Python 程序中有效地使用它。
|
||||
|
||||
在开始之前,我希望你已经安装了 Python 并设置了一个基本的编辑器。如果没有,请参考我的以下指南:
|
||||
|
||||
- [如何在 Ubuntu/Linux 上安装 Python 3.11][2]
|
||||
- [如何在 Windows 上安装 Python][3]
|
||||
- [如何为 Python 设置 IDLE 编辑器][4]
|
||||
|
||||
### Python Input 函数
|
||||
|
||||
Python 中的 input 函数用于接受用户输入的字符串。它提示用户输入一个值并等待用户提供输入。然后将用户的输入作为字符串存储在变量中,稍后可以在程序中使用。
|
||||
|
||||
#### 句法
|
||||
|
||||
```
|
||||
input("your message to user")
|
||||
```
|
||||
|
||||
当你运行 input 函数时,它会向用户显示消息并_等待_输入。光标等待。当用户输入并按下回车键,input 函数就会读取用户的输入。该值存储在你指定的变量中。
|
||||
|
||||
让我们看几个例子。
|
||||
|
||||
### 示例 1:简单的 input 函数用法
|
||||
|
||||
以下代码片段接受输入并显示带有附加字符串的输出。
|
||||
|
||||
|
||||
```
|
||||
your_name = input("Enter your name:")
|
||||
print("Hello " + your_name)
|
||||
```
|
||||
|
||||
**输出:**
|
||||
|
||||
```
|
||||
Enter your name:arindamHello arindam
|
||||
```
|
||||
|
||||
![Python 输入函数:一个简单的例子][5]
|
||||
|
||||
### 示例 2:以整数和浮点数作为输入
|
||||
|
||||
在使用 input() 函数时,你还可以在运行时将输入转换为 int() 或 float() 。这是一个例子。
|
||||
|
||||
```
|
||||
no_of_books = int(input("Enter total books ordered:"))
|
||||
print ("Total number of books:", no_of_books)
|
||||
|
||||
price_of_each_book = float(input("Enter unit price:"))
|
||||
print ("Total price:", no_of_books * price_of_each_book)
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
```
|
||||
Enter total books ordered:5
|
||||
Total number of books: 5
|
||||
Enter unit price:10.1
|
||||
Total price: 50.5
|
||||
```
|
||||
|
||||
### 示例 3:连接列表
|
||||
|
||||
你还可以使用其他函数(例如列表)来接受一组值并将它们转换为 Python 中的 [list][6]。这是一个接受输入并将其转换为列表的示例。然后使用另一组值并附加到第一个列表。
|
||||
|
||||
```
|
||||
# 获取第一个列表的输入
|
||||
list_1 = list(input("Enter numbers for list 1:"))
|
||||
|
||||
# 获取第二个列表的输入
|
||||
list_2 = list(input("Enter some letters for list 2:"))
|
||||
|
||||
# 循环遍历第二个列表并添加到第一个列表
|
||||
for j in list_2:
|
||||
list_1.append(j)
|
||||
|
||||
# 打印修改后的第一个列表
|
||||
print(list_1)
|
||||
```
|
||||
|
||||
**输出:**
|
||||
|
||||
```
|
||||
Enter numbers for list 1:1234
|
||||
Enter some letters for list 2:ABCD
|
||||
['1', '2', '3', '4', 'A', 'B', 'C', 'D']
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
我希望这个简单的指南通过几个示例阐明了 input() 函数。对于简单的场景,它是一个强大的功能,可以从标准输入中接受值。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/input-function-python/
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://www.debugpoint.com/wp-content/uploads/2023/03/pyinput.jpg
|
||||
[2]: https://www.debugpoint.com/install-python-3-11-ubuntu/
|
||||
[3]: https://www.debugpoint.com/install-python-windows/
|
||||
[4]: https://www.debugpoint.com/install-idle-ubuntu-linux/
|
||||
[5]: https://www.debugpoint.com/wp-content/uploads/2023/03/Python-Input-function-a-simple-example.jpg
|
||||
[6]: https://docs.python.org/3/library/stdtypes.html?highlight=list#lists
|
Loading…
Reference in New Issue
Block a user