Using Print | Python for Beginners [5 of 44]

Using Print | Python for Beginners [5 of 44]

>>Okay. So Christopher’s
shown you how to get Visual Studio code working
with the Python extension, now, let’s go into
Visual Studio code and actually start trying
out some of our code. Now, when you work inside
Visual Studio code, you’re going to need to create
a folder on your file system first. That’s a place where you can keep all your files together
that you’re working on. So I’ve already gone ahead
and I’ve created a folder. So as soon as I open
up Visual Studio code, I’m going to want to go to File, Open Folder, and open up
that folder that I’ve pre-created. So I’ve already created a folder called Intro to
Python Development. I’m going to open that folder now. Now, that gives me a place to store all the files I’m
going to be working on. Once I’ve got that folder
open and I can see the folder name listed
over here on the left, then I can say File New File to create a new Python file
inside that folder. So now, I can try out
that print statement. So we say print ‘hello world.’ Now, initially, there’s no way that Visual Studio Code can tell that what I’ve written is a Python module. So it’s when I save the file that I tell Visual
Studio it’s Python. So I’m going to say that by going to File Save or Control S if
you like keyboard shortcuts, and once it’s saved, I’m going to make sure I call
it “” and that PY extension is a cue to Visual Studio Code that I’ve
just created a Python script. You’ll see right away,
some colors appearing. That’s a good sign because
it’s actually using code highlighting to indicate that the ‘hello world’ as a String by
showing up in red and so on. So I can tell now that Visual Studio Code recognizes
this as a Python script. Then I want to run that. So to run that code, and this will be
your ultimate test of whether you’ve got all your Python
installed correctly, you just type Python
and then the name of your script and I called
mine “”. If you forget your file name, don’t worry, it’s at
the top of the tab, and then you hit ”Return”
and sure enough, there’s ‘hello world’
displayed on the screen. Okay. So I’ve successfully run
my first script in Python. As I mentioned, you can put single quotes or double quotes
around your string. I’m just going to use Control S
to save that file again. Go back to a command line. I’m going to use the up arrow
which allows me to go to the previously run command
and re-run my script, but this time with
double quotes around the string and sure enough, still displays ‘hello world.’ So as I mentioned, single quotes, double quotes; either
one will work just fine. Now, what if I want
someone to input a value? Well, I could create
a variable called name and say input “What is your name?” Then I can say “hello” and then maybe after I say “hello,” I say “Let’s print
the name on the screen”. So let’s say “hello” to the person
who’s typed in their name. So now, save that. Go to my code, re-run that, and it says “What is my
name?” My name is Susan. Now I see Hello Susan
appearing in the output. So the input command is that is
a way for me to ask for input from a user and then I can take that input and store it
back inside a variable. Once you start having multiple lines of output displaying on the screen, you may want those blank lines. So at anytime, you can
do something like print “Hello” and then I can just say print “world” and you will notice
those appear on different lines. So you can see ‘hello world’
appear in different lines. I’m going to show you
another useful little command in Visual Studio code here. We’re starting to get to the bottom
of the command line output. So I just type in the word “CLS”, will clear the screen and take us back to the top of this output here. Useful little thing when you’ve got a lot of outputs
displaying on the screen. Another way of putting output over multiple lines is
you can use that /n. /n is an escape character that
means please insert a new line. So that will also display a line
of code over two lines of output. So /n is a way of saying
newline right inside a string. This is really handy
when you just want to display output to user
in a console application. But as I mentioned,
print is also very popular for coders trying
to debug their code. So if I was writing some code
that was doing some math; X equals 532 plus 12 and then
I say Y equals X divided by 0. Now, I know that dividing by
zero is going to cause an error, and then I say “Print”. Yeah, I did math. So I write a very simple little
program here and I save it, and I go down and I run that program, and it blows up. Now, I’m using
a very simplified example. You can probably look at my code and guess which line
of code blowed up; it’s the divide by zero. But what if this was inside a 500, 600 line program with
multiple modules and functions? Sometimes it’d be tough to track down where that line is blowing up, even though in the error message, it does give you a line number
which gives you a hint. So sometimes when I’m
trying to debug my code, I will literally write
things like saying adding numbers, let’s try it. Promised I would use
single quotes here. Adding numbers, and
then I add my numbers, and then I’ll say “Print
Dividing numbers”, and then I will divide the numbers, and then I have my print statement
saying “Yeah, I did math”. In that way, when I
go and I run my code, you can see that the adding
numbers message appeared, the dividing numbers
message appeared, and then I got an error message. So it can help me flow back through my code to figure out
what code executed successfully, and whichever print statement
I didn’t see is probably the one right
after the line that failed. So don’t be afraid to
use print statements as a debugging tool as
you’re learning to code.

6 thoughts on “Using Print | Python for Beginners [5 of 44]

  • September 20, 2019 at 4:27 pm

    print() ->shows () this on console, print("") only shows the blank line

  • September 20, 2019 at 5:23 pm

    I prefer double quotes. Makes more sense to me.

  • September 20, 2019 at 10:14 pm

    Got stuck at lesson 6; python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or

    operable program. Check the spelling of the name, or if a path was included, verify that the

    path is correct and try again.

  • September 20, 2019 at 11:28 pm

    How do i see my output in the console ? is there a compile button?

  • September 21, 2019 at 8:08 am

    When ran with Python 3.8, got the following error: python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or

    operable program. Check the spelling of the name, or if a path was included, verify that the

    path is correct and try again.
    I then downgraded to 3.7 and success.

  • September 21, 2019 at 9:17 am

    The auto generated CC is correct, but the pre generated CC is for the next video


Leave a Reply

Your email address will not be published. Required fields are marked *