python syntax. basic python syntax lists dictionaries looping conditional statements

22
Python Syntax

Upload: nick-boorman

Post on 02-Apr-2015

230 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

Python Syntax

Page 2: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

Basic Python syntax

• Lists• Dictionaries• Looping• Conditional statements

Page 3: 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]

Page 4: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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?

Page 5: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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?

Page 6: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 7: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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?

Page 8: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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”

Page 9: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 10: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 11: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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 ?

Page 12: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 13: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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 ?

Page 14: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 15: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

While loop

password = “nothing”while password != “GIS”: password = raw_input(“Enter your Password: “)

Page 16: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 17: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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”

Page 18: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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)

Page 19: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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’

Page 20: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 21: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

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

Page 22: Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements

Read relevant sections from online resources as needed!