Thank you Derek for such an amazing tutorial. However, I've realized that the outer while loop should have been while i > 0 and not while i > 1 because it still has to check for the last time, if you use > 1 there are some values that return the value in index 0 greater than the one in index 1
i am a recent graduate in programming, i remember doing the bubble sort in visual basic. syntax is a little different but logic is the same. great video!
Can't wait to dig into this series. I would be really interested in anything re: FRP, Elm or any of the others would be cool. Also, just want to say that my recent interviews have been going really well, the information you provide has helped me a ton. Looking forward to becoming a patron when I'm able.
Your videos are priceless, Trust me i am a construction engineer and i knew nothing about coding, i am learning from u and edx and that's all. I have a goal of building applications, data analysis and later ML and ultimately AI...... so big plans and u are helping me everyday, I wish i could say u how grateful i am.. Thanks again.
Great video. There is a way to improve the Bubble sort algorithm, sometimes the list or the array is already sorted so it would be unnecessary to look for elements that are not sorted, again. So we could break the loop and improve it a little bit even though the algorithm is much slower than others.
Thank you :) Very soon we'll start solving problems similar to the Project Euler problems. By the end anyone that sticks with the tutorial should be quite good at solving programming problems.
List Comprehension for the Win! I played around with using list comprehension and was able to build the enter multiplication table in a single statement: multiTable = [[(i+1) * (j+1) for j in range(9)] for i in range(9)] The print statement does not appear to support list comprehension (or, at least I haven't figured out how to use it).
Awesome Derek ! Suggestion: You could cover Algorithms and Data Structures you didn't do with Java such as: Graphs - I've read that Google and Facebook - at least in their early days were based off of graph data structures -That would be interesting. Thanks In Advance !
question to the random list: i printed the list for range of 100 values and noticed that each time the randomly generated number was already present in the list the position returned was related to the position for which the number was assigned for the first time. What if I really want each new random number also having a unique position in the list?
Thanks! Great video, as usual. Here's a version that cleans up the output: mt = [[i * x for i in range(1, 10)] for x in range(1, 10)] for group in mt: s = "" # empty string for i in group: s = '{} {:>2s}'.format(s, str(i)) print(s)
Hi Derek I've a little problem! So i was writing a code and i wanted to compare each of the values in my list with 2 but i keep getting error. This is what I did:num=[ input ("Enter a value:") for i in range (10)] for i in range (10): If num [0] < 2: print ("Not prime") else: ....i keep having an error message at: if num [0] < 2:Also, how do i create a class then declare a variable of type that class and make it a list?well in algorithm it's something like: Structure Student name: integer; Sex: char; Mark: real;Algorithm Student_Info Var S: Student [50];
Hey Derek, here's a weird glitch for you. I like to import math as m so I don't have to write out math, all the time, but still keep from cluttering up my namespace with all the math functions. Works fine most of the time, however, in the comprehension it was confusing your m with my m. somehow. I fixed the problem by changing your m to an n, then everything worked fine. I don't have any clue as to why it did that, but whatever works, I say!
hey Derek! for the last question why do you need to create a 10 by 10 list when you only use up 9 by 9 spaces? This is my code: mul_table = [[0] * 9 for i in range(9)] for i in range(1, 10): for j in range(1, 10): mul_table[i][j] = '{} * {} = {}'.format(i, j, i * j) for i in range(1, 10): for j in range(1, 10): print(mul_table[i][j], end='|') print() when I run the code I get this error 'IndexError: list assignment index out of range'
I'm just putting this out there as another way of doing it. This was my way of creating a list of 9 lists of 9 elements as the multiplying table required,instead of a ten by ten list which doesn't use the first list (list number 0 at all and neither the first element (number 0) of each list) list=[[0]*9 for i in range(9)] for i in range(0,9): for j in range(0,9): list[i][j]=(i+1)*(j+1) print(list[i][j],end=",") print() print(list)
Good day sir, I'm confused in the multi-dimensional list. if i initialized: multiDList = [[0]*4 for i in range(4)] this will create a 5x5 list with an initial value of 0. | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 | 0 | what if I want to create a 5 rows with 3 columns each? | 0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 | is it [[0]*2 for i in range(4)] or [[0]*4 for i in range(2)]
Hi, computer starts counting from 0... so when you multiply the list by 4 ( you have 5 columns (0,1,2,3,4) )... same for the in range command! I hope I help you, even after some months lol
In [[0]*4 for i in range(4)] [0]*4 is saying that the length of your list is 4... and in range(4), your are going to create 4 rows :) ... so it would be a 4x4 matrix. For 5 rows and 3 columns you should use: multiDList = [[0]*3 for i in range(5)]
This code below will output the same result as the one you shown. Is there any reason to have 2 for loops instead of 1 as shown below? Thanks! :) ---------------------------------------------------------------- multTable = [[0] * 10 for i in range(10)] for i in range(1, 10): for j in range(1, 10): multTable[i][j] = i * j print(multTable[i][j], end=", ") print()
Bubble sort: for i in range(0, len(randomList) - 1): for j in range(0, len(randomList) - 1): if randomList[j] > randomList[j+1]: randomList[j], randomList[j+1] = randomList[j + 1], randomList[j]
// just creating multipliction tables for i in range(1, 11): print("{}'s Multiplication table. ".format(i)) for j in range(1, 11): print("{} * {} = {}".format(i, j, i*j)) print("-"*15) //Your wanted way table = [[0 for i in range(11)] for j in range(11)] for i in range(1, 11): for j in range(1, 11): print(i*j, end=", ") table[i][j] = i*j print()
Thank you so much for all these tutorials! You truly have a gift for explaining difficult things in an easy way. i see some 'incl me' had some difficulty understanding the part of: [[0] * 10 for i in range(10)] Hope i can breakdown what exactly is happening. If i understand it correctly, then derek has generated the following 10x10 multitable at the end of this tutorial: (you just don't see all the zero's because he adjusted and printed from [1,1] and up, instead of [0,0] and up. So it looks like a 9x9) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, If you truly want to make the 9x9 table, then i think the code below might also work. Notice that in this case it will start in the [0,0] 'which derek showed in previous example is the very first box in the top left corner' and it wil put the number (0+1)*(0+1) = 1. which is exactly what we want for our first number. Hope i make sense and explained it correctly. multiDlijst = [[0]*9 for i in range(9)] for i in range(9): for j in range(9): multiDlijst[i][j] = (i+1)*(j+1) print(multiDlijst[i][j], end = ", ") print()
Hi Derek, Hi All, What is the point of using the list() function with range when creating a list? The range function in itself creates a list right? So what is the difference between oneToTen = range(10) and oneToTen = list(range(10))? Thank you!
In case anyone comes across this same question: the difference is that range(10) is a object of the range class, and the list(range(10)) is an object of the list class. So they have a different class type. This is critical when you for example are trying to access functions that are inside the list class (for example the insert function).
for others that like lists to be left to right: for i in numList: pass print(numList) and for the bubble sort, i like to have the starting random list: print(" random list:", numList)
Bubble sort in simple "" Bubble Sort Algorithm @:param unsorted List @:return sorted List """ def bubbleSort(inpList): for i in reversed(range(len(inpList))): # Counting in reversed order for the outer loop for j in range(i): # Inner loop counting if inpList[j] > inpList[j + 1]: temp = inpList[j + 1] # storing a temp var inpList[j + 1] = inpList[j] # replacing with the bigger one inpList[j] = temp # implementing the temp var return inpList
Hey Derek! I have a question pertaining to lists, I want to make a database of sorts (I know MySQL is made for them but I want to try with python). So far what I did was collect user input and add it to list, however it doesn't save. Here is my code, all I could think of was use a library but I was curious if I could do it without a library. def main(): a = raw_input("Input something to add to list: ") dlist = [] dlist.append(a) print dlist while True: main() It adds variable a to the list but then it removes it every time, I'm trying to make it so that a would be permanently added to the list every time. Thank you!
Nice, another one . I recently started learning programming and started with java, but after a while it was a bit confusing. Then i moved to these tutorials and it's going better. Thanks for making them. Btw to anyone who sees this: should I upgrade to windows 10?. I heard there were some issues so i didn't upgrade because I am fine with 8.1, but now there is not much time left so I'm thinking about upgrading. Is it better? And are there any real issues?
Not that it is important at all but you did a small mistake with your BubbleSort as you can see with the result at 12:45 The outer loop should go while i > 0 and not i > 1, because when the last round starts with i = 2, j swaps the first two and then the third with the second but it can still occur that we would have to swap the first two again afterwards
Hey Derek in the last example maybe it is for ease of understanding and avoid summing 1 to i and j in the loop, but aren't you really creating a table with 10 lines and 10 rows when we only need 9 and ignoring the first line and the first row when filling it and printing it when in reallity we are creating and filling something like: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, Sorry if i'm missing something or if i'm not being clear about what i'm talking about. Cheers and thanks a lot for all these videos
Hi Derek ! First of all, thanks a lot for your knowledge ! Second: The language of Shakespeare is not my mother tong so … apologies ^^ But I have a little problem here : I try to know how many times (how many loops it'll need to do) will it take for the computer to make me a numlist like that : [1,2,3,4,5] with the random of course. My code : import random import math numlist = [] i = 0 while (numlist != [1,2,3,4,5]): for j in range(5): numlist.append(random.randrange(1,5)) i +=1 print(i, numlist) And if I put the "print(i, numlist)" in the while loop to see what's happening, I see that the numlist become too big. I tried also to find a shorter result, like numlist = [1], but one time it works, and the second it's crazy ^^ I hope that you'll anderstand it all ^^ Thx
Hi, You're very welcome. This program will create a list of 5 values that are random between the values of 1 and 5. I hope it helps. import random numlist = [] # Cycle through each box in the list for i in range(5): # Generate a random value between 1 and 5 # and assign it to the current box in the list numlist.append(random.randint(1, 6)) print(numlist)
That part I get it, what I want is : when the program create randomly a list with 5 numbers, from 1 to 5, and it will return for example : [1,4,5,3,3], [1,2,2,4,5] … but how many time do I have to run the program to get exactly [1,2,3,4,5] ? And I want to put the "How many time" into the "i". That's the goal of my program. Beter ? Thx again :)
Hello Derek. I don't understand why we need this part : while i > 1 ? What does it do and why we need to decrement it by 1? BTW i really appreciate your videos! It helps me a lot.
---Please note, there's an error in the code.--- Hi Derek! Thank you for your videos! They are awesome. I believe you have an error in your code. On 13:03 you can see that the list is not fully sorted (i.e '2' and '1' should switch places) The reason is 'while i > 1' should be 'while i > 0' on line 11. I think it would be helpful if you put some annotation-rectangular message about this. Thanks again!:)
@derekbanas You could also do this? import random import math for i in range (5): randList =[] randList.append(random.randint(0,10)) print(randList[0]) ------------- MultTable = [[1] * 10 for i in range(10)] for i in range(1,10): for j in range(1,10): MultTable[i][j] = "{} x {}".format(i,j) if(j==1): print () print("-------Table of "+str(i)+"--------") print () print(MultTable[i][j]+" = "+str(i*j),)
My multidimensional list :) j = 1 MultList = [i * j for i in range(1, 10)] for i in range(1, 10): for j in range(1, 10): MultList = i * j print(MultList, end=", ") print()
Last Problem is a lot easier with inline for loops, although it might be easier to understand the way you did it;) list = [[i * j for j in range(1,10)] for i in range(1,10)] print(list) Also, why did you not say anything about the way you can simply call print(list)? To explain lists it's obviously beneficial to use for loops over the elements, but in general one should simply use print(list) if possible
Yes I did it the way I did to make the code more understandable. Teaching problem solving is my main goal. Sorry I didn't mention you can print lists with print(list). I'll show that in the next video. Thanks for the input :)
have you tried running this a couple dozen times ? Sometimes (very rarely) it gives stuff like : Switch 3, 1, 3, 4, 6, END OF ROUND 3, 1, 3, 4, 6, And that's where the program ends. Is this supposed to happen (I don't think so)
Dear Derek, is it cool if I make notes about this set of tutorials in a github repo, of course giving you all due credit and pointing to the original source? I'm trying to document my progress...
Creating a multidimensional array with 4 columns and 3 rows in which all elements are arbritary multidarray = [[int(random()*10) for i in range(4)] for j in range(3)]
Hi Derek, i am wondering why this doesn't work. could you please help me? Thankyou randlist =[] for i in range(5): randlist[i]= random.randrange(1,9) print(randlist)
I got a little confuse with the list of list and overall when the list part began. What does the j and i represent on multTable[i][j]? Is i the index and j the value? Although I didn't understand very well I got the result of the last problem right with this code: multi_table = [[0] * 10 for i in range(10)] for i in range(1, 10): for j in range(1, 10): k = j * (i + 1) multi_table[i][j] = "{}".format(k) for i in range(10): for j in range(10): print(multi_table[i][j], end=" ") print() but since you didn't use k and just did i * j can you explain me what am I understanding wrong here pls. Thanks, your tutorials are amazing xD!
The i represents each row and the j represents the column of each row. This allows use to store information in a grid or matrix. I'm using k to store the value of the multiplication. I then store it in the grid.
I will completely cover solving programming problems while also covering Python to the extent that people will be able to do most anything with it. It is going to be a long tutorial series.
Solution to multi dimension list using list comprehensions- y = list(range(1,10)) multi_list = [[row*p for p in range(1,10) ] for row in y] multi_list #Display the multi dimensional list
here is my solution to the multiplication table problem utilizing list comprehension: values = list(range(1, 11)) multiplicationTable = [[i * j for j in values] for i in values]
+Derek Banas I got you beat Derek, well age wise not in years of experience...Plus, I am pretty sure yoy could program circles around me. Anyway, great video! Your videos are always clear, concise, and consistently of high quality. Anytime some one asks me anything along the lines of how do they get started programing i say go to your channel. Thx for all your awesome videos. keep it up :)
Thank you for the very nice compliments :) Back in my day we saved code on cassette tapes, there were 3 books on programming at the library and Zork was my favorite game. I am so old.
Next is recursive functions, files and then oop. The OOP tutorial will probably be a multipart tutorial. I'll upload a new one every Wednesday and Saturday.
Sir, i am not able to create logic for programs how can i be able to do that and sometimes when you are writing the programs, i am getting confused in that also, just sometimes not everytime and sir really your way of teaching is very good thanks for your tutorials sir
Sir, by logic i mean solutions like how can i do bubble sort without using the sort function (there may be a mathematical equation for that) so how can i do that. means like to print the prime numbers you designed a program so i was confused that how does it work you entered some mathematical equations. in that. Sir, should i be good in mathematics to be good at programming
Hey Pavaan. I've seen your posts in this playlist a couple of times. The best advice I can give is to practice very simple problems. 1. Take notes on any new function in a lesson like .isdigit(), or .count() and try to use them on your own. 2. Next, try to use that function in a loop. Any kind of loop and print() every variable in every step. (Printing out variables and iterations helped me out a lot when i started. I'm still a beginner, but I'm making progress.) Also, Derek moves quickly. If you don't fully understand what's happened, that's completely understandable. 3. Pause the video and click on his transcript link, in the description. Stare at the code and talk yourself through the steps in your own words. For instance what is 'i' during this loop? what number is it? what is it keeping track of? I, myself, didn't understand all of the pieces to the bubble sort, so I'm going to look through the transcript and work through it step by step before moving on. Good luck
its like this in every row multiply a column witch has a value of 0 10 times which means creates a 10 column in every row with the value of zero u see he is not multiplying the zero itself he is creating a 10 small lists inside a bigger list which u can call a row thats how i understand it maybe im wrong or my explanation is some what complex .
if i understand it correctly, then derek has generated the following 10x10 multitable at the end of this tutorial: you just don't see all the zero's because he adjusted and printed from [1,1] and up, instead of [0,0] and up. So it looks like a 9x9. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, if you truly want to make the 9x9 table, then i think the code below might also work. Notice that in this case it will start in the [0,0] 'which derek showed in previous example is the very first box in the top left corner' and it wil put the number (0+1)*(0+1) = 1. which is exactly what we want for our first number. Hope i make sense and explained it correctly. multiDlijst = [[0]*9 for i in range(9)] for i in range(9): for j in range(9): multiDlijst[i][j] = (i+1)*(j+1) print(multiDlijst[i][j], end = ", ") print()
Hi Niels, This is what was bothering me that he had extra 0s. What I ended up doing, very similarly to yours: listOfValues = [[] for i in range(9)] for i in range(9): listOfValues[i] = [(i+1)*j for j in range(1,10)] Then you can print out or w.e.