Input a List using Loops in Python
Вставка
- Опубліковано 24 сер 2024
- Python Programming: Input a List using Loops in Python
Topics discussed:
1. Problem associated with the input() Method.
2. Input a List using Loops.
Python Programming Playlist: • Python Programming
Follow Neso Academy on Instagram: @nesoacademy (bit.ly/2XP63OE)
Contribute: www.nesoacadem...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacadem...
Website ► www.nesoacadem...
Forum ► forum.nesoacad...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#PythonByNeso #PythonProgramming #ListsInPython
Great video sir.
You are a life saver. Thanks ❤
Clear explanation.
It was really helpful
Thank you
Thank you for your class sir
This can also be achieved using below code for storing items as string not int.
items = input("Enter multiple items: ")
list = items.split()
print(list)
Tems??
No its still a string..
Python3IDE(Python 3.7) running!
enter the 3 digit number234
['234']
Pytho3IDE run end!
@Neso academy Great video , it helps me a lot as usual but what about if we want a list which is a collection of different types of items ? because with the example in the course all the items of our list must be an integer
Sir, instead of using for loop to input a list we can use split function...
numbers = input().split()
n=int(input("enter the number of elements of your list: ")) #input data from the user and convert it to integer type
numbers=[] #create an empty list
for i in range(n): #iterate my list from index 0 to n-1 and repeat the block of instructions below for each i
x = int(input(f"enter the elements for numbers {[i]}: ")) #store the elements of the list from user in x variable
numbers.append(x) #add each element at the end of the list numbers
print(numbers) #output the new list
I don't get where the i came from in for loop
@@user-ev5fj1rm3ywe just take a variable i to loop thru.. in this case for loop
NICE!!
what about this code ?
numbers = [ ]
for i in range(3):
x = input('Enter the element: ')
numbers.append(x)
print(numbers)
The input function always returns a value as a string. If we are to obtain numbers then we'll have to convert the type of the obtained item to integer (int) or float.
we can also do,
numbers = [ ]
for i in range (3):
x = input()
y = int(x)
numbers.append(y)
The method in the video is for getting numbers as input and type is being converted in the same line in which input is being taken.
sir i have a doubt
can't we directly use eval function for entering the list
sir i had tried it and also work so why to use loop method
Thanks for the vid. Just a suggestion… what if you need to input hundreds of numbers? The user is not going to want take a count of each number and possibly miscount.
Might be better just to input numbers and type END when finished.
I guess using while loop would be better then
Please use an ide... Command prompt sucks
We started using VS Code after covering the basics.
❤❤❤🎉