1 / 22

Python Syntax

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:

cecile
Download Presentation

Python Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Syntax

  2. Basic Python syntax • Lists • Dictionaries • Looping • Conditional statements

  3. 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]

  4. 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?

  5. 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?

  6. 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’}

  7. 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?

  8. 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”

  9. 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

  10. 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

  11. 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 ?

  12. 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 ?

  13. 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 ?

  14. 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 ?

  15. While loop password = “nothing” while password != “GIS”: password = raw_input(“Enter your Password: “)

  16. 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

  17. 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”

  18. 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)

  19. 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’

  20. 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

  21. 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

  22. ? Read relevant sections from online resources as needed!

More Related