Friday, February 8, 2013

Make Life Easier

     Sometimes Python can get really confusing. If you ever need to change a program with lots of code, you're going to forget what the heck you were thinking! A good way to avoid that is to put comments at the end of the lines to help you keep track of what that part or line means. But how do you do comments? It's simple. Just put a pound sign or number sign (#) wherever you want. Anything after that pound will be a comment (unless it's inside quotes, of course). But, what are comments? They are parts in the program that help anyone looking at the code what the lines mean. Comments do nothing, but they are helpful. When you run a program with comments in it, you'll see that they don't show up! So, that's what comments are. There's not much to talk about them...
     Your programs can also have a lot of lines due to all the variables you have. If you have a bunch of variables you want to print or whatever at once, you can put them in a list. Lists are variables that hold more than one value. Here's an example:

family=['Jack', 'Tim', 'Mom', 'Dad', 'Willy', 'Betty']

     This grouped your family names together. It can be a quick way of printing things. Here's what you should get if you say:

print(family)

['Jack', 'Tim', 'Mom', 'Dad', 'Willy', 'Betty']

     So, just plain printing lists might not be a wise idea for your programs. But you can print them out one by one, like this:

print(family[0:2])
['Jack', 'Tim']

     Sometimes that just happens. But, instead of starting at 1, lists start at 0. If you tell Python to print family from 1 to 2, you would get this:

['Tim']

     So, Jack is 0, Tim is 1, Mom is 2, Dad is 3, Willy is 4, and Betty is 5. However, you didn't get Mom in the list! Remember, in lists, you don't get the last one when you print it. You would only get 4 of them if you typed "print(family[0:5])."
     If you don't want the square brackets or apostrophes (or quotes, if you used those), type this:

print(family[1])
Tim

     You can only do one at a time, though, if you don't want those. When you put more than one number there, it gives you that. Look at the difference here:

print(family[1:2])
['Tim']

     When you put that other number there, it thinks you want it to display more than 1 item from the list. Only putting one number, though, tells Python that you only want 1 thing displayed there.
     The first time, we got back an item or something from the list. The second time, we got back the list with that item. A single index (Index means the position of something. The plural of index is indices, but some people also use indexes as the plural. EX: If you're 7th in line, your index in line is 7. But if you're the 7th in a Python list, your index is 6, because Python list indices start at 0.) was used the first time to get 1 thing out of the list. The second time, we used something called slice notation (slicing a list is when you get only part of it, like when you typed "print(family[0:2])." A slice notation is the square brackets.) to get a 1-item slice of the list. More will be told about lists in the next article.
     Another thing to make programming easier is making your lines shorter. If you get a really long line and need to scroll to the side to read it or fix it, you can make it smaller with the back slash character, which is \. In a program with a long line, just put a \ wherever then press Enter to make that line shorter. Keep making those back slashes until you think that line is divided up enough. However, that doesn't work too well with the print statement. If you want to make one of those shorter, you will have to put end="" where you think it is appropriate to make the line shorter. That end="" lets the program know the print statement isn't finished yet. However, you can't put that around the quotes. Something you should have figured out from the last article is that you can put variables and other things in between the print statement. After the quotes, put a comma, then your variable or math problem or whatever, then put a comma after that, then you can continue the string.
     Now, there's a quick function I must name: type(). This is a function that tells you the class of things. Type() is a simple and sort of useless function. Let me show you how to use it:

type("Bob")

<class 'str'>

     So, when we put "Bob" in the parenthesis of this function, it told us that it is a string. Let's try some others:

type(3)

<class 'int'>

type("3")

<class 'str'>

     What happened here? Well, anything that is put in quotes or apostrophes, Python takes as a string. Now, let's try this function with our list:

type(family)

<class 'list'>

     Well, we already new that was a list, but type will tell you the class of anything. Now, what about the items in the list?

type(family[4])

<class 'str'>

     Like I said, everything in quotes or apostrophes are strings. Well, I guess that wraps up the type() function.
     So, in this article, you learned about comments, lists, and shorter lines. Try these to test what you know:
     Create comments in the Multiplier program from the last article. Make the first print statement shorter. You don't have to try anything with lists if you don't want to, since you've experimented a lot in this article.

Tuesday, February 5, 2013

Programming Time

     Another tradition in learning how to program is writing one you don't even understand. In Python Shell, go to "File," then click on "New Window." Then write the following program in the new window:


import random

prob1=0
prob2=0
guess=0
incorrect=0
answer=0

print("""Let's see how well you know multiplication! You have to get as many right as you can. When you miss 3, you lose.""")
while incorrect < 3:
    prob1=random.randint(2, 12)
    prob2=random.randint(2, 12)
    answer=prob1 * prob2
    print("What is", prob1, "X", prob2, "?")
    guess=int(input())
    if guess == answer:
        print("Correct!")
    elif guess != answer:
        print("That is not correct.")
        incorrect=incorrect + 1
    if incorrect == 3:
        print("You missed 3. Better luck next time!")


     Save the program. Name it whatever you like (I named mine "Multiplier"), but you have to put ".py" at the end, so your computer knows it is a Python program. Now go to "Run", then click "Run Module F5." The program will start running in Python Shell. You will see something like "====================RESTART===================." That tells when a program starts running. If nothing shows up, press Enter once or twice, enough times to get the program to start running. You should get what is in the print statement, then a multiplication problem, each factor a random number from 2 to 12. The answer is the product of the numbers. You should keep getting random problems until you miss 3, which will end the program. Also, for the indents, most programmers use 4 spaces, so it's best you do the same thing.

     Now, there's a few things I forgot to mention in the earlier article about basic Python. You can even put words together! Try this:

print("Cat" + "Dog")

CatDog

     That seems a little useless, but it can be very useful. There's a good use for everything in Python. You can even print words a certain amount of times. Here's an example:

print("Cat" * 5)

CatCatCatCatCat

     Again, seems useless, right? It can be important as well. Also, you probably don't even have to use the print statement. Try typing this in Python Shell:

"Cat"

'Cat'

     So, just doing that is like the print statement, except this time, there are apostrophes around "Cat."
     In this article, you created a program. Try reading your program and see if you can understand it, at least a little. It will help.

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.