Thứ Năm, Tháng Ba 6, 2025
spot_img
HomeInput () function in python - quantrimang.com

Input () function in python – quantrimang.com

Input in python What is that? How to use input function in python how? Let's find out with Quantrimang.com!

If you ask which profession has the highest salary today, you will surely hear the most programming answers. It is a “cradle” to produce a lot of utility products such as applications, software, even entertainment games and more.

Programming is really a difficult subject for many people but it is not difficult to learn if you are really passionate. Because programming has many different languages, choose to focus on the language you really love and be used popular today. Python is one of them.

If you have learned programming for a while, you must be no stranger to Python. This language has contributed to the success of many applications and websites. Learning Python is not too difficult. You only need to start with the knowledge and learn how to use the most basic jaw.

Yes, Python has many interesting and interesting functions. Just grasp them, you can easily create quality products. In this article, we will find out together Input statement in python.

Input or input can come from users directly via the keyboard or external source like file or database. The results can be directly shown to the console or IDE, to the screen via GUI (graphic user interface) or from external sources.

Input () function built -in Python allows users to enter Chain data And return the content imported.

Input function ()

input([prompt])

Parameter of input function ()

Jaw input() There is a single parameter prompt – This is a series of suggestions for users to enter data. This parameter is the option may have or not

For example:

1. Ask the username without parameters prompt And printed with greetings:

print('Nhập tên của bạn:')
x = input()
print('QuanTriMang xin chào bạn, ' + x)

Suppose the input users enter as “meta”. Running the program, the return results are:

Nhập tên của bạn:
META
QuanTriMang xin chào bạn, META

2. Ask for the username and print the greeting:

x = input('Nhập tên của bạn: ')
print('QuanTriMang xin chào bạn, ' + x)

Results after you enter the name:

Nhập tên của bạn: Ngọc Vân
QuanTriMang xin chào bạn, Ngọc Vân

Change the data type entered from input ()

As we mentioned above, the data type entered from input() is the string form. To switch it to numeric form and use as some to calculate, it is necessary to combine with the definition of the external data input().

👉 Learn about data types in Python

Example 1: Calculate the sum of 2 numbers entered by the user

x = int(input('Nhập số thứ nhất: '))
y = int(input('Nhập số thứ hai: '))

print('Tổng 2 số vừa nhập là: ', x+y)

If you enter 2 numbers: 4 and 7, there will be the following results:

Nhập số thứ nhất: 4
Nhập số thứ hai: 7
Tổng 2 số vừa nhập là:  11

Example 2: Calculate the area of ​​the rectangle based on the edge length by the user

With this example, you can simply need to put the calculation formula and print the results as follows:

print("Tính Diện tích hình chữ nhật bằng Python:")

x = int(input('Nhập cạnh a: '))
y = int(input('Nhập cạnh b: '))

S=x*y
print('Diện tích hình chữ nhật là: ', S)

The results obtained with 2 sides 4 and 9 are as follows:

Tính Diện tích hình chữ nhật bằng Python:
Nhập cạnh a: 4
Nhập cạnh b: 9
Diện tích hình chữ nhật là:  36

However, if you are a professional user, you should use the PYTHON function defined by the user to calculate the rectangular area, the room when you still use it again in some cases. How to calculate the rectangular area using the self -made function in Python as follows:

print("Tính Diện tích hình chữ nhật bằng Python:")

x = int(input('Nhập cạnh a: '))
y = int(input('Nhập cạnh b: '))

def DT_chu_nhat(a,b):
    S=a*b
    return S

print('Diện tích hình chữ nhật là: ', DT_chu_nhat(x,y))

The output results will be similar to the above but the code will be more professional because you will gradually have the habit of using the self -made function.

Take two lists as Input and connect them

Taking data for users is a string and separates each character with a list () to convert it into characters.

# Lấy list1 mà người dùng nhập vào là danh sách
list1 = list(input("Please Enter Elements of list1: "))

#Lấy list2 của người dùng nhập vào là danh sách
list2 = list(input("Please Enter Elements of list2: "))


# Kết hợp list2 thành list1 bằng hàm .append function
for i in list2:
list1.append(i)

# in list1
print(list1)

Result:

Examples of Input functions in Excel

See also: Python functions are built

Maybe you know what Input () is in Python, right? As you can see, the way it uses it is not too difficult. Just grasp the basic syntax and appropriate application, you can master this Python function proficiently.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments