Introduction to Programs Data Types and Variables Writing a basic program. Basics of data types, variables and conditional statements
Introduction to Programs Data Types and Variables
⇐ Use this menu to view and help create subtitles for this video in many different languages. You'll probably want to hide YouTube's captions if using these subtitles.
- What I want to do in this video is to expose you
- and introduce you to the idea
- to what a computer program is.
- And just in case you want to follow along
- I highly recommend you do that
- because the real way to learn computer science
- is to really fiddle with things yourself.
- This is a Python environment
- so I'm going to be doing a lot of the programming in Python.
- And right here, this environment is called PyScripter.
- It's free. It's an open-source piece of software.
- And I'm using Python..Python...
- I believe I'm using Python 2.6 or 2.7.
- As long as you are using Python 2
- your examples will be the same as mine,
- they work the same way.
- But If you're using Python 3
- you are going to have to use slightly different variations
- every now and then to make it work properly.
- I'll try to make notes for those when they occur.
- So let's just start writing ourselves a computer program.
- What's cool about this is,
- we can write our computer program right here.
- And really we are just editing text in a file.
- That's all it is.
- It's a set of instructions
- and the computer is going to start for the most part.
- At the top of this file and just go down
- and read these instructions.
- Although you will later on
- that there's a way to tell the computer
- to jump around and to loop around within the instructions,
- so that it can do things over and over again or skip other thing.
- With that said, let's write ourselves a simple program
- and while we do this,
- we will expose ourselves to some of the core concepts
- that exist within a computer program.
- Let me write a very very simple computer program.
- So one very simple computer program
- would literally just be an expression.
- So let me just write 'print 3+7',
- so it's literally just going to take 3+7 and print it.
- It's going to pass it to the print function
- which comes with Python.
- Maybe I will write it like this: print(3+7)
- Let's save this file. So there's literally only one..
- if you think about it, only one command
- here on the top line here. That says print 3+7.
- Actually, let's add another command,
- just so you can see that it's going to go top down.
- Let me add another one: print(2-1)
- and then let's do: print("this is a chunk of text")
- Let's see what this computer program right here
- is going to do.
- So let me save it.
- So let me save it.
- I saved it as the file "testarea.py". Tells a... a...That's a...
- The .py extension signifies it is a Python file.
- Now let me run the program.
- What's nice about this development environment,
- this IDE or Integrated Development Environment,
- is that you can kind of type and run your program in the same place.
- It also color-codes your text,
- so you can see what's a function, what's not a function,
- the different data-types...
- we will talk about more data types in the future.
- Let's just run this program to see what happens.
- So there we go, we ran it!
- So it printed 10 [ten], then it printed 1 [one],
- then it printed "this is a chunk of text"
- So it did exactly what we told it to do.
- And it did it in the order.
- It started up here, it evaluated 3+7 as equal to 10 [ten]
- and it printed it, it printed 10 [ten] here.
- and then it printed 2-1,
- and then it printed "this is a chunk of text"
- Now one thing I want to introduce you to, fairly early on,
- it's the idea of data types.
- So even when you saw this example,
- you might have the gut feeling that
- look, there is something kind of different
- about a 3 [three] or 2 [two] or 1 [one] or 7 [seven] and this chunk of text.
- This is a number...I feel like, I can just kind of add numbers.
- They're representing some type of quantity.
- While this over here is representing a chunk of text.
- And your intuition would be right.
- These are different data types.
- The 3 [three] and 7 [seven] and 1 [one] ... these are numerical literals.
- In this particular case, they are integers.
- And you can..in this one over here,
- this is actually a String,
- which is a word you hear a lot in computer science.
- this is really..referring to a string...of characters.
- and in Python we can actually ask
- what are the types of these things.
- So you can pass them to the function "type"
- so now it should print the type of 3+7, not just 10.
- Let's try that. I'll just print 2-1 to just show you the difference.
- Then I'll print the type of this chunk of text.
- The type of this chunk of text.
- And so let's save it. I just type CTRL+S,
- That's a shortcut to save this.
- and then I'll try to run this program.
- So there you go.
- It evaluates this statement.
- To evaluate this, it starts at the inner parenthesis.
- 3+7 is 10. Then it tries to take the type of 10,
- which is a type int, then it prints that type int.
- You see it right here. It says type 'int'.
- int is short for Integer.
- Then it says print(2-1).
- It does that on this line right here,
- prints 1,
- and then it prints the type of this whole thing right over here.
- So instead of printing itself,
- it prints its type. And its type is a String.
- Now the next thing I want to introduce you to
- as we just fiddle our way experimenting with programs
- is the idea of a Variable.
- Because one of the things is we are going to want
- to store these things in different places.
- We will learn in future videos that in Python
- it's more like we will have labels for these things,
- and the labels can change.
- Let's see, or we can put them in different types of labels.
- So let's write a completely different program using variables.
- So let's ... What's cool about Python
- some people don't like it, is
- you can put any type of data in any type of variable.
- So you can say a=3+5,
- then we can say b=a*a-a-1
- [note: * means "times", it is used for multiplication.]
- and then you can say c=a*b
- Then you can have something like...
- I will put some space here just to make it a little bit cleaner.
- c = a*b
- Then we can say, let's print c.
- So if you want, you can go ahead
- and try to figure out what c is going to look like
- or we can just run this program.
- So let's run the program first
- and then we can go back to see
- if it actually did the right thing.
- So I'm going to save the program,
- and now I'm going to run it.
- We got 440 for c. Let's see if that makes sense.
- So 3+5 is 8. So the label "a" will refer to 8.
- So any place in the program, until we redefine "a",
- any time you use "a", it's going to say: a is 8. a is referring to 8.
- So when you go down over here, we're defining "b"
- it'll say OK, a*a. It uses order of operations.
- So in order of operations, you do your multiplications first.
- Especially when you're comparing against subtraction.
- So a*a that's going to be 64.
- Then we have 64 - a is 64 - 8, is 56. Minus 1 is 55.
- So "b" is 55. And "c" is going to be a...which is 8.... times 55...
- And 8 times 55 is indeed 440.
- So it all worked out.
- So maybe you want to see what happens
- when you get different "a"s.
- You can try that out.
- you can just change what happens here for the different a's.
- So maybe we'll have a is equal to ...
- Let's make it equal to -6
- Now let's run our program to see what happens.
- We get -246. And you can verify it by yourself.
- You go line by line, and have these variables refer to
- what they are defined to be referring to,
- and see if you get this response right over here.
- Now, if programs were just a bunch of commands
- and you just always go straight through,
- you wouldn't be able to do really interesting things.
- So to do really interesting things you are going to
- start seeing things like Conditionals and Loops.
- And Conditionals and Loops are something like
- Let's do it like this
- So...if....so I'll just leave that stuff over there.
- And we'll say "if (a<0):". Maybe we will print(c)
- And If or "else:", print ... or otherwise ...we'll print (c-a).
- So this is interesting. You might already have a gut
- for what's going to happen here. Let's save it.
- It's amazing how much you can get done
- with just these conditionals. So this is saying
- if "a" is less than 0, do this,
- Otherwise if "a" is not less than 0, do this over here.
- So notice we are not going just straight down.
- Depending on whether "a" is less than 0 or not,
- it's going to either execute this line,
- or it's going to execute this line.
- And the way that Python knew to only execute this line,
- if "a" was less than 0 is it's indented here.
- And the indent is part of this clause.
- The way it knows that there are new clauses forming right here
- is this colon right over here.
- And then the way to know what to execute
- If none of these happens
- If "a" is not less than 0, then it's within this "else" clause.
- And If you want to do something else after this,
- regardless of whether "a" is less than 0 or not,
- You can just take it out of the clause
- by getting rid of the indentation.
- So now we can just print
- "we are done with the program".
- Actually, let's do add some other stuff in this clause.
- So let's print here "a<0".
- So notice: this is not going to be evaluated.
- We have this inside of a string,
- so it is just going to print that thing.
- And then over here we will say print("a is not less than 0")
- This is an interesting program. Let's just run it now. Alright.
- Let's hope it runs. I saved it. Now let's run the program.
- And it says, it printed "a<0",
- -- so we could scroll up a little bit --
- It printed...so this is, we run the program..it printed 'a<0'.
- so it shows we are inside of this clause.
- Then it printed this. Then it printed "c", which is -246.
- It does not execute this,
- because this needed to be executed only if a was not less than 0.
- But then it breaks out of this clause
- and prints this no matter what:
- "we are done with the program"
- Let's just change "a" to try to see
- if we can get this other clause to break.
- Let's make "a" greater than 0.
- So let's make "a" equal to 9 and now let's run the program.
- So there. "a" is 9. So it says "is a less than 0?".
- Well, 9 is not less than 0.
- So it's not going to execute this.
- It's going to go to the else clause.
- So it's going to print "a is not less than 0"
- which it did over here. Then it printed c-a
- which is 630. It breaks out of that clause.
- And regardless of whether "a" is less than 0 or not,
- it prints "we are done with the program".
Be specific, and indicate a time in the video:
At 5:31, how is the moon large enough to block the sun? Isn't the sun way larger?
Have something that's not a question about this content?
This discussion area is not meant for answering homework questions.
Share a tip
When naming a variable, it is okay to use most letters, but some are reserved, like 'e', which represents the value 2.7831...
Have something that's not a tip or feedback about this content?
This discussion area is not meant for answering homework questions.
Discuss the site
For general discussions about Khan Academy, visit our Reddit discussion page.
Flag inappropriate posts
Here are posts to avoid making. If you do encounter them, flag them for attention from our Guardians.
- disrespectful or offensive
- an advertisement
- low quality
- not about the video topic
- soliciting votes or seeking badges
- a homework question
- a duplicate answer
- repeatedly making the same post
- a tip or feedback in Questions
- a question in Tips & Feedback
- an answer that should be its own question
about the site