Python for Beginners, Part 15: Solution Circle of Squares (Exercise)

Written by cleverprogrammer | Published 2022/04/08
Tech Story Tags: python | python-programming | learn-to-code | tutorial-for-beginners | clever-programmer | learn-python-programming | python-basics | youtube-transcripts

TLDRUp until now in the series, we have talked about downloading and installing python, what are: variables, strings, functions, loops, primitive data types, lists list-method etc. And in the last video, I gave you all an exercise, viz., making a circle of squares, to do on your own, hopefully you've completed it and so now let's get in and look at the solution for it.via the TL;DR App

Let's go over the solution of how to create a circle out of squares.
In case you missed it, here are the previous parts of the series:

Transcript

0:00 okay in the last video I proposed a
0:01 challenge to you make a square out make
0:03 a circle out of squares so hopefully you
0:06 give it a try
0:07 hopefully you've put it in the comments
0:10 below, okay so if you're watching this
0:11 video right now whatever you got I don't
0:13 care if it's crap I don't care what
0:15 it is I want you to put it in the
0:17 comments below
0:18 okay so whether you go
0:23 to my website at clever programmer comm
0:27 and put it in learn Python for beginners
0:31 we have a comment section so for example
0:33 you can go into one of these videos I'm
0:37 gonna have this video up there as well
0:39 when you guys come to watch I want you
0:41 to put your code in the comments below I
0:42 will look at it personally and one of
0:45 our members will also look at it okay
0:47 so right over here or you can just put
0:50 it on the YouTube channel that's totally
0:52 fine as well
0:53 alright so let's get to it first of all
0:57 what I want to do is I have the square
1:00 function which is really cool but it's
1:03 really repetitive right it does um
1:07 I mean if you can see all all a square
1:09 is is you move forward and you turn
1:12 right and you repeat that four times
1:13 right so why do we have it here like
1:16 this this is really bad um there's a lot
1:21 of redundancy in our logic right um like
1:26 if you were telling your sister your
1:29 brother your dad your friend whoever D
1:31 steps you would be like move forward
1:32 turn right and then do that four times
1:34 that's how you would tell it to them
1:36 okay
1:36 you wouldn't say move forward turn right
1:38 move forward turn right ah move forward
1:43 and then turn right and oh yeah move
1:46 forward and turn right right so you
1:47 wouldn't do that so if we look at this
1:49 actually we're doing the step once here
1:52 then we're doing the same step twice
1:55 here then we're doing the same step
1:58 three times here and we're doing the
2:01 same step four times here what does that
2:05 mean when we're doing something over and
2:07 over again well it sounds like a loop so
2:11 I want this thing to happen for
2:13 how do I sit do something in four times
2:16 in Python I just say for something in
2:18 range so I'll say for I in range for
2:20 that's it this will do that thing four
2:24 times and I'll simply go here and remove
2:27 that code
2:28 okay so already our code looks much
2:33 nicer and that's a way better way of
2:36 making a square okay so notice it still
2:41 takes length and angle so if we're
2:44 making a function we need to pass those
2:45 variables length and angle right so then
2:48 length here and angle here can become
2:50 those so we want like let's say length
2:52 to be 100 and our angle to be 90 degrees
2:55 so square makes a square let's verify
2:59 our hypothesis here and remember this
3:02 function all functions you got to put
3:05 open closed parenthesis after them and
3:07 you got to pass in an argument for this
3:10 function here so we're gonna say 100 for
3:12 the length and I'm gonna say 90 for the
3:14 angle okay I can also say it like this
3:17 just so the code is you know more clear
3:20 what it's doing okay I can say it like
3:25 that but let's keep it keep it short and
3:27 sweet here let's run it and that should
3:31 make a square right it made a square
3:33 great mmm
3:35 now what we're going to do is well we
3:38 want to keep making squares right so we
3:41 know we're going to put it in some kind
3:42 of loop let's say we want to do it 100
3:45 times okay
3:49 alright so I'm gonna put it in the loop
3:51 because I want to make a lot of squares
3:54 and each time I make a square I want to
3:57 turn so let me first get out of this
3:59 loop and let me do this let me say my
4:03 turtle dot right ninety degrees okay and
4:08 let me do this now and you'll see that
4:12 as soon as I make that I turn a turn a
4:14 little bit actually not ninety degrees
4:16 let's turn I don't know ten degrees 90
4:21 degrees is silly so I make a square and
4:23 then I turn just like that I'm ready to
4:25 make another square so let's do that
4:27 let's make another square right here I
4:32 make a square I turn a little bit I make
4:34 another square okay you get the idea now
4:37 we need this process to repeat a lot of
4:39 times so we need to make a square and we
4:41 need to turn right right so it's really
4:42 this step and we want to do it again and
4:46 again and again and again so sounds like
4:48 we need to put it in a loop let me
4:50 uncomment that line hit tab to indent it
4:54 under the for loop so it knows it's a
4:56 part of a for loop this variable here
4:59 does not need to be called I by
5:00 convention in the world of Python we
5:03 usually call it I you call it bananas
5:06 you can call it that you can call it an
5:07 underscore you can call it whatever you
5:09 want okay just a little bit about
5:12 variables variables can't start with the
5:14 number like five or something okay
5:17 so perfect this looks pretty good let's
5:21 give it a try okay I make a square I
5:26 turn a little bit I make another square
5:28 I turn a little bit I make another
5:29 square I turn a little bit beautiful
5:31 that's looking really good let me close
5:34 this let me make the speed really fast
5:36 so let me make this seis turtle that's
5:39 speed zero and that makes it run really
5:41 really fast so it's going and going and
5:46 going and going and going and going and
5:49 going
5:49 but now notice the problem it's not
5:54 drawing anything new but it's moving hmm
5:59 so it's moving a lot but it's not make
6:02 anything new where does it stop making
6:05 new things let's check it out let's say
6:08 we want to do this how many times let's
6:10 say we want to do this 30 or let's say
6:15 we want to do this loop 40 times let's
6:18 take let's check it out so let's see
6:20 where it stops making it new so the last
6:25 four times it ran you guys probably
6:27 didn't catch it but the last four times
6:29 that ran it actually didn't make
6:30 anything new so the 37th time it ran
6:32 didn't make anything new 38 times 39th
6:34 time and 40th time so what the heck
6:37 happened well here's what happened three
6:41 hundred and sixty degrees right those
6:44 that's the number of degrees in a circle
6:46 so a circle has 360 degrees now we want
6:52 to make this circle like really nice and
6:56 dense we don't want it to be how it was
6:59 it our circle look pretty empty and the
7:03 reason is okay so the circle has 360
7:05 degrees now if we're turning ten degrees
7:09 every single time right what's 360
7:13 divided by ten that's how many times we
7:16 can go in a direction if we turn right
7:19 only ten degrees is 360 divisible by 10
7:23 and the answer is yes and the answer is
7:26 actually 36 right 360 divided by 10
7:32 obviously you don't have to be a math
7:33 genius and all that but you get back 36
7:36 which means 10 perfectly goes into 360
7:39 and isn't that awesome
7:42 that that's the number of times you can
7:45 actually make unique squares if you do
7:47 if you choose 10 degrees
7:48 it's beautiful seeing divisibility in
7:51 visualizations so even math starts to
7:53 become beautiful when you put it in
7:55 terms of computer programming you guys
7:57 this was one of my most favorite
7:59 discoveries and I still get excited when
8:01 I think about this I think that's pretty
8:03 freaking awesome what can we do well
8:07 pick a number that is not divisible
8:10 right that 360 is not divisible by so
8:14 how about
8:15 great number I don't know like a prime
8:19 number and what's a prime number that's
8:21 really close to ten eleven so let's pick
8:24 eleven right because 360 divided by
8:28 eleven gives you something that's not
8:31 divisible right like three hundred six
8:33 is not divisible by 11
8:34 eleven is a prime number so let's run
8:37 this bad boy and check it out and let's
8:39 run it a hundred times right not forty
8:41 hundred times and let's check it out
8:47 starting eleven degrees every time it
8:49 makes a square you'll notice that it
8:51 almost never repeats itself and they'll
8:54 probably never repeat itself and it just
8:56 keeps making a circle darker and darker
8:58 and darker and denser and that's how you
9:01 want it right look at how beautiful that
9:04 looks
9:05 dark in the middle and starts to get
9:08 lighter on the outer edges and those
9:10 webs are interweaving and it's gorgeous
9:13 right bonus bonus project I can give you
9:17 guys for this is bring some colors out
9:20 make some cool designs go look up
9:22 trinket dot IO go to that website check
9:26 it out um look at some examples paste
9:29 your code below you can help out other
9:32 people I can help you out if I look at
9:36 your code you have some errors or it
9:39 looks really cool and maybe I can help
9:41 you make it look even better or maybe
9:43 you'll just make something that will
9:44 blow my mind because I haven't made such
9:46 beautiful circles out of squares anyways
9:50 I hope you guys enjoyed it I hope it
9:52 makes sense please like the video
9:54 subscribe I'll see you in the next
9:56 lesson

Written by cleverprogrammer | Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.
Published by HackerNoon on 2022/04/08