Sunday, February 3, 2013

Print("Hello") to Python Code!

    Programming is easy when you find the code that is simple to you. Python might just be your answer! It's a very simple code, and it's easy to memorize how it works, since it's not much like a secret code or anything. Although things may get tricky at times, you can always find your way through. Learning from your mistakes makes you better at Python! Never give up, always keep going, and you will be great at this, and someday think, "Why did I think this was so hard?"
    Python is a great program to learn. It's simple, fun, and hard to forget. But, first, you need to figure out how the basics work before you get flowing. Download Python 3.3.0 at this link (sorry to say this, but the people who run that site have discontinued the version I'm teaching you. If you know a link that works, please tell it in the comments. But for now, you'll have to figure out the differences between this version and the other version you choose, latest one is recommended). After you have that downloaded, try typing this in Python IDLE:

print("Hello World!")

     This is what the output should be:

Hello World!

     Printing "Hello World!" as the first thing you do is a tradition in learning how to program. You are now following an old tradition!
     The print statement tells Python to display whatever is in the parenthesis. This is the most common thing you'll do in your programs.
     Now let's try something else. Type this in Python IDLE:

print(5 + 3)

     This is what you should get:

8

     So, Python can do math, too. What if we edit the problem a little? Try typing this in:

print("5 + 3")

5 + 3

     Why did Python display 5 + 3 instead of doing the problem? Well, whatever is not in quotes, the print statement treats it as a math problem. Whatever is in quotes, the print statement actually copies. We're going to do a quick tutorial on how Python works with math. Try typing this:

print((20 - 4) / (9 * 10) + 4)

4.177777777777778

     Now, what happened here? Well, Python IDLE gave us an answer that's so exact, we don't get it. The / symbol means divide, and it looks like there's a remainder in our problem. Python doesn't do remainders unless told to, so it makes the remainder small enough to fit it into the answer. But if we're looking for an answer we can understand, we would type this in:

print(round((20 - 4) / (9 * 10) + 4, 0))

4.0

     The round() function tells how many decimal places to round the answer to. The 0 is the number of decimal places the answer is being rounded to. Python is great for solving long and confusing problems. But what do the other symbols mean? The / symbol means divide, as we've said, the + symbol means add, which we all know, the - symbol means subtract, which is, again, obvious, and the * symbol means multiply. Also, we haven't shown this symbol yet, but just so you know, the % symbol gives you the remainder of a division problem. Try out symbol math problems. If they don't work, you might want to use the round() function to round them to 0 decimal places.
     So, does the print function make sense? It displays whatever you tell it. It's the most common command in Python, so it's best you learn that first.
     Let's try another thing real quick. Type this in Python IDLE:

person='Bob'
print(person)

Bob

     How did that happen? Well, we created a variable, which is person, in this case, and gave it a value, which is Bob, in this case. Notice that you didn't put quotes around person? When you do that, Python takes a variable of that name you said earlier, and displays the value. You might think that's useless, but it's not. It is another common thing in programs, but not as common as the print statement. I also used apostrophes instead of quotes. It's a sort of faster way to tell Python that something is a string, or word.
     So, you learned about the print statement, how Python can do math, and about variables and values. Try experimenting with those things and see what happens.


2 comments:

  1. If what I say isn't getting through to you, try getting the book "Hello World! Computer Programming for Kids and Other Beginners."

    ReplyDelete
  2. I may have made some typos in these articles... If something isn't right or I might have used the wrong word, tell me.

    ReplyDelete