Python Workshop Cheng Li. PYTHON 101 (MATERIALS ARE ADAPTED FROM

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Games in Python – the easy way
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Lists (also called Arrays) A list is an example of a collection: a data type that is capable of storing other data types. foods = ["spam", "eggs", "sausage",
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 5 List Variables and Loops.
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
Mike Scott University of Texas at Austin
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Python for Informatics: Exploring Information
Chapter 24 Lists, Stacks, and Queues
Python Mini-Course University of Oklahoma Department of Psychology
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Arrays Dr. Jey Veerasamy July 31 st – August 23 rd 9:30 am to 12 noon 1.
Course A201: Introduction to Programming 10/28/2010.
Modern Programming Languages, 2nd ed.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Briana B. Morrison Adapted from William Collins
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
CMPT 120 Functions and Decomposition
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Subtraction: Adding UP
Container Types in Python
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Noadswood Science,  To understand the flow procedure when writing programs Thursday, January 15, 2015.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Python: Your new best friend print “Adam Avison”.
Python Basics: Statements Expressions Loops Strings Functions.
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
Building Java Programs Chapter 14
Median and Mode Lesson
Introduction to Python
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
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.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills
Introduction to Python
Lecture 2 Python Basics.
Introduction to Python
Introduction to Python
Photochemical processes on Titan
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Algorithmic complexity: Speed of algorithms
Variables, Lists, and Objects
Algorithmic complexity: Speed of algorithms
Introduction to Python
CISC101 Reminders Assignment 3 due today.
Introduction to Computer Science
Presentation transcript:

Python Workshop Cheng Li

PYTHON 101 (MATERIALS ARE ADAPTED FROM

0. Setting Up OS X  Go to Terminal What you should see Last login: Sat Apr 24 00:56:54 on ttys001 ~ $ python Python (r251:54863, Feb , 19:02:12) [GCC (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> exit()

0. Setting Up Linux What You Should See $ python Python (r265:79063, Apr , 05:28:39) [GCC (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>

1. A Good First Program ex1.py # -*- coding: utf-8 -*- print "Hello World!" print "Hello Again" print "I like typing this." print "This is fun." print 'Yay! Printing.’ print "I'd much rather you 'not'." print 'I "said" do not touch this.' In Terminal run the file by typing: >>>python ex1.py

2. Comment me off ex2.py # A comment, this is so you can read your program later. # Anything after the # is ignored by python. print "I could have code like this." # and the comment after is ignored # You can also use a comment to "disable" or comment out a # piece of code: # print "This won't run." print "This will run." $ python ex2.py I could have code like this. This will run.

3. Numbers and Math ex3.py print "I will now count my chickens:” print "Hens", / 6 print "Roosters", * 3 % 4 print "Now I will count the eggs:” print % / print "Is it true that < 5 - 7?” print < print "What is 3 + 2?", print "What is 5 - 7?", print "Oh, that's why it's False.” $ python ex3.py I will now count my chickens: Hens 30 Roosters 97 Now I will count the eggs: 7 Is it true that < 5 - 7? False What is 3 + 2? 5 What is 5 - 7? -2 Oh, that's why it's False.

4. Variables and Names ex4.py cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90 cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car average_passengers_per_car = passengers / cars_driven print "There are", cars, "cars available." print "There are only", drivers, "drivers available." print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_passengers_per_car, "in each car." $ python ex4.py There are 100 cars available. There are only 30 drivers available. There will be 70 empty cars today. We can transport people today. We have 90 to carpool today. We need to put about 3 in each car.

5. More Variables and Printing ex5.py my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches my_weight = 180 # lbs my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown’ print "Let's talk about %s." % my_name print "He's %d inches tall." % my_height print "He's %d pounds heavy." % my_weight print "Actually that's not too heavy." print "He's got %s eyes and %s hair." % (my_eyes, my_hair) print "His teeth are usually %s depending on the coffee." % my_teeth # this line is tricky, try to get it exactly right print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight) $ python ex5.py Let's talk about Zed A. Shaw. He's 74 inches tall. He's 180 pounds heavy. Actually that's not too heavy. He's got Blue eyes and Brown hair. His teeth are usually White depending on the coffee. If I add 35, 74, and 180 I get 289.

6. Strings and Text ex6.py x = "There are %d types of people." % 10 binary = "binary" do_not = "don't" y = "Those who know %s and those who %s." % (binary, do_not) print x print y print "I said: %r." % x print "I also said: '%s'." % y hilarious = False joke_evaluation = "Isn't that joke so funny?! %r" print joke_evaluation % hilarious w = "This is the left side of..." e = "a string with a right side.” print w + e $ python ex6.py There are 10 types of people. Those who know binary and those who don't. I said: 'There are 10 types of people.'. I also said: 'Those who know binary and those who don't.'. Isn't that joke so funny?! False This is the left side of...a string with a right side.

7. Functions and Variables ex7.py def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" % boxes_of_crackers print "Man that's enough for a party!" print "Get a blanket.\n” print "We can just give the function numbers directly:" cheese_and_crackers(20, 30) print "OR, we can use variables from our script:" amount_of_cheese = 10 amount_of_crackers = 50 cheese_and_crackers(amount_of_cheese, amount_of_crackers) $ python ex7.py We can just give the function numbers directly: You have 20 cheeses! You have 30 boxes of crackers! Man that's enough for a party! Get a blanket. OR, we can use variables from our script: You have 10 cheeses! You have 50 boxes of crackers! Man that's enough for a party! Get a blanket.

8. Functions Can Return Something ex8.py def add(a, b): print "ADDING %d + %d" % (a, b) return a + b def subtract(a, b): print "SUBTRACTING %d - %d" % (a, b) return a – b print "Let's do some math with just functions!" age = add(30, 5) height = subtract(78, 4) print "Age: %d, Height: %d " % (age, height) $ python ex8.py Let's do some math with just functions! ADDING SUBTRACTING 78 – 4 Age: 35, Height: 74

9. If Else ex9.py people = 30 cars = 40 if cars > people: print "We should take the cars." elif cars < people: print "We should not take the cars." else: print "We can't decide.“ $ python ex9.py We should take the cars.

10. For Loops and Lists ex10.py the_count = [1, 2, 3, 4, 5] # this first kind of for-loop goes through a list for number in the_count: print "This is count %d" % number # we can also build lists, first start with an empty one elements = [] # then use the range function to do 0 to 5 counts for i in range(0, 6): print "Adding %d to the list." % i # append is a function that lists understand elements.append(i) # now we can print them out too for i in elements: print "Element was: %d" % i $ python ex10.py This is count 1 This is count 2 This is count 3 This is count 4 This is count 5 Adding 0 to the list. Adding 1 to the list. Adding 2 to the list. Adding 3 to the list. Adding 4 to the list. Adding 5 to the list. Element was: 0 Element was: 1 Element was: 2 Element was: 3 Element was: 4 Element was: 5

11. Doing Things to Lists ex11.py ten_things = "Apples Oranges Crows Telephone Light Sugar" print "Wait there are not 10 things in that list. Let's fix that." stuff = ten_things.split(' ') more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] while len(stuff) != 10: next_one = more_stuff.pop() print "Adding: ", next_one stuff.append(next_one) print "There are %d items now." % len(stuff) $ python ex11.py Wait there are not 10 things in that list. Let's fix that. Adding: Boy There are 7 items now. Adding: Girl There are 8 items now. Adding: Banana There are 9 items now. Adding: Corn There are 10 items now.

12. While Loops ex12.py i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1 print "Numbers now: ", numbers print "At the bottom i is %d" % I print "The numbers: for num in numbers: print num $ python ex12.py At the top i is 0 Numbers now: [0] At the bottom i is 1 At the top i is 1 Numbers now: [0, 1] At the bottom i is 2 At the top i is 2 Numbers now: [0, 1, 2] At the bottom i is 3 At the top i is 3 Numbers now: [0, 1, 2, 3] At the bottom i is 4 At the top i is 4 Numbers now: [0, 1, 2, 3, 4] At the bottom i is 5 At the top i is 5 Numbers now: [0, 1, 2, 3, 4, 5] At the bottom i is 6 The numbers:

13. Dictionaries, Oh Lovely Dictionaries >>> stuff = {'name': 'Zed', 'age': 39, 'height': 6 * } >>> print stuff['name'] Zed >>> print stuff['age'] 39 >>> print stuff['height'] 74 >>> stuff['city'] = "San Francisco" >>> print stuff['city'] San Francisco

14. Plot like an artist! scatter_demo.py """ Simple demo of a scatter plot. """ import numpy as np import matplotlib.pyplot as plt N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses plt.scatter(x, y, s=area, c=colors, alpha=0.5) plt.show()

PYTHON ADVANCED TOPIC

More on Lists append(x), sort(), index(x) “=” is not really “copy”, shallow copy.copy() is really “copy”, deep copy List comprehension – X=[i for i in range(10)] – [x for x != 2 else 0 for x in range(10)] a+b, a in [1,2,3] Tuples – a, b, c = 1, 2, 3

More on Strings

Argument list def fprintf(file, format, *args) unpack arguments args = [3, 6] range(*args)

Lambda expression f = lambda x: 2 *x + 3 Create function on the fly! Lambda expression with list comprehension [lambda x: 2 * x if x!=2 else 0 for x in range(10)]

Functional programming def f(x) : return x %2 map(f, range(1, 11)) filter(function, sequence) reduce(add, range(1, 11)) Functional programming with string operations X = ‘ ’ import operator

Data Structure Management Queue – pop(0) Stack – append() – pop() Dictionary – tel = {‘a’ : 1, ‘b’ : 2} – tel[‘a’] – dict([(‘a’, 1), (‘b’, 2)]) – Enumerate()

Input/output readline – split(), float(), int(), …, map() genfromtxt print write

Numerics and Plotting import numpy import matplotlib import pylab import scipy X = array([1,2,3]) X = zeros((4, 4)) Be careful about degenerate arrays, squeese Slicing is a little bit inconvenient: ix_()

Classes and OO programming Class Myclass __init__() __call__() __del__()

PYTHON PYTHON IN REAL RESEARCH

Research Topic #1: Saturn’s Giant Storm Sayanagi et al.,

Challenges Model thermodynamic processes in Saturn’s Atmosphere Adiabatic movement (assuming no heat exchange with the surrounding atmosphere) Latent heat release during the movement Multiple Species (different saturation curve, latent heat, etc)

Python Package and Classes __init__.py import package using classes to organize code structure Essential for code readability, reuse and extension see “Thermodiagram.py”

Ammonia mixing ratioPotential temperature make_movie.py

Research Topic #2: Titan Photochemistry Run a 1D Photochemistry model that gives the distribution of hydrocarbons Match model result to the observation of hydrocarbons Use Levenberg-Marquardt (an improved version of gradient descent) to fit model parameters.

35 The 6 th satellite of Saturn Extensive Atmosphere Surface liquid

Data comes from CIRS limb and FUV stellar occultation Altitude (km) CH 4 C2H2C2H2 C2H4C2H4 C2H6C2H6 CH 3 CCHC3H8C3H8 C4H2C4H2 C6H6C6H6 Mixing ratio 36 Vinatier et al., 2009; Kammer et al., 2011

37 Chemical production Chemical loss Vertical transport Eddy diffusivity Mixing ratio Number density The simplest 1D photochemical model

Challenges Running a photo chemical model is expensive Retrieve 6~10 parameters simultaneously Each parameter should have realistic bounds Bounded Parallel Least Square Fitting see “fitC2H2.py”

Retrieval Result

Making beautiful plots subplot_adjust minorticks fontsize fontname mec, mfc linewidth Hack “matplotlib” –.config/matplotlib/matplotrc

Log your code for revision git version control system git init git status git log git add git commit –a/-m

Extension of Python Numerics MCMC data fitting (Markov chain Monte Carlo) – – Well documented with examples Machine Learning Toolbox – Climate analysis –

QUESTIONS?