TranslateProject/sources/tech/20230315.0 ⭐️ Input Function in Python Concepts and Examples.md
2023-03-21 08:47:42 +08:00

4.2 KiB
Raw Blame History

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.

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.

Lets 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:

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 users 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 users input. The value is stored in the variable you specify.

Lets 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

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 in Python. Heres 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. Its a powerful function to accept values from standard input for simple use cases.


via: https://www.debugpoint.com/input-function-python/

作者:Arindam 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出