Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements.

Similar presentations


Presentation on theme: "Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements."— 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 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] numList = [1, 6, 5, 3, 2, 4] 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 fList = [“Water”, “Streams”, “Roads”, “Schools”] for lyr in fList: print lyr for name in [“Carter”, “Regan”, “Bush”] print name + “was a US president.” Example: 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 Get a list of Keys dictList.keys() Result:['Soils', ‘Roads', 'Wells'] Get a list of Values dictList.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?

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: 1.For loops 2.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 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 ?

12 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

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 Python executes the entire block of code after the colon until the condition is true Python detects the block with colon & indentation x =0 while x < 11: x = x + 1 print x Same or different result ? x =0 while x < 11: x = x + 1 print x

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.randomint(1,10) Result: 4 random.random() # will generate floating point number between 0 & 1 Result: random.choice([‘Chair’, ’Table’, ’Book’]} Result: ‘Table’

20 Some useful functions import math math.sqrt(144) Result: 12.0 math.pi Result: math.trunc( ) Result: 22 int( ) Result: 22 round( ) Result: 23.0 round( ) Result: 22.0

21 Common Python Operator OperatorSymbol Example Addition = 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!


Download ppt "Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements."

Similar presentations


Ads by Google