+ 1
For what this is used for?
input()
5 Antworten
+ 1
This is use for taking input from user.
+ 1
lst = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
ele = int(input())
lst.append(ele)
print(lst)
0
In this
0
How is it works
0
Basically, the python code that you have provided is for inputting some elements into a list and then printing out this list.
However, since 'int()' is present, the list is basically restricted to integers only while strings will yield an error and floats will be rounded to the nearest integer.
Let me explain the usage of input() in your code:
### Python code ###
lst = [] #<------ the list where you're going to append the integers.
#Now, you are prompting the user to enter the number of digits he
#wants to add to the list.
n = int(input("Enter number of elements : "))
#Then, using the loop below, the user is again prompted to enter
#integer value 'n times'(valued entered above) and then, this is
# added to the list, lst.
for i in range(0, n):
ele = int(input())
lst.append(ele)
#Finally, you print whatever has been input by the user in the
#form of a list
print(lst)
By the way, he is an elegant python one-liner for your code:
print([int(input()) for i in range(int(input()))])
Cheers