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

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

Python Syntax. Basic Python syntax Lists Dictionaries Looping Conditional statements.
Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Programming in Visual Basic
Lecture 1 The Basics (Review of Familiar Topics).
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Python Crash Course by Monica Sweat. Python Perspective is strongly typed, interpreted language is used to define scripts (Don't even need to define a.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Programming Training Main Points: - Python Statements - Problems with selections.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Fortran: Expressions and Assignments Session Two ICoCSIS.
Computer Science 101 Introduction to Programming.
Class Review. Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm remove files: rm rename files: mv rename.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
C++ Programming: Basic Elements of C++.
1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects.
Introduction to Programming with RAPTOR
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
Calculations Chapter Twelve. More serious than Chapter Four Page 199 some sizes (in bytes) of various primititive types: Do Not Memorize typeSize(bits)Approx.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Main Points: - Python Statements - Problems with selections.
Line Continuation, Output Formatting, and Decision Structures
C Short Overview Lembit Jürimägi.
Introduction to Programming
MATLAB DENC 2533 ECADD LAB 9.
MATLAB: Structures and File I/O
Line Continuation, Output Formatting, and Decision Structures
Arithmetic operations, decisions and looping
Introduction to Programming Using Python PART 2
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Spreadsheets Objective 6.02
In this class, we will cover:
Spreadsheets Objective 6.02
Class code for pythonroom.com cchsp2cs
Presentation transcript:

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

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: ode.pdf Good formatting example: ode.pdf ode.pdf ode.pdf General Programming Tips

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

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

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 / degrad = m.pi / k = m.cos(a * degrad) k = m.cos(a * degrad) Math Functions

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

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

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

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

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

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

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

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

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

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

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

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

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