Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons...
-
Upload
delilah-anderson -
Category
Documents
-
view
216 -
download
0
description
Transcript of Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons...
![Page 1: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/2.jpg)
Python Basics
A simple interpreted language
![Page 3: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/39.jpg)
Python Basics
Values do have types
![Page 40: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/48.jpg)
Python Basics
Numbers
![Page 49: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/49.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/50.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/51.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/52.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/53.jpg)
Python Basics
Arithmetic
![Page 54: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/54.jpg)
Python Basics
ArithmeticAddition + 35 + 22 57
![Page 55: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/55.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/56.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/57.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/58.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/59.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/60.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/61.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/62.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/63.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/64.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/65.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/66.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/67.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/68.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/69.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/70.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/71.jpg)
Python Basics
Comparisons
![Page 72: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/72.jpg)
Python Basics
Comparisons3 < 5 True
![Page 73: Basics Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/73.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/74.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/75.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/76.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/77.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/78.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/79.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b29e1/html5/thumbnails/80.jpg)
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.