Jul 15, 2024
input() always returns user input as a string.numbers = input("Enter a list of numbers")
"67 48 90"[67, 48, 90] (with indices 0, 1, 2)['6', '7', ' ', '4', '8', ' ', '9', '0'] (8-character string list)Receive the Number of Elements
n = int(input("Enter the number of elements"))
Create an Empty List
numbers = []
Use a For Loop to Receive Individual Items
for i in range(n):
x = int(input())
numbers.append(x)
range(n): Loop runs n times.int(input()): Typecast input to integer and store in x.numbers.append(x): Add x to the numbers list.Prompt and Receive Number of Elements
3n now points to 3 (integer).Create Empty List
numbers = []For Loop Execution
67x points to 67numbers.append(x) adds 67 to numbers list. List now: [67].48x points to 48numbers.append(x) adds 48 to numbers. List now: [67, 48].90x points to 90numbers.append(x) adds 90 to numbers. List now: [67, 48, 90].Check the Final List
print(numbers)
[67, 48, 90]