Functions. What are they?
In math you know a function as f(x)=3x+3. substitute any number for x and you get a value. Typically a function is used to define something, such as the rate of change in speed of a car, or a runner. One variable is time, the other is speed. If you plug the value in for one your can get the other, as long as they follow the rules of the function created to define their motion (which for now we are assuming they do). If you wanted to write that same function in Python, and all you wanted it to do is print out the value, you could write:
>>>def function(x):
print(4*x+2)
Now every time you want to call the function, you only have to type, say...
>>>function(3)
14
The value x in "def function(x)" replaces the x in the indented lines following the defining statement and your function runs, printing 4 * 3 + 2. But the function doesn't have to take a value.
>>def declaration():
print("When in the Course of human events.")
Here the function just prints out what the function is. A benefit to functions is that you can modularize your program. You can break it up into it's basic functions which can then be used over and over again, not only in that program but in others as well. If you create a function that asks for a user name, you can use that in any program that requires the user to input their name.
You can also nest functions. Such as...
>>>def repeat(f):
f()
f()
This function takes a function name as it's input and outputs that function twice.
>>repeat(declaration)
When in the Course of human events.
When in the Course of human events.
You may have noticed when you typed repeat( ... a helpful box comes up with (f), reminding you that the function takes f as it's value. But this isn't always helpful. You could instead write a docstring, or documentation string, giving more helpful information to remind you and others what to input here. You do this by placing three double quotes before and after as shown.
>>>def repeat(f):
"""Input a function name here"""
f()
f()
This is what it should look like. There are many, many uses for functions in programming. We'll be using functions in most, if not all, of the posts from here on out.
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Saturday, September 18, 2010
Thursday, September 9, 2010
Declaring Variables w/ Python
Hello again! It's Thursday, which is one of my favorite days, if only because it means Friday is almost here! I'd like to end my Thursday with a lesson on variables. Before we get started however I thought it would be a good learning experience, for myself and perhaps others, to do my logic work in psedocode, and actual code (and perhaps a flowchart). For the "real code" I'm going to use Python.
There are a few really good links for help on Python programming. One is from the Python website itself here. As well as from the Introduction to Computer Science and Programming course on MIT's OpenCourseWare (OCW) website. The MIT OCW website has many classes and programs you can go through free of charge and look very interesting. Harvard also has a similar setup here and it looks like they use C whereas MIT is using Python in instruction.
If you want to follow along with me you should go to the Python website and download version 2.7 If you prefer to use 3.0 that's fine but 2.7 is backward and forward compatible whereas 3.0 has gotten rid of some things that I may be using since I'm following the MIT OCW course, recorded in 2008.
Once you've installed Python, bring up the Python Shell also called IDLE (Python GUI) which should look like this:
From here you can input all the commands which I will preface with >>> to look the same as the Python Shell.
Now, onto the lesson: Variables are memory locations, the contents of which may vary throughout the program. For example:
>>>numberOne = 1 #Hit Return
*Note - # is Python for comment. Meaning, typing it would not hurt, but does nothing for you. I am only letting you know here to hit return to enter 1 as the value for numberOne.
This sets a memory location, nicknamed "numberOne" to the value of 1. To write this in pseudocode you can write it the same "numberOne = 1" since this is a simple statement, or to be more clear:
set numberOne = 1
Remember I said variables can change. We can now type in the Shell:
>>>numberOne = numberOne + 1
This may look weird, but it should be simple enough to see what is happening. We are taking numberOne and setting it equal to numberOne + 1, or just adding one to it. If you type:
>>>print numberOne
You should now get the result: 2.
The last thing I want to mention briefly is the data type of this number:
2
The number 2 is an integer or int, and has no decimal value. In Python, and most other programming languages, this is important. The type is decided when you initialized the variable. To make this make sense try typing 7/2 into the Python Shell and hit return. Notice the answer is not 3.5 but 3! An integer has no decimal value, and your attempt at dividing, which would create a decimal is ignored and it throws out the remainder.
If you are writing a program that requires the decimal values, you can use what is called a floating point or float. To make it a float, just type 7.0 (the .0 part making it float) and divide that by 2. What do you see? 3.5!
Be careful when writing your programs that you keep track of this. There are ways in many programming languages to change the type, but doing so unnecessarily can cause leaks and bugs in your program.
Lastly, a neat trick to see the remainder of the integer division is by using the % sign. Using % divides one side by the other and gives the remainder.
>>>7%2
1
Tomorrow I'll try and post something a little more... interesting to program. Until then enjoy the tutorials and let me know what you think!
There are a few really good links for help on Python programming. One is from the Python website itself here. As well as from the Introduction to Computer Science and Programming course on MIT's OpenCourseWare (OCW) website. The MIT OCW website has many classes and programs you can go through free of charge and look very interesting. Harvard also has a similar setup here and it looks like they use C whereas MIT is using Python in instruction.
If you want to follow along with me you should go to the Python website and download version 2.7 If you prefer to use 3.0 that's fine but 2.7 is backward and forward compatible whereas 3.0 has gotten rid of some things that I may be using since I'm following the MIT OCW course, recorded in 2008.
Once you've installed Python, bring up the Python Shell also called IDLE (Python GUI) which should look like this:
![]() |
| IDLE (Python GUI) / Python Shell |
Now, onto the lesson: Variables are memory locations, the contents of which may vary throughout the program. For example:
>>>numberOne = 1 #Hit Return
*Note - # is Python for comment. Meaning, typing it would not hurt, but does nothing for you. I am only letting you know here to hit return to enter 1 as the value for numberOne.
This sets a memory location, nicknamed "numberOne" to the value of 1. To write this in pseudocode you can write it the same "numberOne = 1" since this is a simple statement, or to be more clear:
set numberOne = 1
Remember I said variables can change. We can now type in the Shell:
>>>numberOne = numberOne + 1
This may look weird, but it should be simple enough to see what is happening. We are taking numberOne and setting it equal to numberOne + 1, or just adding one to it. If you type:
>>>print numberOne
You should now get the result: 2.
The last thing I want to mention briefly is the data type of this number:
2
The number 2 is an integer or int, and has no decimal value. In Python, and most other programming languages, this is important. The type is decided when you initialized the variable. To make this make sense try typing 7/2 into the Python Shell and hit return. Notice the answer is not 3.5 but 3! An integer has no decimal value, and your attempt at dividing, which would create a decimal is ignored and it throws out the remainder.
If you are writing a program that requires the decimal values, you can use what is called a floating point or float. To make it a float, just type 7.0 (the .0 part making it float) and divide that by 2. What do you see? 3.5!
Be careful when writing your programs that you keep track of this. There are ways in many programming languages to change the type, but doing so unnecessarily can cause leaks and bugs in your program.
Lastly, a neat trick to see the remainder of the integer division is by using the % sign. Using % divides one side by the other and gives the remainder.
>>>7%2
1
Tomorrow I'll try and post something a little more... interesting to program. Until then enjoy the tutorials and let me know what you think!
Thursday, September 2, 2010
Structured Programming
Structured programming states that any program can be created through the use of three procedures: sequence, selection, and repetition.
1. Sequence: One thing happens, then another, then another... Get bowl, Place bowl, fill bowl with 1 cup cereal, put 1/4 cup milk in bowl, scoop cereal, eat cereal. To make this work each process must be done in order, else you may be eating your cereal off of the table.
2. Selection: Selection is the decision portion of your program. If you don't have a bowl in front of you, then grab a bowl. If you have a bowl to put on the table, then put it on the table. If there is no cereal in your bowl, then put cereal in the bowl... by now I'm sure you get the gist.
3. Repetition: Repetition tells the program to do something again and again as needed (or specified by you, the programmer). At the end you can add a selection, if hungry repeat. You already have a bowl, so the program continues to the next step. The bowl is filled (because you have only taken one bite), you continue until you are either no longer hungry or the bowl is empty, in which case the bowl would refill and you would eat until no longer hungry.
Anything else you want to do can be done by placing one or more of these within themselves (i.e. nesting statements) but every programming element is a composition of one or more of these three.
The examples given are not necessarily complete, there are practically an infinite number of steps that could be inserted (breathing between bites, wait time between bites, chewing, swallowing, etc. etc. etc.), but you should have a general understanding of structured programming.
1. Sequence: One thing happens, then another, then another... Get bowl, Place bowl, fill bowl with 1 cup cereal, put 1/4 cup milk in bowl, scoop cereal, eat cereal. To make this work each process must be done in order, else you may be eating your cereal off of the table.
2. Selection: Selection is the decision portion of your program. If you don't have a bowl in front of you, then grab a bowl. If you have a bowl to put on the table, then put it on the table. If there is no cereal in your bowl, then put cereal in the bowl... by now I'm sure you get the gist.
3. Repetition: Repetition tells the program to do something again and again as needed (or specified by you, the programmer). At the end you can add a selection, if hungry repeat. You already have a bowl, so the program continues to the next step. The bowl is filled (because you have only taken one bite), you continue until you are either no longer hungry or the bowl is empty, in which case the bowl would refill and you would eat until no longer hungry.
Anything else you want to do can be done by placing one or more of these within themselves (i.e. nesting statements) but every programming element is a composition of one or more of these three.
The examples given are not necessarily complete, there are practically an infinite number of steps that could be inserted (breathing between bites, wait time between bites, chewing, swallowing, etc. etc. etc.), but you should have a general understanding of structured programming.
Tuesday, August 31, 2010
An introduction to Computer Logic
Logic is defined as the study of arguments. In logic you are finding the proper way to make a statement that is understandable and is a true statement. A computer program is exactly that, a series of arguments, all with a value of true or false. This makes sense when you think that computer code, binary, is a string of ones and zeros. The computer circuit can read the presence of electrical current as a 1 and absence as a 0. Even the most complex programs can be broken down into strings of true and false.
Subscribe to:
Posts (Atom)


