Python Programming Fundamentals

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Fundamentals of Python: From First Programs Through Data Structures
Computer Science 101 Introduction to Programming.
Python.
Fundamentals of Python: First Programs
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Linux+ Guide to Linux Certification, Third Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Python Let’s get started!.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Programming
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Programming
Introduction to Programming
Fundamentals of Programming I Overview of Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Introduction to Python
Introduction to Programming
Variables, Expressions, and IO
Basic operations in Matlab
Intro to PHP & Variables
Introduction to Programming
Number and String Operations
WEB PROGRAMMING JavaScript.
An Introduction to Python
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Introduction to Programming
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming with Python
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Introduction to Programming
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Python Programming Fundamentals GIT461 GIS Python Programming Fundamentals

Python Programming Environment We will use the “PythonWin” version 2.7 system This programming environment includes a code editor and a code interpreter A code interpreter executes statements line-by-line as you type them – very useful for testing A Code editor is used to type a complete program that is later tested and “de-bugged”

PythonWin Environment Interpreter window is displayed first. You can type and execute simple statements in this window.

PythonWin Environment To type a Python script program select “File > New > Script”. Type in code and save with a .py extention. A good idea to have a folder such as “\PythonProgs\” just for source code scripts.

PythonWin Environment To load an existing python script select “File > Open” to load script into an edit window.

PythonWin Environment To run a script you should load it, and then select “File > Run” Note that you may enter “command line arguments” at this point if needed Select “OK” button to run the script

Python Programming Fundamentals Google “Python Language Reference” to load this help file

Python Data Types String: a sequence of alphanumeric characters Integer: a whole number that has no fractional component Float: a number that contains a fractional component String example: “105 Glendale Avenue” (note that strings are enclosed in quotes) Integer examples: 100, -19, 0, 9999999 Float examples: 1.0, -123.678, 1.6745E3

Python Assignment Statement The “=“ sign is the assignment operator as it is in most programming languages X = 1 Print X # the number “1” will appear on the screen X = X + 5 Print X # the number “6” will appear on the screen

Python Comments A python comment begins with a “#”. Anything after the “#” is ignored by Python The 1st line in the below script is a comment line- it is ignored by Python The characters to the right of the “#” on lines 2-5 are ignored # get x1, y1, x2, y2 from the command line x1param = float(StripComma(sys.argv[1])) # x1 y1param = float(StripComma(sys.argv[2])) # y1 x2param = float(StripComma(sys.argv[3])) # x2 y2param = float(StripComma(sys.argv[4])) # y2

Python Operators (in order of precedence) Multiplication: * Division: / Modulus: % Addition: + Subtraction: -

Expressions Expressions are combinations of data and operators:

Built-in Python Functions A function takes an “argument” or “arguments” and returns a value that can be used in an assignment statement In the below satements abs(x) and pow(x,y) are built-in functions in every implementation of the Python language x = abs(-8) print x # the number “8” would appear in the interactive window y = pow(2,3) print y # the number “8” would appear in the interactive window

Python Built-In Functions abs(x) # returns the absolute value of x float(x) # returns the string x converted to a floating point number int(x) # returns the string x converted to a integer number pow(x,y) # returns the number x rasied to the y power round(x,n) # rounds the number x to n decimal places str(x) # returns the string equivalent of the object x

More Complex Expressions using Functions and Exponentiation Note that trig functions use radian angular values. You must convert degrees to radians before using the trig functions ( radians = degrees * 3.1416/180.0). Note that before using the trig functions the math “module” had to be imported.

Controlling Program Flow: Conditional Statements A statement uses “reserved” python language words to initiate an action A conditional statement makes a decision at run-time X = 10 If x == 10 : print “X=10” Note: everything indented Below the “If” statement is Part of the statement.

Conditional Statements: More Complex IF/ELIF/ELSE construct The “elif” and “else” keywords can be used to construct more complex decision structures x = random.randint(1,10) If x == 1 : print “you are in first place” Elif x == 2 : print “you are in second place” Elif x == 3 : print “you are in third place” Else : print “ sorry, you didn’t place this time”

Controlling program flow with a loop: While statement While statements can be used to repeat a section of code until some condition is reached. i = 0 While i <= 10 : print I i = i + 1 # you could also use i += 1

For Loops A “For” loop uses a “list”. First a list must be built before it can be used in the For loop Mylist = [“A”, “B”, “C”, “D”] For letter in Mylist : print letter # the letters “A” through “D” would print to the screen

Getting User Input: command line Command line parameters are entered at run time in the “Run Script” window

Getting User Input: “Input” function The “input” function prompts the user for input during the running of the script

User Input: Input function Note that if you use the “input” function and you enter a string value it must be enclosed in quotes (single or double).

Functions A function is a stand-alone section of code that is designed to accomplish a specific task based on one or more parameters passed to the function The function returns a calculated result so a function normally appears in the main code to the right of an assignment (=) statement so the returned value is stored in the variable on the left side of the assignment statement

Function Placement Functions are normally placed at the top of the main program file because they need to be defined before they are referenced in the main program Below is an example function that converts a longitude string value (“-0883015”) to its decimal degree number equivalent