#23 Python Tutorial for Beginners | Printing Patterns in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Devin Wendy and let's continue with the series on Python now at this point we have talked about loops while loop for loop and then do those beautiful keywords and in this video we'll draw some patterns okay normally if you are in college or if you are learning a programming this is important because it will improve the way you think programming is all about solving a problem right so you have to understand the problem statement and you have to put that in a code so mentally in your mind you'll be having those solutions ready but that will be in an abstract format you have to implement that on your machine if the above code so what we'll do is we'll try to do some patterns so that it will improve the way you think about about logic so what I do is initially we'll do this in fact yeah I know this is the assignment question you had in the last week last last video so we want to print this pattern of course everyone have their own way of doing this and what you quote you can also simply do is you can print this multiple hash multiple times right so I want to print this so what I will do is I will simply so you can't do that it's very simple just print hash hash hash hash four times and then you can do the same every language do that here okay and then you can so by doing this you can see we got fold hash right so at least we got the first stuff but then what if I say you cannot use you can only use one hash in one print of course you can you can draw the entire box using food print statement but let's say if you want to print only one hash within one print so what I want is I don't want to have multiple hash I just want to have one hash because if another is code now you can see we got one hash I want to do at least so we will first complete the first row now you can see we have for those and four columns right so let's complete the first row now if you want to copy the first row you will print this multiple times but you will write print four times okay let's do that so I will just pop it and we'll paste it here of a message here with bestit here so we got four prints right and if I on this code or we got food has but then if we want it in row format right not in vertical format I want horizontal format so it is happening here is after every period it is coming on new line so solution would be we can give a comma and say hey don't just come on new line so we can say end and we can do the same thing for all the statement it means do it here and now if I don't this code you can see we got fold hash in white line so this is what we got we got the first block now what I want is I want four blocks right in fact there is one problem with this for as well we are reading the same state in four times don't you think it is weird so the best option here would be I want to do the same thing multiple times right so we can use a loop and I will be using a loop here which is a for loop so I will say for reduce J and you can use any variable name right we are discussed about that but let's use J here so for J in I want to have a range of let's say full now when you say danger for it will start from 0 and it will end at 3 right I will give a : and neither this is what the same output right so instead of typing four times we can just use a loop let's say so we got first row right I want the second row as well so we can just copy this code because this will generate the second row paste so we are writing a for loop two times so one for the first row and second for the first second row and if I love this code now you can see regard oh we got eight hash but then in one line the thing is we need to make sure that before second for loop I want to come on new line right let's run this or we got it so we got regard for those or two rows or columns okay this works in fact if I do the same thing for third time we will need to print a print as well the new line so that so so that will come a new line right so I find this code you can see we caught three times if I do the same thing we caught four times but then there is a issue here don't you think we are doing same thing repeatedly and then we know that if you do the same thing repeatedly well you eat only once but then we will use a loop again so the entire block then you just give a tab the end a block will come under another loop which is in this case I use a for loop I in range of fold so the entire block is a part of the another loop right so I find this code now to get this it's so simple right so this is how you learn about programming this how you learn about different logic so what we are doing is we are printing hash but multiple times in total we are printing hash steal time you can see this loop runs 16 times they just do it so apply the breakpoint here what's a debug and let's iterate will say f8 you can see the value of I is 0 the value of J is 0 and then it will print hash and you can see in the console we got the value 1 hash then the same loop will iterate so the value of J becomes 1 it will print hash the value of J becomes 2 it will print hash the value of J becomes 3 it will print hash I know this is the fourth time right so it will go for if we got the fourth hash now it will jump outside the loop and in fact it will come on newline first you can see it is it will jump a newline now and then the value of I becomes one yeah and then divide then the J value becomes 0 2 again it will start from 0 to 3 that's how it goes and then we got four hash and thankfully it was a new line and it will jump out so these are this is how thing works right it's so simple okay now once we have done this what if I want this pattern now so in this you can see we don't have the perfect square we have a triangle shape right so how would I do triangle now if you concentrate here look at the number of rows and columns because in the earlier version where you have the entire box we were having for those and four columns so we can say this J represents the number of column you have and I represent another for those you have right because J is responsible to print the column at the end so now here is its triangle you can see the number of columns in a particular row is depend upon the door number right so in the first row we have only one hash in the second row we have two hash right so so the number of hash is depend upon the door number right so what you can do is you can control this range so we have to say hey don't go till four because if it go till four it will it will print for hash I want to go till I okay so I will go to the I if I have this code oh we got a triangle okay because now we are not going at the end we are going till only till I so when the value of I is zero you can see it is not printing any hash this is output for that when you either al you is one is spending one hash when the value of I is 2 it is spending two hash right but then this is something weird ok we wanted for the four hash at the end the problem is the value of I starts with 0 right so we will go for one so it should be I plus one so when your high value is zero it is actually first I want one so you can see we got four hash now this is what we wanted so this was the earlier title we had and now I want to go for the next button which is this one now you can see this is also triangle but in another way what is different here is I'm printing the same triangle but in reverse order right the number of flows okay so in the first row we have four in the second row we have three in the third row we have two and then it ends with one that means as your row number increases your number of hash decreases right so what if you can say 4 minus I now you'll be thinking what is this think about this when your value is zero in that case the j will go till four right it will print for hash when your I value is 2 the value for J will be three right it will print three hash so depend upon your I value I just painting those above hash right so if I run this code you can see this is what you got right so this is how the thing works here so this is our the another pattern we have worked with I hope you enjoying the city let me know in the comment section and do subscribe for further videos you
Info
Channel: Telusko
Views: 731,906
Rating: 4.9096656 out of 5
Keywords: telusko, navin, reddy, tutorial, python, for, while, loop, pattern, patterns, print
Id: k8SXsT5TLxQ
Channel Id: undefined
Length: 7min 56sec (476 seconds)
Published: Sun Jul 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.