Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.

Similar presentations


Presentation on theme: "Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan."— Presentation transcript:

1 Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan

2 Spacing Spacing Comments Comments Header Header Consistency with variables – keep it simple Consistency with variables – keep it simple Set all variables equal to zero initially Set all variables equal to zero initially Notes on changes to code – version control Notes on changes to code – version control Good formatting example: http://www.personal.psu.edu/amd5554/resources/documented_c ode.pdf Good formatting example: http://www.personal.psu.edu/amd5554/resources/documented_c ode.pdf http://www.personal.psu.edu/amd5554/resources/documented_c ode.pdf http://www.personal.psu.edu/amd5554/resources/documented_c ode.pdf General Programming Tips

3 Declaring variables – don't need to create variable initially Declaring variables – don't need to create variable initially Indenting in loops – no end statement Indenting in loops – no end statement Capitalization matters – Temp, temp, tEmp, TEMP are all different variables Capitalization matters – Temp, temp, tEmp, TEMP are all different variables Python Basics

4 Mathematical expressions are the same Mathematical expressions are the same + Addition + Addition - Subtraction - Subtraction * Multiplication * Multiplication / Division / Division ** Exponentiation ** Exponentiation 9.8E-8 = 9.8 * (10 ** (-8)) 9.8E-8 = 9.8 * (10 ** (-8)) Numerical Arithmetic

5 Built-in functions Built-in functions float, int, max, min, abs float, int, max, min, abs Imported functions Imported functions sin, cos, tan, asin, acos, atan, log (natural log), log10 (base 10 log), exp, sqrt, pi, e sin, cos, tan, asin, acos, atan, log (natural log), log10 (base 10 log), exp, sqrt, pi, e Trigonometric functions work exclusively in radians Trigonometric functions work exclusively in radians k = m.cos(a * m.pi / 180.0) k = m.cos(a * m.pi / 180.0) degrad = m.pi / 180.0 degrad = m.pi / 180.0 k = m.cos(a * degrad) k = m.cos(a * degrad) Math Functions

6 Some commands/functions need to be imported in order to be used Some commands/functions need to be imported in order to be used Some libraries that can be imported: math, numpy, pylab Some libraries that can be imported: math, numpy, pylab Different ways to import Different ways to import from math import cos, sin, acos, pi from math import cos, sin, acos, pi import math import math  k = math.cos(a * m.pi / 180.0) import math as m import math as m  k = m.cos(a * m.pi / 180.0) Importing

7 Linecount += 1 ==> linecount = linecount + 1 Linecount += 1 ==> linecount = linecount + 1 Average /= linecount ==> average = average / linecount Average /= linecount ==> average = average / linecount Balance -= payment ==> balance = balance – payment Balance -= payment ==> balance = balance – payment Population *= growth ==> population = population * growth Population *= growth ==> population = population * growth Shortcut Operators

8 Need to distinguish between read-only (input) files and writeable (output) files Need to distinguish between read-only (input) files and writeable (output) files “r” = read-only, “w” = writeable “r” = read-only, “w” = writeable infile = open(“weather.csv”, “r”) infile = open(“weather.csv”, “r”) outfile = open(“pressure.txt”, “w”) outfile = open(“pressure.txt”, “w”) Input/Output

9 Reading input files Reading input files vap_list = infile.readlines() vap_list = infile.readlines() for vaporpressure in vap_list: for vaporpressure in vap_list: Print statements Print statements Print >> outfile, x, y, vaporpressure Print >> outfile, x, y, vaporpressure If a number immediately follows the %, it is the width (in spaces) of the field in which the object will be written If a number immediately follows the %, it is the width (in spaces) of the field in which the object will be written Print ‘%4f’ % x, ‘%4f’ % y  this will print x and y as floating point numbers over 4 spaces Print ‘%4f’ % x, ‘%4f’ % y  this will print x and y as floating point numbers over 4 spaces Using Input/Output Files

10 Types: for, if, while loops Types: for, if, while loops Indenting denotes code is in loop Indenting denotes code is in loop To close loop, unindent the next line To close loop, unindent the next line Example of a simple loop - counts # of x's in xlist Example of a simple loop - counts # of x's in xlist for x in xlist: y += 1 print y Loops

11 Determinant loop – use when you know how long you want the program to run Determinant loop – use when you know how long you want the program to run Similar to the “do loop” in ForTran and C++ Similar to the “do loop” in ForTran and C++ Two examples of for loops – can use either an input file or an array Two examples of for loops – can use either an input file or an array for station in stations: for k in range(n): For Loops

12 Used to make logical decisions Used to make logical decisions Can be imbedded inside for loops Can be imbedded inside for loops if logical_expression_1: # do this block when logical_expression_1 is true elif logical_expression_2: # do this block when logical_expression_2 is true else: # do this block when neither logical expression above is true If Loops

13 Comparisons of one variable to another or to a constant using comparison operators Comparisons of one variable to another or to a constant using comparison operators == equals == equals < less than < less than <= less than or equal to <= less than or equal to != not equals != not equals > greater than > greater than >= greater than or equal to >= greater than or equal to Logical Expressions in Python

14 Indeterminant loop – use when duration of loop is unknown Indeterminant loop – use when duration of loop is unknown Can be imbedded inside for loops Can be imbedded inside for loops General while loop structure General while loop structure while logical_expression: # statements to run as long as logical_expression stays true While Loops

15 Can use to terminate a loop or part of a specific loop if a statement becomes true Can use to terminate a loop or part of a specific loop if a statement becomes true Example of how break statement is used Example of how break statement is used x = 0 for x in xlist: if x >= 40: x += 1 breakelse: Break Statement

16 Collection of strings, floating point numbers, or integers listed in some order Collection of strings, floating point numbers, or integers listed in some order Arrays are special form of list in which all elements are of same data type Arrays are special form of list in which all elements are of same data type Numeric Python module (numpy) is used to work with arrays Numeric Python module (numpy) is used to work with arrays Lists

17 List – create a defined list-type object List – create a defined list-type object x = list([4.0, ‘Hypsometric’, 34]) x = list([4.0, ‘Hypsometric’, 34]) Range – returns list of integers in specified range – important in for loops Range – returns list of integers in specified range – important in for loops range(4) returns [0, 1, 2, 3] range(4) returns [0, 1, 2, 3] range (2,4) returns [2, 3] range (2,4) returns [2, 3] Len – counts how many numbers are in a list Len – counts how many numbers are in a list len(range(2,4)) produces a value of 2 len(range(2,4)) produces a value of 2 Sum – adds up the numbers in a list Sum – adds up the numbers in a list List Operators

18 Input files consists of strings which can be split into component strings and then converted into numbers Input files consists of strings which can be split into component strings and then converted into numbers Split method is used to break strings into more useful components Split method is used to break strings into more useful components Default separator is a blank space, but separators can be anything, such as, : ; / - Default separator is a blank space, but separators can be anything, such as, : ; / - Line splitting most useful when done inside a loop Line splitting most useful when done inside a loop Line = “32, 32.4, 36.8, Freezing Points” Line = “32, 32.4, 36.8, Freezing Points” q = float(line.split(“,”)[2]) = 36.8 q = float(line.split(“,”)[2]) = 36.8 Line Splitting


Download ppt "Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan."

Similar presentations


Ads by Google