Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Python By Neil Cook Twitter: njcuk9999 Slides/Notes:"— Presentation transcript:

1 Introduction to Python By Neil Cook Twitter: njcuk9999 Email: neil.james.cook@gmail.com Slides/Notes: star.herts.ac.uk/~ncook/pythonneil.james.cook@gmail.com

2 1.1 Introduction

3 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

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

5 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

6 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

7 Credits These course are heavily based on the lecture notes from Geert Barentsen (g.barentsen@herts.ac.uk) 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: https://github.com/astrofrog/py4sci In addition, some course material on scipy and sympy has been adapted from: https://github.com/scipy-lectures/scipy-lecture-notes 1.1 Introduction

8 1.2 What is Python?

9 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?

10 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?

11 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?

12 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 3. 1.2 What is Python?

13 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?

14 Installing Anaconda 2a To install: 1) Go to http://continuum.io/downloadshttp://continuum.io/downloads 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 2.7 - Graphical Installer After downloading the installer, double click the.exe file and follow the instructions on the screen.

15 Installing Anaconda 2b To install: 1) Go to http://continuum.io/downloadshttp://continuum.io/downloads 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-2.1.0- MacOSX-x86_64.sh Note that you should type "bash" regardless of whether or not you are actually using the bash shell.

16 Installing Anaconda 2c To install: 1) Go to http://continuum.io/downloadshttp://continuum.io/downloads 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-2.1.0- Linux-x86.sh Note that you should type "bash" regardless of whether or not you are actually using the bash shell.

17 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?

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

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

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

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

22 Using Anaconda 6 1.2 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

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

24 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.

25 1.3 Basic Objects 1: Numbers, Strings and Lists

26 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.

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

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

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

30 Manipulation 1 1.3 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:

31 Manipulation 2 1.3 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:

32 Manipulation 3 1.3 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:

33 Manipulation 4 1.3 Basic Objects 1: Numbers, Strings and Lists

34 Manipulation 5 1.3 Basic Objects 1: Numbers, Strings and Lists

35 Manipulation 6 1.3 Basic Objects 1: Numbers, Strings and Lists

36 Manipulation 7 1.3 Basic Objects 1: Numbers, Strings and Lists

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

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

39 Strings 1 1.3 Basic Objects 1: Numbers, Strings and Lists

40 Strings 2 1.3 Basic Objects 1: Numbers, Strings and Lists

41 Strings 3 1.3 Basic Objects 1: Numbers, Strings and Lists

42 Strings 4 1.3 Basic Objects 1: Numbers, Strings and Lists

43 Strings 5 1.3 Basic Objects 1: Numbers, Strings and Lists

44 Strings 6 1.3 Basic Objects 1: Numbers, Strings and Lists

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

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

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

48 Strings 11 1.3 Basic Objects 1: Numbers, Strings and Lists More about strings: https://docs.python.org/2/library/string.htmlhttps://docs.python.org/2/library/string.html

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

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

51 Lists 1 1.3 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.

52 Lists 2 1.3 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.

53 Lists 3 1.3 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.

54 Lists 4 1.3 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.

55 Lists 5 1.3 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.

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

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

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

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

60 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)

61 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.

62 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)

63 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.

64 Converting between types 1 1.3 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.

65 Converting between types 2 1.3 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.

66 Converting between types 3 1.3 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.

67 1.3B More on Strings

68 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

69 String Formatting 2 1.3B More on Strings

70 String Formatting 3 1.3B More on Strings

71 The “format” method 1 1.3B More on Strings

72 The “format” method 2 1.3B More on Strings

73 Adding tabs and new lines 1 1.3B More on Strings

74 Adding tabs and new lines 2 1.3B More on Strings

75 Special Characters 1 1.3B More on Strings

76 Special Characters 2 1.3B More on Strings For more escape characters see here: http://www.tutorialspoint.com/python/python_strings.htm

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

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

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

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

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

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

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

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

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

86 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

87 Booleans 1 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

88 Booleans 2 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

89 Booleans 3 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

90 Booleans 4 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

91 Booleans 5 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

92 Booleans 6 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

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

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

95 Tuples 1 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

96 Tuples 2 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

97 Dictionaries 1 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

98 Dictionaries 2 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

99 Dictionaries 3 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

100 Dictionaries 4 1.4 Basic Objects II: Booleans, Tuples, and Dictionaries

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

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

103 1.5 Boolean Algebra

104 Boolean Algebra 1.5 Boolean Algebra

105 Booleans in Python 1 1.5 Boolean Algebra

106 Booleans in Python 2 1.5 Boolean Algebra

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

108 Basic Operations 1 1.5 Boolean Algebra

109 Basic Operations 2 1.5 Boolean Algebra

110 Laws 1.5 Boolean Algebra

111 Order of Operations 1 1.5 Boolean Algebra Recall from your maths: BODMASS

112 Order of Operations 2 1.5 Boolean Algebra Recall from your maths: BODMASS

113 Rewriting Expressions 1.5 Boolean Algebra

114 Problems 1 1.6 Problems 1


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

Similar presentations


Ads by Google