python syntax. basic python syntax lists dictionaries looping conditional statements
TRANSCRIPT
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:dList = [“Soils”, “Roads”, “Rails”, “Parcel”]numList = [1, 2, 3, 4, 5, 6]
Basic List Operations
• Get the number of items in a listlen(numList) Result: 6
• Sorting the list: orders a listnumList.sort() Result:[1,2,3,4,5,6]
• Append the list: adds an object to the end of a listnumList.append(7) Result:[1,2,3,4,5,6,7]
numList = [1, 6, 5, 3, 2, 4]
Remember that sort, append, etc are the methods for listbut len is a built in function. What are the other methods of list?
Application of list
fList = [“Water”, “Streams”, “Roads”, “Schools”]for lyr in fList:
print lyr
for name in [“Carter”, “Regan”, “Bush”]print name + “was a US president.”
Example:
Examp le:
What would be the output?
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 dictionaryExample:dictList = {“Soils” : ”Polygon” , “Roads” : “Polyline”, “Wells” : ”Point”}fList = {‘food’ : ’bread’, ‘quantity’ : 4, ‘type’ : ’wheat’}
Basic Dictionaries Operatios
• Get a list of KeysdictList.keys() Result:['Soils', ‘Roads', 'Wells']
• Get a list of ValuesdictList.values() Result:[‘Polygon', ‘Polyline', ‘Point']
dictList = {“Soils”:”Polygon” , “Roads”:“Polyline”, “Wells”:”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 statementsy = 1If 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: #assignmentif 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:1. For loops2. 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 i in range (1,10,2):print i
for i in range (1,10):print i
What would be the result ?
For loops execute a block of statements a predetermined number of times
for i in range (10):print i
Will you get the same result ?
What would be the result ?
For loop
for name in [‘Carter’,’Regan’,’Bush’]:print name + “was a US president”
What would be the result ?
Python’s for loop can also operate on a list of items
Looping the loop
for n in range (1,11):for m in range (1,11):
print n, “*”, m, “=“, n*mprint “……………………………”
What would be the result ?
While loop
• Python executes the entire block of code after the colon until the condition is true
• Python detects the block with colon & indentation
x =0while x < 11:
x = x + 1print x
Same or different result ?
x =0while x < 11:
x = x + 1print x
While loop
password = “nothing”while password != “GIS”: password = raw_input(“Enter your Password: “)
While looppassword = “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 looppassword = “nothing”count = 0while 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 modulesExample:import math (import math module)import arcpy (import arcpy module)
Useful ModulesTo generate random numberimport random
random.randomint(1,10)Result: 4
random.random() #will generate floating point number between 0 & 1 Result: 0.4466987867
random.choice([‘Chair’ , ’Table’ , ’Book’]}Result: ‘Table’
Some useful functions
import math
math.sqrt(144)Result: 12.0
math.pi Result: 3.1415926535897931
math.trunc(22.6758)Result: 22
int(22.6758)Result: 22
round(22.6758)Result: 23.0
round(22.2344)Result: 22.0
Common Python OperatorOperator 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!