Python Resources

Updated on Feb 5 2022

Python Resources

Deborah R. Fowler



Review

Posted on Feb 2  2019
Updated on
April 19  2020

EXERCISE 2 Hurricane is due Class 09 - this is a review of concepts.

Key concepts that we have studied so far in VSFX 160:

During the course of this assignment, it was clear that some were not fully understanding the above concepts. Let me go over these in context of the Exercise. I am posting this before your Exercise is due so be sure to read this over if you are having difficulties.


Variables - are places to store information, it is a good idea to name them something related to what they are storing.

category or cat for storing the category of a storm.
variables are given a value by  using the =

x = 3.0

This means that x is storing the value 3.0, it is assigned the value of 3.0

The variable is on the left (LHS - left hand side) the value or expression is on the right (RHS) of the symbol =.

You can also assign the variable with it's own name in an expression

x = int(x)

It will use the value x currently when evaluating the expression on the RHS and assign it to the variable x, thus x will now be 3

Try this in a python shell now.
So if you have something like the hurricane where you have grabbed the value from your list you could convert it to a number using float or int. ie. myVal = float(modlist[3])


Truth statements (if else) - allows you to select code

In the hurricane exercise a good example of using this is to skip writing the category when it has not changed.
Since you know the current category, in order to see if it has changed you will need to keep track of the previous value. You can do that by putting the value into a variable to save for the next time you go into the loop. 

Think about it, will this work? What case do you not have a previous value? The first time you enter the loop. You can set this to a value that you know will not be read from the data so that when you compare it, it will write the first category.

previousCategory = category

How can you use this? Before you write over the value of previousCategory, check to see if the category is the same as the previousCategory. Remember that to compare two variables you need to use a == to compare or !=.
= is already being used for assignment.

What would that look like?

if category != previousCategory:     
            t.write(category, font=("Arial",16))

Try this in a python file now.

Now what if you wanted this in a loop?

Add a loop to the above.
Another version could be written:MISSING IMAGE
Note that it does not print the 0 because it was not different from the previous one, but all the other numbers were different from the previous. In your exercise, your loop is reading lines and the category values often repeat.


Looping - allows you to repeat a set of statements as seen above. In this exercise you are looping for each line in your file.

Functions - by wrapping code in a function you can call it when you need to. It also allows you to tackle problems modularly. In this exercise you are calling a function to set up the window and turtle, you write a function to draw the data on the window using turtle.

I/O - for this exercise you are reading from a file. The previous exercise some of you read from the console or terminal window. The next exercise you will output to a file. In this exercise you are also using turtle output (turtle.write function).

Lists - in this exercise you have made lists by using the strip function, and you have accessed elements of the list by using subscripts.