PH2150 Scientific Computing Skills

Slides:



Advertisements
Similar presentations
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Advertisements

Structured programming
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.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Python quick start guide
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
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.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
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.
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.
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Python Scripting for Computational Science CPS 5401 Fall 2014 Shirley Moore, Instructor October 6,
Introduction to python programming
Introduction to python
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Functions Defining and Calling a Void Function
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Computer Application in Engineering Design
Python Let’s get started!.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Computer Programming Fundamentals
Motivation and Overview
Matlab Training Session 4: Control, Flow and Functions
PH2150 Scientific Computing Skills
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB DENC 2533 ECADD LAB 9.
PH2150 Scientific Computing Skills
Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
PH2150 Scientific Computing Skills
Coding Concepts (Basics)
Variables Title slide variables.
Topics Designing a Program Input, Processing, and Output
Variables, Lists, and Objects
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Learning VB2005 language (basics)
Introduction to Value-Returning Functions: Generating Random Numbers
EECE.2160 ECE Application Programming
mdul-cntns-sub-cmmnts2.f90
CSCI N207 Data Analysis Using Spreadsheet
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Topics Designing a Program Input, Processing, and Output
Simulation And Modeling
General Computer Science for Engineers CISC 106 Lecture 03
CISC101 Reminders Assignment 3 due today.
Numpy, pylab, matplotlib (follow-up)
Arrays, Part 1 of 2 Topics Definition of a Data Structure
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Introduction to Python
Presentation transcript:

PH2150 Scientific Computing Skills #Lecture 2: Common mistakes from the first week # raw_input returns a string, some were using the int () constructor, which caused unexpected errors when running the code. C = raw_input ('C=?') # raw_input takes a string as argument C = float (C) # redefines C as a floating point number The function eval ( ) can be used to input an argument that is a string, and treat the string as if it was a Python expression. # Another error due to incorrectly defined type Y = 1/3 # returns 0, integer division Y = 1.0/3.0 # returns 0.33333333333333333

PH2150 Scientific Computing Skills Importing functions from Modules/Packages When you launch Canopy, Welcome to Canopy's interactive data-analysis environment! with pylab-backend set to: qt Type '?' for more information. import numpy import matplotlib from matplotlib import pylab, mlab, pyplot np = numpy plt = pyplot from IPython.display import display from IPython.core.pylabtools import figsize, getfigs from pylab import * from numpy import * This creates over a thousand names in the ‘namespace’, This can be modified from within Canopy in the dropdown menu: EDITPREFERENCES PYTHON

PH2150 Scientific Computing Skills Importing functions from Modules/Packages In contrast, in a python module (*.py file, i.e the script that you are writing and saving), you must explicitly import every name which you use. This is much more robust, and since you are not constantly typing and retyping as you would do at the prompt, the extra typing is well worth it. We will have a quick look at the different ways of doing. import modulename e.g. import math # then you can use function by calling modulename.functionname() from modulename import functionname e.g. from math import sqrt # Explicitly import a function by its name also you to call the functionname() from modulename import * # imports all the functionnames from the module import modulename as alias A Module: A file containing Python definitions (functions, variables and objects) themed around a specific task. The module can be imported into your program. The convention is that all import statements are made at the beginning of your code.

PH2150 Scientific Computing Skills Importing functions from Packages A Package: Is a set of modules or a directory containing modules linked through a shared theme. Importing a package does not automatically import all the modules contained within it. Some useful packages: SciPy (maths, science and enginering module), Numpy (Numerical Python for arrays, fourier transforms and more), matplotlib (graphical representation), mayavi (3D visualisation module in Enthought distribution) from PackageName import  specific_submodule import PackageName.submodulename as alias e.g. import matplotlib.pyplot as plt Then to use the function xlabel from matplotlib.pyplot to set the x-axis label: plt.xlabel(‘axis label’)

PH2150 Scientific Computing Skills Importing functions from Packages If when running your script you get the error: ImportError: No module named [module/package name] And you are sure that you have made no errors in the package name, then you may need to install the package via the Canopy Package manager

PH2150 Scientific Computing Skills Control Structures: Section 6 Repeat a block of statements Repeat the same operation on elements in a list Branch off along multiple paths while and for loops if, else, elif statements

PH2150 Scientific Computing Skills User defined functions: Section 7 def name (parameters,…,…): “”” documentation string””” <Block of statements>

PH2150 Scientific Computing Skills The NumPy Array http://docs.scipy.org/doc/numpy/reference/arrays.html The number of elements in an array is fixed. You cannot add elements to an array once it is created, or remove them. The elements of an array must all be of the same type, such as all floats or all integers. You cannot mix elements of different types in the same array and you cannot change the type of the elements once an array is created.

PH2150 Scientific Computing Skills The NumPy Array Lists, as we have seen, have neither of these restrictions. Why would we ever use an array if lists are more flexible? The answer is that arrays have several significant advantages over lists as well: Arrays can be two-dimensional, like matrices in algebra. That is, rather than just a one-dimensional row of elements, we can have a grid of them. Indeed, arrays can in principle have n-dimensions. Lists, by contrast, are always just one-dimensional containers. Arrays behave roughly like vectors or matrices: you can do arithmetic with them, such as adding them together, and you will get the result you expect. As we have seen this is not true with lists, addition results in concatenation of the list. Arrays work faster than lists. Especially if you have a very large array with many elements then calculations may be significantly faster using an array.

PH2150 Scientific Computing Skills The NumPy Array http://docs.scipy.org/doc/numpy/reference/arrays.html #Creating Arrays in Numpy import numpy as np x=np.array([[1,2,3,4],[5,6,7,8]]) print 'size of array', np.size(x) print 'shape of array', np.shape(x) print 'x=', x y=np.linspace(0,1,20) print 'y=',y

PH2150 Scientific Computing Skills The NumPy Array: Indexing and Slicing

PH2150 Scientific Computing Skills The NumPy Array: Indexing and Slicing