Upload
delilah-anderson
View
216
Download
0
Embed Size (px)
DESCRIPTION
PythonBasics A simple interpreted language no separate compilation step
Citation preview
Basics
Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution LicenseSee http://software-carpentry.org/license.html for more information.
Python
Python Basics
A simple interpreted language
Python Basics
A simple interpreted language no separate compilation step
Python Basics
$ python>>>
A simple interpreted language no separate compilation step
Python Basics
$ python>>> print 1 + 23>>>
A simple interpreted language no separate compilation step
Python Basics
$ python>>> print 1 + 23>>> print 'charles' + 'darwin'charlesdarwin>>>
A simple interpreted language no separate compilation step
Python Basics
Put commands in a file and execute that
Python Basics
Put commands in a file and execute that$ nano very-simple.py
Python Basics
Put commands in a file and execute that$ nano very-simple.py
print 1 + 2print 'charles' + 'darwin'
Python Basics
Put commands in a file and execute that$ nano very-simple.py
print 1 + 2print 'charles' + 'darwin'
$ python very-simple.py3charlesdarwin$
Python Basics
Use an integrated development environment (IDE)
Python Basics
Use an integrated development environment (IDE)
Sourcefile
Python Basics
Use an integrated development environment (IDE)
Sourcefile
Executionshell
Python Basics
Variables are names for values
Python Basics
Variables are names for valuesCreated by use
Python Basics
Variables are names for valuesCreated by use: no declaration necessary
Python Basics
Variables are names for valuesCreated by use: no declaration necessary
>>> planet = 'Pluto'>>>
Python Basics
Variables are names for valuesCreated by use: no declaration necessary
>>> planet = 'Pluto'>>> print planetPluto>>>
Python Basics
Variables are names for valuesCreated by use: no declaration necessary
variable value
planet 'Pluto'
>>> planet = 'Pluto'>>> print planetPluto>>>
Python Basics
Variables are names for valuesCreated by use: no declaration necessary
>>> planet = 'Pluto'>>> print planetPluto>>> moon = 'Charon'>>>
variable value
planet
moon
'Pluto'
'Charon'
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'
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'
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'
Python Basics
A variable is just a name
Python Basics
A variable is just a nameDoes not have a type
Python Basics
A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>>
Python Basics
A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet 'Pluto'
variable value string
Python Basics
A variable is just a nameDoes not have a type>>> planet = 'Pluto'>>> planet = 9>>> planet 'Pluto'
9
variable value
integer
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
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
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
Python Basics
Must assign value to variable before using it
Python Basics
Must assign value to variable before using it>>> planet = 'Sedna'>>>
Python Basics
Must assign value to variable before using it>>> planet = 'Sedna'>>> print plant # note the deliberate misspelling
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>>>
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>>>
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>>>
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>>>
Python Basics
Values do have types
Python Basics
Values do have types>>> string = "two">>> number = 3>>> print string * number # repeated concatenationtwotwotwo>>>
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>>>
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'
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?
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…
Python Basics
Use functions to convert between types
Python Basics
Use functions to convert between types>>> print int('2') + 35>>>
Python Basics
Use functions to convert between types>>> print int('2') + 35>>> print 2 + str(3)23>>>
Python Basics
Numbers
Python Basics
Numbers14 32-bit integer
(on most machines)
Python Basics
Numbers14 32-bit integer
(on most machines)14.0 64-bit float
(ditto)
Python Basics
Numbers14 32-bit integer
(on most machines)14.0 64-bit float
(ditto)1+4j complex number
(two 64-bit floats)
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
Python Basics
Arithmetic
Python Basics
ArithmeticAddition + 35 + 22 57
Python Basics
ArithmeticAddition + 35 + 22 57
'Py' + 'thon'
'Python'
Python Basics
ArithmeticAddition + 35 + 22 57
'Py' + 'thon'
'Python'
Subtraction - 35 - 22 13
Python Basics
ArithmeticAddition + 35 + 22 57
'Py' + 'thon'
'Python'
Subtraction - 35 - 22 13Multiplication * 3 * 2 6
Python Basics
ArithmeticAddition + 35 + 22 57
'Py' + 'thon'
'Python'
Subtraction - 35 - 22 13Multiplication * 3 * 2 6
'Py' * 2 'PyPy'
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
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
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...
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
Python Basics
Prefer in-place forms of binary operators
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>>
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>>
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>>
The same as years = years + 1
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>>
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>>
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>>
The same as years = years % 10
Python Basics
Prefer in-place forms of binary operators>>> years = 500>>> years += 1>>> print years501>>> years %= 10>>> print years5>>>
Python Basics
Comparisons
Python Basics
Comparisons3 < 5 True
Python Basics
Comparisons3 < 5 True3 != 5 True
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False Single = is assignment
Double == is equality
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True1 < 5 > 3 True But please don't
do this
Python Basics
Comparisons3 < 5 True3 != 5 True3 == 5 False3 >= 5 False1 < 3 < 5 True1 < 5 > 3 True3+2j < 5 error
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.