80
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Python

Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Embed Size (px)

DESCRIPTION

PythonBasics A simple interpreted language no separate compilation step

Citation preview

Page 1: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Basics

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution LicenseSee http://software-carpentry.org/license.html for more information.

Python

Page 2: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A simple interpreted language

Page 3: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A simple interpreted language no separate compilation step

Page 4: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

$ python>>>

A simple interpreted language no separate compilation step

Page 5: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

$ python>>> print 1 + 23>>>

A simple interpreted language no separate compilation step

Page 6: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

$ python>>> print 1 + 23>>> print 'charles' + 'darwin'charlesdarwin>>>

A simple interpreted language no separate compilation step

Page 7: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Put commands in a file and execute that

Page 8: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Put commands in a file and execute that$ nano very-simple.py

Page 9: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Put commands in a file and execute that$ nano very-simple.py

print 1 + 2print 'charles' + 'darwin'

Page 10: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Put commands in a file and execute that$ nano very-simple.py

print 1 + 2print 'charles' + 'darwin'

$ python very-simple.py3charlesdarwin$

Page 11: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use an integrated development environment (IDE)

Page 12: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use an integrated development environment (IDE)

Sourcefile

Page 13: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use an integrated development environment (IDE)

Sourcefile

Executionshell

Page 14: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for values

Page 15: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use

Page 16: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

Page 17: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>>

Page 18: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>> print planetPluto>>>

Page 19: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

variable value

planet 'Pluto'

>>> planet = 'Pluto'>>> print planetPluto>>>

Page 20: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>> print planetPluto>>> moon = 'Charon'>>>

variable value

planet

moon

'Pluto'

'Charon'

Page 21: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>> print planetPluto>>> moon = 'Charon'>>> p = planet>>>

variable value

planet

moon

'Pluto'

'Charon'

Page 22: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>> print planetPluto>>> moon = 'Charon'>>> p = planet>>>

variable value

planet

moon

p

'Pluto'

'Charon'

Page 23: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Variables are names for valuesCreated by use: no declaration necessary

>>> planet = 'Pluto'>>> print planetPluto>>> moon = 'Charon'>>> p = planet>>> print pPluto>>>

variable value

planet

moon

p

'Pluto'

'Charon'

Page 24: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a name

Page 25: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type

Page 26: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>>

Page 27: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet 'Pluto'

variable value string

Page 28: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet = 9>>> planet 'Pluto'

9

variable value

integer

Page 29: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet = 9>>> planet 'Pluto'

9

variable value

Values are garbage collected

Page 30: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet = 9>>> planet 'Pluto'

9

variable value

Values are garbage collected

If nothing refers to data any longer, it can be recycled

Page 31: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet = 9>>> planet 'Pluto'

9

variable value

Values are garbage collected

If nothing refers to data any longer, it can be recycled

Page 32: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it

Page 33: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it>>> planet = 'Sedna'>>>

Page 34: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it>>> planet = 'Sedna'>>> print plant # note the deliberate misspelling

Page 35: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it>>> planet = 'Sedna'>>> print plant # note the deliberate misspellingTraceback (most recent call last): print plantNameError: name 'plant' is not defined>>>

Page 36: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it

Python does not assume default values for variables

>>> planet = 'Sedna'>>> print plant # note the deliberate misspellingTraceback (most recent call last): print plantNameError: name 'plant' is not defined>>>

Page 37: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it

Python does not assume default values for variablesDoing so can mask many errors

>>> planet = 'Sedna'>>> print plant # note the deliberate misspellingTraceback (most recent call last): print plantNameError: name 'plant' is not defined>>>

Page 38: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Must assign value to variable before using it

Python does not assume default values for variablesDoing so can mask many errorsAnything from # to the end of the line is a comment

>>> planet = 'Sedna'>>> print plant # note the deliberate misspellingTraceback (most recent call last): print plantNameError: name 'plant' is not defined>>>

Page 39: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types

Page 40: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>>

Page 41: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>> print string + numberTraceback (most recent call last) number + stringTypeError: cannot concatenate 'str' and 'int' objects>>>

Page 42: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>> print string + numberTraceback (most recent call last) number + stringTypeError: cannot concatenate 'str' and 'int' objects>>>

Would probably be safe here to produce 'two3'

Page 43: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>> print string + numberTraceback (most recent call last) number + stringTypeError: cannot concatenate 'str' and 'int' objects>>>

Would probably be safe here to produce 'two3'But then what should '2'+'3' be?

Page 44: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>> print string + numberTraceback (most recent call last) number + stringTypeError: cannot concatenate 'str' and 'int' objects>>>

Would probably be safe here to produce 'two3'But then what should '2'+'3' be?Doing too much is as bad as doing too little…

Page 45: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use functions to convert between types

Page 46: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use functions to convert between types>>> print int('2') + 35>>>

Page 47: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Use functions to convert between types>>> print int('2') + 35>>> print 2 + str(3)23>>>

Page 48: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Numbers

Page 49: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Numbers14 32-bit integer

(on most machines)

Page 50: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Numbers14 32-bit integer

(on most machines)14.0 64-bit float

(ditto)

Page 51: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Numbers14 32-bit integer

(on most machines)14.0 64-bit float

(ditto)1+4j complex number

(two 64-bit floats)

Page 52: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Numbers14 32-bit integer

(on most machines)14.0 64-bit float

(ditto)1+4j complex number

(two 64-bit floats)x.real, x.imag

real and imaginary parts of complex number

Page 53: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Arithmetic

Page 54: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

Page 55: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Page 56: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13

Page 57: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

Page 58: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Page 59: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

'Py' * 2 'PyPy'Division / 3.0 / 2 1.5

Page 60: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

'Py' * 2 'PyPy'Division / 3.0 / 2 1.5

3 / 2 1

Page 61: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

'Py' * 2 'PyPy'Division / 3.0 / 2 1.5

3 / 2 1Exponentiation

** 2 ** 0.5 1.41421356...

Page 62: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

ArithmeticAddition + 35 + 22 57

'Py' + 'thon'

'Python'

Subtraction - 35 - 22 13Multiplication * 3 * 2 6

'Py' * 2 'PyPy'Division / 3.0 / 2 1.5

3 / 2 1Exponentiation

** 2 ** 0.5 1.41421356...

Remainder % 13 % 5 3

Page 63: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators

Page 64: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>>

Page 65: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>>

Page 66: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>>

The same as years = years + 1

Page 67: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>>

Page 68: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>>

Page 69: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>>

The same as years = years % 10

Page 70: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>> print years5>>>

Page 71: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons

Page 72: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True

Page 73: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True

Page 74: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False

Page 75: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False Single = is assignment

Double == is equality

Page 76: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False

Page 77: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True

Page 78: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True1 < 5 > 3 True But please don't

do this

Page 79: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Basics

Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True1 < 5 > 3 True3+2j < 5 error

Page 80: Basics Copyright  Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

October 2010

created by

Greg Wilson

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution LicenseSee http://software-carpentry.org/license.html for more information.