Python: Your new best friend print “Adam Avison”.

Slides:



Advertisements
Similar presentations
What we will cover today… Where is the camera on my phone? Taking a photo Zoom in and out Deleting a photo Where do my photos go to? Viewing my photos.
Advertisements

Lab III – Linux at UMBC.
23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
Python tutorial for scientific computing Steve Byrnes 16 March 2012.
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Computation for Physics 計算物理概論
Python Basics: Statements Expressions Loops Strings Functions.
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Hello, Pig! Hello, Rabbit! Look at this – I am making a list!
Introduction to Python
Lecture 4 Basic Scripting. Administrative  Files on the website will be posted in pdf for compatibility  Website is now mirrored at:
ENG 1181 College of Engineering Engineering Education Innovation Center Microsoft Excel: Data Analysis Trendlines Formulas Functions Cell Addressing.
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
Title Center for Coastal and Ocean Mapping NOAA/UNH Joint Hydrographic Center 31-Oct-2006 A Python Tutorial Kurt Schwehr 31 Oct 2006.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
The University of Adelaide Table Talk: Using tables in Word Peter Murdoch March 2014 PREPARING GOOD LOOKING DOCUMENTS.
Introduction to JavaScript for Python Programmers
By. What advantages has it? The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent.
Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
PYTHON PLOTTING CURVES CHAPTER 10_5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
European Computer Driving Licence Syllabus version 5.0 Module 4 – Spreadsheets Chapter 22 – Functions Pass ECDL5 for Office 2007 Module 4 Spreadsheets.
Introduction to Python John Reiser May 5 th, 2010.
Python: An Introduction
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Computer Programming for Biologists Oct 30 th – Dec 11 th, 2014 Karsten Hokamp  Fill out.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Title Center for Coastal and Ocean Mapping NOAA/UNH Joint Hydrographic Center 31-Oct-2006 A Python Tutorial Kurt Schwehr 31 Oct 2006.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
CS 4720 Dynamic Web Applications CS 4720 – Web & Mobile Systems.
Where will PyRAF lead us?: The future of data analysis software at STScI Perry Greenfield Science Analysis Tools Project Space Telescope Science Institute.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
GISMO/GEBndPlan Overview Geographic Information System Mapping Object.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Programming for GCSE 1.0 Beginning with Python T eaching L ondon C omputing Margaret Derrington KCL Easter 2014.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
1-d Arrays & Plotting.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Practical Kinetics Exercise 0: Getting Started Objectives: 1.Install Python and IPython Notebook 2.print “Hello World!”
Introduction to CASA Crystal Brogan ALMA CASA Subsystem Scientist (NRAO/NAASC) With lots of help from Steve Myers (Project Scientist)
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Python Lab Matplotlib - I Proteomics Informatics, Spring 2014 Week 9 25 th Mar, 2014
Linux Administration Working with the BASH Shell.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
PH2150 Scientific Computing Skills
CSE 374 Programming Concepts & Tools
Development Environment
MET4750 Techniques for Earth System Modeling
Python Let’s get started!.
Introduction to Python
PYTHON: AN INTRODUCTION
Lecture 1 Introduction to Python Programming
CompSci 230 Software Construction
Prepared by Kimberly Sayre and Jinbo Bi
PH2150 Scientific Computing Skills
Introduction to Value-Returning Functions: Generating Random Numbers
Scientific Python Introduction
Input and Output Python3 Beginner #3.
Matplotlib and Pandas
Functions, Procedures, and Abstraction
Presentation transcript:

Python: Your new best friend print “Adam Avison”

Disclaimer Though I’ve extensively programmed in Python, I have ~0 formal programming training. So some terminology I use may be total gibberish to those better taught than I. My mantra is: “If it works it works, who cares about the fancy terminology”.

Python version Here I will talk about functions within Python 2.6.x / 2.7.x (the JBCA system default) The separate development stream of Python 3.x... Most of what I say won’t work, or will work very differently.

