Using Dictionaries, For Loops and While Loops in Python!

LeMaster Tech
181 followers
17 Video Views·Apr 23, 2025
This is the best place to learn and practice python skills for beginners looking to get started with Python! Learn how to create dictionaries, for loops and while loops in Python!!
This is episode 6 in an introduction to python series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=0Nc0kqLk81E
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
this will be video six in our python
tutorial series so if you missed the
first ones on installing and setting up
python and pycharm initial introductions
to variable types getting started with
functions things along those lines be
sure to check those out if those are
concepts you're unfamiliar with we'll be
using all that kind of stuff going
forward
in our last video we took a look at
using functions so that we could repeat
certain important pieces of code and in
this video we're going to be looking at
dictionaries while loops for loops and
exponentials
so what i have created what you see on
the screen right now is actually called
a dictionary within python as identified
by these squiggly brackets
um a dictionary contains what's called a
key and then a value and this together
is a key value pair and what that means
is you can look something up inside of a
dictionary by knowing the key for
example if i want to print
the
month associated with
you know january the first month
and i know the key for it i type the key
into these square brackets as i call the
dictionary and you'll see
we get january
but what's also kind of nice you can
use
a function that python has built in
called
dot get so you do your list name dot get
and then the key and it gives you the
value and so that looks just the same
[Music]
but what you'll notice is you can
actually the nice thing about the dot
get function is you can leave a default
so if it doesn't find it then you can
say
no
key value pair
exists so the the limitation of what we
just did
prior to this by putting the key in the
square brackets is
there's no
if statement you know if it's either the
key better exists or you're going to get
an error with the dot get function you
have the option of giving it a default
if it doesn't exist so i'll punch in 13
and you'll see no key value pair exists
and the dot get function is actually
really nice when you're uncertain about
your keys obviously with months that's
not something there's too much
uncertainty around so you really don't
need the dot get if you're comfortable
saying
i'm pretty sure i know when my key pair
exists and when it doesn't
um the the applications of dictionaries
are are really extensive you can use
them for
storing users or user data login
information passwords
if you're keeping something like a
gradebook you could have the student and
then their affiliated grade or you can
have the assignment and then what score
they got with the assignment
the dictionary really can be used for
any sort of easy lookup that you might
have a whole bunch of information in one
side of it and you just want an easy
index value to look it up by so a really
complex but doable example of a
dictionary would be if you had a student
name as your key so student name and
then you can actually have any type of
value as the value for your dictionary
so you could put an entire list
instead of just january you could say
okay well this student got a 14 on his
first assignment a 13 percent on his
second assignment a 15 and a 99 because
he really pulled it together near the
end
well if you go and you
print the
we have to call months because that's
what our that's what our list is but if
you print the
value associated with the key student
then what you're going to see is you get
that entire list printed out so even
though what i went with was months and i
showed obviously a string for the key
and a string for the value just know you
can use anything for either of those you
can use
any variable type python accepts as a
key and as the value pair
so moving on we're actually going to
dive into while loops because it goes
nicely with this list and what we're
gonna do is we're gonna create a while
loop that is going to show us every
month until we get through our list
so we're gonna create a counter to do
that and then we're gonna create our
while loop and you you've probably seen
in our previous video or you're just
familiar with the way functions work
while loops are fairly similar you
put the keyword while and then your
condition for checking this goes back to
the for loops we used in previous videos
as well
and then you're looking for a condition
and in this case we're checking that
while the
length and this len is a python built-in
function for checking the length of
objects like lists dictionaries anything
along those lines anything that has a
countable length so even strings have
have that function where it tells you
how many characters
and while our counter is less than that
should be less than or equals to while
our counter is less than or equal to the
length of the total
list we're going to
print out
whatever
the the value associated with that key
is and so to do that we're going to call
the string
conversion of our counter because our
counter is an integer but obviously
these these uh keys as we defined them
were strings so we're just going to
convert our character string which we've
talked about previously as well and then
we're going to print whatever month is
associated with that but what you'll see
here is actually we've created an
infinite loop because there's nothing
that is ever going to make the counter
go up so this is very important you need
to
put
a condition that increments the counter
on the end of a while statement python's
actually going to see an infinite loop
as an error statement and rather than
let the computer crash by running a loop
forever it should stop you so we're
going to actually increment the counter
once we've printed the months and this
should
there we go so just by doing that we
just printed out in order every key
value pair in our loop and then stopped
at december and ended the code
so a while loop is really nice
especially if you have an application of
unknown length where
maybe you need every value that's inside
of a dictionary but you don't even know
how long the dictionary is or you're
calling data from somewhere outside of
your realm you're using an api or a
plugin or something
while loops are really advantageous a
lot of game logic and things like that
use while loops
but they also they
they need a condition like what we have
in here where there's a
specified
and yet still open-ended things so if
you were to extend the months you know
for some reason they had a 13th month
then this automatically is going to run
13 times and so
let's say we need a loop to only run
four times
regardless of whether or not this
changes
well there's a different kind of loop
that we're going to talk about now that
is the for loop
and a for loop is actually a little bit
a little bit
easier to
know how many times it's going to run so
you don't have to worry about the
endless infinite loop scenario of a
while loop a for loop you would say for
i
in range and you're actually defining
the range of
how many times it's going to run
uh and you so we'll say one to four
print
and let's just use our months dictionary
again and say print
the string version of i
and what you'll see immediately upon
running this
is we get
the first one the second one and the
third one a few things to talk about
here
the range function goes up to but does
not include the end cap of your range so
it does count your starting point but it
does not execute this one so if you
actually need a for loop to run four
times you need to have the range from
zero to five
and so again we're using essentially the
exact same we're using essentially the
exact same functionality and i i could
have called i anything um i know we
called it counter for the while loop i
could have called it counter that would
be fine
um and
and so it's it's really doing the exact
same thing if we decide to
make the range up to 13 but what we have
now is if for some reason some dummy
adds a 13th month
you know
this for loop is actually just going to
stop at 12 it's never going to give you
more than 13. so that's just two
different types of loops that's kind of
good to know
um
but
the the scenario where you would
need one or the other and wouldn't be
able to use whichever one you're more
familiar with is fairly rare and by the
time you're comfortable with loops it's
the sort of thing that's going to make
itself apparent
so for now that's just a basic example
of both loops but loops are really
useful to repeat code without having to
add it so what becomes really powerful
is when you put functions which we've
talked about previously
inside of loops and that's actually what
we're going to do next we're going to
create an exponential scenario so we're
going to say
we are going to create a function and
we'll call it define
[Music]
uh
exponential
and we don't need anything passed in but
what we are going to do actually we do
need two variables passed in we need the
base number
and we need the power it's being raised
to
and inside of the exponential function
we're going to have loop so i'll go
ahead and get rid of our counter loop
and what think about what an exponential
is doing an exponential is saying your
base number
is being multiplied
times your base number again
and that's going to be
done over and over again until it's it's
resulted in the power you need so
so actually if you think about it what
if we put a zero into this so
you're going to start with a one and
we're going to create a variable called
answer and initially answer
equals one and then
counter
equals
zero
and we will say
while
counter
less than
should this be less than or equal to
we'll do less than for now let's see
while counter is less than the power
we are going to do this what we wrote
down here
we're going to do base num times itself
and then
the answer
is going to be
equal to that
although actually this is
counter equal to power
equal to
we need the answer to start out equal to
the base number
answer equals yeah because if we did
this then every time the loop executed
it would just be multiplying the base
number times itself twice and that's not
useful for anybody
so here what we're saying is uh
answer equals base num
counter equals zero well counter is less
than power answer equals base num times
answer and we go ahead and we add one to
our counter and one thing to say about
what i did right here this plus equals
is exactly the same thing as saying
counter is equal to counter plus one
python gives you the option to get rid
of having to type the variable a second
time and you can just tack the function
right before the equal sign and that's
saying it's going to add one to itself
but okay let's see how close we got and
what we need to tweak after this so
let's just go ahead and create
exponential and let's say we want three
to the third power and if our code is
perfect which it never is first try this
would give us 27
and it gave us nothing most likely
because we did not
print
the answer
which is what we want to do at the end
of our code
so there we actually got 81 which 3
to the first power is three second power
is nine third power is 27 so something
we did
answer equals base number
let's try power minus one
so that's 27 but let's see if we do
two to the fifth
32 okay and let's try just five
squared
25. okay my job is not to reinvent the
wheel and create a calculator
from scratch but i am just trying to
show you functionality and application
of while loops and how they can be used
to you know rather than when we do
let's make this quite a bit larger 5 to
the fifth power
rather than having to multiply 5 times 5
and then 25 times 5 and then 125 times 5
over and over and over again we've
written the code to do it once and we've
just defined the conditions for how many
times it needs to happen so you can
think of an exponential function really
as a while loop
that's maybe over simplifying it but you
can see right away the power of while
loops the application you could
absolutely create this with a for loop
as well this video is getting a little
lengthy and i don't want to do that to
you guys now
but hopefully this was useful
introduction to dictionaries while loops
for loops and exponentials if you have
any questions about anything you saw
here today or have any ideas for videos
you'd like to see in the future be sure
to let me know in the comments section
and hopefully you found this helpful and
feel free to like and subscribe if you'd
like to stay tuned for future tutorials
and as always thanks for checking out
the channel thanks bye
you
