220 likes | 395 Views
Python Syntax. Basic Python syntax . Lists Dictionaries Looping Conditional statements. Lists. Lists are collection of objects List can hold any type of object-numbers, string List are indexed (zero based) List can grow and shrink Variables can hold a list. Example:
E N D
Basic Python syntax • Lists • Dictionaries • Looping • Conditional statements
Lists • Lists are collection of objects • List can hold any type of object-numbers, string • List are indexed (zero based) • List can grow and shrink • Variables can hold a list Example: dList = [“Soils”, “Roads”, “Rails”, “Parcel”] numList = [1, 2, 3, 4, 5, 6]
Basic List Operations numList = [1, 6, 5, 3, 2, 4] • Get the number of items in a list len(numList) Result: 6 • Sorting the list: orders a list numList.sort() Result:[1,2,3,4,5,6] • Append the list: adds an object to the end of a list numList.append(7) Result:[1,2,3,4,5,6,7] Remember that sort, append, etc are the methods for list but len is a built in function. What are the other methods of list?
Application of list Example: fList = [“Water”, “Streams”, “Roads”, “Schools”] for lyr in fList: print lyr What would be the output? Examp le: for name in [“Carter”, “Regan”, “Bush”] print name + “was a US president.” What would be the output?
Dictionaries • Dictionaries are similar to list storing objects in pairs • It is composed of a set of key & value pairs separated by commas and enclosed by curly braces • Like lists, it can grow and shrink • Variables can hold a dictionary Example: dictList = {“Soils” : ”Polygon” , “Roads” : “Polyline”, “Wells” : ”Point”} fList = {‘food’ : ’bread’, ‘quantity’ : 4, ‘type’ : ’wheat’}
Basic Dictionaries Operatios dictList = {“Soils”:”Polygon” , “Roads”:“Polyline”, “Wells”:”Point”} • Get a list of Keys dictList.keys() Result:['Soils', ‘Roads', 'Wells'] • Get a list of Values dictList.values() Result:[‘Polygon', ‘Polyline', ‘Point'] dicList = dicList.Keys( ) for lyr in dicList: print lyr What would be the output?
Conditional statements (if/elif/else) • Conditional statements are used to see if a condition is true or false • Very common in decision making • if….elif….else: Python’s conditional logic if y == 1: print “y is 1” elif y == 2: print “y is 2” else: print “y is neither 1 nor 2”
Conditional statements y = 1 If y > 2: print “y is greater than 2” else: print “y is less than or equal to 2” Question: What would be printed? • Colon used at the end of each condition • Indentation defines what executes for each condition • Python automatically indents y = 2: #assignment if y == 2: #testing condition • One equal sign (=) for assignment, two(==) for conditions
Looping! Looping allows your program to repeat over and over as necessary • Two basic types of loop: • For loops • While loops • For loops execute a block of statements a predetermined number of times • A While loop executes until some condition is met
For loop For loops execute a block of statements a predetermined number of times for i in range (1,10,2): print i for i in range (1,10): print i What would be the result ? for i in range (10): print i Will you get the same result ? What would be the result ?
For loop Python’s for loop can also operate on a list of items for name in [‘Carter’,’Regan’,’Bush’]: print name + “was a US president” What would be the result ?
Looping the loop for n in range (1,11): for m in range (1,11): print n, “*”, m, “=“, n*m print “……………………………” What would be the result ?
While loop x =0 while x < 11: x = x + 1 print x x =0 while x < 11: x = x + 1 print x • Python executes the entire block of code after the colon until the condition is true • Python detects the block with colon & indentation Same or different result ?
While loop password = “nothing” while password != “GIS”: password = raw_input(“Enter your Password: “)
While loop password = “nothing” while password != “GIS”: password = raw_input(“Enter your Password: “) if password == “GIS”: print “Congratulations! You’re in” else: print “Please try again!” Pay attention to the indentation, colon, etc
Using a counter with a loop password = “nothing” count = 0 while password != “GIS”: password = raw_input(“Enter your Password: “) count = count + 1 If password == “GIS”: print “Congratulations! You’re in” else: print “Please try again!” print “It takes you” + str(count) + “trial”
Python Modules • Python extends its capability by incorporating functions from external modules • You use import to bring modules Example: import math (import math module) import arcpy (import arcpy module)
Useful Modules To generate random number import random random.random() #will generate floating point number between 0 & 1 Result:0.4466987867 random.randomint(1,10) Result: 4 random.choice([‘Chair’ , ’Table’ , ’Book’]} Result: ‘Table’
Some useful functions import math math.pi Result:3.1415926535897931 math.sqrt(144) Result: 12.0 int(22.6758) Result: 22 math.trunc(22.6758) Result: 22 round(22.2344) Result: 22.0 round(22.6758) Result: 23.0
Common Python Operator Operator Symbol Example • Addition + 7 + 3 = 10 • Subtraction - 7 – 3 = 4 • Multiplication * 7 * 4 = 28 • Division / 7 / 3 = 2 • Remainder % 7 % 3 = 1 • Equal to == a == b • Not Equal to != or < > a != b or a < > b
? Read relevant sections from online resources as needed!