python - software carpentry · python basics. must assign value to variable before using it...

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

Upload: others

Post on 19-Jul-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Basics

Python

Basics

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Page 2: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A simple interpreted language

Python Basics

Page 3: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A simple interpreted language

no separate compilation step

Python Basics

Page 4: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

$$$$ python

A simple interpreted language

no separate compilation step

$$$$ python

>>>>>>>>>>>>

Python Basics

Page 5: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

$$$$ python

A simple interpreted language

no separate compilation step

$$$$ python

>>>>>>>>>>>> print 1 + 2

3

>>>>>>>>>>>>

Python Basics

Page 6: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

$$$$ python

A simple interpreted language

no separate compilation step

$$$$ python

>>>>>>>>>>>> print 1 + 2

3

>>>>>>>>>>>> print 'charles' + 'darwin'

charlesdarwin

>>>>>>>>>>>>

Python Basics

Page 7: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Put commands in a file and execute that

Python Basics

Page 8: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Put commands in a file and execute that

$$$$ nano very-simple.py

Python Basics

Page 9: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Put commands in a file and execute that

$$$$ nano very-simple.py

print 1 + 2print 1 + 2

print 'charles' + 'darwin'

Python Basics

Page 10: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Put commands in a file and execute that

$$$$ nano very-simple.py

print 1 + 2print 1 + 2

print 'charles' + 'darwin'

$$$$ python very-simple.py

3

charlesdarwin

$$$$

Python Basics

Page 11: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use an integrated development environment (IDE)

Python Basics

Page 12: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use an integrated development environment (IDE)

SourceSource

file

Python Basics

Page 13: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use an integrated development environment (IDE)

SourceSource

file

Python Basics

Execution

shell

Page 14: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Python Basics

Page 15: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use

Python Basics

Page 16: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

Python Basics

Page 17: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>>

Python Basics

Page 18: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>>

Python Basics

Page 19: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

variablevariablevariablevariable valuevaluevaluevalue

planet 'Pluto'

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>>

Python Basics

Page 20: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>> moon = 'Charon'

>>>>>>>>>>>>

variablevariablevariablevariable valuevaluevaluevalue

planet

moon

'Pluto'

'Charon'

Python Basics

Page 21: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>> moon = 'Charon'

>>>>>>>>>>>> p = planet

>>>>>>>>>>>>

variablevariablevariablevariable valuevaluevaluevalue

planet

moon

'Pluto'

'Charon'

Python Basics

Page 22: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>> moon = 'Charon'

>>>>>>>>>>>> p = planet

>>>>>>>>>>>>

variablevariablevariablevariable valuevaluevaluevalue

planet

moon

p

'Pluto'

'Charon'

Python Basics

Page 23: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Variables are names for values

Created by use: no declaration necessary

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> print planet

Pluto

>>>>>>>>>>>> moon = 'Charon'

>>>>>>>>>>>> p = planet

>>>>>>>>>>>> print p

Pluto

variablevariablevariablevariable valuevaluevaluevalue

planet

moon

p

'Pluto'

'Charon'

Python Basics

Pluto

>>>>>>>>>>>>

Page 24: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Python Basics

Page 25: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

Python Basics

Page 26: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>>

Python Basics

Page 27: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

string>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>>planet 'Pluto'

variablevariablevariablevariable valuevaluevaluevaluestring

Python Basics

Page 28: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> planet = 9

>>>>>>>>>>>>planet 'Pluto'

9

variablevariablevariablevariable valuevaluevaluevalue

integer

Python Basics

Page 29: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> planet = 9

>>>>>>>>>>>>planet 'Pluto'

9

variablevariablevariablevariable valuevaluevaluevalue

Values are garbage collected

Python Basics

Values are garbage collected

Page 30: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> planet = 9

>>>>>>>>>>>>planet 'Pluto'

9

variablevariablevariablevariable valuevaluevaluevalue

Values are garbage collected

Python Basics

Values are garbage collected

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

Page 31: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

A variable is just a name

Does not have a type

>>>>>>>>>>>> planet = 'Pluto'

