From the course: Programming Foundations: Fundamentals

Solution: What's the output? - Python Tutorial

From the course: Programming Foundations: Fundamentals

Solution: What's the output?

(upbeat music) - How'd it go? Ready for the answer? Let's work our way through the code line by line to figure out the output. Starting here on line number one, it's our familiar print statement. This is just going to print out challenge one. Moving on to line number two, it's a blank line, so Python just ignores this completely. Coming down to line number three, notice that it starts with a hash mark, this means that it's a comment. So whenever Python sees a hash mark, it just ignores the rest of the line. No output here either. Next on line number four we come to our very first variable, it's named message and the value is the string, this is going to be tricky. Whenever I come to a variable, I write it down and I keep its current value. So let's create a box for message, and its value, this is going to be tricky. Great. Next on line number five we update the message variable with the value, very tricky. Actually is that what happened here? Did you catch our trick? We created a new variable named capital m message, so let's write that one down. We'll create a new box and it's capital m message, and its value is very tricky. On to line number six. We print out the lower case m message variable. Keep in mind variables in Python are case sensitive so this is only going to print out our very first message variable, and then next we have a hash mark. Once again this is an end line comment so Python is just going to ignore this entire section of the code. Hopefully you're starting to get the hang of how Python ignores white space and comments, so let's move to the next one, line number seven. This is also a blank line, nothing to do here. Coming down to line number eight we have another hash mark, this entire line is a comment, there's no output. Next, on line number nine, we have a new variable and it's named result, let's create a box for it as well. Perfect. And if we look at the value of result it's two with two stars and a three. Did you remember that two stars next to each other represent an exponential operation in Python? If so, that's awesome. So this is two to the third power, or eight. Let's write it down inside of our box. Then on line number 10 we're going to print out the value of our result variable, but first we're going to print two to the third power equals, great. Moving on to line number 11, we're changing the value of our result variable just to a simple subtraction, five minus three. So now we can update result with the value of two. Cool. On to line number 12, here we print out the result, or do we? No, we don't actually, can you spot why? Yes, it's because of the hash mark, whenever Python sees this it's going to treat this entire line just like a comment and completely ignore it. Moving on to line number 13, it's just another blank line, nothing to see here, and then finally, line number 14, we print out challenge complete. Were you able to figure out the output on your own? If so, that's awesome, you're well on your way to mastering the ins and outs of variables and data types. If not, that's okay too, it takes lots of time and practice for most people to really understand how these things work. I'll be including reminders and tips as we go along, and you can always check the exercise files for more practice, but whatever you do, don't give up.

Contents