By ben blake andrew dzambo paul flanagan
Download
1 / 18

Python - PowerPoint PPT Presentation


  • 122 Views
  • Uploaded on

By: Ben Blake, Andrew Dzambo , Paul Flanagan. Python. Spacing Comments Header Consistency with variables – keep it simple Set all variables equal to zero initially Notes on changes to code – version control

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Python' - aira


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
By ben blake andrew dzambo paul flanagan

By: Ben Blake, Andrew Dzambo, Paul Flanagan

Python


General programming tips

  • Spacing

  • Comments

  • Header

  • Consistency with variables – keep it simple

  • Set all variables equal to zero initially

  • Notes on changes to code – version control

  • Good formatting example: http://www.personal.psu.edu/amd5554/resources/documented_code.pdf

General Programming Tips


Python basics

Python Basics


Numerical arithmetic

Numerical Arithmetic


Math functions

  • Built-in functions initially

    • float, int, max, min, abs

  • Imported functions

    • sin, cos, tan, asin, acos, atan, log (natural log), log10 (base 10 log), exp, sqrt, pi, e

  • Trigonometric functions work exclusively in radians

    • k = m.cos(a * m.pi / 180.0)

    • degrad = m.pi / 180.0

    • k = m.cos(a * degrad)

Math Functions


Importing

Importing


Shortcut operators

  • Linecount used += 1 ==>linecount= linecount + 1

  • Average /= linecount==> average = average / linecount

  • Balance -= payment ==> balance = balance – payment

  • Population *= growth ==> population = population * growth

Shortcut Operators


Input output

Input/Output


Using input output files

  • Reading input files writeable (output) files

    • vap_list = infile.readlines()

    • for vaporpressure in vap_list:

  • Print statements

    • 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

      • Print ‘%4f’ % x, ‘%4f’ % y  this will print x and y as floating point numbers over 4 spaces

Using Input/Output Files


Loops

  • Types: for, if, while loops writeable (output) files

  • Indenting denotes code is in loop

  • To close loop, unindent the next line

  • Example of a simple loop - counts # of x's in xlist

    for x in xlist:

    y += 1

    print y

Loops


For loops

For Loops


If loops

  • Used to make logical decisions program to run

  • 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


Logical expressions in python

Logical Expressions in Python


While loops

While Loops


Break statement

Break Statement


Lists

Lists


List operators

  • List – create a defined list-type object listed in some order

    • x = list([4.0, ‘Hypsometric’, 34])

  • Range – returns list of integers in specified range – important in for loops

    • range(4) returns [0, 1, 2, 3]

    • range (2,4) returns [2, 3]

  • Len – counts how many numbers are in a list

    • len(range(2,4)) produces a value of 2

  • Sum – adds up the numbers in a list

List Operators


Line splitting

  • 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

  • Default separator is a blank space, but separators can be anything, such as , : ; / -

  • Line splitting most useful when done inside a loop

    • Line = “32, 32.4, 36.8, Freezing Points”

    • q = float(line.split(“,”)[2]) = 36.8

Line Splitting