Python | Lesson 1
Hey guys, this is M Kids, and on with Python Lesson One. So this is going to be the first of many Python tutorials that I'm going to be making, in which I will teach you the basics to pretty cool stuff in Python. Not to say that the basics aren't cool.
So the reason I'm doing this specifically with Python is because I made some tutorials on other languages, and people, you know, requested languages; as you know, they want to learn something. So Python was the most requested language, and it's pretty cool. I learned it, and I'm going to teach it to you.
I'm going to be using terminal to code in Python. Terminal is a good environment, in my opinion. If you want to use it, that's all right. If you don't want to use it, then you can use a text editor or something else. But I'm going to be using terminal to program in Python.
So we're going to start out by editing our first Python application. Now, on Unix, there's one liner code before every Python program you want to put, and that is #!/usr/bin/python
. That is just for executable stuff. You don't have to understand that; that's really not part of Python itself.
Under this is where all our code actually goes. So let's say we want to make a Hello World application. That's normally the first thing anyone makes in a programming language. So what we're going to do is do print "Hello, World!"
. So this will print out the text "Hello, World!".
You see, we have print
, which is a command. We have left quote "Hello, World!" right quote, and this will print out the text "Hello, World!". So to run this, we type python <space>
and then the file name, and it'll say "Hello, World!".
All right, so we've gotten started; we're doing our first Python program. Now, let's say we want to make the user enter their name, and then it'll say hello to them. What we're going to do is say, "Enter name, please." Now, we're going to say name = raw_input()
. Now we're going to say print "Hello, " + name
.
So what this does, essentially, is print out "Enter name, please," then it creates a variable called name
and assigns it whatever they type in. A variable is like in math; something that can change, that's normally represented with something that's not a number.
Now, in math, variables can only be one letter; in programming, they can be any length or, you know, they have a limited length, but you know it can be longer than one letter. So here, we're assigning name
to be whatever they type in that happens to be called raw_input
, and then this following line prints out "Hello, " + their name + "!".
So you might wonder how does this last line work? The answer to that is that it takes the string "Hello," then it adds onto it name
, which is also a string, and adds onto that the string "!". Now, a string is a piece of text; it's a string of characters, essentially, and that's why it's called a string.
So, you're going to be hearing that term as well as int
, which stands for integer, which is a whole number a lot in programming in general. So it's good to know those. So if we run this right now, enter name "Alex," and it says "Hello, Alex."
So we've already gotten input from the user; it's pretty impressive, and it only took three lines of code. So let's say now we want to ask them for their age, and based on their age, say something that we think about them.
So we're going to do from there is print "Enter your age, please." We're going to get it in years, so we're going to say age = raw_input()
. The thing is, the age is going to be a string; we can't really use it as if it's an integer if it's a string.
So we're going to say n_age
, which stands for number age, equals int(age)
. And what this essentially does is convert the string age
to an int. So there's a function called int
which, you know, you give it age
, and once again, if you've done functional notation in math, you will understand this a bit better.
So we've done this. Now we're going to say if
—and this is just like if statements on a programmable calculator, for instance. So it's just comparing things, and it's going to do something based on what we're comparing. So we're checking if...