Homebrew Python Tutorial Part 1
What you will learn:
- What variables are and how to assign data to them.
- What the function 'raw_input()' does.
- What 'while' and 'for' loops are.
- What 'if statements' are.
- An assignment to test what you've learned.
What are variables and how do we assign data to them?
Variables contain data, or actually they refer to a specific area with data. A variable takes the data assigned to it, variables are quite important. That's because we name variables, and we can change the data assigned to a variable. One more thing, variables have certain rules bound to the naming process. Variables cannot start with a capital letter and they are case sensitive.
So let's get to work with them; the best way to learn is to practice:
Open IDLE or CMD and type:
moo = 666 #Our variable is now assigned to a number; 666.
print moo
As you see moo displays as 666! Now let's try the following, with the same variable:
moo = "Hai, lads" #Our variable is now assigned to a string.
print moo
Did you just see that?! Unbelievable! Our variable 'moo' just got assigned a new piece of data and it lost the other! Do you remember the part when we did these math things with the print function? We can do that with variables and text too!
Let's first try that without variables:
print "He ran and " + "ran" #Easy enough to understand right?
print "He ran" + ("and ran" * 6)
Now let's try it with:
moo = "He ran"
moo2 = "and ran"
print moo + (moo2 * 6)
They both look the same.
Gosh, I totally forgot to tell you something important about Python math:
m = 13
print m
m = m + 5
print m #Weird huh? But just see it as 'm' becomes 'm' + 5 and we'll be alright.
What is 'raw_input()'?
This is such a fun function, and it opens up a lot of options. The raw_input() function is a function that collects or needs user input. This is an important function, as we're now able to create an interactive environment:
response = raw_input("TELL ME URZ NAME! NOW!11!!") #We just saved the response of the user, in the variable 'response'.
print "Hai, %s, yu liek mudkipz?!11!" % response #We used the string operator '%s' to display our 'response' variable
#in the middle of another string.
All kidding aside, we can use this to create a calculator application:
sub1 = int(raw_input("What is the number you want to subtract from? "))
sub2 = int(raw_input("Enter how much you would like to subtract: "))
print "%d - %d = %d" % (sub1, sub2, sub1 - sub2)
We wrapped raw_input() in another call, called int(),
int() stores the input as data and packs it together so if the user types "1 2 3" it gets packed as 123.
'While' and 'For' loops.
Loops help to shorten code; look at this example for the 'for(each)' loop:
for i in range(1,6):
... print "%d x 8 = %d" % (i, i*12)
...
(Look carefully at the indentation, and don't add the dots. IDLE automatically adds them when code spans more than one line)
If we translate that to English we would get something like this:
"For each number in range from 1 until 6, display results for number 1 until 6 x 8."
We can also do this with letters/words:
for l in 'cow': print l
"For each letter in cow, print letter"
and with full words:
for word in ('I','HAF', 'FREE', 'BOOTS'): print word
If we put a comma after that line, it will display them one after another on the same line, as the "," disables the print function's ability to make new lines.
We won't use 'For' loops that often, so we will move on to a more important loop, the "While" loop.
Let's take this as an example:
>>> c = 3
>>> while c <= 12:
... print "%d + 12 = %d" % (c, c+12)
... c = c + 1
Meaning as long as c is less than 12, the loop will execute. c = c + 1 increments the variable, so every time c goes back to the while loop, it will have a higher value.
We can also put different things after the while loop:
== means equal to.
!= not equal to.
<= smaller or equal to
=> bigger or equal to.
Your best friend: The If Statement
The 'if' statement is a commonly used conditional statement. It is fairly easy to understand and fairly easy to use.
print "Luls, test!"
j = 5
if j > 10:
print "This is never printed"
else:
print "This gets printed"
So, "if j is bigger than 10 it won't print "This is never printed", but it will print "This gets printed" (The else-statement!).
We can combine < and >:
j = 10
if (j < 20) or (j > 20):
print "Out of range"
And finally...
The Assignment!
menu = """
Pick an operation(1-3)
1)Add
2)Subtract
3)Multiply
4)Quit
"""
operation = int(raw_input(menu))
while operation != 4:
if operation == 1:
resp1 = int(raw_input("\nFirst number?"))
resp2 = int(raw_input("\nSecond number?"))
resp3 = resp1 + resp2
print resp3
operation = int(raw_input(menu))
Oh noes! My script is not yet finished!!
You need to do that!
And you could with all the stuff you've learned.
The only piece you need to concentrate on is:
if operation == 1:
resp1 = int(raw_input("\nFirst number?"))
resp2 = int(raw_input("\nSecond number?"))
resp3 = resp1 + resp2
print resp3
Don't mess with the others and keep the same structure.
(Tip, start out with. "if operation == 2:)
If you are done with it, or are having problems with it, CMS me, and I'll look it over.
I hope you learned something from my tutorial! |