Introduction to Python By Neil Cook Twitter: njcuk9999 Slides/Notes:

Slides:



Advertisements
Similar presentations
Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party.
Advertisements

Introduction to Python
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Introduction to Python John Alexis Guerra Gómez CMSC433 Spring 2010 University of Maryland.
Introduction to Python
Android Application Development Stephen Diniz Computer/Electrical Engineer Lecture 01 Introduction.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Introduction to a Programming Environment
Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Recitation 1 Programming for Engineers in Python.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to VB.NET Tonga Institute of Higher Education.
CS110/CS119 Introduction to Computing (Java)
An Introduction to Textual Programming
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 1 Getting Started 4/5/09 Python Mini-Course: Day 1 - Lesson 1 1.
Introduction to Shell Script Programming
Introduction to Python
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python From the book “Think Python”
Ch 1. A Python Q&A Session Spring Why do people use Python? Software quality Developer productivity Program portability Support libraries Component.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
Introduction to CS520/CS596_026 Lecture Two Gordon Tian Fall 2015.
9/2/ CS171 -Math & Computer Science Department at Emory University.
Introduction to PHP Advanced Database System Lab no.1.
The Software Development Process
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.
ECET – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Practical Kinetics Exercise 0: Getting Started Objectives: 1.Install Python and IPython Notebook 2.print “Hello World!”
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Getting Started With Python Brendan Routledge
MET4750 Techniques for Earth System Modeling MET 5990 Techniques for Earth System Modeling and Research (
Development Environment
CST 1101 Problem Solving Using Computers
MET4750 Techniques for Earth System Modeling
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
PH2150 Scientific Computing Skills
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Week 1 Gates Introduction to Information Technology cosc 010 Week 1 Gates
Engineering Innovation Center
Prepared by Kimberly Sayre and Jinbo Bi
Introduction to Python
Coding Concepts (Basics)
Section 1 Introduction To Programming
Introduction to Programming with Python
12th Computer Science – Unit 5
Review of Previous Lesson
Introduction to Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:

1.1 Introduction

Python Python is a very powerful programming language. I, like many other scientists, use Python every day to process data, analyse results and create visualisations, like the ones below: 1.1 Introduction

The Course Week 1 ( ) 1.1 Introduction 1.2 What is Python 1.3 Basic Objects Week 2 ( ) 1.3B More on Strings 1.4 Basic Objects II 1.5 Boolean Algebra Week - ( ) No Lecture - Exercises 1.6 Problems 1 Week 3 ( ) 1.6 Problems Control Flow Week 4 ( ) 2.2 Introduction to Numpy 2.3 Numpy Functions Week 5 ( ) 2.4 Introduction to Matplotlib 2.5 Matplotlib Plots Week - ( ) No Lecture - Exercises 2.6 Problems 2 Week 6 ( ) 2.6 Problem Functions 3.2 Defining Modules 3.3 Reading and Writing Files Week - ( ) No Lecture - Exercises 3.4 Problems 3 Week 7 ( ) 3.4 Problems Fitting a curve 4.2 Debugging and automated testing Week - ( ) Easter 4.3 Problems 4 Week - ( ) Easter 4.3 Problems 4 Week 8 ( ) 5.1 AstroPy: Unit conversion 5.2 AstroPy: Tables 5.3 AstroPy: Handling FITS files Week 9 ( ) 5.4 Object-oriented programming Week 10 ( ) 5.5 Own Projects 1.1 Introduction

Exercises The only real way to learn Python is to try it yourself. The more you practise the better you will be. So the emphasis is on you to try the exercises. You are encouraged to work together and help each other. 1.1 Introduction

Google is your friend Pretty much every problem you have, someone else will have had that problem Use google… BUT Do not copy and paste, you must understand every line in your code before using it Blindly copying and pasting is an easy way to get many problems!! 1.1 Introduction

Credits These course are heavily based on the lecture notes from Geert Barentsen as part of Professional Skills optinal mini-course and the programming part of the "Programming and Statistics" course at the University of Hertfordshire (7PAM1023). These course notes are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. This course is based, in part, on an existing course called "Python: Programming for Scientists" by Thomas Robitaille, which is available here: In addition, some course material on scipy and sympy has been adapted from: Introduction

1.2 What is Python?

First, what is 'Programming'? Being good at programming is not simply about knowing a programming language, it is about: understanding a problem conceptually and being able to translate it into a computer program knowing how to fix your program when it does not work writing a program that is fast enough (not the fastest possible) writing a program that can be understood by other people (or by yourself in a year or two) In science, the last point is important: reproducible research open science Likewise in industry many companies demand that you write code that can be read and understood by your colleagues. Graduates who can combine math and science skills with programming are in very high demand on the job market. 1.2 What is Python?

Why choose Python over other languages? It's free! It's relatively easy to pick up It has a clean and simple syntax which is easy to read It gives detailed errors by default which makes it easier to fix bugs It can be used interactively Many employers want python programmers Huge standard library It can interact with existing C/Fortran code when speed is important Nearly every problem you have will have been done before (Google is your friend) Generally good quality documentation Platform agnostic (works on Windows, Mac, Linux etc) Very common use of BSD/MIT-style licensing with third-party modules Emphasis on code readability (see PEP-8) Useful for a really broad range of programming tasks from little shell scripts to enterprise web applications to scientific uses; it may not be as good at any of those as a purpose-built programming language but it can do all of them, and do them well (e.g. you don't see web apps written as bash scripts nor do you see Linux package managers written in Java) 1.2 What is Python?

Python We will be using distribution of python called Anaconda and will we use Spyder to run Python scripts. It is pre-installed on computers in C160 Start > Anaconda > Spyder If you would like to use Python at home or on your laptop, you can install the free Anaconda Python distribution.Anaconda Python distribution 1.2 What is Python?

A note on Python Versions We will be using Python version 2.7.x The latest version of Python is currently 3.3, but many people are still using version 2.7 (the latest 2.x release) because Python 3.x includes some changes which means that not all packages are ready. In this course, we will learn Python 2.7.x syntax, which is almost identical to Python What is Python?

Installing Anaconda 1 There are many ways of using python. You are free to use any version distribution of python. However, you may need to install modules that do not come with your chosen distribution. Anaconda comes will all modules that we will be using and in easy to install and use. 1.2 What is Python?

Installing Anaconda 2a To install: 1) Go to 2) Choose your installer (Windows, Mac or Linux should detect your version automatically) 1.2 What is Python? For Windows 64 bit: Windows 64-Bit Python 2.7 Graphical Installer - Size: 366M For Windows 32 bit: Windows 32-bit - Python Graphical Installer After downloading the installer, double click the.exe file and follow the instructions on the screen.