>>>>>>>>>>>> planet = 9

>>>>>>>>>>>>planet 'Pluto'

9

variablevariablevariablevariable valuevaluevaluevalue

Values are garbage collected

Python Basics

Values are garbage collected

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

Page 32: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

Python Basics

Page 33: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>>

Python Basics

Page 34: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>> print plant # note the deliberate misspelling

Python Basics

Page 35: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>> print plant # note the deliberate misspelling

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

print plantprint plantprint plantprint plant

NameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not defined

>>>>>>>>>>>>

Python Basics

Page 36: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>> print plant # note the deliberate misspelling

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

Python does not assume default values for variables

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

print plantprint plantprint plantprint plant

NameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not defined

>>>>>>>>>>>>

Python Basics

Page 37: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>> print plant # note the deliberate misspelling

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

Python does not assume default values for variables

Doing so can mask many errors

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

print plantprint plantprint plantprint plant

NameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not defined

>>>>>>>>>>>>

Python Basics

Doing so can mask many errors

Page 38: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Must assign value to variable before using it

>>>>>>>>>>>> planet = 'Sedna'

>>>>>>>>>>>> print plant # note the deliberate misspelling

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

Python does not assume default values for variables

Doing so can mask many errors

Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):Traceback (most recent call last):

print plantprint plantprint plantprint plant

NameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not definedNameError: name 'plant' is not defined

>>>>>>>>>>>>

Python Basics

Doing so can mask many errors

Anything from # to the end of the line is a comment

Page 39: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

Python Basics

Page 40: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

>>>>>>>>>>>> string = "two"

>>>>>>>>>>>> number = 3

>>>>>>>>>>>> print string * number # repeated concatenation>>>>>>>>>>>> print string * number # repeated concatenation

twotwotwo

>>>>>>>>>>>>

Python Basics

Page 41: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

>>>>>>>>>>>> string = "two"

>>>>>>>>>>>> number = 3

>>>>>>>>>>>> print string * number # repeated concatenation>>>>>>>>>>>> print string * number # repeated concatenation

twotwotwo

>>> >>> >>> >>> print string + number

Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)

number + stringnumber + stringnumber + stringnumber + string

TypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objects

>>>>>>>>>>>>

Python Basics

Page 42: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

>>>>>>>>>>>> string = "two"

>>>>>>>>>>>> number = 3

>>>>>>>>>>>> print string * number # repeated concatenation>>>>>>>>>>>> print string * number # repeated concatenation

twotwotwo

>>> >>> >>> >>> print string + number

Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)

number + stringnumber + stringnumber + stringnumber + string

TypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objects

>>>>>>>>>>>>

Python Basics

Would probably be safe here to produce 'two3'

Page 43: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

>>>>>>>>>>>> string = "two"

>>>>>>>>>>>> number = 3

>>>>>>>>>>>> print string * number # repeated concatenation>>>>>>>>>>>> print string * number # repeated concatenation

twotwotwo

>>> >>> >>> >>> print string + number

Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)

number + stringnumber + stringnumber + stringnumber + string

TypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objects

>>>>>>>>>>>>

Python Basics

Would probably be safe here to produce 'two3'

But then what should '2'+'3' be?

Page 44: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Values do have types

>>>>>>>>>>>> string = "two"

>>>>>>>>>>>> number = 3

>>>>>>>>>>>> print string * number # repeated concatenation>>>>>>>>>>>> print string * number # repeated concatenation

twotwotwo

>>> >>> >>> >>> print string + number

Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)Traceback (most recent call last)

number + stringnumber + stringnumber + stringnumber + string

TypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate 'str' and 'int' objects

>>>>>>>>>>>>

Python Basics

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: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use functions to convert between types

Python Basics

Page 46: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use functions to convert between types

>>>>>>>>>>>> print int('2') + 3

5

>>>>>>>>>>>>>>>>>>>>>>>>

Python Basics

Page 47: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Use functions to convert between types

>>>>>>>>>>>> print int('2') + 3

5

>>> >>> >>> >>> print 2 + str(3)>>> >>> >>> >>> print 2 + str(3)

