yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Print statements and adding values | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

Programs manipulate data in the forms of integers, floats, booleans, and strings. But how do they manipulate data?

Perhaps the most obvious thing we can do here is add values together. But in order to do that, we'll need the plus operator. In programming languages, operators are symbols that act on values. For example, here we have the value five, which is an integer, the operator plus, and the value two, which is also an integer. Together, these form an expression.

You can think of an expression like a phrase; it's not quite a sentence, but it combines words together in a meaningful way. When a computer encounters an expression in a program, it will try and evaluate that expression down to a single value. Think evaluate single value. This is similar to humans; when you saw the expression 5 + 2, you probably did that calculation in your head and said, "Oh, that's seven." The computer does the same thing; it tries to put every expression in simplest form so it's easier for it to work with.

So what happens when we add different data types together? An expression that adds an integer and an integer evaluates to an integer. That makes sense because if you add 1 plus one, you're going to get two, and two is an integer. You're never going to get a random fraction that appears out of nowhere, and you're definitely not going to get a string.

When we add two floats together, similarly, we get another float. What happens when we add an integer and a float together? We get a float. Because if you add 1 plus 2.5 together, you get 3.5, and 3.5 is a float because it still has that decimal portion; it didn't go anywhere.

You're right that two strings make a string, but adding strings is called string concatenation. Concatenating two strings means that we just kind of smoosh them together and then put quotation marks around the whole thing. So the last character of the first string goes right up against the first character of the second string. There's no magic here of inserting spaces or punctuation or anything, so it'll literally just get smooshed right together.

Last few odd cases here: What happens if we add an integer and a string or a float and a string? Turns out that's an error; the computer doesn't know what to do with that. Similarly, I wouldn't know what to do with that. If you asked me to add a word and a number together, I'd be like, "Huh?" What the computer has that same kind of reaction; it just kind of throws its hands up in the air and is like, "I don't know," and blows up.

Last case: What happens when you add booleans? Turns out this isn't something that programmers do often, so we're not going to talk about it too much here, but if you're curious, try it out.

Let's write our first expressions in the code editor itself. We're going to start with the line 99 + 100. Now, it's probably obvious that this expression should evaluate to 199, but let's run the program and see what the computer says. Huh? Nothing? Why isn't it telling us the answer?

Turns out computers are pretty literal creatures. You can think about it like a stubborn younger sibling. You're like, "Will you evaluate this expression?" and the computer's like, "Yes." And you're like, "Okay, well what is it?" And it's like, "Well, you asked me will I evaluate it, and I did." You didn't ask me will you tell me what this expression evaluates to.

To add that extra instruction to ask the computer to tell us the result, we need to use the print function. The print function is the instruction that says display whatever is inside these parentheses in the console.

We can print single values like we could print 8, or print "hello," or print 5634. Or we can print the result of an expression. So if we put an expression inside the parentheses here, the computer is going to evaluate this expression down to a single value first and then print that result in the console.

So let's try this again. We have print 99 + 100, and if we run the program now, we see 199 show up because we asked the computer to tell us the result.

I can do this again with a few other expressions, and you can see each time I hit run, we see the results of those expressions, what those expressions evaluate to, show up in the console.

Some quick things to pay attention to here: Notice that I put each instruction on a separate line. The computer gets confused if I tell it to do multiple things at once. I can't put print here and print here. If I try running that, it blows up. Every time we have a new instruction—in this case, a new expression or a new print function—it needs to go on a separate line.

Also note that the output in the console corresponds to the order in which the print functions appear in the program. That's because the computer is going through the program in order, and the value for each of these print functions appears on a separate line in the console. So each time it prints a value, it's skipping to the next line in the console and printing that value on a line all by itself.

Now you try: open up the code editor and experiment with evaluating some expressions of your own and printing the results.

More Articles

View All
Find new inspiration with these time-tested approaches to creativity | Anthony Brandt | Big Think
Human beings have one foot in the familiar and one foot in the novel. And we’re actually biologically built to be that way. We don’t want to run on autopilot. We don’t want to live Groundhog Day, doing the same thing, saying the same thing to the same peo…
Starbucks Stock: Are Silly Incentives Burning Shareholders? (w/ @HamishHodder)
[Music] Hey guys, welcome back to the channel. We’re continuing on with the new money advent calendar for yet another episode and yet another collaboration. This time I’ve got someone that you guys would be very familiar with, and that is of course Hamis…
History vs. Tamerlane the Conqueror - Stephanie Honchell Smith
He was born in the 1330s in the Chaghatayid Khanate, formerly the Mongol Empire in Central Asia. On the unforgiving steppe, he rose from a lowly sheep thief to become one of history’s greatest conquerors, uniting nearly all of Central Asia, Afghanistan, a…
Co-Founder Equity Mistakes to Avoid | Startup School
[Music] Hello, I’m Michael Cybal, and today I’m going to talk about co-founder equity splits and co-founder breakups. To be clear, we want people who are building tech software startups that they expect to be VC funded. You know, this is advice for you. …
Atheists don't hate God, no srsly!
Hi Yob, I’m sure I’m not the first person to have tried to explain these things to you, but uh, I’m going to do it all the same. Atheists were never angry with God. I can see why you might be getting confused. Uh, we do seem to be making fun of God from …
“Drug use is your birthright” | Dr. Carl Hart
The Declaration of Independence is the founding document of the country. It’s not a law; it’s the ideal which we are striving to get to. The major ideals that are espoused in the Declaration are that every human has at least three birth rights: life, libe…