100 likes | 122 Views
Learn essential Python assignment techniques for geography tasks, including variable naming, boolean expressions, logical operators, and conditional statements. Master loops for efficient data processing.
E N D
Assignments in Python • Assignment target on left of ‘=‘ and assignment object is on right fc = “C:\\ProjectData\\SanDiego.mdb\\Freeways.shp” • Remember, variables are case sensitive fc = “Freeways.shp” Fc = 20000 • Remember, variables can hold different data types • numbers, strings, lists, files
Review: Variable naming conventions • Upper case versus lower case • First word lower case, capitalize each successive word tableFieldName = “Street” - Acronym at the beginning, use lower case letters gdbPath = “C:\\SanDiego.mdb” • Acronym in the middle or at the end, use upper case letters inputFC = “Streets.shp” • Avoid special characters (for example / \ & % # !) • Use descriptive variable names
Comparison: Boolean Expressions • A boolean expression is an expression that is either true or false. • In Python an expression that is true has the value 1, and an expression that is false has the value 0. • In this case the equal comparison (not assignment) operator is as follows. >>> 5 == 5 1 >>> 5 == 6 0
Boolean Expressions The == operator is one of the comparison operators; the others are: x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y
Logical Operators • There are three logical operators: and, or, and not. • The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.
Conditional Decision Statement • Conditional statement: if…elif…else if x == 1: print “x is 1” elif x == 2: print “x is 2” else: print “x is not 1 or 2” • Colons are used at the end of each condition • Indentation defines what executes for each condition
Looping - 1 • While loop x = 1 while x < 10: print x x = x + 1 • Colons used at end of each statement • Indentation defines what executes for the loop
Looping - 2 • Counted loop for x in range(1, 5): print x • Counted loops increment and test a variable on each iteration of the loop • The last value is not executed
Looping - 3 • List loop x = [1, 2, 3] For a in x: print a • List loops iterate over each value in a list • The loop will execute once for each value in the list