Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python
Basics A simple interpreted language
PythonBasics A simple interpreted language no separate compilation step
PythonBasics $ python >>> A simple interpreted language no separate compilation step
PythonBasics $ python >>> print >>> A simple interpreted language no separate compilation step
PythonBasics $ python >>> print >>> print 'charles' + 'darwin' charlesdarwin >>> A simple interpreted language no separate compilation step
PythonBasics Put commands in a file and execute that
PythonBasics Put commands in a file and execute that $ nano very-simple.py
PythonBasics Put commands in a file and execute that $ nano very-simple.py print print 'charles' + 'darwin'
PythonBasics Put commands in a file and execute that $ nano very-simple.py print print 'charles' + 'darwin' $ python very-simple.py 3 charlesdarwin $
PythonBasics Use an integrated development environment (IDE)
PythonBasics Use an integrated development environment (IDE) Source file
PythonBasics Use an integrated development environment (IDE) Source file Execution shell
PythonBasics Variables are names for values
PythonBasics Variables are names for values Created by use
PythonBasics Variables are names for values Created by use: no declaration necessary
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>>
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>>
PythonBasics Variables are names for values Created by use: no declaration necessary variablevalue planet 'Pluto' >>> planet = 'Pluto' >>> print planet Pluto >>>
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> variablevalue planet moon 'Pluto' 'Charon'
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon 'Pluto' 'Charon'
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> variablevalue planet moon p 'Pluto' 'Charon'
PythonBasics Variables are names for values Created by use: no declaration necessary >>> planet = 'Pluto' >>> print planet Pluto >>> moon = 'Charon' >>> p = planet >>> print p Pluto >>> variablevalue planet moon p 'Pluto' 'Charon'
PythonBasics A variable is just a name
PythonBasics A variable is just a name Does not have a type
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>>
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet 'Pluto' variablevalue string
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue integer
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled
PythonBasics A variable is just a name Does not have a type >>> planet = 'Pluto' >>> planet = 9 >>> planet 'Pluto' 9 variablevalue Values are garbage collected If nothing refers to data any longer, it can be recycled
PythonBasics Must assign value to variable before using it
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>>
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling
PythonBasics Must assign value to variable before using it >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
PythonBasics Must assign value to variable before using it Python does not assume default values for variables >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
PythonBasics Must assign value to variable before using it Python does not assume default values for variables Doing so can mask many errors Anything from # to the end of the line is a comment >>> planet = 'Sedna' >>> print plant # note the deliberate misspelling Traceback (most recent call last): print plant NameError: name 'plant' is not defined >>>
PythonBasics Values do have types
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>>
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>>
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3'
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: cannot concatenate 'str' and 'int' objects >>> Would probably be safe here to produce 'two3' But then what should '2'+'3' be?
PythonBasics Values do have types >>> string = "two" >>> number = 3 >>> print string * number # repeated concatenation twotwotwo >>> print string + number Traceback (most recent call last) number + string TypeError: 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…
PythonBasics Use functions to convert between types
PythonBasics Use functions to convert between types >>> print int('2') >>>
PythonBasics Use functions to convert between types >>> print int('2') >>> print 2 + str(3) 23 >>>
PythonBasics Numbers
PythonBasics Numbers bit integer (on most machines)
PythonBasics Numbers bit integer (on most machines) bit float (ditto)
PythonBasics Numbers bit integer (on most machines) bit float (ditto) 1+4j complex number (two 64-bit floats)
PythonBasics Numbers bit integer (on most machines) bit float (ditto) 1+4j complex number (two 64-bit floats) x.real, x.imagreal and imaginary parts of complex number
PythonBasics Arithmetic
PythonBasics Arithmetic Addition
PythonBasics Arithmetic Addition 'Py' + 'thon''Python'
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy'
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / 21.5
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21 Exponentiation**2 **
PythonBasics Arithmetic Addition 'Py' + 'thon''Python' Subtraction Multiplication*3 * 26 'Py' * 2'PyPy' Division/3.0 / / 21 Exponentiation**2 ** Remainder%13 % 53
PythonBasics Prefer in-place forms of binary operators
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>>
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>>
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> The same as years = years + 1
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>>
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>>
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> The same as years = years % 10
PythonBasics Prefer in-place forms of binary operators >>> years = 500 >>> years += 1 >>> print years 501 >>> years %= 10 >>> print years 5 >>>
PythonBasics Comparisons
PythonBasics Comparisons 3 < 5True
PythonBasics Comparisons 3 < 5True 3 != 5True
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False Single = is assignment Double == is equality
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True But please don't do this
PythonBasics Comparisons 3 < 5True 3 != 5True 3 == 5False 3 >= 5False 1 < 3 < 5True 1 3True 3+2j < 5error
October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.