23

>>>>>>>>>>>>

Python Basics

Page 48: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Numbers

Python Basics

Page 49: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Numbers

14 32-bit integer

(on most machines)(on most machines)

Python Basics

Page 50: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Numbers

14 32-bit integer

(on most machines)(on most machines)

14.0 64-bit float

(ditto)

Python Basics

Page 51: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Numbers

14 32-bit integer

(on most machines)(on most machines)

14.0 64-bit float

(ditto)

1+4j complex number

(two 64-bit floats)

Python Basics

Page 52: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Numbers

14 32-bit integer

(on most machines)(on most machines)

14.0 64-bit float

(ditto)

1+4j complex number

(two 64-bit floats)

Python Basics

x.real, x.imag real and imaginary parts of complex number

Page 53: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Python Basics

Page 54: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

Python Basics

Page 55: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Python Basics

Page 56: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Python Basics

Page 57: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

Python Basics

Page 58: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Python Basics

Page 59: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Division / 3.0 / 2 1.5

Python Basics

Page 60: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Division / 3.0 / 2 1.5

3 / 2 1

Python Basics

3 / 2 1

Page 61: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Division / 3.0 / 2 1.5

3 / 2 1

Python Basics

3 / 2 1

Exponentiation ** 2 ** 0.5 1.41421356...

Page 62: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Arithmetic

Addition + 35 + 22 57

'Py' + 'thon' 'Python''Py' + 'thon' 'Python'

Subtraction - 35 - 22 13

Multiplication * 3 * 2 6

'Py' * 2 'PyPy'

Division / 3.0 / 2 1.5

3 / 2 1

Python Basics

3 / 2 1

Exponentiation ** 2 ** 0.5 1.41421356...

Remainder % 13 % 5 3

Page 63: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

Python Basics

Page 64: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>>>>>>>>>>>

Python Basics

Page 65: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>>>>>>>>>>>>>>>>>>>>>>>

Python Basics

Page 66: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>>>>>>>>>>>

The same as years = years + 1>>>>>>>>>>>>

Python Basics

Page 67: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>> >>> >>> >>> print years>>> >>> >>> >>> print years

501

>>>>>>>>>>>>

Python Basics

Page 68: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>> >>> >>> >>> print years>>> >>> >>> >>> print years

501

>>>>>>>>>>>> years %= 10

>>>>>>>>>>>>

Python Basics

Page 69: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>> >>> >>> >>> print years>>> >>> >>> >>> print years

501

>>>>>>>>>>>> years %= 10

>>>>>>>>>>>>

The same as years = years % 10

Python Basics

Page 70: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Prefer in-place forms of binary operators

>>>>>>>>>>>> years = 500

>>> >>> >>> >>> years += 1

>>> >>> >>> >>> print years>>> >>> >>> >>> print years

501

>>>>>>>>>>>> years %= 10

>>> >>> >>> >>> print years

5

>>>>>>>>>>>>

Python Basics

Page 71: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

Python Basics

Page 72: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

Python Basics

Page 73: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

Python Basics

Page 74: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 False

Python Basics

Page 75: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 FalseSingle = is assignment

Double == is equality

Python Basics

Page 76: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 False

3 >= 5 False

Python Basics

Page 77: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 False

3 >= 5 False

1 < 3 < 5 True

Python Basics

Page 78: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 False

3 >= 5 False

1 < 3 < 5 True

1 < 5 > 3 TrueBut please don't

do this

Python Basics

do this

Page 79: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

Comparisons

3 < 5 True

3 != 5 True3 != 5 True

3 == 5 False

3 >= 5 False

1 < 3 < 5 True

1 < 5 > 3 True

3+2j < 5 errorerrorerrorerror

Python Basics

3+2j < 5 errorerrorerrorerror

Page 80: Python - Software Carpentry · Python Basics. Must assign value to variable before using it >>>>> planet = 'Sedna' >>>>> print plant # note the deliberate misspelling Traceback (most

October 2010

created by

Greg Wilson

October 2010

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.