Installing Anaconda 2b To install: 1) Go to 2) Choose your installer (Windows, Mac or Linux should detect your version automatically) 1.2 What is Python? For Mac: Mac OS X - 64-Bit Python 2.7 Graphical Installer - Size: 275M (OSX 10.7 or higher) After downloading the installer, double click the.pkg file and follow the instructions on the screen. COMMAND-LINE INSTALLS: After downloading the installer, in the shell execute: bash Anaconda MacOSX-x86_64.sh Note that you should type "bash" regardless of whether or not you are actually using the bash shell.

Installing Anaconda 2c To install: 1) Go to 2) Choose your installer (Windows, Mac or Linux should detect your version automatically) 1.2 What is Python? For Linux 64 bit: Linux 64-Bit - Python 2.7 For Linux 32 bit: Linx 32-bit After downloading the installer, in the shell execute: bash Anaconda Linux-x86.sh Note that you should type "bash" regardless of whether or not you are actually using the bash shell.

Using Anaconda 1 Rest of this is how it will look in windows. Find and run the sypder IDE (Integrated Development Environment) Windows: Start --> Anaconda --> Sypder 1.2 What is Python?

Using Anaconda What is Python? The interface should look something like as follows:

Using Anaconda What is Python? The interface should look something like as follows:

Using Anaconda What is Python? The interface should look something like as follows:

Using Anaconda What is Python? Make sure you change the directory to your folder

Using Anaconda What is Python? I recommend using the ipython console (may need to change) Menu bar: Interpreter --> Open Ipython Console Menu bar: Console --> Open Ipython Console

Using Anaconda 5 This console should read something like the following: 1.2 What is Python?

First line of code In the console type: 1.2 What is Python? The print function will allow us to display information from our code.

1.3 Basic Objects 1: Numbers, Strings and Lists

Basic Numeric Types 1.3 Basic Objects 1: Numbers, Strings and Lists The basic numeric data types are similar to those found in other programming languages.

Integers (int): 1.3 Basic Objects 1: Numbers, Strings and Lists Real whole numbers (can be positive or negative)

Floating point values (float) 1.3 Basic Objects 1: Numbers, Strings and Lists Real rational numbers (i.e. can include decimal numbers)

Complex values (complex): 1.3 Basic Objects 1: Numbers, Strings and Lists Real part and imaginary part i.e i

Manipulation Basic Objects 1: Numbers, Strings and Lists Manipulating these objects behaves the way you would expect, so an operation (+, -, /, *, **, etc.) on two values of the same type produces another value of the same type, while an operation on two values with different types produces a value of the more 'advanced' type:

Manipulation Basic Objects 1: Numbers, Strings and Lists Manipulating these objects behaves the way you would expect, so an operation (+, -, /, *, **, etc.) on two values of the same type produces another value of the same type, while an operation on two values with different types produces a value of the more 'advanced' type:

Manipulation Basic Objects 1: Numbers, Strings and Lists Manipulating these objects behaves the way you would expect, so an operation (+, -, /, *, **, etc.) on two values of the same type produces another value of the same type, while an operation on two values with different types produces a value of the more 'advanced' type:

Manipulation Basic Objects 1: Numbers, Strings and Lists

Manipulation Basic Objects 1: Numbers, Strings and Lists

Manipulation Basic Objects 1: Numbers, Strings and Lists

Manipulation Basic Objects 1: Numbers, Strings and Lists

Exercise 1.3a 1.3 Basic Objects 1: Numbers, Strings and Lists

Solution 1.3a 1.3 Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists

Strings Basic Objects 1: Numbers, Strings and Lists Strings like most objects have many methods associated with them, here are a few:

Strings Basic Objects 1: Numbers, Strings and Lists Strings like most objects have many methods associated with them, here are a few:

Strings Basic Objects 1: Numbers, Strings and Lists Like most objects strings have many methods associated with them, here are a few:

Strings Basic Objects 1: Numbers, Strings and Lists More about strings:

Exercise 1.3b 1.3 Basic Objects 1: Numbers, Strings and Lists

Solution 1.3b 1.3 Basic Objects 1: Numbers, Strings and Lists

Lists Basic Objects 1: Numbers, Strings and Lists There are several kinds of ways of storing sequences in Python, the simplest being the list which is simply a sequence of any Python object.

Lists Basic Objects 1: Numbers, Strings and Lists There are several kinds of ways of storing sequences in Python, the simplest being the list which is simply a sequence of any Python object.

Lists Basic Objects 1: Numbers, Strings and Lists There are several kinds of ways of storing sequences in Python, the simplest being the list which is simply a sequence of any Python object.

Lists Basic Objects 1: Numbers, Strings and Lists There are several kinds of ways of storing sequences in Python, the simplest being the list which is simply a sequence of any Python object.

Lists Basic Objects 1: Numbers, Strings and Lists There are several kinds of ways of storing sequences in Python, the simplest being the list which is simply a sequence of any Python object.

Exercise 1.3c 1.3 Basic Objects 1: Numbers, Strings and Lists

