Would you like to learn how to write computer programs? Would you like to learn how to become a software developer? Would you like to learn how to make a computer do cool things? Python is a computer language, and by learning Python you can do all three of these things. It is super easy to get started, so let's go!
The traditional program that people start with in any computer language like Python is called the "Hello World!" program. You write a program that causes the computer to say, "Hello World!". Here is how you do this in Python:
To run this program, click the run button, which is shaped like a triangle. You will see the program's output, which is the words "Hello World!" Click the pencil icon to go back to the Python code.
You can change the program. Have it say "Hello John!", or add in your name, or have the program say anything you like. Try it! Change the program and run it again. You can't hurt anything. Worst case scenario - if everything blows up, just reload the page in the browser and it will reset to the original form.
There, you have written your first computer program! You can now say to your friends, "I am a software developer!" because you have developed some software right here. It is very simple software, but software nonetheless.
What you want to do now is learn to write more and more advanced software, so that you can make the computer do more and more interesting things for you.
So let's say that you want the computer to write the words "Hello World!" 5 times. One way to do it would be to repeat the "print" command 5 times, like this:
Now run this program by clicking the run button (the small triangle). It works! It prints "Hello World!" 5 times.
That does work, but if you wanted to print "Hello World!" a hundred times, you would get tired of retyping the same command over and over again. There is a better way to do it. You can use a command that tells the computer to repeat something. So the new program would look like this:
That's great - It printed "Hello world!" five times just like the other program, but it only took two lines of code to do it. You used something called a "for loop" to make the computer repeat the print statement 5 times. The "range" part tells the for loop how many times to repeat.
Even better is this: if we change the number 5 to 20 in the program, it will print "Hello World!" 20 times. Like this:
How do you know there are really 20 copies of "Hello World!" there when you run the program? You could count them by hand, but who has time for that? Let's have the computer number the lines for us. Here's what that program looks like:
Run the program and you will see that the lines now have numbers. It worked!
But, if you are a normal person, and you are new to computer programming, you are thinking, "Wait a minute - what the heck is that '%d' thing doing in there, and the '% (x)' thing???" It is not intuitively obvious at all. This is one thing about computer programming that you will come to understand soon. Sometimes, to write computer software, it looks a little weird. You have to write the software the way the language wants you to write it or your program will not run. In this case, this is how the Python language does it, so you write your program to make Python happy. For now, understand that the "%d" is a placeholder in the line for a number that you want to print, and the "% (x)" is the value of the number that you want to print.
You might also be thinking, "I don't want it to print the number 0 up to the number 19. I want it to go from 1 to 20 like normal." There are two ways you could make that happen. One way would be to change the numbers in the range to go from 1 to 21. That will work. The other way would be to change "% (x)" to "% (x+1)". Try both now. You can edit the code above. Change the code yourself and run your new program.
What you will find, as you learn to write computer software, is that you see an example like this, and you remember it. Here you saw an example of how to number the lines in your output. The next time you want to number the lines like this, you will either have that example in your head, or you will come back here and look at this little piece of code in order to remind yourself how to do it.
Play around with this code a little. Edit it to print "Hello World!" a hundred times. Change the text from "Hello World!" to something else. Put the line number at the end of the line instead of the front of the line. Get comfortable change the code and playing around.
What happens if you do something wrong? For example, what if you misspell the word "for" as "forr", or you misspell the word "print" as "prnt" by leaving out the i or "Print" with a capital P instead of a small p. If you make a mistake like that, Python will look at your code and it will not understand what you mean. And it will give you an error message sometime, and sometimes it will simply refuse to run your program. Try it. Misspell the word "print". You will see the error message. Then fix the error and your program will run again.
In the next tutorial, we will learn how to get some input from the user...