Why Python? The current astronomers favourite... CASA (interferometric data reduction package is written in it). Its free! (unlike IDL...*) *I’m yet to find something IDL can do that Python can’t...

Why Python?

First we need to set up which version of python your linux box will default to. In your home area type: >emacs –nw.cshrc This will open an in terminal text editor. Press the down key until you see the line: #USER MODIFICATIONS After this type alias python2.7 ‘/usr/local/lib/python2.7/bin/python2.7’ The type ctrl-x ctrl-s. Close your terminal and open another and we’re good to go. BEFORE GETTING STARTED

Getting started From the command line ‘python’ will get you into the python environment. Within which you can start some basic work. e.g. >>> a=3.141*0.005 >>> b=7.0**2.0 >>> c=a+b >>> print c

Scripting Adding those lines into a file named e.g. ‘test.py’ will then be executable by the command >python test.py Will result in

Dynamic whitespace In python whitespace is important, unlike e.g. Perl. Your left hand indentation matters. So this will work: for x in range(len(array)): print x y=x**2.7 print y print y # will print the last #value of y This won’t: for x in range(len(array)): print x y=x**2.7 print y print y # we’ll have crashed #before we reach here Remember for later your ‘if’s, ‘elif’s and ‘else’s need to line up!

Importing modules A lot of functionality can be imported into your scripts with import commands e.g. import numpy As python is object orientated you call a ‘numpy’ task as follows: numpy.sqrt(2.0) #will give us square-root of 2 But because we’re lazy we don’t want to type numpy over and over so we can instead use: import numpy as np So the above becomes np.sqrt(2.0) #will still give us square-root of 2

Importing modules 2. Some times we only want a couple of functions from a module for this we can use a ‘from’: from numpy import sqrt, other_function Now: sqrt(2.0) #will give us what we’re after

Why isn’t there a function for this? If the function you’re after doesn’t exist... Write your own! In your code you can create your own functions, here is an example: def my_function(arg1, arg2): z=np.sqrt(arg1)*np.exp(arg2) return z Which can then be called later in your code simply as: something=my_function(arg1, arg2)#something will then == z With the same number of arguments.

Example functions Example functions I’ve created: 1.Calculating colour-colour plots from incomplete data lists. 2.Find the peak flux in a spectrum. 3.Finding Zeeman split line pairs and calculating the local magnetic field strength in ex-OH masers. 4.Calculating the rms noise in an ALMA map... Etc etc Functions are good because they mean you don’t have to re- type code umpteen times throughout a script.

Useful python modules for Astronomy numpy – array and matrix mathematics, nice load from txt options... scipy – Scientific functions, e.g. correlation, signal processing, ffts... matplotlib – plotting... Makes beautiful plots. pyfits – FITS file manipulation. astropy - many useful astronomy modules and packages all in one... APLpy for making nice FITS images.

Examples resFOV.py

Basic syntax stuff and quick plot Page 1 of 2 import numpy as np import matplotlib.pyplot as plt x=np.arange(1.0,10.0,1.0) #creates an array from 1 to 9 for value in x: if value ==4.0: print “wow a 4!” elif value == 5.0: print “and now a 5!” else: print value y=np.sqrt(np.exp(x)) #just for something to plot against x!

fig1 = plt.figure(1) ax1 = fig1.add_subplot(111) #sets up a plot environment to # plot on ax1.plot(x,y,’bo-’) #plots x v. y, ‘bo-’ sets it to #plot blue circles with a solid #line joining them ax1.set_xlabel(‘x’) ax1.set_ylabel(‘y’)#take a guess! plt.show()#shows our plot Basic syntax stuff and quick plot Page 2 of 2

Challenge Using the basics demonstrated in this tutorial write a script which calculates the Schwarzschild radius for black holes of mass = to each of the solar system planets. Extra credit, plot mass vs. radii and label each planet.