Solution 1.3c 1.3 Basic Objects 1: Numbers, Strings and Lists

Functions that operate on lists: len 1.3 Basic Objects 1: Numbers, Strings and Lists

Functions that operate on lists: sum and sort 1.3 Basic Objects 1: Numbers, Strings and Lists

A note on Python objects 1.3 Basic Objects 1: Numbers, Strings and Lists Most things in Python are objects. But what is an object? Every thing we have showed you (integers, floats, strings etc.) are actually objects with a type and associated attributes and methods. For example: x = [1, 3, 4, 5] x.append() is a method of a list object (x)

A note on Python objects 1.3 Basic Objects 1: Numbers, Strings and Lists To understand the attributes and methods of an object, use tab completion in Ipython to inspect objects. mylist = [3, 1, 4, 5] mylist. This will show the available attributes and methods for the Python list mylist.

A note on Python objects 1.3 Basic Objects 1: Numbers, Strings and Lists Using completion and help is a very efficient way to learn and later remember object methods and attributes If you want to know what a method does, you can use a question mark (to access the help files)

Dynamic typing 1.3 Basic Objects 1: Numbers, Strings and Lists Unlike many other programming languages where types have to be declared for variables, Python is dynamically typed.

Converting between types Basic Objects 1: Numbers, Strings and Lists There may be cases where you want to convert a string to a float, or an integer to a string, etc.

Converting between types Basic Objects 1: Numbers, Strings and Lists There may be cases where you want to convert a string to a float, or an integer to a string, etc.

Converting between types Basic Objects 1: Numbers, Strings and Lists There may be cases where you want to convert a string to a float, or an integer to a string, etc.

1.3B More on Strings

String Formatting 1 1.3B More on Strings Strings are an important objects. A string is what we call a group of characters (letters numbers and symbols), words and sentences. There are many format options with strings

String Formatting 2 1.3B More on Strings

String Formatting 3 1.3B More on Strings

The “format” method 1 1.3B More on Strings

The “format” method 2 1.3B More on Strings

Adding tabs and new lines 1 1.3B More on Strings

Adding tabs and new lines 2 1.3B More on Strings

Special Characters 1 1.3B More on Strings

Special Characters 2 1.3B More on Strings For more escape characters see here:

String Methods 1 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

String Methods 2 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

String Methods 3 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

String Methods 4 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

String Methods 5 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

String Methods 6 1.3B More on Strings There are many methods that you can use on strings. Here are just a few:

The String Module 1 1.3B More on Strings These is also the built in string module

The String Module 2 1.3B More on Strings These is also the built in string module

The String Module 3 1.3B More on Strings These is also the built in string module

1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Booleans Basic Objects II: Booleans, Tuples, and Dictionaries

Exercise 1.4a 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

Solution 1.4a 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

Tuples Basic Objects II: Booleans, Tuples, and Dictionaries

Tuples Basic Objects II: Booleans, Tuples, and Dictionaries

Dictionaries Basic Objects II: Booleans, Tuples, and Dictionaries

Dictionaries Basic Objects II: Booleans, Tuples, and Dictionaries

Dictionaries Basic Objects II: Booleans, Tuples, and Dictionaries

Dictionaries Basic Objects II: Booleans, Tuples, and Dictionaries

Exercise 1.4b 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

Solution 1.4b 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

1.5 Boolean Algebra

Boolean Algebra 1.5 Boolean Algebra

Booleans in Python Boolean Algebra

Booleans in Python Boolean Algebra

Booleans in Python Boolean Algebra Though please don’t try doing maths with Booleans!

Basic Operations Boolean Algebra

Basic Operations Boolean Algebra

Laws 1.5 Boolean Algebra

Order of Operations Boolean Algebra Recall from your maths: BODMASS

Order of Operations Boolean Algebra Recall from your maths: BODMASS

Rewriting Expressions 1.5 Boolean Algebra

Problems Problems 1