python project
Let's learn some basic python. Previously we're looked at the logical Prolog code and seen how creative we can be using Object-Oriented Java code. Now we're going to write some technical code.
We're going to write Python code that will ask a user for some numbers, and then count up the number of high numbers. Python can be very confusing. It has a very simple syntax. The most of the syntax is defined by spacing. Rather than the brackets { } that are in Java, spaces define when blocks (like if-statements or functions) start and end. Be very careful with your spacing! If you use some tabs and some spaces, Python won't like it and will throw an error. Keep your spacing consistent throughout your entire program.
On to code!
We're going to write Python code that will ask a user for some numbers, and then count up the number of high numbers. Python can be very confusing. It has a very simple syntax. The most of the syntax is defined by spacing. Rather than the brackets { } that are in Java, spaces define when blocks (like if-statements or functions) start and end. Be very careful with your spacing! If you use some tabs and some spaces, Python won't like it and will throw an error. Keep your spacing consistent throughout your entire program.
On to code!
Let's start with a simple hello world.
First we define a function: "def HighNumberCounter():" This means we want to create a new function called HighNumberCounter and it will not take any input.
Then we simply print "Hello World". If you try to run this program, notice that nothing happens!
SAVE THIS FILE WITH A .py extension!!! Ex: HighNumberCounter.py
First we define a function: "def HighNumberCounter():" This means we want to create a new function called HighNumberCounter and it will not take any input.
Then we simply print "Hello World". If you try to run this program, notice that nothing happens!
SAVE THIS FILE WITH A .py extension!!! Ex: HighNumberCounter.py
After the function (notice how we go back to 0 leading spaces), we add a line of code that calls or invokes this function.
Now when the program runs, Python will read and interpret the HighNumberCounter definition. Then Python finds no additional function definitions, but instead finds a call to HighNumberCounter(). Since Python has already seen the definition for this function, it knows what to do.
So now our program will actually output something!
Now when the program runs, Python will read and interpret the HighNumberCounter definition. Then Python finds no additional function definitions, but instead finds a call to HighNumberCounter(). Since Python has already seen the definition for this function, it knows what to do.
So now our program will actually output something!
How do we run our program?!?!
Unlike Prolog, where we used SWI-Prolog, or Java, where we used BlueJay, we will run Python code straight from the command line. This is extremely Windows specific, but you should be able to do something similar for Mac and Linux.
First we need to open a cmd or command window. In Windows 7, this screen shot shows the easiest way to do this.
Unlike Prolog, where we used SWI-Prolog, or Java, where we used BlueJay, we will run Python code straight from the command line. This is extremely Windows specific, but you should be able to do something similar for Mac and Linux.
First we need to open a cmd or command window. In Windows 7, this screen shot shows the easiest way to do this.
This is a command window. First we need to move to the folder where our code is saved.
We do this using the 'cd' command.
If your folder path has spaces in the name, you have to put quotes around it.
Ex: cd "C:\Users\Mark\Documents\Folder Name With Spaces\Python\More Spaces Folder"
Then we call the program, simply by typing our python file name.
Then it outputs! Huzzah!
We do this using the 'cd' command.
If your folder path has spaces in the name, you have to put quotes around it.
Ex: cd "C:\Users\Mark\Documents\Folder Name With Spaces\Python\More Spaces Folder"
Then we call the program, simply by typing our python file name.
Then it outputs! Huzzah!
Let's make this code more interesting. First let's write code that will ask the user for a series of numbers.
First we need to create an empty list that will hold the numbers the user will input: "numberList = list()". list() is a Python function included in the standard library that will create a new empty list that we will assign to a variable we call numberList.
Now let's ask the user for how many numbers they want in this list, and assign that number to a variable we will call arraySize: "arrazySize = int(input("Please enter size of array: "))". A couple things to note here. First int( ) is used to convert the String we enter into an integer number. We have no error checking here, so if you input something other than a number it will crash (try it!). Second, input("Words go here") prints out the words in quotes to the command line and then waits for the user to type something on the command line and hit enter. Once the user hits enter, the program continues running. Notice that I put a space after "array: ", this will keep the user's input on the same line, I think it just looks nicer.
Now we need to ask for some numbers. We'll do this by using a for-loop. A for-loop basically says: "while a variable is less than a maximum value, do the stuff inside the loop". We'll use two for-loops in this program, and I'll explain both. For this for-loop we're saying "for each loop, create a variable x, run this loop arraySize times". So if the user inputs an arraySize of 3, the code inside the for-loop will run 3 times.
Inside the for-loop: "x = int(input("Enter next number: ")". This is just like the input statement above. We're asking the user for a number, converting it to an int, and then assigning that value to a variable x. Then we add this value to the list variable, numberList: "numberList.append(x)".
Make sure your program runs. It won't output anything, but make sure it doesn't have syntax or other errors!
Now let's add some code to do something with this list. Let's count the number of numbers in our list that are above some threshold. I will randomly pick 50 as the threshold. So first let's create a new variable called "highNumberThreshold" and assign it the value of 50. Now let's create another variable called "highNumberCount" and assign it the value 0; this will be our counter variable to add up the number of values in the list that are above our threshold of 50.
Now we need to add another for-loop to iterate over the values in our list. Notice the for-loop is similar except for the key word "range". For this for-loop we only need to pass in the list, and Python will take care of iterating over it.
Inside the for-loop, we add an if-statement to check if the current number is greater than the threshold. If it is, than we will increment the highNumberCount variable by using the syntax "highNumberCount += 1". This could also be written as: "highNumberCount = highNumberCount + 1".
Now let's write two print statements. One for a nice message and one to output the highNumberCount variable. Go run and test your program!
Now we need to add another for-loop to iterate over the values in our list. Notice the for-loop is similar except for the key word "range". For this for-loop we only need to pass in the list, and Python will take care of iterating over it.
Inside the for-loop, we add an if-statement to check if the current number is greater than the threshold. If it is, than we will increment the highNumberCount variable by using the syntax "highNumberCount += 1". This could also be written as: "highNumberCount = highNumberCount + 1".
Now let's write two print statements. One for a nice message and one to output the highNumberCount variable. Go run and test your program!
As you can see from running this code, it correctly finds that two numbers are above our threshold of 50.
Now let's add some code to find and output the final value in the list that is above the threshold.
Now let's add some code to find and output the final value in the list that is above the threshold.
To find and output the final number in the list that is above the threshold, we first need to create a variable to hold that value. So we create a variable called "lastHighNumber". We could assign it anything. It could be initialized to 0, or as in the code here as the first number in the list. What happens if none of the numbers in the list are above the threshold? Is the program now outputting incorrect data? What is a better way to initialize this?
Now we add one additional line of code in the if-statement inside the for-loop: "lastHighNumber = number". Do you see how this will always hold the very last number in the list that is above the threshold?
Finally, we add other two print statements for the listHighNumber variable.
Now we add one additional line of code in the if-statement inside the for-loop: "lastHighNumber = number". Do you see how this will always hold the very last number in the list that is above the threshold?
Finally, we add other two print statements for the listHighNumber variable.
And here is the output from running this code.
What would happen, if all of the values in the list are below the threshold? Try it!
What would happen, if all of the values in the list are below the threshold? Try it!