Python Beginner Projects - State Capitals Look Up Function - Practice Programs Episode 2

LeMaster Tech
181 followers
25 Video Views·Apr 23, 2025
Prepare for a technical interview, practice building an app or get ready for an upcoming test or quiz with these sample projects! This episode covers creating a state capital lookup function by creating dictionaries of stored data and gathering user input to relate state to associated capital!
This is episode 2 in a Python beginner project 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=I9L1a10QfUA
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
today's video is going to be episode 2
in a
beginner projects with python series
coming out of this channel
and today we're going to create a state
capital checker so we're going to create
a lookup function that you type in a
state
and they'll
the python code we write will spit out
the capital of that state
so let's go ahead and get started and
the first thing you need to do when you
have lookup functions if you're not
looking something up from an external
source like the internet or some kind of
plug-in you have to create your
dictionary of data so
for us we're gonna have a dictionary
that we call states and
capitals
and a dictionary remember goes inside
these curly brackets and it's created
with what's called key value pairs so
the first thing you put in a dictionary
is your key and in this case the state
name is going to be the key
and then the value that gets paired with
it is going to be the
montgomery is going to be the
capital of that state and then you
separate your values by a comma so the
second one would be
arizona
and
phoenix and i just did these first two
here um so that you can see the format
of a properly formatted dictionary it's
key then value pairs with a colon in
between the two and a comma separating
key value pairs
and this would be a really long and
boring video if i made you guys watch me
type in 50 states and their capitals
so i have those typed out a little bit
because i don't hate you guys that much
and so if we go ahead and
there we go alabama alaska arizona
arkansas
um
so
the indentation doesn't matter too much
i know this looks kind of ugly but i
don't want this to be a crazy long video
so let's go ahead and get into the
actual code we can just assume i
correctly entered the data but now we
have a dictionary that we're able to
pull data from called states and
capitals and the code is actually going
to be pretty simple from here really all
we need to do is get an input from a
user so we'll say state is
equal to input
enter a state
and then we'll go ahead and do print
and let's say our text is going to be
the capital
of
plus state
plus
is we're gonna need some spaces in here
and remember you can use these plus
signs to concatenate a string as long as
everything in it is a string so we have
a variable that we know is going to be a
string as it gets entered
then
over here we have a string using single
quotation marks and over here we have a
string using double quotation marks this
is all valid to just add together with
the concatenate because we're still just
adding strings together
so then the last piece to this is going
to be plus and now here's where we need
to format so we'll do string
and then we'll use the
states and capitals dictionary that we
created
and we'll use a built-in python function
for dictionaries called dot get
and we will get whatever value is
associated with the state that was
entered
or so the cool thing about the dot get
function is you can also enter a value
if it's not found in the dictionary so
if it's not found in the dictionary what
we want to say is
state plus
blank
was not
found
in
table
so um that's really all we need to do
for this lookup function and i'll go
ahead and just run it a few times and we
can see it's really pretty cool
enter a state let's go ahead and check
ohio
uh that was is columbus might be nice to
add a space after is
let's go ahead and do
new york i guess
it's albany that's great okay now let's
do one that we're not going to get let's
do pete
the capital of pete is
pete was not found in the table so
there's a lot of ways you can format the
outputs i mean there are ways you could
write this that would not be as silly as
the capital of pete is pete was not
found in the table but the point is
with just a dictionary of info and
prompting a user for a key
we can look up the associated data with
that key and we can handle exceptions
for when that key is not found so this
is a pretty cool little function and uh
hopefully you found that useful and
interesting
this is a nice short project you can
tackle
and it's really
useful to be able to look up data from
data sets and dictionaries and tables
and anything like that and it's all
pretty much going to follow this same
format so
if you
have done similar projects in a
different way or you have any questions
about what you saw today
go ahead and let me know about it in the
comments below otherwise if you found
the content on this channel useful feel
free to leave a like and subscribe i
really appreciate that and as always i
appreciate you watching the channel and
good luck with your code thanks bye
