Flow Control For Today… A review of tricky concepts from last week Introduction to Automating Tasks If/Else Statements [do something if a condition is true, otherwise do something else] While Loops [do something as long as a condition is true] For Loops [for every item in a list, do something to that item]
This cell sets up the VARIABLE called list This cell sets up the VARIABLE called list This cell calls the FUNCTION list()to make a list …and Python doesn’t know the difference
Solution 1: don’t use function names for your variables This cell sets up the VARIABLE called list produce This cell calls the FUNCTION list()to make a list
Solution 2: restart the kernel and run ONLY the offending cell Kernel Restart But if we don’t fix the underlying problem, we’re right back to error town.
are 500 polygon shapefiles. Make a single Excel spreadsheet that lists the area of each one.” 2. “Tell me the watershed area of the Animas River, the San Juan River, the Colorado River, Junction Creek, the Dolores River, the San Miguel River, and…actually, just tell me the watershed area of every stream longer than 5 miles in Colorado” 3. “Can you reproject these 5,000 shapefiles from UTM to Web Mercator, please?” I SAID PYTHON CAN MAKE YOUR LIFE EASIER, AND I (SERIOUSLY) WASN’T LYING
values without writing them all out 2. Python does a ton of work on all those values 3. I get a ton of new values What if I wanted to know the square root of every number between 1 and 1,000,000? …or, what if I wanted to clip a million shapefiles?
Flow Control If/Else Statements [do something if a condition is true, otherwise do something else] While Loops [do something as long as a condition is true] For Loops [for every item in a list, do something to that item] I’m going to present each one using: • A real-life example • A GIS example • Some Python code
do something else] IF I have more than 20 minutes THEN - Preheat oven to 430 - Bake for 16-20 minutes OTHERWISE - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes
do something else] IF I have more than 20 minutes THEN - Preheat toaster oven to 430 - Bake for 18-22 minutes OTHERWISE - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes IF I have more than 10 minutes but less than 20 minutes THEN - Preheat oven to 430 - Bake for 16-20 minutes
do something else] IF I have more than 20 minutes: - Preheat toaster oven to 430 - Bake for 18-22 minutes ELSE: - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes ELSE IF I have more than 10 minutes but less than 20 minutes: - Preheat oven to 430 - Bake for 16-20 minutes
do something else] FEMA flood hazard maps if house is in floodway: add to floodway_houses.shp charge ridiculous insurance rates else if house is in floodplain: add to floodplain_houses.shp charge high insurance rates else: add to no_risk.shp charge reasonable insurance rates
do something else] FEMA flood hazard maps if house is in floodway: add to floodway_houses.shp charge ridiculous insurance rates else if house is in floodplain: add to floodplain_houses.shp charge high insurance rates else: add to no_risk.shp charge reasonable insurance rates ONE OF THESE THINGS IS ALWAYS TRUE!
true, then stop] While temperature is less than 145 degrees keep cooking the burgers Eat the burgers This gets done while the burgers are less than 145 degrees
true, then stop] How to generate 20 random fish sampling locations? point = 1 sample_points = 20 while point < sample_points: generate random point on line x + 1 print ‘that’s 20 points!’
to that item] here’s a plate, two glasses, a salad bowl, a spatula, a knife, three forks, a cutting board, six spoons, and a crock pot Make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack
to that item] for each dish in the list of dishes: make soapy water wash dish with sponge rinse with hot water place in drying rack make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack
to that item] for each dish in the list of dishes: make soapy water wash dish with sponge rinse with hot water place in drying rack Make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack This one is efficient This one takes forever
to that item] list = [1,2,3] for item in list if item > 2 add item to list “items greater than 2” else add item to list “items less than 2” There’s nothing stopping you from nesting loops!