#27 Python Tutorial for Beginners | Array values from User in Python | Search in Array

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan Wendy and let's continue this series on Python so till this point we have seen some basics on Python and we have talked about a day as well but we want to do one more example here so the previous assignment was about taking the values from the user and that's where the problem starts right how can you create an array where the values will be coming from the user because when you say you your values will be coming from the user you don't the size of the array so if you don't know the size of the array how can you create the adding right so in this example we'll do that so we'll create an array a blank array and then we will ask a user to enter the values and at the end we'll ask a user hey you want to search the element and if I user says ok I want to search for the element and then let's say if I use say say I want to search for number 5 and tell me the index value of it we'll be doing that example here what I'm going to do here is very simple we want to take the we want to get an array and we want to take the values from the user so step one we need to get an array right so I would say a our R is equal to now this a our I want it to be a blank added and the way you do that is by saying array and then of course right when you work with a day you need to use you need to mention the type of the add type of the values right so you will say I and you will give a comma here and then you have to mention the adding values between but then we want empty and right so we'll give a square bracket so this is how you create an empty array now we have one small issue we are using added but we have before word to import the array module so we'll say import or not input will say from add a import I want to input everything so I would say start right so I want all the classes all the functions all the variables will say start now once you've got the add a I want to add the elements so when you say you want to add the elements you will be asking users to enter value repeatedly right so when you say we'll say hey user entered next value enter next value right then next value but how many times you have to say that so before asking for the values we will ask for the length of the array so we'll say hey hey user how many values you need so user says hey I want I want to insert five values let's do that so we'll say n and in this I want to ask for the length of the added okay so user will enter the value we have to if you have to use input function but input will give you strings so we will convert that into integer so we'll say int and here we'll say input and in this bracket you have to mention the you have to mention the message right so I would say enter the length of the ADI right now once you got the length now the next step is I want to ask the user here user and that the body you want to insert but then I want to do that repeatedly right so I have to so if I use a say say I want to insert five values I have to ask for the element five times now of course when you say you want to do something repeatedly you have to use a for loop right of course while loop will do here but I will use a for loop and I will use a variable I I in now when you say you want to go for five times the best ways to go for range and the range would be five will give a colon and enter now every time this loop runs I want to ask a user to enter the value and I will put that value in a variable X right so it's a X equal to the way you can ask a value is very simple I get the same thing you need to use this function I will just copy and paste it here now instead of saying enter the length of the array we will change the message I will say enter the value right or we can say enter the next value this makes sense right so every time I user enters the value that will go into X and the same value X will go into array now and the way you do that is by saying hey are out dot how do we add by this in the addy so for that we have a function called as a pendant right so we'll use append here and every time you want to add the element just pass X and your job is done so your loop iterates and you will put the element in the array now how would you verify this so we will go at the end and here will print the values of adding so we can drag the printed we can say print and so this is one way you can dial it from the array or you can use a for loop right your choice I will just run this code I hope it will work again let me just 25 if I say F shift f10 you can see it is asking for the length I would say okay I want to insert for Banjos okay now it is asking for the next value so I would say this is 16 or the next value is lesser 20 and then we got for the age we got 21 and we got let's say then so this is this is these are my four values right oh it is going five times that's bad we've we said four likewise doing five times because I made a small mistake it or we ignore something my bad instead of saying range five I should I should have seen n right because we are asking for the length my bad okay let's do that again asking for the length how is a fold when to the value of 16 2014 52 you can see I've entered four values some of it has to enter we got four values right so that's your daddy and you that's how you can take them from the user so every time a user enters the value you just have to append it to the array okay this works now what ever do is I want to ask a user to enter a value and then we will search for the index example if a user says value 14 then we need to print the index number of it which is in this case 2 right now there are two ways of doing this one manually and second by using some inbuilt functions now what do you think we should be going for manual first right so we will do it manually first and then we'll see which function we can use to fetch it so how do you do it manually now when you say you want to get the index number what you could do is let's say if a user says in value 14 so you have to check you to compare is 14 is same as the first value of a day every same you got the index number if it is not same shift just check for the second value is it is it matching no then checked I mean next one which is 14 and 14 matching yes so you got the answer right so every time you you run to the loop you have to increment the value right and that will be our index number so what I'm saying is just take a fault though in fact first of all we have to ask a user to enter a value right so we'll say we'll say a value well itself and we'll ask a user to enter the values I will use the same I would say enter the value for search right this is what we will print will be asking the user and with the government user enters a value that will go into value now we will ask you we will run a loop we'll say I in array the moment you iterate this loop you will check if I is equal to equal to the value which you have now said this I is the value of this I is coming from a range right so the range would be 0 1 2 3 4 but this I hear is not coming from the range the value is coming from the attic itself I know the we have the same name so we'll say II okay stop saying I will say because this have a represent right so I we can take it as a it crater or the counter variable and we will just say E is an element right so so value of e will be coming from the ad itself one one element so we are comparing if this e is matching with the valve in case if it is matching we will print the index number right so oh how do we know the index number here that's a confusion right now to maintain the index number we will use a counter variable with say K the initial value of K would be zero right okay we'll start with zero and every time this loop iterates we will increment the value of K so it's a k plus is equal to 1 right so we are incrementing it but what if it is matching in that case you will print the value of K that's it so even if it is matching but simply say break because we don't want to search ahead right if this matching that's it that that's the end so it's a break now I hope it will work let's verify I would say okay so what is happening once again as you are using a loop and the value of e it will be coming from the area itself now if that is matching with the value which I use I inserted you will print the index number right we just key in this case and you will say break let's run this code and you can see it is asking for the length again I will save for and I went to the value which is 14 or 15 to 98 36 and you can see we have entered four values and that's your added there now it is asking Q for the value which you want to search for I want to search let's say 52 now it does say enter you can see it says 1 because that's an index number for 52 right so this is our you can work with it right it's very simple you can try it out what I would suggest you is after watching this video don't like test that but that way by watching the video after finishing watching the video okay you can pause it and try it by yourself don't look at the code because when you look at the code you you you feel it's this simple but when you try it by yourself that's where the problem starts right so try it out let me know in the comment section how it is going so that's how you insert the value in the a day that's how you search for it oh we are missing one more thing right use functions here my bad if you want to use function you don't have to do anything like this you simply say print and in this print you will use an array which you will use a D and you will say a metric or less index and just pass the value which is well you know I don't have to do anything else you don't have to use this loop stuff you can simply say hey we have an array of 5 let's insert the values some bigger values this time okay so you can see I've inserted this values and now it is asking you for the value which you want to search I want to search for 587 right which is index number 3 the moment I say enter you can see record 3 and 3 so we got the first 3 because of the loop we caught second 3 because of this method because this function now that's the advantage you know so when you work with Python because so many functions to work with and it saves your time but knowing how to search manually it will be helpful if you want to get your own algorithm in future so that's it I hope you enjoyed this video let me in the comment section if you have any more questions make sure that you post those questions as well do like the video because that's what motivates me and if you have not subscribed to now do subscribe it so thanks for watching everyone bye bye you
Info
Channel: Telusko
Views: 508,942
Rating: 4.9600806 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, accept, print, array, value, number, user, store, create, blank array, length, array length, index, array index, function
Id: 9c9qhIcB3NA
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Fri Jul 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.