please remember there are additional
resources and things such as code
samples at elithecomputerguy outcom so
if you're watching one of these classes
and you need to know what code looks
like or if you need links to the
resources that we're using please go to
elithecomputerguy
docomo there also please remember that
free to the end user classes are not
actually free it cost me a lot of money
to be able to provide this type of
content so if you could click on the
Donate button and throw in a couple of
dollar every month this will help me be
able to continue to provide you this
type of material welcome back as you
know I am Eli the computer guy and in
today's class we're going to be going
over variables and concatenation in
Python so up until this point in the
python series we've talked about what
python is and the basics of how to write
and how to run Python scripts now what
we need to get into is actually talking
about variables and something called
concatenation so what concatenation is
is you have a string so a string is text
uh think of a string as a message
something like Hello World what
concatenation allows you to do is
basically allows you to glue variable
values together into a string so you can
say hello person's name you know how is
the weather today something like that
that would be catenation hello plus the
value for the person's name plus the
rest of the message so this is going to
be very important uh with going forward
in Python it's important to understand
with any of these programming languages
that we're always going to be
interacting with variable values right
so we're going to have a variable name
that variable name will be assigned a
value somehow and then that is what the
script is going to work off of if you
have an if else statement if this
variable value is above a certain amount
or below a certain amount do something
while Loops while uh the count is below
the number of this variable value uh
continue to do this Loop uh input data
into a database basically input variable
values into a database so everything
that we're going to be interacting with
uh with python is going to need those
variable values sometimes we're going to
manually assign the variable Val
so we're actually going to go into the
code and we're going to say like name
equals Bob and that's us manually
assigning it within the code itself uh
other times there's going to be a user
input uh so using something called the
input function or something like that we
are going to ask the user uh for
information for that variable value
they'll Tippy tap type it in they'll hit
enter and now that variable value will
have whatever value that they assign to
it or many times uh with variable values
uh you'll be pulling information out of
databases or data stores uh so basically
you go to a database you make a query
when you pull the information out from
the query uh the name uh for that query
will be assigned to a certain uh
variable the age for that query will be
assigned to a variable the shirt size
for that query will be signed to a
variable so on and so forth so variables
are very important and it's important to
understand basically everything is going
to be working around these variables now
with different programming languages uh
you know they deal with variables in
different ways C++ deals with variables
in a way uh P PHP deals with variables
in a way uh and python deals with
variables in its own way uh one of the
important things to remember with uh
python is that it does have data types
so you may hear uh JavaScript vanilla
JavaScript uh is not a typed language uh
so what a type means is basically the
data uh that's in a variable value has
to be a certain type of data uh so an
INT so an INT is a whole number 1 11 256
330,000 right that is an INT a float is
a decimal point uh
12.25 uh
3065 right that's a float a string is is
text hello world or something like that
basically a message right those are
different uh data types so one of the
things in certain programming languages
right you do what's called declare a
variable you declare the variable this
is the name of the variable and you
assign a data type so name is a string
age is an INT that type of thing so some
uh some programming languages force you
to to to create the variable uh and also
assign a data type when you create the
variable some programming languages are
simply do not have a data type at least
for the the plane vanilla version
there's something called typescript in
the JavaScript World we're not going
anywhere near it though but in
JavaScript uh basically you just simply
have variable values without a data type
where python becomes interesting because
everything has to be its own quirky way
is python actually does have data types
but especially as a new person it can
get confusing because it auto assigns
the data type when the variable value is
assigned to the variable so if you have
name equals Bob right name the value for
name will be Bob python will see that
it's text and we'll assign it the string
data type uh you know age equals 11 it
will see that it's a it's a an integer
and it will be assigned as an INT you
know cost is25 $25 50 cents it will see
25.50 and it will sign it as a float so
it is a typed uh programming language it
but it auto assigns the type based off
of the information that you give it this
is one of those things like when you
first start out it's like wow this is
easy this is wonderful and then as you
go on for a little while you're like
this is stupid I hate it it's a pain in
the ass because one of the problems you
can get into is basically with data
types what the pro why programming
languages have data types is so that you
don't do something stupid like you don't
add 12 to Bob right so imagine you have
variable values and let's say you're
trying to add them up and then divide
them by something to get like an average
well what if you have 12 plus 34 plus 65
plus Bob divided by four right that
could be a problem and so why types are
important is the the programming
language will see in INT in string and
it will fail it out you'll get a hard
fail to say you tried to add a string to
ins and that's stupid right that's the
important thing in non-typed languages
you can get weird errors where 12 + 16
plus 34 plus Bob might actually give you
an answer the issue that you run into in
the python World though is since many
times you're not manually assign
assigning the data type is that you'll
run into issues when you go to add
numbers when you think two numbers are
ins or two numbers or floats one of the
things when you run your code you might
find out one is a float and one is a
string right
2.50 is a float but it can actually be
assigned the string data type so when
you try to add
10.20 Plus
25.50 but
25.50 is a string not a float then you
will get an error and so you can run
into that sometimes especially like with
concatenation uh you you you write
things out and it'll tell you you know
you can't concatenate an INT and so then
you've got to go back and then change
the data type and I'm going to show you
how to do that today uh in today's class
uh so the important thing to walk away
with this is that this is a type python
is a typed language but it automatically
signs the data types for you uh and then
beyond that like with all of it you know
it's just work basically what what's
going to happen is you're going to go
Tippy tap type and you're going to get
very very very very frustrated and then
you're going to keep doing it again and
again and again and after a while it
will make complete sense to you and some
Noob will walk up to you and start
screaming about Auto automatic data
types in Python you'll be like ha ah
Noob I remember when I was like you two
weeks ago so anyways that's the that's
the kind of stuff that we're going to be
talking about in today's class now
before we jump into the today's class I
do want to tell you about the absolute
best resource on the entire internet it
might might actually beat Chad GPT W3
schools let me tell you they are not
paying me a dime but over the past five
years I've gotten so much value out of
this website it's just amazing uh
basically this is an online versions of
a dummy book and almost any programming
language you're looking at whether it's
HTML CSS python Java PHP react MySQL
whatever else they just have a
tremendous number of lessons and
basically just information Snippets that
make it very easy for you to understand
how to do whatever it is in the language
that you're trying to learn again the
big problem that you're going to have
learning programming is a lot of idiocy
of the syntax right did you put the
comma in the right place did you do
things how you think you should have
done it so many times you just need a
place to go I need to understand the
syntax a little bit better uh with this
if you click on let's say python here
you'll see you get all this intro to
python syntax casting strings Tuple sets
if L statements functions the whole nine
yards so if you're sitting there and
after a class that I've done you don't
quite understand IFL statements or
something like that you can come here
and they give you just a really freaking
simple breakdown without a lot of
additional information so if you want to
move faster then I'm going in these
classes or if you need any information
that isn't uh that I don't present to
you for some reason I'm telling you go
to W3 schools they have not paid me a
penny but the value that they' provided
me over the years is just absolutely
immense absolutely great resource now
since I already started talking about W3
schools we might as well just throw up
their web page on data types no reason
for me to retype this out again
basically when we're talking about data
types is the type of data that the the
variable value has right uh so we have
text type so string St Str is string
when we're talking about a string we're
talking about a user's name Bob we're
talking about a message hello world that
type of things in a string you have
numeric types int so that's a whole
number
1125
556 you have float
25.50 33.33 the whole nine yards and we
have complex we are not going to go into
here uh there are sequence types list
tupple and ranges we're going to talk
about those in future classes mapping
type this is a dictionary this is kind
of like a named key array sort of sort
of if you're from PHP or something like
that uh we have set types Boolean bytes
and nun types so some of this may seem a
little bit confusing to you but one of
the things I want to show you is that
there is a relatively limited number of
data types within python right some
programming languages you get into they
have a crap ton of data types just for
numbers and so whether you're going to
use a float or whether you're going to
use a decimal maybe maybe a huge thing
um I actually know somebody that was uh
creating this really cool app that was
using
GPS and I didn't realize it I I forget
which is which but basically he was
using floats but floats cut off the
decimal after like nine nine decimal
places it cuts off whatever the
remainder is and so whenever he was
doing
tracking um the uh the the tracking was
just kind of little all over the board
because he was using GP GPS Precision uh
and so literally all he had to do was
flip over to using a decimal data type
and that gave far more uh you know
digits after the decimal point and so he
was actually able to see a better G GPS
track for for what vehicles we're doing
uh and so that's one thing to be
thinking about with whatever programming
language you're you're dealing with is
what kind of data types there are uh to
verify issues like that is the data type
you're using chopping off numbers you
know after the nine decimal places or
something like that that you may
actually need uh the cool thing with
python is it doesn't get that
sophisticated again strings uh basic
numbers uh basic whole numbers uh
decimal is floats uh then we have things
like lists we'll deal with frozen sets
we'll deal with Boolean Boolean is
either on or off uh and then bytes uh
these these binary types down here uh
these are generally for files uh so when
you're dealing with an image file when
you're dealing with an audio file many
times you'll have to turn them into
bytes so this is pretty uh simple here
if you go down it can actually show you
a little bit about setting the data
types again string hello world int 20 uh
float 20.5 complex 1j list is like apple
banana Cherry tupple apple banana Cherry
that's his own thing a range of numbers
uh dictionary name is John age is 36 you
have a set you have a frozen set you
have Boolean true you have btes you have
the bite array so on and so forth so
this kind of just gives you a basic over
uh over overview of the data types
within the python world just remember in
the python World data types are
relatively simple um you just have to to
make sure you know what data type you're
supposed to be dealing with at any one
time so here is a brief example of
creating your variables and then
assigning values to them and then seeing
how python then automatically assigns
the data type right so we have message
equals hello so this should be a string
number underscore VAR equals 1 so that
should be an INT float uncore VAR equals
1.11 right so this is how uh we would
create and then we would assign the
values for variables when we're creating
the python script uh down here we then
have the print function and one of the
things that that we we have is a
function called type and so what type
does is it will tell us the data type of
a variable so we're going to say print
the data type of message print the data
type of number VAR print the data type
of float VAR right so basically if I go
and I click on the Run button here we
will see that message is a class of
string number VAR equals a class of in
number float or or float VAR uh equals a
class of float so we simply assigned the
value to the variable and then it auto
assigned the data type for us now at the
bottom here um I have commented out a
little bit of code so this is a way you
can comment out multiple lines of code
uh double quotation mark double
quotation mark double quotation mark
that starts a multi-line comment and
then to close it double quotation mark
double quotation mark double quotation
mark closes a multi-line comment right
so I'm going to get rid of this here so
I'm going to just delete that and then
basically with this this is how you can
change the data type of a
variable so by using int for float VAR
this will turn the float VAR value into
an INT and then here we're assigning
that new value to float VAR so we take
float VAR is
1.11 we turn it into an INT so if we're
turning it into an INT what you have to
be careful about is it's going to get
it's going to get get rid of those
decimal places and so float VAR should
now be one so if we do print and then
simply float VAR we should simply get a
one there so if we do this again we see
string and Float one right so we had
1.11 we turned 1.11 into an INT an INT
does not have a decimal place so
therefore everything past the decimal
place gets tossed and then we print out
the new value for float R and that is
one so this gives you just a basic idea
of how to create an variable how to
assign the variable and understanding
how those automatic data types work so
now let's take a moment to talk about
concatenation so concatenation is
basically where we're going to
automatically create uh strings so we're
going to create messages many times to
the user right so you log in and the
website says hello Bob how are you doing
today that type of thing right that's
what concatenation is uh so up here uh
we're going to create three variables
we're going to have uh two string
variables and one int variable now we're
going to have message so a message is
just whatever message is so it's going
to equal single quotation marks and then
whatever is between those single
quotation marks will be the message so
single quotation marks hello now it's
important to understand in the python
World more or less
99.9999% of the time it doesn't matter
whether you use a single quotation mark
or a double quotation mark you just got
to use one of them you open with a
single or double quotation mark and you
close with a single or double quotation
mark and whatever is in the middle
99.999% of the time it will not matter
so a lot of times in my lessons I'm
going to be using the single quotation
mark uh and that's simply because
apparently that's that's how the young
kids do it nowadays again it is kind of
weird doing technology for a long time
I'm used to the days when everything was
double quotation marks so I'm just used
to doing double quotation marks but now
all the kids do single quotation marks
and I I look like a boomer if I use use
double quotation marks so I use single
quotation marks Welcome to the Real
World of coding why do you do it that
way cuz I don't want to be called a
boomer
anymore anyways right message equals
hello name equals
age equals 36 now there's a number of
ways that you can
concatenate one is with pluses the other
way is with commas and the other way is
with something called an F string um so
I'm going to go through and I'm going to
print all this out for you so you can
kind of see how this works uh I just
want to take a look at these first three
uh to begin with so I will actually
comment out these uh these last two so
if you put the pound sign or the hash
sign in front of a line that comments
the line out that means that python will
no longer try to run whatever code is on
that line so when you're debugging or
trying to find a problem uh again using
comments uh can be a great way to do it
you just comment out a chunk of code
that you don't care about verify your
first code works properly then uncomment
things out and go from there so I'm just
going to comment this out for a second
cuz we don't want to deal with that now
so anyways so message so so we're going
to say print so print when we do it this
way everything has to be within
parenthesis the whole thing has to be
within parentheses and then message so
the value of message so this is within
parentheses and then you do plus name
within parenthesis so the value of
message plus the value of name plus
single quotation mark I'm happy you're
here now you'll notice for this I don't
have any uh punctuation in here is I'm
happy you're here we'll talk about why I
did that in a minute but basically so
this will print out all as one line
print message comma name comma single
quotation I'm happy you're here the same
thing right this will print out all in
one line and then F string F strings are
the best way to do it you put F first F
single quotation mark squiggly bracket
mess Mage value then there's a normal
space here name value so with an F
string it makes it really easy because
basically you just plug in the variable
values as you're going uh you notice
there's no quotation marks or anything
else or pluses message name I'm happy
you're here uh so if I run
this right so what you're going to see
here is so message plus name plus I'm
happy you're here is hello Bob I'm happy
you're here when you use the pluses when
you use the pluses it does not automat
automatically add a space for you hello
is right on top of Bob which is right on
top of I'm happy for you right so it's
all basically all of that gets scooched
together so depending on what you're
doing that might be a good or bad thing
if you're trying to concatenate let's
say numbers together if you're trying to
concatenate some kind of identification
that you might use for for a squl
statement you might want want everything
scoo together but with the pluses you
get no additional uh spaces unless you
purposely put them in there with the
commas what you'll notice is hello space
Bob space I'm happy you're here so with
the comma it automatically adds a space
after every variable value that you plug
in is that a good thing is that a bad
thing depends on what you're trying to
accomplish at the time then we have the
FST string which is basically the best
way to do all of this F to be clear F
string is how we're going to be doing
almost everything with the F string it's
just literally formatted exactly as you
show it so message and then I put a
space here right so there is actually a
space here so that's the space name
value that's actually space I'm happy
you're here and that prints out exactly
as I put it here again the nice thing
with the F string so if I put in lots of
spaces and I print this out you will
notice the F string is formatted uh
exactly like I formatted it here and so
that is why the F string is so nice uh
so with that let me uh toss these uh let
me get rid of let me comment out the top
ones here and then we will oops and then
we will uncomment out the bottom bottom
ones um to show you
something okay so now with these bottom
ones uh so we're using the F string here
but one of the things that you have to
worry about um especially whenever
you're doing things like concatenation
is that you don't accidentally Escape
out of whatever it is that you're trying
to do right so if you open with a double
quotation mark you have to close with a
double quotation mark if you open with a
single quotation mark you have to close
with a sing s Le quotation mark well you
notice up here where I open with a
single quotation mark and then I don't
have any any other pron uh uh single
quotation marks in here if I open with a
single quotation mark before the I and I
put another single quotation mark before
the M right for that for for that that
would actually exit out so we do I and
then close out and the rest of this
would be would be basically you would
cause an error for you right you open
and then you close so so this is one of
the things that you can think about with
the double quotation marks and the
single quotation marks and where again
it is important every once in a while so
I open with a double quotation mark
message name I single quotation mark am
happy your single quotation mark
here I opened with the double quotation
mark so it will not close until I get a
double quotation mark So since I'm using
single quotation marks here and that is
all fine and that will work properly the
other way uh to deal with things let's
say uh again you want to use single
quotation marks is to escape out right
so this is opening with a single
quotation mark message name I and then
you do the slash before the single
quotation mark and that escapes out the
single quotation mark So what this tells
to python is ignore the single quotation
mark uh as if it is actual python code
just essentially read it as a string and
again I escape out over here for the
single quotation mark again and then
close with a single quotation mark here
and so when I run either of these again
basically you'll get hello Bob I'm happy
you're here hello Bob I'm happy you're
here so
either either of these will get you the
same results now the important thing
again uh let's say so I open here with a
with a double quotation mark um if I
Tred
to let's say do
this if I wanted to be like snarky if I
want to be snarky I'm happy I'm happy
you're here right and just to show you
so double quotation mark message name
I'm and then I want to put Happy between
double quotation marks right I do the
double quotation mark But as soon as I
do that I actually close this out
and then this is going to be an error so
if I run this it'll tell me I have an
error line 15 print message name I'm
happy invalid syntax perhaps you forgot
a comma all right so basically what I'm
going to do is if I want if I want to be
snarky if I want to be snarky I can do
the Escape so I do the Slash and then I
do the
slash so that escapes out of those
double quotation marks says to ignore
them and then if I run this
now hello Bob I'm happy you're here
right so this is this is some stuff to
be thinking about uh with this
concatenation the next thing that I want
to show you is again it goes back to
that whole uh data type thing and the
problem if you're trying to do something
with the improper data type right okay
so here here is basically like uh this
up here right message name whatever
so print message plus name plus Ur plus
age right so string plus string plus
Text Plus in in a
concatenation if we try to run this as
is because that's an ins we will get
this failure type type error can only
concatenate string not into to string
right so basically when we're sitting
here and we're dealing with this it
wants string plus string plus Text Plus
string that's an in and there we get a
failure because of data type so what we
can
do is kind of like what I showed you
before age equals string age so we set
we set age up here to 36 so we set age
to 36 it auto assigns the data type of
age to be an INT because it is 36
so what we're going to do here is we're
now going to assign the data type of age
to be a string so it's now text so age
is going to equal string of
age and then if we run
this it's all smoos together but hello
Bob you are
36 right so this kind of gives you an
idea of how concatenation Works uh the
big thing uh that I'll tell you is f
strings F strings are what's going to
make your life very happy generally
don't do this this other type of
concatenation but it does exist and then
with f strings again just remember
whether you use uh double quotation
marks or single quotation marks you open
close and so that's where you just kind
of got to figure out uh what you're
trying to do uh with whatever it is uh
that that you're trying to write out so
do you want to escape out of something
do you want to use single quotation
marks you can open with single quotation
marks and have double quotation marks in
the middle uh so like with this right I
could
do I could do single quotation mark
single quotation mark oh no and then I
have that
anyways then you got to go through and
figure all this out and that's that's
just the mess of again trying to figure
out that that single quotation mark and
double quotation mark whenever you're
dealing with long messages and it's just
one of those things that you know you'll
work your way through uh so this is the
basic of text
concatenation and don't worry we're
going to be doing a metric crap ton of
it uh with the classes that that are
going to come after this one so that was
the class on the basics of variables
data types and concatenation within
python again if some of this is a little
bit confusing to you right now don't
worry we've got 20 plus more classes
just on the basics of python that
doesn't even what get into when we start
getting into the fun stuff with uh with
artificial intelligence or computer
vision or anything like that everything
that I'm showing you today is just a
very very basic uh set of information
where after a week or two this will come
to you uh with SEC but like second
nature again the more that you write
code the more that you program and
frankly the more that you fail the more
you'll see where you run into problems
and then realize how it is is that you
need to solve those problems again the
concatenation is a perfect example of
where in the programming world really in
technology there's always 10 ways to
skin a cat right uh so if you want to
concatenate basically put text together
you can use pluses you can use commas
you can use f strings there's different
ways to do concatenation how you do it
really depends on your programming style
and more importantly for you how you do
it may actually revolve a lot around
what the previous coder did and the
Legacy code that you're walking into and
basically trying to keep that all nice
and neat that is one thing to be
thinking about uh if you're going to be
going into a production environment if
you're going to be going into like a
development company where you are
writing a code or if you're modifying
code other people have written is you
want to make sure the syntax is all more
or less the same it's very similar so
whatever style you know person in
concatenation is you should probably
keep that style up going from pluses to
commas to F strings back to pluses up to
F strings couple of commas for
concatenation that can be very confusing
to read uh and so whenever you're
designing your code one of the big
things is just figure out the style
figure out the syntax that you want to
use and basically just stick with that
again if you're going back and modifying
somebody else's code many times
code
readability is more important than quote
unquote perfect code right that's what
you get from a lot of people I would not
write inefficient code or whatever well
here's the thing somebody else wrote the
code base you're modifying it frankly
you're GNA leave right you're so amazing
you're so amazing and smart you're gonna
be leaving you know pretty soon anyway
so somebody else is going to have to
come in behind you and one of the things
you find in the real world is sometimes
you know writing things inefficiently or
doing things that you would consider not
the best quote unquote the best way to
do it might actually be easiest again
from that readability standpoint if you
have 10 people that have gone in and
modified code and they've all modified
it with the same styling method it's
relatively easy to read like you might
have to go oh this is how they did it in
the script so I just got to remember
that but you know like I say it's much
harder when you're you're bouncing from
one thing to the next to the next to the
next to the next right if every single
person that modified a chunk of code
modified it with their own their their
own preferred styling it becomes a real
mess to try to maintain that going in
the future so that's just one of those
things to keep in mind um again like f
strings apparently is a newer way of
doing concatenation so again when we're
doing our projects we're almost always
going to be doing F strings but if
you're dealing with code from 10 years
ago you know they might be using the
pluses or the commas or whatever else
and so it might be better just to stay
with that styling uh versus you know
modified a quote unquote better way of
doing it just one of those things uh to
keep in mind again uh with python it is
important to understand that this is a
typed language that means you do have
data types ins floats again we're going
to deal with lists and sets and oh
dictionaries and all that in the future
don't worry about that the important
thing to remember though is that it is
typed it's not like JavaScript where
it's not typed it is typed you just
don't necessarily manually set the type
and so
again that can run you into some funny
issues and that's why whenever you write
you you run your code you always look
down at the bottom and see what errors
are popping up many times that will show
you the issue that you're dealing with
uh so just just be careful with that and
again and remember if you're going from
float to int again if you're new new to
programming right if float has nesal
points and int does not so if you take a
float number and turn it into an INT any
of those decimal points simply go away
uh so imagine oh let's say you're doing
uh some kind of like cash register
system and so you know you're tallying
all this information and then you change
the data type into an in all the decimal
points goes go go away right let's say
let's and that's where you have to be
careful with with this kind of thing um
let's say you're trying to print things
out on a screen and so you say okay I'm
going to take this flow and I'm going to
turn it into an INT so I can concatenate
it and print it out on the screen but
once you've turned it into an INT Are
You Now using that int to add up the
tally so that all of those decimal
points all those pennies dimes and
pennies have have not been counted when
you actually add everything up
that's the kind of thing that you do
have to be careful about with going
between data types but anyways we're
going to do a lot of projects and a lot
of other things so hopefully that'll
make a lot more sense for you going into
the future uh and the final thing too is
again if you get confused with anything
that I say or if you just need some
additional information please go to W3
schools whether you're learning PHP
JavaScript HTML CSS or python or any of
the other languages it is just an
invaluable uh resource for you and'll
make you your life a lot easier if you
use it so as always I enjoy teaching
this class and look forward to see you
the next one