Hey guy's R4GE VipeRzZ here and welcome back to episode 2 of my Python 3 series, so in
this episode we're gonna go over print statements and variables, so the first
thing we're going do is we're going to open up Idle, so I'm just going to search Idle
in the search bar and I'm gonna open Idle up, so then the first thing we're
gonna do is we're going to go file, new file and then we can close this other
window and I will make it fullscreen for you and before we start typing out any
code or we're first gonna go to file and then save as, now you can save the file
wherever you want and you can name it whatever you want I'm just gonna save it
here and I'm going to name it Python 3 ep2 like so, so it's important you
save the file before you start typing any Python code that's because you cannot
run the code until you actually save the file, so it's best to save the file
before you start typing out any code, so now we've saved the file the first thing
we're gonna go over is print statements, so a print statement will print some
text on to the screen, so to create yourself a print statement you just need
to type print and then open bracket and then a closed bracket, so the brackets
contain whatever you want to print, so I'm just gonna print the text hello
world and if we run this code at the minute we would get an error. Now that's
because whenever you want to use text in Python you need to surround it with
speech marks, so I'm gonna put a speech mark at the beginning and a speech mark at the
end, so these two speech marks tell Python that all the text following this
is just text that needs to literally be written to the screen, so if we didn't
put speech marks around it, so if I remove these. Then it would think that hello was
a name of something and world was a name of something it wouldn't just print
hello world to the screen, so I will surround this with the speech marks and
then before we can run it we first need to save it, so you can either go file and
save or you can press Ctrl + S to save it,
so once you've saved it you can then click run and you can run the
module, so as you can see is now printed hello
world to the terminal, so I will to show you what happens if we didn't have the
speech marks around it, so if I remove them and I saved it and I run it, as you can
see it's invalid syntax, so if you're getting an invalid syntax error then
first check that you have your speech marks in the correct place so if we put
speech marks around it again, that now tells Python this is text and if we run
it, then you can see in now print hello world to the screen again, so now the
next thing we are going to go over is variables, so the best way to understand
variables is to think of them like boxes, so in real life if you had a box and you
wrote something on it, and then you put something into the box and store it away
somewhere for later, then if you come back and you find the box with the name on
it and you open it up you can find whatever you put inside of it, so to
create a variable in Python you first need to think of a name for the variable
so I'm gonna create a variable to store a name so I'm just going to call the
variable name, so the first thing you need to do when creating a variable is
you need to type the name of the variable that you want to create and
then once you've done that you want to just press space then equals and then
space and then you want to type out whatever you want to save into the
variable, so I'm gonna save the name Steve into the name variable so to do
with that I'm gonna do speech marks then I'm gonna type Steve and then I'm going
to close it with speech marks, so as you can see again I've used speech marks as
I want to save the text Steve to the variable if I didn't put these speech
marks around it then Python wouldn't know that that was text, so now
I've saved Steve to the name variable we can now create ourselves another print
statement and this time instead of putting text into it we're just going to
type name, so
name is the name of this variable so if
it prints name it should print Steve, now that's because we've saved the text
Steve to the variable name, so if we print name it should then print whatever
been stored in the variable, so if we just save this and we run it as you can
see it now prints Steve, so basically a variable is a section in memory that you
can save stuff to, so I've created the variable name name and I've set Steve to
it so if I change this from Steve to Bob. Then this time when we save the code and
we run it it should now print Bob to the screen now as you can see it's printed
hello world and it's printed Bob so the way Python works is it goes line by line
for the code so when you first run the program it finds the first line and that
tells it print the text hello world to the screen then when it gets to this
line it see okay you've created a variable called name and then you've set
it equal to the text Bob, so then it saves Bob in the variable called name and then
when it gets to this line it sees it's a print statement and your printing the
variable name, so guys that's gonna be the end of this video if you liked it
don't forget to hit the like button, if you disliked it hit the dislike button,
subscribe more content like this and I'll see you another time, bye.
you
Không có nhận xét nào:
Đăng nhận xét